x86, fpu: disable XSAVE if init buf too small
RFC PATCH 1/2 x86, fpu: disable XSAVE if init buf too small
This is _very_ lightly tested. Just RFC for now. The fallback code is not tested at all because I'm hitting another bug.
--
The recent x86 FPU rework changed a dynamic allocation to a static one. But, the static one is undersized and we overrun it, corrupting memory in some cases.
This patch detects the situation and disables the XSAVE feature.
I have not been able to test this in practice because I'm hitting another bug. tsk->signal gets corrupted if I disable XSAVE. tsk->signal is conveniently located a few hundred bytes after the 'struct fpu' which is now embedded in task_struct, so I suspect this is another overrun. Anyone should be able to reproduce this by under-sizing XSTATE_MAX_SIZE in the next patch.
---
b/arch/x86/include/asm/fpu/internal.h | 1 b/arch/x86/kernel/fpu/init.c | 17 ++++++++------ b/arch/x86/kernel/fpu/xstate.c | 41 +++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 20 deletions(-)
arch/x86/include/asm/fpu/internal.h renamed from arch/x86/include/asm/fpu/internal.h~disable-xsave-if-init-buf-too-small +1 -0
@@ -42,6 +42,7 @@ extern void fpu__init_cpu_xstate(void);extern void fpu__init_system (struct cpuinfo_x86 *c);extern void fpu__init_check_bugs (void );extern void fpu__resume_cpu (void ); +extern void fpu__clear_all_xsave_cpu_caps (void );/* * Debugging facility:
arch/x86/kernel/fpu/init.c renamed from arch/x86/kernel/fpu/init.c~disable-xsave-if-init-buf-too-small +10 -7
@@ -301,20 +301,23 @@ static int __init no_387(char *s) }__setup ("no387" , no_387); -/* -* Disable all xstate CPU features: -*/ -static int __init x86_noxsave_setup (char *s) +void fpu__clear_all_xsave_cpu_caps (void ) { -if (strlen (s)) -return 0 ; -setup_clear_cpu_cap (X86_FEATURE_XSAVE );setup_clear_cpu_cap (X86_FEATURE_XSAVEOPT );setup_clear_cpu_cap (X86_FEATURE_XSAVES );setup_clear_cpu_cap (X86_FEATURE_AVX );setup_clear_cpu_cap (X86_FEATURE_AVX2 ); +} +/* +* Disable all xstate CPU features: +*/ +static int __init x86_noxsave_setup (char *s) +{ +if (strlen (s)) +return 0 ; +fpu__clear_all_xsave_cpu_caps ();return 1 ; }__setup ("noxsave" , x86_noxsave_setup);
arch/x86/kernel/fpu/xstate.c renamed from arch/x86/kernel/fpu/xstate.c~disable-xsave-if-init-buf-too-small +28 -13
61 lines not on this page · raw message
_
RFC PATCH 2/2 x86, fpu: enlarge xregs_state
This takes a stab at sizing the xsave buffer so that it will work for AVX-512 systems. The logic is explained in the comment.
---
b/arch/x86/include/asm/fpu/types.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-)
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~enlarge-init-fpu-buf +20 -7
@@ -159,10 +159,19 @@ struct xstate_header {u64 reserved[6 ]; } __attribute__((packed)); -/* New processor state extensions should be added here: */ -#define XSTATE_RESERVE (sizeof(struct ymmh_struct) + \ - sizeof(struct lwp_struct) + \ - sizeof(struct mpx_struct) ) +/* +* The largest xsave buffer known today is 2752 bytes on a system +* implementing AVX-512. This includes the 512-byte i387 state +* and 64-byte header. We add a small amount of padding in case +* an implementation adds some padding or wastes some space. +* +* Note, if we overflow this, we will disable XSAVE completely. +* +* Also, note that the real size we need is enumerated by +* cpuid leaves and can not be known at compile time. +*/ +#define XSTATE_MAX_SIZE (2752 + 256) +/* * This is our most modern FPU state format, as saved by the XSAVE * and restored by the XRSTOR instructions.
@@ -172,9 +181,13 @@ struct xstate_header {* Not all CPUs support all the extensions. */ struct xregs_state { -struct fxregs_state i387; -struct xstate_header header; -u8 __reserved[XSTATE_RESERVE ]; +union { +struct { +struct fxregs_state i387; +struct xstate_header header; + }; +u8 __reserved[XSTATE_MAX_SIZE ]; + }; } __attribute__ ((packed,aligned (64 )));/*
_
Re: [RFC][PATCH 2/2] x86, fpu: enlarge xregs_state
arch/x86/include/asm/fpu/types.h struct xstate_header { · line 173
Quoted 12 lines · Dave Hansen
arch/x86/include/asm/fpu/types.h renamed from arch/x86/include/asm/fpu/types.h~enlarge-init-fpu-buf +12
-0+/* +* The largest xsave buffer known today is 2752 bytes on a system +* implementing AVX-512. This includes the 512-byte i387 state +* and 64-byte header. We add a small amount of padding in case +* an implementation adds some padding or wastes some space. +* +* Note, if we overflow this, we will disable XSAVE completely. +* +* Also, note that the real size we need is enumerated by +* cpuid leaves and can not be known at compile time. +*/ +#define XSTATE_MAX_SIZE (2752 + 256)
BTW, this has one big-ish side-effect. It takes the size of task_struct from ~3.5k to ~5.4k for me:
slabinfo before:task_struct 198 198 3456 9 8 : ...
after:task_struct 166 180 5376 6 8 : ...
I'm sure folks on small systems are going to cringe at eating 2k/thread, so we've got to revisit this _somehow_.