x86/fpu: Fix nofxsr regression
PATCH 1/1 x86/fpu: Fix nofxsr regression
From: Andi Kleen <[email protected]>
Vegard Nossum reports:
The commit for this patch in mainline
(ccb18db2ab9d ("x86/fpu: Make XSAVE check ...")) causes the kernel to hang on boot when passing the "nofxsr" option:
$ kvm -cpu host -kernel arch/x86/boot/bzImage -append "console=ttyS0 nofxsr earlyprintk=ttyS0" -serial stdio -display none -smp 2 early console in extract_kernel input_data: 0x0000000001dea276 input_len: 0x0000000000500704 output: 0x0000000001000000 output_len: 0x00000000012c79b4 kernel_total_size: 0x0000000000f24000 booted via startup_32() Physical KASLR using RDRAND RDTSC... Virtual KASLR using RDRAND RDTSC...
Decompressing Linux... Parsing ELF... Performing relocations... done. Booting the kernel.
[..hang..]
<<<
Sebastian Siewior did the following analysis:
as a result of nofxsr we do:
[0] setup_clear_cpu_cap(X86_FEATURE_FXSR); [1] setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT); [2] setup_clear_cpu_cap(X86_FEATURE_XMM);
the commit in question removes then XFEATURE_MASK_SSE from `xfeatures_mask'.
Boot stops in fpu__init_cpu_xstate() / xsetbv() due to #GP:
|If an attempt is made to set XCR0[2:1] to 10b. (from Vol. 2C).
[1] is "harmless". Dropping [2] does not fix the issue because [0] still clears all three flags due to
| static const struct cpuid_dep cpuid_deps[] = {
…
| { X86_FEATURE_XMM, X86_FEATURE_FXSR },
Clearing additionally XMM2 (and adding the missing bits to xsave_cpuid_features/xfeature_names) would boot further. Later it crashes in raid6 while probing for AVX/2 code…
Disabling XMM+XMM2 in order get (and fixing it up for AVX+AVX2) would give use XSAVE instead of FSAVE.
This won't work on 64bit userland because it expects SSE to be around (and FXSR to save the SSE bits).
Even my 32bit Debian Wheezy doesn't work because it wants FXSR :)
So if it is unlikely to have XSAVE but no FXSR I would suggest to add "fpu__xstate_clear_all_cpu_caps()" to nofxsr and behave like "nofxsr noxsave".
<<<
Also nofxsr is useless on 64bit kernels because 64bit user space always uses SSE2, and without FXSR there is no SSE support.
This patch:
- Makes nofxsr 32bit only
It was already documented to be 32bit only, but not implemented this way.
- Implements Sebastian's suggestion of calling fpu__xstate_clear_all_cpu_caps() for nofxsr to clear all depending bits.
With this a 32bit kernel boots on qemu with nofxsr upto user space crashing (I don't have a 32bit image that doesn't need SSE2), and a 64bit kernel also fully boots with nofxsr (by ignoring the option)
--- arch/x86/kernel/fpu/init.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/fpu/init.c +3 -1
@@ -255,7 +255,9 @@ static void __init fpu__init_parse_early_param(void)if (cmdline_find_option_bool (boot_command_line,"no387" ))setup_clear_cpu_cap (X86_FEATURE_FPU ); -if (cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +if (!IS_ENABLED (CONFIG_64BIT ) && +cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +fpu__xstate_clear_all_cpu_caps ();setup_clear_cpu_cap (X86_FEATURE_FXSR );setup_clear_cpu_cap (X86_FEATURE_FXSR_OPT );setup_clear_cpu_cap (X86_FEATURE_XMM );
Signature · 1 line
2.20.1
arch/x86/kernel/fpu/init.c static void __init fpu__init_parse_early_param(void) · line 263
Quoted 8 lines · Andi Kleen
arch/x86/kernel/fpu/init.c +3
-1-if (cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +if (!IS_ENABLED (CONFIG_64BIT ) && +cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +fpu__xstate_clear_all_cpu_caps ();setup_clear_cpu_cap (X86_FEATURE_FXSR );setup_clear_cpu_cap (X86_FEATURE_FXSR_OPT );setup_clear_cpu_cap (X86_FEATURE_XMM );
This is a mixture of disabling features explicitely and having the dependencies in cpuid-deps. Even 2 of the existing ones are pointless because clear(FXSR) already clears the other two.
Why not make XSAVE depend on XMM or whatever is the right dependency?
Thanks,
tglx
Quoted 14 lines · Thomas Gleixner
arch/x86/kernel/fpu/init.c static void __init fpu__init_parse_early_param(void) · line 263
Quoted 8 lines · Andi Kleen
arch/x86/kernel/fpu/init.c +3
-1-if (cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +if (!IS_ENABLED (CONFIG_64BIT ) && +cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +fpu__xstate_clear_all_cpu_caps ();setup_clear_cpu_cap (X86_FEATURE_FXSR );setup_clear_cpu_cap (X86_FEATURE_FXSR_OPT );setup_clear_cpu_cap (X86_FEATURE_XMM );This is a mixture of disabling features explicitely and having the dependencies in cpuid-deps. Even 2 of the existing ones are pointless because clear(FXSR) already clears the other two.
Quoted 1 line
Why not make XSAVE depend on XMM or whatever is the right dependency?
I have something half way done.
Quoted 3 lines
Thanks,
tglx
Sebastian
PATCH 1/1 x86/fpu: Make no387 and nofxsr work a little better on modern CPUs
In reply to Thomas GleixnerThe command line option `no387' is designed to disable the FPU entirely. The documentation says to disable the coprocessor and the Kconfig entry for MATH_EMULATION says to set it in order to use emulation. It should be restricted to 32bit only because 64bit expect SSE (which includes basic FPU and there is not emulation for SSE).
The command line option `nofxsr' should also be limited to 32bit because FXSR is part of the required flags on 64bit so turning it off is not possible.
Clearing X86_FEATURE_FPU without emulation enabled will not work anyway and hang in fpu__init_system_early_generic() before console is enabled.
Setting additioal dependencies, ensures that the CPU still boots on a modern CPU. Otherwise, dropping FPU will leave FXSR enabled causing the kernel to crash early in fpu__init_system_mxcsr().
With XSAVE support it will crash in fpu__init_cpu_xstate(). The problem is that xsetbv() with YMM set and SSE cleared is not allowed. That means XSAVE has to be disabled. The XSAVE support is disabled in fpu__init_system_xstate_size_legacy() but it is too late. It can be removed, it has been added in commit
1f999ab5a1360 ("x86, xsave: Disable xsave in i387 emulation mode")
to use `no387' on a CPU with XSAVE support.
All this happens before console output.
After hat, the next possible crash is in RAID6 detect code because MMX remained enabled. With a 3DNOW enabled config it will explode in memcpy() for instance due to kernel_fpu_begin() but this is unconditionally enabled.
This is enough to boot a Debian Wheezy on a 32bit qemu "host" CPU which supports everything up to XSAVES, AVX2 without 3DNOW. Later, Debian increased the minimum requirements to i686 which means it does not boot userland atleast due to CMOV.
After masking the additional features it still keeps SSE4A and 3DNOW* enabled (if present on the host) but those are unused in the kernel.
Restrict no387' and nofxsr' otions to 32bit only. Add dependencies for FPU, FXSR to additionaly mask CMOV, MMX, XSAVE if FXSR or FPU is cleared.
--- arch/x86/kernel/cpu/cpuid-deps.c | 5 +++++ arch/x86/kernel/fpu/init.c | 17 +++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/cpuid-deps.c +5 -0
@@ -20,6 +20,7 @@ struct cpuid_dep {* but it's difficult to tell that to the init reference checker. */ static const struct cpuid_dep cpuid_deps[] = { + {X86_FEATURE_FXSR ,X86_FEATURE_FPU }, {X86_FEATURE_XSAVEOPT ,X86_FEATURE_XSAVE }, {X86_FEATURE_XSAVEC ,X86_FEATURE_XSAVE }, {X86_FEATURE_XSAVES ,X86_FEATURE_XSAVE },
@@ -27,7 +28,11 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_PKU, X86_FEATURE_XSAVE }, { X86_FEATURE_MPX, X86_FEATURE_XSAVE }, { X86_FEATURE_XGETBV1, X86_FEATURE_XSAVE }, + { X86_FEATURE_CMOV, X86_FEATURE_FXSR }, + { X86_FEATURE_MMX, X86_FEATURE_FXSR }, + { X86_FEATURE_MMXEXT, X86_FEATURE_MMX }, { X86_FEATURE_FXSR_OPT, X86_FEATURE_FXSR }, + { X86_FEATURE_XSAVE, X86_FEATURE_FXSR }, { X86_FEATURE_XMM, X86_FEATURE_FXSR }, { X86_FEATURE_XMM2, X86_FEATURE_XMM }, { X86_FEATURE_XMM3, X86_FEATURE_XMM2 },
arch/x86/kernel/fpu/init.c +7 -10
@@ -204,12 +204,6 @@ static void __init fpu__init_system_xstate_size_legacy(void)*/ if (!boot_cpu_has (X86_FEATURE_FPU )) { -/* -* Disable xsave as we do not support it if i387 -* emulation is enabled. -*/ -setup_clear_cpu_cap (X86_FEATURE_XSAVE ); -setup_clear_cpu_cap (X86_FEATURE_XSAVEOPT ); fpu_kernel_xstate_size =sizeof (struct swregs_state ); }else {if (boot_cpu_has (X86_FEATURE_FXSR ))
@@ -252,14 +246,17 @@ static void __init fpu__init_parse_early_param(void)char *argptr = arg;int bit; +#ifdef CONFIG_X86_32 if (cmdline_find_option_bool(boot_command_line, "no387")) +#ifdef CONFIG_MATH_EMULATION setup_clear_cpu_cap (X86_FEATURE_FPU ); +#else +pr_err ("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n" ); +#endif -if (cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +if (cmdline_find_option_bool (boot_command_line,"nofxsr" ))setup_clear_cpu_cap (X86_FEATURE_FXSR ); -setup_clear_cpu_cap (X86_FEATURE_FXSR_OPT ); -setup_clear_cpu_cap (X86_FEATURE_XMM ); - } +#endif if (cmdline_find_option_bool (boot_command_line,"noxsave" ))fpu__xstate_clear_all_cpu_caps ();
Signature · 1 line
2.20.1
PATCH 1/1 x86/fpu: Inline fpu__xstate_clear_all_cpu_caps()
All fpu__xstate_clear_all_cpu_caps() does is to invoke one simple function since commit
73e3a7d2a7c3b ("x86/fpu: Remove the explicit clearing of XSAVE dependent features")
so it will be better to simply invoke that function directly.
Inline fpu__xstate_clear_all_cpu_caps().
--- arch/x86/include/asm/fpu/xstate.h | 1 - arch/x86/kernel/fpu/init.c | 2 +- arch/x86/kernel/fpu/xstate.c | 11 +---------- 3 files changed, 2 insertions(+), 12 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -1
@@ -47,7 +47,6 @@ extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];extern void __init update_regset_xstate_info (unsigned int size,u64 xstate_mask); -void fpu__xstate_clear_all_cpu_caps (void );void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr);const void *get_xsave_field_ptr (int xfeature_nr);int using_compacted_format (void );
arch/x86/kernel/fpu/init.c +1 -1
@@ -259,7 +259,7 @@ static void __init fpu__init_parse_early_param(void)#endif if (cmdline_find_option_bool (boot_command_line,"noxsave" )) -fpu__xstate_clear_all_cpu_caps (); +setup_clear_cpu_cap (X86_FEATURE_XSAVE );if (cmdline_find_option_bool (boot_command_line,"noxsaveopt" ))setup_clear_cpu_cap (X86_FEATURE_XSAVEOPT );
arch/x86/kernel/fpu/xstate.c +1 -10
@@ -67,15 +67,6 @@ static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];*/ unsigned int fpu_user_xstate_size; -/* -* Clear all of the X86_FEATURE_* bits that are unavailable -* when the CPU has no XSAVE support. -*/ -void fpu__xstate_clear_all_cpu_caps (void ) -{ -setup_clear_cpu_cap (X86_FEATURE_XSAVE ); -} -/* * Return whether the system supports a given xfeature. *
@@ -709,7 +700,7 @@ static void fpu__init_disable_system_xstate(void) { xfeatures_mask =0 ;cr4_clear_bits (X86_CR4_OSXSAVE ); -fpu__xstate_clear_all_cpu_caps (); +setup_clear_cpu_cap (X86_FEATURE_XSAVE ); }/*
Signature · 1 line
2.20.1
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Inline fpu__xstate_clear_all_cpu_caps()
x86/fpu: Inline fpu__xstate_clear_all_cpu_caps()
Commit-ID: 7891bc0ab739a31538b5f879a523232b8b07a0d3 Gitweb: https://git.kernel.org/tip/7891bc0ab739a31538b5f879a523232b8b07a0d3 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 4 Jul 2019 08:07:43 +0200 Committer: Thomas Gleixner <[email protected]> CommitDate: Sun, 7 Jul 2019 12:01:47 +0200
x86/fpu: Inline fpu__xstate_clear_all_cpu_caps()
All fpu__xstate_clear_all_cpu_caps() does is to invoke one simple function since commit
73e3a7d2a7c3b ("x86/fpu: Remove the explicit clearing of XSAVE dependent features")
so invoke that function directly and remove the wrapper.
--- arch/x86/include/asm/fpu/xstate.h | 1 - arch/x86/kernel/fpu/init.c | 2 +- arch/x86/kernel/fpu/xstate.c | 11 +---------- 3 files changed, 2 insertions(+), 12 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -1
@@ -47,7 +47,6 @@ extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];extern void __init update_regset_xstate_info (unsigned int size,u64 xstate_mask); -void fpu__xstate_clear_all_cpu_caps (void );void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr);const void *get_xsave_field_ptr (int xfeature_nr);int using_compacted_format (void );
arch/x86/kernel/fpu/init.c +1 -1
@@ -259,7 +259,7 @@ static void __init fpu__init_parse_early_param(void)#endif if (cmdline_find_option_bool (boot_command_line,"noxsave" )) -fpu__xstate_clear_all_cpu_caps (); +setup_clear_cpu_cap (X86_FEATURE_XSAVE );if (cmdline_find_option_bool (boot_command_line,"noxsaveopt" ))setup_clear_cpu_cap (X86_FEATURE_XSAVEOPT );
arch/x86/kernel/fpu/xstate.c +1 -10
@@ -67,15 +67,6 @@ static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];*/ unsigned int fpu_user_xstate_size; -/* -* Clear all of the X86_FEATURE_* bits that are unavailable -* when the CPU has no XSAVE support. -*/ -void fpu__xstate_clear_all_cpu_caps (void ) -{ -setup_clear_cpu_cap (X86_FEATURE_XSAVE ); -} -/* * Return whether the system supports a given xfeature. *
@@ -709,7 +700,7 @@ static void fpu__init_disable_system_xstate(void) { xfeatures_mask =0 ;cr4_clear_bits (X86_CR4_OSXSAVE ); -fpu__xstate_clear_all_cpu_caps (); +setup_clear_cpu_cap (X86_FEATURE_XSAVE ); }/*
tip-bot for Sebastian Andrzej Siewior
merge bot[tip:x86/fpu] x86/fpu: Make 'no387' and 'nofxsr' command line options useful
In reply to Sebastian Andrzej Siewiorx86/fpu: Make 'no387' and 'nofxsr' command line options useful
Commit-ID: 9838e3bff0f92f23fcd50fe1ff1d4b3e91b8a448 Gitweb: https://git.kernel.org/tip/9838e3bff0f92f23fcd50fe1ff1d4b3e91b8a448 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Wed, 3 Jul 2019 10:32:47 +0200 Committer: Thomas Gleixner <[email protected]> CommitDate: Sun, 7 Jul 2019 12:01:46 +0200
x86/fpu: Make 'no387' and 'nofxsr' command line options useful
The command line option `no387' is designed to disable the FPU entirely. This only 'works' with CONFIG_MATH_EMULATION enabled.
But on 64bit this cannot work because user space expects SSE to work which required basic FPU support. MATH_EMULATION does not help because SSE is not emulated.
The command line option `nofxsr' should also be limited to 32bit because FXSR is part of the required flags on 64bit so turning it off is not possible.
Clearing X86_FEATURE_FPU without emulation enabled will not work anyway and hang in fpu__init_system_early_generic() before the console is enabled.
Setting additioal dependencies, ensures that the CPU still boots on a modern CPU. Otherwise, dropping FPU will leave FXSR enabled causing the kernel to crash early in fpu__init_system_mxcsr().
With XSAVE support it will crash in fpu__init_cpu_xstate(). The problem is that xsetbv() with XMM set and SSE cleared is not allowed. That means XSAVE has to be disabled. The XSAVE support is disabled in fpu__init_system_xstate_size_legacy() but it is too late. It can be removed, it has been added in commit
1f999ab5a1360 ("x86, xsave: Disable xsave in i387 emulation mode")
to use `no387' on a CPU with XSAVE support.
All this happens before console output.
After hat, the next possible crash is in RAID6 detect code because MMX remained enabled. With a 3DNOW enabled config it will explode in memcpy() for instance due to kernel_fpu_begin() but this is unconditionally enabled.
This is enough to boot a Debian Wheezy on a 32bit qemu "host" CPU which supports everything up to XSAVES, AVX2 without 3DNOW. Later, Debian increased the minimum requirements to i686 which means it does not boot userland atleast due to CMOV.
After masking the additional features it still keeps SSE4A and 3DNOW* enabled (if present on the host) but those are unused in the kernel.
Restrict no387' and nofxsr' otions to 32bit only. Add dependencies for FPU, FXSR to additionaly mask CMOV, MMX, XSAVE if FXSR or FPU is cleared.
--- arch/x86/kernel/cpu/cpuid-deps.c | 5 +++++ arch/x86/kernel/fpu/init.c | 17 +++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/cpuid-deps.c +5 -0
@@ -20,6 +20,7 @@ struct cpuid_dep {* but it's difficult to tell that to the init reference checker. */ static const struct cpuid_dep cpuid_deps[] = { + {X86_FEATURE_FXSR ,X86_FEATURE_FPU }, {X86_FEATURE_XSAVEOPT ,X86_FEATURE_XSAVE }, {X86_FEATURE_XSAVEC ,X86_FEATURE_XSAVE }, {X86_FEATURE_XSAVES ,X86_FEATURE_XSAVE },
@@ -27,7 +28,11 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_PKU, X86_FEATURE_XSAVE }, { X86_FEATURE_MPX, X86_FEATURE_XSAVE }, { X86_FEATURE_XGETBV1, X86_FEATURE_XSAVE }, + { X86_FEATURE_CMOV, X86_FEATURE_FXSR }, + { X86_FEATURE_MMX, X86_FEATURE_FXSR }, + { X86_FEATURE_MMXEXT, X86_FEATURE_MMX }, { X86_FEATURE_FXSR_OPT, X86_FEATURE_FXSR }, + { X86_FEATURE_XSAVE, X86_FEATURE_FXSR }, { X86_FEATURE_XMM, X86_FEATURE_FXSR }, { X86_FEATURE_XMM2, X86_FEATURE_XMM }, { X86_FEATURE_XMM3, X86_FEATURE_XMM2 },
arch/x86/kernel/fpu/init.c +7 -10
@@ -204,12 +204,6 @@ static void __init fpu__init_system_xstate_size_legacy(void)*/ if (!boot_cpu_has (X86_FEATURE_FPU )) { -/* -* Disable xsave as we do not support it if i387 -* emulation is enabled. -*/ -setup_clear_cpu_cap (X86_FEATURE_XSAVE ); -setup_clear_cpu_cap (X86_FEATURE_XSAVEOPT ); fpu_kernel_xstate_size =sizeof (struct swregs_state ); }else {if (boot_cpu_has (X86_FEATURE_FXSR ))
@@ -252,14 +246,17 @@ static void __init fpu__init_parse_early_param(void)char *argptr = arg;int bit; +#ifdef CONFIG_X86_32 if (cmdline_find_option_bool(boot_command_line, "no387")) +#ifdef CONFIG_MATH_EMULATION setup_clear_cpu_cap (X86_FEATURE_FPU ); +#else +pr_err ("Option 'no387' required CONFIG_MATH_EMULATION enabled.\n" ); +#endif -if (cmdline_find_option_bool (boot_command_line,"nofxsr" )) { +if (cmdline_find_option_bool (boot_command_line,"nofxsr" ))setup_clear_cpu_cap (X86_FEATURE_FXSR ); -setup_clear_cpu_cap (X86_FEATURE_FXSR_OPT ); -setup_clear_cpu_cap (X86_FEATURE_XMM ); - } +#endif if (cmdline_find_option_bool (boot_command_line,"noxsave" ))fpu__xstate_clear_all_cpu_caps ();