[PATCH RFC] x86/fpu: set X86_FEATURE_OSXSAVE feature after enabling OSXSAVE in CR4

From: Feng Tang
Date: Mon Jul 31 2023 - 04:45:34 EST


Commit b81fac906a8f ("x86/fpu: Move FPU initialization into
arch_cpu_finalize_init()" optimized the FPU init order, and moves the
CR4_OSXSAVE enabling into a later flace.

arch_cpu_finalize_init
identify_boot_cpu
identify_cpu
generic_identify
get_cpu_cap --> setup cpu capability
...
fpu__init_cpu
fpu__init_cpu_xstate
cr4_set_bits(X86_CR4_OSXSAVE);

This makes 'X86_FEATURE_OSXSAVE' missed in cpu capability setup. Many
security module like 'camellia_aesni_avx_x86_64' depends on this feature
and will fail to load after the commit. 0Day caught a 34% regression
in stress-ng's 'af-alg' test case [1].

So set X86_FEATURE_OSXSAVE feature after OSXSAVE enabling to fix it.

[1]. https://lore.kernel.org/lkml/202307192135.203ac24e-oliver.sang@xxxxxxxxx/

Fixes: b81fac906a8f ("x86/fpu: Move FPU initialization into arch_cpu_finalize_init()")
Reported-by: kernel test robot <oliver.sang@xxxxxxxxx>
Signed-off-by: Feng Tang <feng.tang@xxxxxxxxx>
---
arch/x86/kernel/fpu/xstate.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index 0bab497c9436..8ebea0d522d2 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -173,6 +173,9 @@ void fpu__init_cpu_xstate(void)

cr4_set_bits(X86_CR4_OSXSAVE);

+ if (!boot_cpu_has(X86_FEATURE_OSXSAVE))
+ setup_force_cpu_cap(X86_FEATURE_OSXSAVE);
+
/*
* Must happen after CR4 setup and before xsetbv() to allow KVM
* lazy passthrough. Write independent of the dynamic state static
--
2.27.0