]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
x86/fpu: Avoid writing LBR bit to IA32_XSS unless supported
authorMitchell Levy <levymitchell0@gmail.com>
Mon, 12 Aug 2024 20:44:12 +0000 (13:44 -0700)
committerThomas Gleixner <tglx@linutronix.de>
Wed, 14 Aug 2024 17:40:20 +0000 (19:40 +0200)
There are two distinct CPU features related to the use of XSAVES and LBR:
whether LBR is itself supported and whether XSAVES supports LBR. The LBR
subsystem correctly checks both in intel_pmu_arch_lbr_init(), but the
XSTATE subsystem does not.

The LBR bit is only removed from xfeatures_mask_independent when LBR is not
supported by the CPU, but there is no validation of XSTATE support.

If XSAVES does not support LBR the write to IA32_XSS causes a #GP fault,
leaving the state of IA32_XSS unchanged, i.e. zero. The fault is handled
with a warning and the boot continues.

Consequently the next XRSTORS which tries to restore supervisor state fails
with #GP because the RFBM has zero for all supervisor features, which does
not match the XCOMP_BV field.

As XFEATURE_MASK_FPSTATE includes supervisor features setting up the FPU
causes a #GP, which ends up in fpu_reset_from_exception_fixup(). That fails
due to the same problem resulting in recursive #GPs until the kernel runs
out of stack space and double faults.

Prevent this by storing the supported independent features in
fpu_kernel_cfg during XSTATE initialization and use that cached value for
retrieving the independent feature bits to be written into IA32_XSS.

[ tglx: Massaged change log ]

Fixes: f0dccc9da4c0 ("x86/fpu/xstate: Support dynamic supervisor feature for LBR")
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/all/20240812-xsave-lbr-fix-v3-1-95bac1bf62f4@gmail.com
arch/x86/include/asm/fpu/types.h
arch/x86/kernel/fpu/xstate.c
arch/x86/kernel/fpu/xstate.h

index eb17f31b06d25c384bc831640b463008cf9284bf..de16862bf230bc7db0fbe764872a06fec5af3d4d 100644 (file)
@@ -591,6 +591,13 @@ struct fpu_state_config {
         * even without XSAVE support, i.e. legacy features FP + SSE
         */
        u64 legacy_features;
+       /*
+        * @independent_features:
+        *
+        * Features that are supported by XSAVES, but not managed as part of
+        * the FPU core, such as LBR
+        */
+       u64 independent_features;
 };
 
 /* FPU state configuration information */
index c5a026fee5e061a463133ef2e78baaeef32500d1..1339f8328db5ade3b1a8931ee02865d38646f27e 100644 (file)
@@ -788,6 +788,9 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
                goto out_disable;
        }
 
+       fpu_kernel_cfg.independent_features = fpu_kernel_cfg.max_features &
+                                             XFEATURE_MASK_INDEPENDENT;
+
        /*
         * Clear XSAVE features that are disabled in the normal CPUID.
         */
index 2ee0b9c53dccc42a4eb0ba9774cd2979e06a342b..afb404cd20591100261ba105617a22db722a8633 100644 (file)
@@ -62,9 +62,9 @@ static inline u64 xfeatures_mask_supervisor(void)
 static inline u64 xfeatures_mask_independent(void)
 {
        if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
-               return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
+               return fpu_kernel_cfg.independent_features & ~XFEATURE_MASK_LBR;
 
-       return XFEATURE_MASK_INDEPENDENT;
+       return fpu_kernel_cfg.independent_features;
 }
 
 /* XSAVE/XRSTOR wrapper functions */