x86/fpu: Move register state into a container struct (part 2)
cover x86/fpu: Move register state into a container struct (part 2)
This is the second part of the effort to support AMX. The first part can be found here:
https://lore.kernel.org/r/[email protected]
With AMX the FPU register state buffer which is part of task_struct::thread::fpu is not going to be extended unconditionally for all tasks on an AMX enabled system as that would waste minimum 8K per task.
AMX provides a mechanism to trap on first use. That trap will be utilized to allocate a larger register state buffer when the task (process) has permissions to use it. The default buffer task_struct will only carry states up to AVX512.
The original approach was to just allocate new register buffer, but that's not the right abstraction.
The current series creates a container which carries information about the fpstate buffer, i.e. feature bits (user and kernel) and sizes (user and kernel).
That allows all relevant code pathes to retrieve the required information from fpstate which avoids conditionals and lets the code just use this instead of reading it from the various global variables which provide this information today.
The series is first introducing the new structure and then converting all usage sites over to it. After that it adds feature and size information and converts the affected code over to use that.
This series is based on:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu-1
and also available from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu-2
The full series with part 3 and 4 on top is available here:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu
Thanks,
tglx
--- include/asm/fpu/api.h | 5 + include/asm/fpu/signal.h | 2 include/asm/fpu/types.h | 42 +++++++++++--- include/asm/fpu/xstate.h | 13 ---- include/asm/processor.h | 9 +-- include/asm/trace/fpu.h | 4 - kernel/fpu/context.h | 2 kernel/fpu/core.c | 140 +++++++++++++++++++++++++++-------------------- kernel/fpu/init.c | 16 ++++- kernel/fpu/internal.h | 7 +- kernel/fpu/regset.c | 28 ++++----- kernel/fpu/signal.c | 70 +++++++++++++---------- kernel/fpu/xstate.c | 56 ++++++++++++------ kernel/fpu/xstate.h | 24 ++++++-- kernel/process.c | 2 kvm/x86.c | 18 ++---- math-emu/fpu_aux.c | 2 math-emu/fpu_entry.c | 4 - math-emu/fpu_system.h | 2 19 files changed, 266 insertions(+), 180 deletions(-)
PATCH 1/21 x86/fpu: Provide struct fpstate
New xfeatures will not longer be automatically stored in the regular XSAVE buffer in thread_struct::fpu.
The kernel will provide the default sized buffer for storing the regular features up to AVX512 in thread_struct::fpu and if a task requests to use one of the new features then the register storage has to be extendend.
The state will be accessed via a pointer in thread_struct::fpu which defaults to the builtin storage and can be switched when extended storage is required.
To avoid conditionals all over the code, create a new container for the register storage which will gain other information, e.g. size, feature masks etc., later. For now it just contains the register storage, which gives it exactly the same layout as the exiting fpu::state.
Stick fpu::state and the new fpu::__fpstate into an anonymous union and initialize the pointer. Add build time checks to validate that both are at the same place and have the same size.
This allows step by step conversion of all users.
--- arch/x86/include/asm/fpu/types.h | 20 +++++++++++++++++++- arch/x86/include/asm/processor.h | 4 ++-- arch/x86/kernel/fpu/core.c | 11 ++++++++++- arch/x86/kernel/fpu/init.c | 9 +++++++-- arch/x86/kernel/fpu/internal.h | 1 + 5 files changed, 39 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/types.h +19 -1
@@ -309,6 +309,13 @@ union fpregs_state {u8 __padding[PAGE_SIZE ]; }; +struct fpstate { +/* @regs: The register state union for all supported formats */ +union fpregs_state regs; + +/* @regs is dynamically sized! Don't add anything after @regs! */ +} __attribute__ ((aligned (64 ))); +/* * Highest level per task FPU state data structure that * contains the FPU register state plus various FPU
@@ -337,6 +344,14 @@ struct fpu {unsigned long avx512_timestamp;/* +* @fpstate: +* +* Pointer to the active struct fpstate. Initialized to +* point at @__fpstate below. +*/ +struct fpstate *fpstate; + +/* * @state: * * In-memory copy of all FPU registers that we save/restore
@@ -345,7 +360,10 @@ struct fpu {* copy. If the task context-switches away then they get * saved here and represent the FPU state. */ -union fpregs_state state; +union { +struct fpstate __fpstate; +union fpregs_state state; + };/* * WARNING: 'state' is dynamically-sized. Do not put * anything after it here.
arch/x86/include/asm/processor.h +2 -2
@@ -537,11 +537,11 @@ struct thread_struct {*/ }; -/* Whitelist the FPU state from the task_struct for hardened usercopy. */ +/* Whitelist the FPU register state from the task_struct for hardened usercopy. */ static inline void arch_thread_struct_whitelist (unsigned long *offset,unsigned long *size) { - *offset = offsetof(struct thread_struct, fpu.state); + *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); *size = fpu_kernel_xstate_size; }
arch/x86/kernel/fpu/core.c +10 -1
@@ -337,10 +337,17 @@ void fpstate_init_user(union fpregs_statfpstate_init_fstate (&state->fsave); } +void fpstate_reset (struct fpu *fpu) +{ +/* Set the fpstate pointer to the default fpstate */ + fpu->fpstate = &fpu->__fpstate; +} +#if IS_ENABLED (CONFIG_KVM )void fpu_init_fpstate_user (struct fpu *fpu) { -fpstate_init_user (&fpu->state); +fpstate_reset (fpu); +fpstate_init_user (&fpu->fpstate->regs); }EXPORT_SYMBOL_GPL (fpu_init_fpstate_user);#endif
@@ -354,6 +361,8 @@ int fpu_clone(struct task_struct *dst)/* The new task's FPU state cannot be valid in the hardware. */ dst_fpu->last_cpu =-1 ; +fpstate_reset (dst_fpu); +if (!cpu_feature_enabled (X86_FEATURE_FPU ))return 0 ;
arch/x86/kernel/fpu/init.c +7 -2
@@ -165,7 +165,7 @@ static void __init fpu__init_task_struct* Subtract off the static size of the register state. * It potentially has a bunch of padding. */ - task_size -=sizeof (((struct task_struct *)0 )->thread.fpu.state); + task_size -=sizeof (current->thread.fpu.__fpstate.regs);/* * Add back the dynamically-calculated register state
@@ -180,10 +180,14 @@ static void __init fpu__init_task_struct* you hit a compile error here, check the structure to * see if something got added to the end. */ - CHECK_MEMBER_AT_END_OF(struct fpu, state); + CHECK_MEMBER_AT_END_OF(struct fpu, __fpstate); CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu); CHECK_MEMBER_AT_END_OF(struct task_struct, thread); +BUILD_BUG_ON (sizeof (struct fpstate ) !=sizeof (union fpregs_state )); + BUILD_BUG_ON(offsetof(struct thread_struct, fpu.state) != + offsetof(struct thread_struct, fpu.__fpstate)); + arch_task_struct_size = task_size; }
@@ -220,6 +224,7 @@ static void __init fpu__init_system_xsta*/ void __init fpu__init_system (struct cpuinfo_x86 *c) { +fpstate_reset (¤t->thread.fpu);fpu__init_system_early_generic (c);/*
arch/x86/kernel/fpu/internal.h +1 -0
@@ -26,5 +26,6 @@ extern void fpu__init_prepare_fx_sw_fram/* Used in init.c */ extern void fpstate_init_user (union fpregs_state *state); +extern void fpstate_reset (struct fpu *fpu);#endif
[tip: x86/fpu] x86/fpu: Provide struct fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 87d0e5be0fac322f4415128def9f16a71a267a40 Gitweb: https://git.kernel.org/tip/87d0e5be0fac322f4415128def9f16a71a267a40 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:27 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:26:24 +02:00
x86/fpu: Provide struct fpstate
New xfeatures will not longer be automatically stored in the regular XSAVE buffer in thread_struct::fpu.
The kernel will provide the default sized buffer for storing the regular features up to AVX512 in thread_struct::fpu and if a task requests to use one of the new features then the register storage has to be extended.
The state will be accessed via a pointer in thread_struct::fpu which defaults to the builtin storage and can be switched when extended storage is required.
To avoid conditionals all over the code, create a new container for the register storage which will gain other information, e.g. size, feature masks etc., later. For now it just contains the register storage, which gives it exactly the same layout as the exiting fpu::state.
Stick fpu::state and the new fpu::__fpstate into an anonymous union and initialize the pointer. Add build time checks to validate that both are at the same place and have the same size.
This allows step by step conversion of all users.
--- arch/x86/include/asm/fpu/types.h | 20 +++++++++++++++++++- arch/x86/include/asm/processor.h | 4 ++-- arch/x86/kernel/fpu/core.c | 11 ++++++++++- arch/x86/kernel/fpu/init.c | 9 +++++++-- arch/x86/kernel/fpu/internal.h | 1 + 5 files changed, 39 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/types.h +19 -1
@@ -309,6 +309,13 @@ union fpregs_state {u8 __padding[PAGE_SIZE ]; }; +struct fpstate { +/* @regs: The register state union for all supported formats */ +union fpregs_state regs; + +/* @regs is dynamically sized! Don't add anything after @regs! */ +}__aligned(64) ; +/* * Highest level per task FPU state data structure that * contains the FPU register state plus various FPU
@@ -337,6 +344,14 @@ struct fpu {unsigned long avx512_timestamp;/* +* @fpstate: +* +* Pointer to the active struct fpstate. Initialized to +* point at @__fpstate below. +*/ +struct fpstate *fpstate; + +/* * @state: * * In-memory copy of all FPU registers that we save/restore
@@ -345,7 +360,10 @@ struct fpu {* copy. If the task context-switches away then they get * saved here and represent the FPU state. */ -union fpregs_state state; +union { +struct fpstate __fpstate; +union fpregs_state state; + };/* * WARNING: 'state' is dynamically-sized. Do not put * anything after it here.
arch/x86/include/asm/processor.h +2 -2
@@ -537,11 +537,11 @@ struct thread_struct {*/ }; -/* Whitelist the FPU state from the task_struct for hardened usercopy. */ +/* Whitelist the FPU register state from the task_struct for hardened usercopy. */ static inline void arch_thread_struct_whitelist (unsigned long *offset,unsigned long *size) { - *offset = offsetof(struct thread_struct, fpu.state); + *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); *size = fpu_kernel_xstate_size; }
arch/x86/kernel/fpu/core.c +10 -1
@@ -337,10 +337,17 @@ void fpstate_init_user(union fpregs_state *state)fpstate_init_fstate (&state->fsave); } +void fpstate_reset (struct fpu *fpu) +{ +/* Set the fpstate pointer to the default fpstate */ + fpu->fpstate = &fpu->__fpstate; +} +#if IS_ENABLED (CONFIG_KVM )void fpu_init_fpstate_user (struct fpu *fpu) { -fpstate_init_user (&fpu->state); +fpstate_reset (fpu); +fpstate_init_user (&fpu->fpstate->regs); }EXPORT_SYMBOL_GPL (fpu_init_fpstate_user);#endif
@@ -354,6 +361,8 @@ int fpu_clone(struct task_struct *dst)/* The new task's FPU state cannot be valid in the hardware. */ dst_fpu->last_cpu =-1 ; +fpstate_reset (dst_fpu); +if (!cpu_feature_enabled (X86_FEATURE_FPU ))return 0 ;
arch/x86/kernel/fpu/init.c +7 -2
@@ -165,7 +165,7 @@ static void __init fpu__init_task_struct_size(void)* Subtract off the static size of the register state. * It potentially has a bunch of padding. */ - task_size -=sizeof (((struct task_struct *)0 )->thread.fpu.state); + task_size -=sizeof (current->thread.fpu.__fpstate.regs);/* * Add back the dynamically-calculated register state
@@ -180,10 +180,14 @@ static void __init fpu__init_task_struct_size(void)* you hit a compile error here, check the structure to * see if something got added to the end. */ - CHECK_MEMBER_AT_END_OF(struct fpu, state); + CHECK_MEMBER_AT_END_OF(struct fpu, __fpstate); CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu); CHECK_MEMBER_AT_END_OF(struct task_struct, thread); +BUILD_BUG_ON (sizeof (struct fpstate ) !=sizeof (union fpregs_state )); + BUILD_BUG_ON(offsetof(struct thread_struct, fpu.state) != + offsetof(struct thread_struct, fpu.__fpstate)); + arch_task_struct_size = task_size; }
@@ -220,6 +224,7 @@ static void __init fpu__init_system_xstate_size_legacy(void)*/ void __init fpu__init_system (struct cpuinfo_x86 *c) { +fpstate_reset (¤t->thread.fpu);fpu__init_system_early_generic (c);/*
arch/x86/kernel/fpu/internal.h +1 -0
@@ -26,5 +26,6 @@ extern void fpu__init_prepare_fx_sw_frame(void);/* Used in init.c */ extern void fpstate_init_user (union fpregs_state *state); +extern void fpstate_reset (struct fpu *fpu);#endif
PATCH 2/21 x86/fpu: Convert fpstate_init() to struct fpstate
In reply to Thomas GleixnerConvert fpstate_init() and related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/core.c | 44 ++++++++++++++++++++--------------------- arch/x86/kernel/fpu/internal.h | 4 +-- arch/x86/kernel/fpu/signal.c | 2 - arch/x86/kernel/fpu/xstate.c | 12 +++++------ 4 files changed, 31 insertions(+), 31 deletions(-)
arch/x86/kernel/fpu/core.c +22 -22
@@ -29,7 +29,7 @@* Represents the initial FPU state. It's mostly (but not completely) zeroes, * depending on the FPU hardware format: */ -union fpregs_state init_fpstate__ro_after_init ; +struct fpstate init_fpstate__ro_after_init ;/* * Track whether the kernel is using the FPU state
@@ -157,7 +157,7 @@ void restore_fpregs_from_fpstate(union fvoid fpu_reset_from_exception_fixup (void ) { -restore_fpregs_from_fpstate (&init_fpstate,xfeatures_mask_fpstate ()); +restore_fpregs_from_fpstate (&init_fpstate.regs,xfeatures_mask_fpstate ()); }#if IS_ENABLED (CONFIG_KVM )
@@ -297,24 +297,24 @@ static inline unsigned int init_fpstate_return fpu_kernel_xstate_size;/* XSAVE(S) just needs the legacy and the xstate header part */ -return sizeof (init_fpstate.xsave); +return sizeof (init_fpstate.regs.xsave); } -static inline void fpstate_init_fxstate (struct fxregs_state *fx) +static inline void fpstate_init_fxstate (struct fpstate *fpstate) { - fx->cwd =0x37f ; - fx->mxcsr =MXCSR_DEFAULT ; + fpstate->regs.fxsave.cwd =0x37f ; + fpstate->regs.fxsave.mxcsr =MXCSR_DEFAULT ; }/* * Legacy x87 fpstate state init: */ -static inline void fpstate_init_fstate (struct fregs_state *fp) +static inline void fpstate_init_fstate (struct fpstate *fpstate) { - fp->cwd =0xffff037fu ; - fp->swd =0xffff0000u ; - fp->twd =0xffffffffu ; - fp->fos =0xffff0000u ; + fpstate->regs.fsave.cwd =0xffff037fu ; + fpstate->regs.fsave.swd =0xffff0000u ; + fpstate->regs.fsave.twd =0xffffffffu ; + fpstate->regs.fsave.fos =0xffff0000u ; }/*
@@ -322,19 +322,19 @@ static inline void fpstate_init_fstate(s* 1) Early boot to setup init_fpstate for non XSAVE systems * 2) fpu_init_fpstate_user() which is invoked from KVM */ -void fpstate_init_user (union fpregs_state *state) +void fpstate_init_user (struct fpstate *fpstate) {if (!cpu_feature_enabled (X86_FEATURE_FPU )) { -fpstate_init_soft (&state->soft); +fpstate_init_soft (&fpstate->regs.soft);return ; } -xstate_init_xcomp_bv (&state->xsave,xfeatures_mask_uabi ()); +xstate_init_xcomp_bv (&fpstate->regs.xsave,xfeatures_mask_uabi ());if (cpu_feature_enabled (X86_FEATURE_FXSR )) -fpstate_init_fxstate (&state->fxsave); +fpstate_init_fxstate (fpstate);else -fpstate_init_fstate (&state->fsave); +fpstate_init_fstate (fpstate); }void fpstate_reset (struct fpu *fpu)
@@ -347,7 +347,7 @@ void fpstate_reset(struct fpu *fpu)void fpu_init_fpstate_user (struct fpu *fpu) {fpstate_reset (fpu); -fpstate_init_user (&fpu->fpstate->regs); +fpstate_init_user (fpu->fpstate); }EXPORT_SYMBOL_GPL (fpu_init_fpstate_user);#endif
@@ -378,7 +378,7 @@ int fpu_clone(struct task_struct *dst)*/ if (dst->flags & (PF_KTHREAD |PF_IO_WORKER )) {/* Clear out the minimal state */ -memcpy (&dst_fpu->state, &init_fpstate, +memcpy (&dst_fpu->state, &init_fpstate.regs,init_fpstate_copy_size ());return 0 ; }
@@ -435,11 +435,11 @@ void fpu__drop(struct fpu *fpu)static inline void restore_fpregs_from_init_fpstate (u64 features_mask) {if (use_xsave ()) -os_xrstor (&init_fpstate.xsave, features_mask); +os_xrstor (&init_fpstate.regs.xsave, features_mask);else if (use_fxsr ()) -fxrstor (&init_fpstate.fxsave); +fxrstor (&init_fpstate.regs.fxsave);else -frstor (&init_fpstate.fsave); +frstor (&init_fpstate.regs.fsave);pkru_write_default (); }
@@ -466,7 +466,7 @@ static void fpu_reset_fpstate(void)* user space as PKRU is eagerly written in switch_to() and * flush_thread(). */ -memcpy (&fpu->state, &init_fpstate,init_fpstate_copy_size ()); +memcpy (&fpu->state, &init_fpstate.regs,init_fpstate_copy_size ());set_thread_flag (TIF_NEED_FPU_LOAD );fpregs_unlock (); }
arch/x86/kernel/fpu/internal.h +2 -2
@@ -2,7 +2,7 @@#ifndef __X86_KERNEL_FPU_INTERNAL_H#define __X86_KERNEL_FPU_INTERNAL_H -extern union fpregs_state init_fpstate; +extern struct fpstate init_fpstate;/* CPU feature check wrappers */ static __always_inline __pure bool use_xsave(void)
@@ -25,7 +25,7 @@ static __always_inline __pure bool use_fextern void fpu__init_prepare_fx_sw_frame (void );/* Used in init.c */ -extern void fpstate_init_user (union fpregs_state *state); +extern void fpstate_init_user (struct fpstate *fpstate);extern void fpstate_reset (struct fpu *fpu);#endif
arch/x86/kernel/fpu/signal.c +1 -1
@@ -243,7 +243,7 @@ static int __restore_fpregs_from_user(vo ret =fxrstor_from_user_sigframe (buf);if (!ret &&unlikely (init_bv)) -os_xrstor (&init_fpstate.xsave, init_bv); +os_xrstor (&init_fpstate.regs.xsave, init_bv);return ret; }else if (use_fxsr ()) {return fxrstor_from_user_sigframe (buf);
arch/x86/kernel/fpu/xstate.c +6 -6
@@ -408,12 +408,12 @@ static void __init setup_init_fpu_buf(vosetup_xstate_features ();print_xstate_features (); -xstate_init_xcomp_bv (&init_fpstate.xsave, xfeatures_mask_all); +xstate_init_xcomp_bv (&init_fpstate.regs.xsave, xfeatures_mask_all);/* * Init all the features state with header.xfeatures being 0x0 */ -os_xrstor_booting (&init_fpstate.xsave); +os_xrstor_booting (&init_fpstate.regs.xsave);/* * All components are now in init state. Read the state back so
@@ -431,7 +431,7 @@ static void __init setup_init_fpu_buf(vo* state is all zeroes or if not to add the necessary handling * here. */ -fxsave (&init_fpstate.fxsave); +fxsave (&init_fpstate.regs.fxsave); }static int xfeature_uncompacted_offset (int xfeature_nr)
@@ -672,11 +672,11 @@ static unsigned int __init get_xsave_siz*/ static bool __init is_supported_xstate_size (unsigned int test_xstate_size) { -if (test_xstate_size <=sizeof (union fpregs_state )) +if (test_xstate_size <=sizeof (init_fpstate.regs))return true;pr_warn ("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n" , -sizeof (union fpregs_state ), test_xstate_size); +sizeof (init_fpstate.regs), test_xstate_size);return false; }
@@ -981,7 +981,7 @@ void __copy_xstate_to_uabi_buf(struct me u32 pkru_val, enum xstate_copy_mode copy_mode) {const unsigned int off_mxcsr = offsetof(struct fxregs_state , mxcsr); -struct xregs_state *xinit = &init_fpstate.xsave; +struct xregs_state *xinit = &init_fpstate.regs.xsave;struct xstate_header header;unsigned int zerofrom;u64 mask;
[tip: x86/fpu] x86/fpu: Convert fpstate_init() to struct fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: f83ac56acdad0815366bb541b6cc9d24f6cea2b2 Gitweb: https://git.kernel.org/tip/f83ac56acdad0815366bb541b6cc9d24f6cea2b2 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:28 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:26:34 +02:00
x86/fpu: Convert fpstate_init() to struct fpstate
Convert fpstate_init() and related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/core.c | 44 ++++++++++++++++----------------- arch/x86/kernel/fpu/internal.h | 4 +-- arch/x86/kernel/fpu/signal.c | 2 +- arch/x86/kernel/fpu/xstate.c | 12 ++++----- 4 files changed, 31 insertions(+), 31 deletions(-)
arch/x86/kernel/fpu/core.c +22 -22
@@ -29,7 +29,7 @@* Represents the initial FPU state. It's mostly (but not completely) zeroes, * depending on the FPU hardware format: */ -union fpregs_state init_fpstate__ro_after_init ; +struct fpstate init_fpstate__ro_after_init ;/* * Track whether the kernel is using the FPU state
@@ -157,7 +157,7 @@ void restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask)void fpu_reset_from_exception_fixup (void ) { -restore_fpregs_from_fpstate (&init_fpstate,xfeatures_mask_fpstate ()); +restore_fpregs_from_fpstate (&init_fpstate.regs,xfeatures_mask_fpstate ()); }#if IS_ENABLED (CONFIG_KVM )
@@ -297,24 +297,24 @@ static inline unsigned int init_fpstate_copy_size(void)return fpu_kernel_xstate_size;/* XSAVE(S) just needs the legacy and the xstate header part */ -return sizeof (init_fpstate.xsave); +return sizeof (init_fpstate.regs.xsave); } -static inline void fpstate_init_fxstate (struct fxregs_state *fx) +static inline void fpstate_init_fxstate (struct fpstate *fpstate) { - fx->cwd =0x37f ; - fx->mxcsr =MXCSR_DEFAULT ; + fpstate->regs.fxsave.cwd =0x37f ; + fpstate->regs.fxsave.mxcsr =MXCSR_DEFAULT ; }/* * Legacy x87 fpstate state init: */ -static inline void fpstate_init_fstate (struct fregs_state *fp) +static inline void fpstate_init_fstate (struct fpstate *fpstate) { - fp->cwd =0xffff037fu ; - fp->swd =0xffff0000u ; - fp->twd =0xffffffffu ; - fp->fos =0xffff0000u ; + fpstate->regs.fsave.cwd =0xffff037fu ; + fpstate->regs.fsave.swd =0xffff0000u ; + fpstate->regs.fsave.twd =0xffffffffu ; + fpstate->regs.fsave.fos =0xffff0000u ; }/*
@@ -322,19 +322,19 @@ static inline void fpstate_init_fstate(struct fregs_state *fp)* 1) Early boot to setup init_fpstate for non XSAVE systems * 2) fpu_init_fpstate_user() which is invoked from KVM */ -void fpstate_init_user (union fpregs_state *state) +void fpstate_init_user (struct fpstate *fpstate) {if (!cpu_feature_enabled (X86_FEATURE_FPU )) { -fpstate_init_soft (&state->soft); +fpstate_init_soft (&fpstate->regs.soft);return ; } -xstate_init_xcomp_bv (&state->xsave,xfeatures_mask_uabi ()); +xstate_init_xcomp_bv (&fpstate->regs.xsave,xfeatures_mask_uabi ());if (cpu_feature_enabled (X86_FEATURE_FXSR )) -fpstate_init_fxstate (&state->fxsave); +fpstate_init_fxstate (fpstate);else -fpstate_init_fstate (&state->fsave); +fpstate_init_fstate (fpstate); }void fpstate_reset (struct fpu *fpu)
@@ -347,7 +347,7 @@ void fpstate_reset(struct fpu *fpu)void fpu_init_fpstate_user (struct fpu *fpu) {fpstate_reset (fpu); -fpstate_init_user (&fpu->fpstate->regs); +fpstate_init_user (fpu->fpstate); }EXPORT_SYMBOL_GPL (fpu_init_fpstate_user);#endif
@@ -378,7 +378,7 @@ int fpu_clone(struct task_struct *dst)*/ if (dst->flags & (PF_KTHREAD |PF_IO_WORKER )) {/* Clear out the minimal state */ -memcpy (&dst_fpu->state, &init_fpstate, +memcpy (&dst_fpu->state, &init_fpstate.regs,init_fpstate_copy_size ());return 0 ; }
@@ -435,11 +435,11 @@ void fpu__drop(struct fpu *fpu)static inline void restore_fpregs_from_init_fpstate (u64 features_mask) {if (use_xsave ()) -os_xrstor (&init_fpstate.xsave, features_mask); +os_xrstor (&init_fpstate.regs.xsave, features_mask);else if (use_fxsr ()) -fxrstor (&init_fpstate.fxsave); +fxrstor (&init_fpstate.regs.fxsave);else -frstor (&init_fpstate.fsave); +frstor (&init_fpstate.regs.fsave);pkru_write_default (); }
@@ -466,7 +466,7 @@ static void fpu_reset_fpstate(void)* user space as PKRU is eagerly written in switch_to() and * flush_thread(). */ -memcpy (&fpu->state, &init_fpstate,init_fpstate_copy_size ()); +memcpy (&fpu->state, &init_fpstate.regs,init_fpstate_copy_size ());set_thread_flag (TIF_NEED_FPU_LOAD );fpregs_unlock (); }
arch/x86/kernel/fpu/internal.h +2 -2
@@ -2,7 +2,7 @@#ifndef __X86_KERNEL_FPU_INTERNAL_H#define __X86_KERNEL_FPU_INTERNAL_H -extern union fpregs_state init_fpstate; +extern struct fpstate init_fpstate;/* CPU feature check wrappers */ static __always_inline __pure bool use_xsave(void)
@@ -25,7 +25,7 @@ static __always_inline __pure bool use_fxsr(void)extern void fpu__init_prepare_fx_sw_frame (void );/* Used in init.c */ -extern void fpstate_init_user (union fpregs_state *state); +extern void fpstate_init_user (struct fpstate *fpstate);extern void fpstate_reset (struct fpu *fpu);#endif
arch/x86/kernel/fpu/signal.c +1 -1
@@ -243,7 +243,7 @@ static int __restore_fpregs_from_user(void __user *buf, u64 xrestore, ret =fxrstor_from_user_sigframe (buf);if (!ret &&unlikely (init_bv)) -os_xrstor (&init_fpstate.xsave, init_bv); +os_xrstor (&init_fpstate.regs.xsave, init_bv);return ret; }else if (use_fxsr ()) {return fxrstor_from_user_sigframe (buf);
arch/x86/kernel/fpu/xstate.c +6 -6
@@ -408,12 +408,12 @@ static void __init setup_init_fpu_buf(void)setup_xstate_features ();print_xstate_features (); -xstate_init_xcomp_bv (&init_fpstate.xsave, xfeatures_mask_all); +xstate_init_xcomp_bv (&init_fpstate.regs.xsave, xfeatures_mask_all);/* * Init all the features state with header.xfeatures being 0x0 */ -os_xrstor_booting (&init_fpstate.xsave); +os_xrstor_booting (&init_fpstate.regs.xsave);/* * All components are now in init state. Read the state back so
@@ -431,7 +431,7 @@ static void __init setup_init_fpu_buf(void)* state is all zeroes or if not to add the necessary handling * here. */ -fxsave (&init_fpstate.fxsave); +fxsave (&init_fpstate.regs.fxsave); }static int xfeature_uncompacted_offset (int xfeature_nr)
@@ -672,11 +672,11 @@ static unsigned int __init get_xsave_size(void)*/ static bool __init is_supported_xstate_size (unsigned int test_xstate_size) { -if (test_xstate_size <=sizeof (union fpregs_state )) +if (test_xstate_size <=sizeof (init_fpstate.regs))return true;pr_warn ("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n" , -sizeof (union fpregs_state ), test_xstate_size); +sizeof (init_fpstate.regs), test_xstate_size);return false; }
@@ -981,7 +981,7 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave, u32 pkru_val, enum xstate_copy_mode copy_mode) {const unsigned int off_mxcsr = offsetof(struct fxregs_state , mxcsr); -struct xregs_state *xinit = &init_fpstate.xsave; +struct xregs_state *xinit = &init_fpstate.regs.xsave;struct xstate_header header;unsigned int zerofrom;u64 mask;
PATCH 3/21 x86/fpu: Convert restore_fpregs_from_fpstate() to struct fpstate
In reply to Thomas GleixnerConvert restore_fpregs_from_fpstate and related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/kernel/fpu/context.h | 2 +- arch/x86/kernel/fpu/core.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/signal.h +1 -1
@@ -40,7 +40,7 @@ extern bool copy_fpstate_to_sigframe(voiextern void fpu__clear_user_states (struct fpu *fpu);extern bool fpu__restore_sig (void __user *buf,int ia32_frame); -extern void restore_fpregs_from_fpstate (union fpregs_state *fpstate,u64 mask); +extern void restore_fpregs_from_fpstate (struct fpstate *fpstate,u64 mask);extern bool copy_fpstate_to_sigframe (void __user *buf,void __user *fp,int size);
arch/x86/kernel/fpu/context.h +1 -1
@@ -74,7 +74,7 @@ static inline void fpregs_restore_userre*/ mask =xfeatures_mask_restore_user () |xfeatures_mask_supervisor (); -restore_fpregs_from_fpstate (&fpu->state, mask); +restore_fpregs_from_fpstate (fpu->fpstate, mask);fpregs_activate (fpu); fpu->last_cpu = cpu;
arch/x86/kernel/fpu/core.c +6 -6
@@ -129,7 +129,7 @@ void save_fpregs_to_fpstate(struct fpu *frstor (&fpu->state.fsave); } -void restore_fpregs_from_fpstate (union fpregs_state *fpstate,u64 mask) +void restore_fpregs_from_fpstate (struct fpstate *fpstate,u64 mask) {/* * AMD K7/K8 and later CPUs up to Zen don't save/restore
@@ -146,18 +146,18 @@ void restore_fpregs_from_fpstate(union f }if (use_xsave ()) { -os_xrstor (&fpstate->xsave, mask); +os_xrstor (&fpstate->regs.xsave, mask); }else {if (use_fxsr ()) -fxrstor (&fpstate->fxsave); +fxrstor (&fpstate->regs.fxsave);else -frstor (&fpstate->fsave); +frstor (&fpstate->regs.fsave); } }void fpu_reset_from_exception_fixup (void ) { -restore_fpregs_from_fpstate (&init_fpstate.regs,xfeatures_mask_fpstate ()); +restore_fpregs_from_fpstate (&init_fpstate,xfeatures_mask_fpstate ()); }#if IS_ENABLED (CONFIG_KVM )
@@ -176,7 +176,7 @@ void fpu_swap_kvm_fpu(struct fpu *save,if (rstor) { restore_mask &=xfeatures_mask_fpstate (); -restore_fpregs_from_fpstate (&rstor->state, restore_mask); +restore_fpregs_from_fpstate (rstor->fpstate, restore_mask); }fpregs_mark_activate ();
[tip: x86/fpu] x86/fpu: Convert restore_fpregs_from_fpstate() to struct fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 18b3fa1ad15fa8d777ac32f117553cce1a968460 Gitweb: https://git.kernel.org/tip/18b3fa1ad15fa8d777ac32f117553cce1a968460 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:30 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:26:38 +02:00
x86/fpu: Convert restore_fpregs_from_fpstate() to struct fpstate
Convert restore_fpregs_from_fpstate() and related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/fpu/signal.h | 2 +- arch/x86/kernel/fpu/context.h | 2 +- arch/x86/kernel/fpu/core.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/signal.h +1 -1
@@ -40,7 +40,7 @@ extern bool copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int sizeextern void fpu__clear_user_states (struct fpu *fpu);extern bool fpu__restore_sig (void __user *buf,int ia32_frame); -extern void restore_fpregs_from_fpstate (union fpregs_state *fpstate,u64 mask); +extern void restore_fpregs_from_fpstate (struct fpstate *fpstate,u64 mask);extern bool copy_fpstate_to_sigframe (void __user *buf,void __user *fp,int size);
arch/x86/kernel/fpu/context.h +1 -1
@@ -74,7 +74,7 @@ static inline void fpregs_restore_userregs(void)*/ mask =xfeatures_mask_restore_user () |xfeatures_mask_supervisor (); -restore_fpregs_from_fpstate (&fpu->state, mask); +restore_fpregs_from_fpstate (fpu->fpstate, mask);fpregs_activate (fpu); fpu->last_cpu = cpu;
arch/x86/kernel/fpu/core.c +6 -6
@@ -129,7 +129,7 @@ void save_fpregs_to_fpstate(struct fpu *fpu)frstor (&fpu->state.fsave); } -void restore_fpregs_from_fpstate (union fpregs_state *fpstate,u64 mask) +void restore_fpregs_from_fpstate (struct fpstate *fpstate,u64 mask) {/* * AMD K7/K8 and later CPUs up to Zen don't save/restore
@@ -146,18 +146,18 @@ void restore_fpregs_from_fpstate(union fpregs_state *fpstate, u64 mask) }if (use_xsave ()) { -os_xrstor (&fpstate->xsave, mask); +os_xrstor (&fpstate->regs.xsave, mask); }else {if (use_fxsr ()) -fxrstor (&fpstate->fxsave); +fxrstor (&fpstate->regs.fxsave);else -frstor (&fpstate->fsave); +frstor (&fpstate->regs.fsave); } }void fpu_reset_from_exception_fixup (void ) { -restore_fpregs_from_fpstate (&init_fpstate.regs,xfeatures_mask_fpstate ()); +restore_fpregs_from_fpstate (&init_fpstate,xfeatures_mask_fpstate ()); }#if IS_ENABLED (CONFIG_KVM )
@@ -176,7 +176,7 @@ void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask)if (rstor) { restore_mask &=xfeatures_mask_fpstate (); -restore_fpregs_from_fpstate (&rstor->state, restore_mask); +restore_fpregs_from_fpstate (rstor->fpstate, restore_mask); }fpregs_mark_activate ();
PATCH 4/21 x86/fpu: Replace KVMs xstate component clearing
In reply to Thomas GleixnerIn order to prepare for the support of dynamically enabled FPU features, move the clearing of xstate components to the FPU core code.
No functional change.
--- arch/x86/include/asm/fpu/api.h | 1 + arch/x86/include/asm/fpu/xstate.h | 1 - arch/x86/kernel/fpu/xstate.c | 12 +++++++++++- arch/x86/kernel/fpu/xstate.h | 2 ++ arch/x86/kvm/x86.c | 14 +++++--------- 5 files changed, 19 insertions(+), 11 deletions(-)
arch/x86/include/asm/fpu/api.h +1 -0
@@ -132,6 +132,7 @@ DECLARE_PER_CPU(struct fpu *, fpu_fpregs/* fpstate-related functions which are exported to KVM */ extern void fpu_init_fpstate_user (struct fpu *fpu); +extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);
arch/x86/include/asm/fpu/xstate.h +0 -1
@@ -128,7 +128,6 @@ extern u64 xstate_fx_sw_bytes[USER_XSTATextern void __init update_regset_xstate_info (unsigned int size,u64 xstate_mask); -void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr);int xfeature_size (int xfeature_nr);int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf);int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave,const void __user *ubuf);
arch/x86/kernel/fpu/xstate.c +11 -1
@@ -908,7 +908,6 @@ void *get_xsave_addr(struct xregs_statereturn __raw_xsave_addr (xsave, xfeature_nr); } -EXPORT_SYMBOL_GPL (get_xsave_addr);#ifdef CONFIG_ARCH_HAS_PKEYS
@@ -1257,6 +1256,17 @@ void xrstors(struct xregs_state *xstate,WARN_ON_ONCE (err); } +#if IS_ENABLED (CONFIG_KVM ) +void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature) +{ +void *addr =get_xsave_addr (&fps->regs.xsave, xfeature); + +if (addr) +memset (addr,0 , xstate_sizes[xfeature]); +} +EXPORT_SYMBOL_GPL (fpstate_clear_xstate_component); +#endif +#ifdef CONFIG_PROC_PID_ARCH_STATUS /* * Report the amount of time elapsed in millisecond since last AVX512
arch/x86/kernel/fpu/xstate.h +2 -0
@@ -21,6 +21,8 @@ extern void __copy_xstate_to_uabi_buf(stextern void fpu__init_cpu_xstate (void );extern void fpu__init_system_xstate (void ); +extern void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr); +/* XSAVE/XRSTOR wrapper functions */ #ifdef CONFIG_X86_64
arch/x86/kvm/x86.c +5 -9
@@ -10689,7 +10689,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcp vcpu->arch.apf.halted = false;if (vcpu->arch.guest_fpu &&kvm_mpx_supported ()) { -void *mpx_state_buffer; +struct fpstate *fpstate = vcpu->arch.guest_fpu->fpstate;/* * To avoid have the INIT path from kvm_apic_has_events() that be
@@ -10697,14 +10697,10 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcp*/ if (init_event)kvm_put_guest_fpu (vcpu); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu->state.xsave, -XFEATURE_BNDREGS ); -if (mpx_state_buffer) -memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndreg_state )); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu->state.xsave, -XFEATURE_BNDCSR ); -if (mpx_state_buffer) -memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndcsr )); + +fpstate_clear_xstate_component (fpstate,XFEATURE_BNDREGS ); +fpstate_clear_xstate_component (fpstate,XFEATURE_BNDCSR ); +if (init_event)kvm_load_guest_fpu (vcpu); }
[tip: x86/fpu] x86/fpu: Replace KVMs xstate component clearing
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 087df48c298c1cb829f4cd468d90f93234b1bc44 Gitweb: https://git.kernel.org/tip/087df48c298c1cb829f4cd468d90f93234b1bc44 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:31 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:26:41 +02:00
x86/fpu: Replace KVMs xstate component clearing
In order to prepare for the support of dynamically enabled FPU features, move the clearing of xstate components to the FPU core code.
No functional change.
--- arch/x86/include/asm/fpu/api.h | 1 + arch/x86/include/asm/fpu/xstate.h | 1 - arch/x86/kernel/fpu/xstate.c | 12 +++++++++++- arch/x86/kernel/fpu/xstate.h | 2 ++ arch/x86/kvm/x86.c | 14 +++++--------- 5 files changed, 19 insertions(+), 11 deletions(-)
arch/x86/include/asm/fpu/api.h +1 -0
@@ -132,6 +132,7 @@ DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);/* fpstate-related functions which are exported to KVM */ extern void fpu_init_fpstate_user (struct fpu *fpu); +extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);
arch/x86/include/asm/fpu/xstate.h +0 -1
@@ -128,7 +128,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 *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr);int xfeature_size (int xfeature_nr);int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf);int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave,const void __user *ubuf);
arch/x86/kernel/fpu/xstate.c +11 -1
@@ -908,7 +908,6 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return __raw_xsave_addr (xsave, xfeature_nr); } -EXPORT_SYMBOL_GPL (get_xsave_addr);#ifdef CONFIG_ARCH_HAS_PKEYS
@@ -1257,6 +1256,17 @@ void xrstors(struct xregs_state *xstate, u64 mask)WARN_ON_ONCE (err); } +#if IS_ENABLED (CONFIG_KVM ) +void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature) +{ +void *addr =get_xsave_addr (&fps->regs.xsave, xfeature); + +if (addr) +memset (addr,0 , xstate_sizes[xfeature]); +} +EXPORT_SYMBOL_GPL (fpstate_clear_xstate_component); +#endif +#ifdef CONFIG_PROC_PID_ARCH_STATUS /* * Report the amount of time elapsed in millisecond since last AVX512
arch/x86/kernel/fpu/xstate.h +2 -0
@@ -21,6 +21,8 @@ extern void __copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsavextern void fpu__init_cpu_xstate (void );extern void fpu__init_system_xstate (void ); +extern void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr); +/* XSAVE/XRSTOR wrapper functions */ #ifdef CONFIG_X86_64
arch/x86/kvm/x86.c +5 -9
@@ -10705,7 +10705,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->arch.apf.halted = false;if (vcpu->arch.guest_fpu &&kvm_mpx_supported ()) { -void *mpx_state_buffer; +struct fpstate *fpstate = vcpu->arch.guest_fpu->fpstate;/* * To avoid have the INIT path from kvm_apic_has_events() that be
@@ -10713,14 +10713,10 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ if (init_event)kvm_put_guest_fpu (vcpu); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu->state.xsave, -XFEATURE_BNDREGS ); -if (mpx_state_buffer) -memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndreg_state )); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu->state.xsave, -XFEATURE_BNDCSR ); -if (mpx_state_buffer) -memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndcsr )); + +fpstate_clear_xstate_component (fpstate,XFEATURE_BNDREGS ); +fpstate_clear_xstate_component (fpstate,XFEATURE_BNDCSR ); +if (init_event)kvm_load_guest_fpu (vcpu); }
PATCH 5/21 x86/KVM: Convert to fpstate
In reply to Thomas GleixnerConvert KVM code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +2 -2
@@ -10389,7 +10389,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fpu->fpr, fxsave->st_space,128 ); fpu->fcw = fxsave->cwd; fpu->fsw = fxsave->swd;
@@ -10412,7 +10412,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fxsave->st_space, fpu->fpr,128 ); fxsave->cwd = fpu->fcw;
Re: [patch 05/21] x86/KVM: Convert to fpstate
Quoted 25 lines · Thomas Gleixner
Convert KVM code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
Signed-off-by Thomas Gleixner <[email protected]>Cc Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +1
-1@@ -10389,7 +10389,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fpu->fpr, fxsave->st_space,128 ); fpu->fcw = fxsave->cwd; fpu->fsw = fxsave->swd;
arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_fpu(struct k · line 10418
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -10412,7 +10412,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fxsave->st_space, fpu->fpr,128 ); fxsave->cwd = fpu->fcw;
[tip: x86/fpu] x86/KVM: Convert to fpstate
In reply to Thomas GleixnerThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 1c57572d754fc54e0b8ac0df5350969ce6292d12 Gitweb: https://git.kernel.org/tip/1c57572d754fc54e0b8ac0df5350969ce6292d12 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:33 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:34:14 +02:00
x86/KVM: Convert to fpstate
Convert KVM code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +2 -2
@@ -10403,7 +10403,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)vcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fpu->fpr, fxsave->st_space,128 ); fpu->fcw = fxsave->cwd; fpu->fsw = fxsave->swd;
@@ -10426,7 +10426,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)vcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu->state.fxsave; + fxsave = &vcpu->arch.guest_fpu->fpstate->regs.fxsave;memcpy (fxsave->st_space, fpu->fpr,128 ); fxsave->cwd = fpu->fcw;
PATCH 6/21 x86/fpu: Convert tracing to fpstate
In reply to Thomas GleixnerConvert FPU tracing code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/trace/fpu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/trace/fpu.h +2 -2
@@ -22,8 +22,8 @@ DECLARE_EVENT_CLASS(x86_fpu, __entry->fpu = fpu; __entry->load_fpu =test_thread_flag (TIF_NEED_FPU_LOAD );if (boot_cpu_has (X86_FEATURE_OSXSAVE )) { - __entry->xfeatures = fpu->state.xsave.header.xfeatures; - __entry->xcomp_bv = fpu->state.xsave.header.xcomp_bv; + __entry->xfeatures = fpu->fpstate->regs.xsave.header.xfeatures; + __entry->xcomp_bv = fpu->fpstate->regs.xsave.header.xcomp_bv; } ), TP_printk("x86/fpu: %p load: %d xfeatures: %llx xcomp_bv: %llx",
[tip: x86/fpu] x86/fpu: Convert tracing to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: cceb496420fa11a6e11989abc68b8e7564dc40f9 Gitweb: https://git.kernel.org/tip/cceb496420fa11a6e11989abc68b8e7564dc40f9 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:34 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:35:04 +02:00
x86/fpu: Convert tracing to fpstate
Convert FPU tracing code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/trace/fpu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/trace/fpu.h +2 -2
@@ -22,8 +22,8 @@ DECLARE_EVENT_CLASS(x86_fpu, __entry->fpu = fpu; __entry->load_fpu =test_thread_flag (TIF_NEED_FPU_LOAD );if (boot_cpu_has (X86_FEATURE_OSXSAVE )) { - __entry->xfeatures = fpu->state.xsave.header.xfeatures; - __entry->xcomp_bv = fpu->state.xsave.header.xcomp_bv; + __entry->xfeatures = fpu->fpstate->regs.xsave.header.xfeatures; + __entry->xcomp_bv = fpu->fpstate->regs.xsave.header.xcomp_bv; } ), TP_printk("x86/fpu: %p load: %d xfeatures: %llx xcomp_bv: %llx",
PATCH 7/21 x86/fpu/regset: Convert to fpstate
In reply to Thomas GleixnerConvert regset related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/regset.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/fpu/regset.c +14 -13
@@ -78,8 +78,8 @@ int xfpregs_get(struct task_struct *targsync_fpstate (fpu);if (!use_xsave ()) { -return membuf_write (&to, &fpu->state.fxsave, -sizeof (fpu->state.fxsave)); +return membuf_write (&to, &fpu->fpstate->regs.fxsave, +sizeof (fpu->fpstate->regs.fxsave)); }copy_xstate_to_uabi_buf (to, target,XSTATE_COPY_FX );
@@ -114,15 +114,15 @@ int xfpregs_set(struct task_struct *targfpu_force_restore (fpu);/* Copy the state */ -memcpy (&fpu->state.fxsave, &newstate,sizeof (newstate)); +memcpy (&fpu->fpstate->regs.fxsave, &newstate,sizeof (newstate));/* Clear xmm8..15 */ -BUILD_BUG_ON (sizeof (fpu->state.fxsave.xmm_space) !=16 *16 ); -memset (&fpu->state.fxsave.xmm_space[8 ],0 ,8 *16 ); +BUILD_BUG_ON (sizeof (fpu->__fpstate.regs.fxsave.xmm_space) !=16 *16 ); +memset (&fpu->fpstate->regs.fxsave.xmm_space[8 ],0 ,8 *16 );/* Mark FP and SSE as in use when XSAVE is enabled */ if (use_xsave ()) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; + fpu->fpstate->regs.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ;return 0 ; }
@@ -168,7 +168,8 @@ int xstateregs_set(struct task_struct *t }fpu_force_restore (fpu); - ret =copy_uabi_from_kernel_to_xstate (&fpu->state.xsave, kbuf ?: tmpbuf); + ret =copy_uabi_from_kernel_to_xstate (&fpu->fpstate->regs.xsave, + kbuf ?: tmpbuf); out:vfree (tmpbuf);
@@ -287,7 +288,7 @@ static void __convert_from_fxsr(struct uvoid convert_from_fxsr (struct user_i387_ia32_struct *env,struct task_struct *tsk) { -__convert_from_fxsr (env, tsk, &tsk->thread.fpu.state.fxsave); +__convert_from_fxsr (env, tsk, &tsk->thread.fpu.fpstate->regs.fxsave); } void convert_to_fxsr(struct fxregs_state *fxsave,
@@ -330,7 +331,7 @@ int fpregs_get(struct task_struct *targereturn fpregs_soft_get (target, regset, to);if (!cpu_feature_enabled (X86_FEATURE_FXSR )) { -return membuf_write (&to, &fpu->state.fsave, +return membuf_write (&to, &fpu->fpstate->regs.fsave,sizeof (struct fregs_state )); }
@@ -341,7 +342,7 @@ int fpregs_get(struct task_struct *targecopy_xstate_to_uabi_buf (mb, target,XSTATE_COPY_FP ); fx = &fxsave; }else { - fx = &fpu->state.fxsave; + fx = &fpu->fpstate->regs.fxsave; }__convert_from_fxsr (&env, target, fx);
@@ -370,16 +371,16 @@ int fpregs_set(struct task_struct *targefpu_force_restore (fpu);if (cpu_feature_enabled (X86_FEATURE_FXSR )) -convert_to_fxsr (&fpu->state.fxsave, &env); +convert_to_fxsr (&fpu->fpstate->regs.fxsave, &env);else -memcpy (&fpu->state.fsave, &env,sizeof (env)); +memcpy (&fpu->fpstate->regs.fsave, &env,sizeof (env));/* * Update the header bit in the xsave header, indicating the * presence of FP. */ if (cpu_feature_enabled (X86_FEATURE_XSAVE )) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FP ; + fpu->fpstate->regs.xsave.header.xfeatures |=XFEATURE_MASK_FP ;return 0 ; }
[tip: x86/fpu] x86/fpu/regset: Convert to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: caee31a36c33ed7788d0b3d93a663860157f6c55 Gitweb: https://git.kernel.org/tip/caee31a36c33ed7788d0b3d93a663860157f6c55 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:36 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:35:29 +02:00
x86/fpu/regset: Convert to fpstate
Convert regset related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/regset.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/fpu/regset.c +14 -13
@@ -78,8 +78,8 @@ int xfpregs_get(struct task_struct *target, const struct user_regset *regset,sync_fpstate (fpu);if (!use_xsave ()) { -return membuf_write (&to, &fpu->state.fxsave, -sizeof (fpu->state.fxsave)); +return membuf_write (&to, &fpu->fpstate->regs.fxsave, +sizeof (fpu->fpstate->regs.fxsave)); }copy_xstate_to_uabi_buf (to, target,XSTATE_COPY_FX );
@@ -114,15 +114,15 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset,fpu_force_restore (fpu);/* Copy the state */ -memcpy (&fpu->state.fxsave, &newstate,sizeof (newstate)); +memcpy (&fpu->fpstate->regs.fxsave, &newstate,sizeof (newstate));/* Clear xmm8..15 */ -BUILD_BUG_ON (sizeof (fpu->state.fxsave.xmm_space) !=16 *16 ); -memset (&fpu->state.fxsave.xmm_space[8 ],0 ,8 *16 ); +BUILD_BUG_ON (sizeof (fpu->__fpstate.regs.fxsave.xmm_space) !=16 *16 ); +memset (&fpu->fpstate->regs.fxsave.xmm_space[8 ],0 ,8 *16 );/* Mark FP and SSE as in use when XSAVE is enabled */ if (use_xsave ()) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; + fpu->fpstate->regs.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ;return 0 ; }
@@ -168,7 +168,8 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, }fpu_force_restore (fpu); - ret =copy_uabi_from_kernel_to_xstate (&fpu->state.xsave, kbuf ?: tmpbuf); + ret =copy_uabi_from_kernel_to_xstate (&fpu->fpstate->regs.xsave, + kbuf ?: tmpbuf); out:vfree (tmpbuf);
@@ -287,7 +288,7 @@ static void __convert_from_fxsr(struct user_i387_ia32_struct *env,void convert_from_fxsr (struct user_i387_ia32_struct *env,struct task_struct *tsk) { -__convert_from_fxsr (env, tsk, &tsk->thread.fpu.state.fxsave); +__convert_from_fxsr (env, tsk, &tsk->thread.fpu.fpstate->regs.fxsave); } void convert_to_fxsr(struct fxregs_state *fxsave,
@@ -330,7 +331,7 @@ int fpregs_get(struct task_struct *target, const struct user_regset *regset,return fpregs_soft_get (target, regset, to);if (!cpu_feature_enabled (X86_FEATURE_FXSR )) { -return membuf_write (&to, &fpu->state.fsave, +return membuf_write (&to, &fpu->fpstate->regs.fsave,sizeof (struct fregs_state )); }
@@ -341,7 +342,7 @@ int fpregs_get(struct task_struct *target, const struct user_regset *regset,copy_xstate_to_uabi_buf (mb, target,XSTATE_COPY_FP ); fx = &fxsave; }else { - fx = &fpu->state.fxsave; + fx = &fpu->fpstate->regs.fxsave; }__convert_from_fxsr (&env, target, fx);
@@ -370,16 +371,16 @@ int fpregs_set(struct task_struct *target, const struct user_regset *regset,fpu_force_restore (fpu);if (cpu_feature_enabled (X86_FEATURE_FXSR )) -convert_to_fxsr (&fpu->state.fxsave, &env); +convert_to_fxsr (&fpu->fpstate->regs.fxsave, &env);else -memcpy (&fpu->state.fsave, &env,sizeof (env)); +memcpy (&fpu->fpstate->regs.fsave, &env,sizeof (env));/* * Update the header bit in the xsave header, indicating the * presence of FP. */ if (cpu_feature_enabled (X86_FEATURE_XSAVE )) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FP ; + fpu->fpstate->regs.xsave.header.xfeatures |=XFEATURE_MASK_FP ;return 0 ; }
PATCH 8/21 x86/fpu/signal: Convert to fpstate
In reply to Thomas GleixnerConvert signal related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/signal.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
arch/x86/kernel/fpu/signal.c +16 -14
@@ -72,13 +72,13 @@ static inline bool check_xstate_in_sigfrstatic inline bool save_fsave_header (struct task_struct *tsk,void __user *buf) {if (use_fxsr ()) { -struct xregs_state *xsave = &tsk->thread.fpu.state.xsave; +struct xregs_state *xsave = &tsk->thread.fpu.fpstate->regs.xsave;struct user_i387_ia32_struct env;struct _fpstate_32 __user *fp = buf;fpregs_lock ();if (!test_thread_flag (TIF_NEED_FPU_LOAD )) -fxsave (&tsk->thread.fpu.state.fxsave); +fxsave (&tsk->thread.fpu.fpstate->regs.fxsave);fpregs_unlock ();convert_from_fxsr (&env, tsk);
@@ -303,7 +303,7 @@ static bool restore_fpregs_from_user(voi* been restored from a user buffer directly. */ if (test_thread_flag (TIF_NEED_FPU_LOAD ) &&xfeatures_mask_supervisor ()) -os_xrstor (&fpu->state.xsave,xfeatures_mask_supervisor ()); +os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ());fpregs_mark_activate ();fpregs_unlock ();
@@ -317,6 +317,7 @@ static bool __fpu_restore_sig(void __usestruct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;struct user_i387_ia32_struct env; +union fpregs_state *fpregs;u64 user_xfeatures =0 ;bool fx_only = false;bool success;
@@ -349,6 +350,7 @@ static bool __fpu_restore_sig(void __useif (__copy_from_user (&env, buf,sizeof (env)))return false; + fpregs = &fpu->fpstate->regs;/* * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is * not modified on context switch and that the xstate is considered
@@ -366,7 +368,7 @@ static bool __fpu_restore_sig(void __use* the right place in memory. It's ia32 mode. Shrug. */ if (xfeatures_mask_supervisor ()) -os_xsave (&fpu->state.xsave); +os_xsave (&fpregs->xsave);set_thread_flag (TIF_NEED_FPU_LOAD ); }__fpu_invalidate_fpregs_state (fpu);
@@ -374,29 +376,29 @@ static bool __fpu_restore_sig(void __usefpregs_unlock ();if (use_xsave () && !fx_only) { -if (copy_sigframe_from_user_to_xstate (&fpu->state.xsave, buf_fx)) +if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx))return false; }else { -if (__copy_from_user (&fpu->state.fxsave, buf_fx, -sizeof (fpu->state.fxsave))) +if (__copy_from_user (&fpregs->fxsave, buf_fx, +sizeof (fpregs->fxsave)))return false;if (IS_ENABLED (CONFIG_X86_64 )) {/* Reject invalid MXCSR values. */ -if (fpu->state.fxsave.mxcsr & ~mxcsr_feature_mask) +if (fpregs->fxsave.mxcsr & ~mxcsr_feature_mask)return false; }else {/* Mask invalid bits out for historical reasons (broken hardware). */ - fpu->state.fxsave.mxcsr &= ~mxcsr_feature_mask; + fpregs->fxsave.mxcsr &= ~mxcsr_feature_mask; }/* Enforce XFEATURE_MASK_FPSSE when XSAVE is enabled */ if (use_xsave ()) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; + fpregs->xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; }/* Fold the legacy FP storage */ -convert_to_fxsr (&fpu->state.fxsave, &env); +convert_to_fxsr (&fpregs->fxsave, &env);fpregs_lock ();if (use_xsave ()) {
@@ -411,10 +413,10 @@ static bool __fpu_restore_sig(void __use*/ u64 mask = user_xfeatures |xfeatures_mask_supervisor (); - fpu->state.xsave.header.xfeatures &= mask; - success = !os_xrstor_safe (&fpu->state.xsave, xfeatures_mask_all); + fpregs->xsave.header.xfeatures &= mask; + success = !os_xrstor_safe (&fpregs->xsave, xfeatures_mask_all); }else { - success = !fxrstor_safe (&fpu->state.fxsave); + success = !fxrstor_safe (&fpregs->fxsave); }if (likely (success))
[tip: x86/fpu] x86/fpu/signal: Convert to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 7e049e8b74591038c831e765585ae9038b7880a1 Gitweb: https://git.kernel.org/tip/7e049e8b74591038c831e765585ae9038b7880a1 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:37 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 22:41:15 +02:00
x86/fpu/signal: Convert to fpstate
Convert signal related code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/kernel/fpu/signal.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
arch/x86/kernel/fpu/signal.c +16 -14
@@ -72,13 +72,13 @@ setfx:static inline bool save_fsave_header (struct task_struct *tsk,void __user *buf) {if (use_fxsr ()) { -struct xregs_state *xsave = &tsk->thread.fpu.state.xsave; +struct xregs_state *xsave = &tsk->thread.fpu.fpstate->regs.xsave;struct user_i387_ia32_struct env;struct _fpstate_32 __user *fp = buf;fpregs_lock ();if (!test_thread_flag (TIF_NEED_FPU_LOAD )) -fxsave (&tsk->thread.fpu.state.fxsave); +fxsave (&tsk->thread.fpu.fpstate->regs.fxsave);fpregs_unlock ();convert_from_fxsr (&env, tsk);
@@ -303,7 +303,7 @@ retry:* been restored from a user buffer directly. */ if (test_thread_flag (TIF_NEED_FPU_LOAD ) &&xfeatures_mask_supervisor ()) -os_xrstor (&fpu->state.xsave,xfeatures_mask_supervisor ()); +os_xrstor (&fpu->fpstate->regs.xsave,xfeatures_mask_supervisor ());fpregs_mark_activate ();fpregs_unlock ();
@@ -317,6 +317,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,struct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;struct user_i387_ia32_struct env; +union fpregs_state *fpregs;u64 user_xfeatures =0 ;bool fx_only = false;bool success;
@@ -349,6 +350,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,if (__copy_from_user (&env, buf,sizeof (env)))return false; + fpregs = &fpu->fpstate->regs;/* * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is * not modified on context switch and that the xstate is considered
@@ -366,7 +368,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* the right place in memory. It's ia32 mode. Shrug. */ if (xfeatures_mask_supervisor ()) -os_xsave (&fpu->state.xsave); +os_xsave (&fpregs->xsave);set_thread_flag (TIF_NEED_FPU_LOAD ); }__fpu_invalidate_fpregs_state (fpu);
@@ -374,29 +376,29 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,fpregs_unlock ();if (use_xsave () && !fx_only) { -if (copy_sigframe_from_user_to_xstate (&fpu->state.xsave, buf_fx)) +if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx))return false; }else { -if (__copy_from_user (&fpu->state.fxsave, buf_fx, -sizeof (fpu->state.fxsave))) +if (__copy_from_user (&fpregs->fxsave, buf_fx, +sizeof (fpregs->fxsave)))return false;if (IS_ENABLED (CONFIG_X86_64 )) {/* Reject invalid MXCSR values. */ -if (fpu->state.fxsave.mxcsr & ~mxcsr_feature_mask) +if (fpregs->fxsave.mxcsr & ~mxcsr_feature_mask)return false; }else {/* Mask invalid bits out for historical reasons (broken hardware). */ - fpu->state.fxsave.mxcsr &= mxcsr_feature_mask; + fpregs->fxsave.mxcsr &= mxcsr_feature_mask; }/* Enforce XFEATURE_MASK_FPSSE when XSAVE is enabled */ if (use_xsave ()) - fpu->state.xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; + fpregs->xsave.header.xfeatures |=XFEATURE_MASK_FPSSE ; }/* Fold the legacy FP storage */ -convert_to_fxsr (&fpu->state.fxsave, &env); +convert_to_fxsr (&fpregs->fxsave, &env);fpregs_lock ();if (use_xsave ()) {
@@ -411,10 +413,10 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,*/ u64 mask = user_xfeatures |xfeatures_mask_supervisor (); - fpu->state.xsave.header.xfeatures &= mask; - success = !os_xrstor_safe (&fpu->state.xsave, xfeatures_mask_all); + fpregs->xsave.header.xfeatures &= mask; + success = !os_xrstor_safe (&fpregs->xsave, xfeatures_mask_all); }else { - success = !fxrstor_safe (&fpu->state.fxsave); + success = !fxrstor_safe (&fpregs->fxsave); }if (likely (success))
PATCH 9/21 x86/fpu/core: Convert to fpstate
In reply to Thomas GleixnerConvert the rest of the core code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/fpu/api.h | 4 +-- arch/x86/kernel/fpu/core.c | 44 +++++++++++++++++++++-------------------- arch/x86/kernel/fpu/init.c | 2 - arch/x86/kernel/fpu/xstate.c | 2 - 4 files changed, 27 insertions(+), 25 deletions(-)
arch/x86/include/asm/fpu/api.h +2 -2
@@ -50,9 +50,9 @@ static inline void kernel_fpu_begin(void }/* -* Use fpregs_lock() while editing CPU's FPU registers or fpu->state. +* Use fpregs_lock() while editing CPU's FPU registers or fpu->fpstate. * A context switch will (and softirq might) save CPU's FPU registers to -* fpu->state and set TIF_NEED_FPU_LOAD leaving CPU's FPU registers in +* fpu->fpstate.regs and set TIF_NEED_FPU_LOAD leaving CPU's FPU registers in * a random state. * * local_bh_disable() protects against both preemption and soft interrupts
arch/x86/kernel/fpu/core.c +23 -21
128 lines not on this page · raw message
arch/x86/kernel/fpu/init.c +1 -1
@@ -38,7 +38,7 @@ static void fpu__init_cpu_generic(void)/* Flush out any pending x87 state: */ #ifdef CONFIG_MATH_EMULATION if (!boot_cpu_has (X86_FEATURE_FPU )) -fpstate_init_soft (¤t->thread.fpu.state.soft); +fpstate_init_soft (¤t->thread.fpu.fpstate->regs.soft); else#endif asmvolatile ("fninit" );
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -1094,7 +1094,7 @@ void __copy_xstate_to_uabi_buf(struct mevoid copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk,enum xstate_copy_mode copy_mode) { -__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.state.xsave, +__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.fpstate->regs.xsave, tsk->thread.pkru, copy_mode); }
[tip: x86/fpu] x86/fpu/core: Convert to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: c20942ce5128ef92e2c451f943ba33462ad2fbc4 Gitweb: https://git.kernel.org/tip/c20942ce5128ef92e2c451f943ba33462ad2fbc4 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:39 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 23:54:26 +02:00
x86/fpu/core: Convert to fpstate
Convert the rest of the core code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/include/asm/fpu/api.h | 4 +-- arch/x86/kernel/fpu/core.c | 44 +++++++++++++++++---------------- arch/x86/kernel/fpu/init.c | 2 +- arch/x86/kernel/fpu/xstate.c | 2 +- 4 files changed, 27 insertions(+), 25 deletions(-)
arch/x86/include/asm/fpu/api.h +2 -2
@@ -50,9 +50,9 @@ static inline void kernel_fpu_begin(void) }/* -* Use fpregs_lock() while editing CPU's FPU registers or fpu->state. +* Use fpregs_lock() while editing CPU's FPU registers or fpu->fpstate. * A context switch will (and softirq might) save CPU's FPU registers to -* fpu->state and set TIF_NEED_FPU_LOAD leaving CPU's FPU registers in +* fpu->fpstate.regs and set TIF_NEED_FPU_LOAD leaving CPU's FPU registers in * a random state. * * local_bh_disable() protects against both preemption and soft interrupts
arch/x86/kernel/fpu/core.c +23 -21
128 lines not on this page · raw message
arch/x86/kernel/fpu/init.c +1 -1
@@ -38,7 +38,7 @@ static void fpu__init_cpu_generic(void)/* Flush out any pending x87 state: */ #ifdef CONFIG_MATH_EMULATION if (!boot_cpu_has (X86_FEATURE_FPU )) -fpstate_init_soft (¤t->thread.fpu.state.soft); +fpstate_init_soft (¤t->thread.fpu.fpstate->regs.soft); else#endif asmvolatile ("fninit" );
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -1094,7 +1094,7 @@ out:void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk,enum xstate_copy_mode copy_mode) { -__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.state.xsave, +__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.fpstate->regs.xsave, tsk->thread.pkru, copy_mode); }
PATCH 10/21 x86/math-emu: Convert to fpstate
In reply to Thomas GleixnerConvert math emulation code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/math-emu/fpu_aux.c | 2 +- arch/x86/math-emu/fpu_entry.c | 4 ++-- arch/x86/math-emu/fpu_system.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/math-emu/fpu_aux.c +1 -1
@@ -53,7 +53,7 @@ void fpstate_init_soft(struct swregs_stavoid finit (void ) { -fpstate_init_soft (¤t->thread.fpu.state.soft); +fpstate_init_soft (¤t->thread.fpu.fpstate->regs.soft); }/*
arch/x86/math-emu/fpu_entry.c +2 -2
@@ -640,7 +640,7 @@ int fpregs_soft_set(struct task_struct * unsigned int pos, unsigned int count, const void *kbuf, const void __user *ubuf) { - struct swregs_state *s387 = &target->thread.fpu.state.soft; + struct swregs_state *s387 = &target->thread.fpu.fpstate->regs.soft;void *space = s387->st_space;int ret;int offset, other, i, tags, regnr, tag, newtop;
@@ -691,7 +691,7 @@ int fpregs_soft_get(struct task_struct * const struct user_regset *regset, struct membuf to) { -struct swregs_state *s387 = &target->thread.fpu.state.soft; +struct swregs_state *s387 = &target->thread.fpu.fpstate->regs.soft;const void *space = s387->st_space;int offset = (S387 ->ftop &7 ) *10 , other =80 - offset;
arch/x86/math-emu/fpu_system.h +1 -1
@@ -73,7 +73,7 @@ static inline bool seg_writable(struct dreturn (d->type &SEG_TYPE_EXECUTE_MASK ) ==SEG_TYPE_WRITABLE ; } -#define I387 (¤t->thread.fpu.state) +#define I387 (¤t->thread.fpu.fpstate->regs)#define FPU_info (I387->soft.info)#define FPU_CS (*(unsigned short *) &(FPU_info->regs->cs))
[tip: x86/fpu] x86/math-emu: Convert to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 63d6bdf36ce1541e656966604c12ac4d9fc5d1f0 Gitweb: https://git.kernel.org/tip/63d6bdf36ce1541e656966604c12ac4d9fc5d1f0 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:40 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 23:57:54 +02:00
x86/math-emu: Convert to fpstate
Convert math emulation code to the new register storage mechanism in preparation for dynamically sized buffers.
No functional change.
--- arch/x86/math-emu/fpu_aux.c | 2 +- arch/x86/math-emu/fpu_entry.c | 4 ++-- arch/x86/math-emu/fpu_system.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/math-emu/fpu_aux.c +1 -1
@@ -53,7 +53,7 @@ void fpstate_init_soft(struct swregs_state *soft)void finit (void ) { -fpstate_init_soft (¤t->thread.fpu.state.soft); +fpstate_init_soft (¤t->thread.fpu.fpstate->regs.soft); }/*
arch/x86/math-emu/fpu_entry.c +2 -2
@@ -640,7 +640,7 @@ int fpregs_soft_set(struct task_struct *target, unsigned int pos, unsigned int count, const void *kbuf, const void __user *ubuf) { - struct swregs_state *s387 = &target->thread.fpu.state.soft; + struct swregs_state *s387 = &target->thread.fpu.fpstate->regs.soft;void *space = s387->st_space;int ret;int offset, other, i, tags, regnr, tag, newtop;
@@ -691,7 +691,7 @@ int fpregs_soft_get(struct task_struct *target, const struct user_regset *regset, struct membuf to) { -struct swregs_state *s387 = &target->thread.fpu.state.soft; +struct swregs_state *s387 = &target->thread.fpu.fpstate->regs.soft;const void *space = s387->st_space;int offset = (S387 ->ftop &7 ) *10 , other =80 - offset;
arch/x86/math-emu/fpu_system.h +1 -1
@@ -73,7 +73,7 @@ static inline bool seg_writable(struct desc_struct *d)return (d->type &SEG_TYPE_EXECUTE_MASK ) ==SEG_TYPE_WRITABLE ; } -#define I387 (¤t->thread.fpu.state) +#define I387 (¤t->thread.fpu.fpstate->regs)#define FPU_info (I387->soft.info)#define FPU_CS (*(unsigned short *) &(FPU_info->regs->cs))
PATCH 11/21 x86/fpu: Remove fpu::state
In reply to Thomas GleixnerAll users converted. Remove it along with the sanity checks.
--- arch/x86/include/asm/fpu/types.h | 18 +++++++----------- arch/x86/kernel/fpu/init.c | 4 ---- 2 files changed, 7 insertions(+), 15 deletions(-)
arch/x86/include/asm/fpu/types.h +7 -11
@@ -352,20 +352,16 @@ struct fpu {struct fpstate *fpstate;/* -* @state: +* @__fpstate: * -* In-memory copy of all FPU registers that we save/restore -* over context switches. If the task is using the FPU then -* the registers in the FPU are more recent than this state -* copy. If the task context-switches away then they get -* saved here and represent the FPU state. +* Initial in-memory storage for FPU registers which are saved in +* context switch and when the kernel uses the FPU. The registers +* are restored from this storage on return to user space if they +* are not longer containing the tasks FPU register state. */ -union { -struct fpstate __fpstate; -union fpregs_state state; - }; +struct fpstate __fpstate;/* -* WARNING: 'state' is dynamically-sized. Do not put +* WARNING: '__fpstate' is dynamically-sized. Do not put * anything after it here. */ };
arch/x86/kernel/fpu/init.c +0 -4
@@ -184,10 +184,6 @@ static void __init fpu__init_task_struct CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu); CHECK_MEMBER_AT_END_OF(struct task_struct, thread); -BUILD_BUG_ON (sizeof (struct fpstate ) !=sizeof (union fpregs_state )); - BUILD_BUG_ON(offsetof(struct thread_struct, fpu.state) != - offsetof(struct thread_struct, fpu.__fpstate)); - arch_task_struct_size = task_size; }
[tip: x86/fpu] x86/fpu: Remove fpu::state
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 2f27b5034244c4ebd70c90066defa771a99a5320 Gitweb: https://git.kernel.org/tip/2f27b5034244c4ebd70c90066defa771a99a5320 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:42 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Wed, 20 Oct 2021 23:58:29 +02:00
x86/fpu: Remove fpu::state
All users converted. Remove it along with the sanity checks.
--- arch/x86/include/asm/fpu/types.h | 18 +++++++----------- arch/x86/kernel/fpu/init.c | 4 ---- 2 files changed, 7 insertions(+), 15 deletions(-)
arch/x86/include/asm/fpu/types.h +7 -11
@@ -352,20 +352,16 @@ struct fpu {struct fpstate *fpstate;/* -* @state: +* @__fpstate: * -* In-memory copy of all FPU registers that we save/restore -* over context switches. If the task is using the FPU then -* the registers in the FPU are more recent than this state -* copy. If the task context-switches away then they get -* saved here and represent the FPU state. +* Initial in-memory storage for FPU registers which are saved in +* context switch and when the kernel uses the FPU. The registers +* are restored from this storage on return to user space if they +* are not longer containing the tasks FPU register state. */ -union { -struct fpstate __fpstate; -union fpregs_state state; - }; +struct fpstate __fpstate;/* -* WARNING: 'state' is dynamically-sized. Do not put +* WARNING: '__fpstate' is dynamically-sized. Do not put * anything after it here. */ };
arch/x86/kernel/fpu/init.c +0 -4
@@ -184,10 +184,6 @@ static void __init fpu__init_task_struct_size(void) CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu); CHECK_MEMBER_AT_END_OF(struct task_struct, thread); -BUILD_BUG_ON (sizeof (struct fpstate ) !=sizeof (union fpregs_state )); - BUILD_BUG_ON(offsetof(struct thread_struct, fpu.state) != - offsetof(struct thread_struct, fpu.__fpstate)); - arch_task_struct_size = task_size; }
PATCH 12/21 x86/fpu: Do not leak fpstate pointer on fork
In reply to Thomas GleixnerIf fork fails early then the copied task struct would carry the fpstate pointer of the parent task.
Not a problem right now, but later when dynamically allocated buffers are available, keeping the pointer might result in freeing the parents buffer. Set it to NULL which prevents that. If fork reaches clone_thread, the pointer will be correctly set to the new task context.
--- arch/x86/kernel/process.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kernel/process.c +2 -0
@@ -87,6 +87,8 @@ int arch_dup_task_struct(struct task_str#ifdef CONFIG_VM86 dst->thread.vm86 =NULL ;#endif +/* Drop the copied pointer to current's fpstate */ + dst->thread.fpu.fpstate =NULL ;return 0 ; }
[tip: x86/fpu] x86/fpu: Do not leak fpstate pointer on fork
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: f0cbc8b3cdf7d1c724155cd9cecffe329bb96119 Gitweb: https://git.kernel.org/tip/f0cbc8b3cdf7d1c724155cd9cecffe329bb96119 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:43 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 09:32:41 +02:00
x86/fpu: Do not leak fpstate pointer on fork
If fork fails early then the copied task struct would carry the fpstate pointer of the parent task.
Not a problem right now, but later when dynamically allocated buffers are available, keeping the pointer might result in freeing the parent's buffer. Set it to NULL which prevents that. If fork reaches clone_thread(), the pointer will be correctly set to the new task context.
--- arch/x86/kernel/process.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kernel/process.c +2 -0
@@ -87,6 +87,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)#ifdef CONFIG_VM86 dst->thread.vm86 =NULL ;#endif +/* Drop the copied pointer to current's fpstate */ + dst->thread.fpu.fpstate =NULL ;return 0 ; }
PATCH 13/21 x86/process: Move arch_thread_struct_whitelist() out of line
In reply to Thomas GleixnerIn preparation for dynamic enabled FPU features move the function out of line as the goal is to expose less and not more information.
--- arch/x86/include/asm/processor.h | 9 +++------ arch/x86/kernel/fpu/core.c | 10 ++++++++++ arch/x86/kernel/fpu/internal.h | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/include/asm/processor.h +3 -6
@@ -461,9 +461,6 @@ DECLARE_PER_CPU(struct irq_stack *, hard DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);#endif /* !X86_64 */ -extern unsigned int fpu_kernel_xstate_size; -extern unsigned int fpu_user_xstate_size; -struct perf_event ;struct thread_struct {
@@ -537,12 +534,12 @@ struct thread_struct {*/ }; -/* Whitelist the FPU register state from the task_struct for hardened usercopy. */ +extern void fpu_thread_struct_whitelist (unsigned long *offset,unsigned long *size); +static inline void arch_thread_struct_whitelist (unsigned long *offset,unsigned long *size) { - *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); - *size = fpu_kernel_xstate_size; +fpu_thread_struct_whitelist (offset, size); } static inline void
arch/x86/kernel/fpu/core.c +10 -0
@@ -405,6 +405,16 @@ int fpu_clone(struct task_struct *dst) }/* +* Whitelist the FPU register state embedded into task_struct for hardened +* usercopy. +*/ +void fpu_thread_struct_whitelist (unsigned long *offset,unsigned long *size) +{ + *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); + *size = fpu_kernel_xstate_size; +} + +/* * Drops current FPU state: deactivates the fpregs and * the fpstate. NOTE: it still leaves previous contents * in the fpregs in the eager-FPU case.
arch/x86/kernel/fpu/internal.h +2 -0
@@ -2,6 +2,8 @@#ifndef __X86_KERNEL_FPU_INTERNAL_H#define __X86_KERNEL_FPU_INTERNAL_H +extern unsigned int fpu_kernel_xstate_size; +extern unsigned int fpu_user_xstate_size;extern struct fpstate init_fpstate;/* CPU feature check wrappers */
[tip: x86/fpu] x86/process: Move arch_thread_struct_whitelist() out of line
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 2dd8eedc80b184bb16aad697ae60367c5bf07299 Gitweb: https://git.kernel.org/tip/2dd8eedc80b184bb16aad697ae60367c5bf07299 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:45 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 09:33:41 +02:00
x86/process: Move arch_thread_struct_whitelist() out of line
In preparation for dynamically enabled FPU features move the function out of line as the goal is to expose less and not more information.
--- arch/x86/include/asm/processor.h | 9 +++------ arch/x86/kernel/fpu/core.c | 10 ++++++++++ arch/x86/kernel/fpu/internal.h | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/include/asm/processor.h +3 -6
@@ -461,9 +461,6 @@ DECLARE_PER_CPU(struct irq_stack *, hardirq_stack_ptr); DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);#endif /* !X86_64 */ -extern unsigned int fpu_kernel_xstate_size; -extern unsigned int fpu_user_xstate_size; -struct perf_event ;struct thread_struct {
@@ -537,12 +534,12 @@ struct thread_struct {*/ }; -/* Whitelist the FPU register state from the task_struct for hardened usercopy. */ +extern void fpu_thread_struct_whitelist (unsigned long *offset,unsigned long *size); +static inline void arch_thread_struct_whitelist (unsigned long *offset,unsigned long *size) { - *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); - *size = fpu_kernel_xstate_size; +fpu_thread_struct_whitelist (offset, size); } static inline void
arch/x86/kernel/fpu/core.c +10 -0
@@ -405,6 +405,16 @@ int fpu_clone(struct task_struct *dst) }/* +* Whitelist the FPU register state embedded into task_struct for hardened +* usercopy. +*/ +void fpu_thread_struct_whitelist (unsigned long *offset,unsigned long *size) +{ + *offset = offsetof(struct thread_struct, fpu.__fpstate.regs); + *size = fpu_kernel_xstate_size; +} + +/* * Drops current FPU state: deactivates the fpregs and * the fpstate. NOTE: it still leaves previous contents * in the fpregs in the eager-FPU case.
arch/x86/kernel/fpu/internal.h +2 -0
@@ -2,6 +2,8 @@#ifndef __X86_KERNEL_FPU_INTERNAL_H#define __X86_KERNEL_FPU_INTERNAL_H +extern unsigned int fpu_kernel_xstate_size; +extern unsigned int fpu_user_xstate_size;extern struct fpstate init_fpstate;/* CPU feature check wrappers */
PATCH 14/21 x86/fpu: Add size and mask information to fpstate
In reply to Thomas GleixnerAdd state size and feature mask information to the fpstate container. This will be used for runtime checks with the upcoming support for dynamically enabled features and dynamically sized buffers. That avoids conditionals all over the place as the required information is accessible for both default and extended buffers.
--- arch/x86/include/asm/fpu/types.h | 12 ++++++++++++ arch/x86/kernel/fpu/core.c | 6 ++++++ arch/x86/kernel/fpu/init.c | 9 +++++++++ arch/x86/kernel/fpu/xstate.c | 3 +++ 4 files changed, 30 insertions(+)
arch/x86/include/asm/fpu/types.h +12 -0
@@ -310,6 +310,18 @@ union fpregs_state { };struct fpstate { +/* @kernel_size: The size of the kernel register image */ +unsigned int size; + +/* @user_size: The size in non-compacted UABI format */ +unsigned int user_size; + +/* @xfeatures: xfeatures for which the storage is sized */ +u64 xfeatures; + +/* @user_xfeatures: xfeatures valid in UABI buffers */ +u64 user_xfeatures; +/* @regs: The register state union for all supported formats */ union fpregs_state regs;
arch/x86/kernel/fpu/core.c +6 -0
@@ -342,6 +342,12 @@ void fpstate_reset(struct fpu *fpu) {/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate; + +/* Initialize sizes and feature masks */ + fpu->fpstate->size = fpu_kernel_xstate_size; + fpu->fpstate->user_size = fpu_user_xstate_size; + fpu->fpstate->xfeatures = xfeatures_mask_all; + fpu->fpstate->user_xfeatures =xfeatures_mask_uabi (); }#if IS_ENABLED (CONFIG_KVM )
arch/x86/kernel/fpu/init.c +9 -0
@@ -212,6 +212,14 @@ static void __init fpu__init_system_xsta } fpu_user_xstate_size = fpu_kernel_xstate_size; +fpstate_reset (¤t->thread.fpu); +} + +static void __init fpu__init_init_fpstate (void ) +{ +/* Bring init_fpstate size and features up to date */ + init_fpstate.size = fpu_kernel_xstate_size; + init_fpstate.xfeatures = xfeatures_mask_all; }/*
@@ -233,4 +241,5 @@ void __init fpu__init_system(struct cpuifpu__init_system_xstate_size_legacy ();fpu__init_system_xstate ();fpu__init_task_struct_size (); +fpu__init_init_fpstate (); }
arch/x86/kernel/fpu/xstate.c +3 -0
@@ -720,6 +720,7 @@ static void __init fpu__init_disable_sys xfeatures_mask_all =0 ;cr4_clear_bits (X86_CR4_OSXSAVE );setup_clear_cpu_cap (X86_FEATURE_XSAVE ); +fpstate_reset (¤t->thread.fpu); }/*
@@ -792,6 +793,8 @@ void __init fpu__init_system_xstate(voidif (err) goto out_disable; +fpstate_reset (¤t->thread.fpu); +/* * Update info used for ptrace frames; use standard-format size and no * supervisor xstates:
[tip: x86/fpu] x86/fpu: Add size and mask information to fpstate
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 248452ce21aeb08da2d2af23d88f890886bd379f Gitweb: https://git.kernel.org/tip/248452ce21aeb08da2d2af23d88f890886bd379f Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:46 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 13:51:42 +02:00
x86/fpu: Add size and mask information to fpstate
Add state size and feature mask information to the fpstate container. This will be used for runtime checks with the upcoming support for dynamically enabled features and dynamically sized buffers. That avoids conditionals all over the place as the required information is accessible for both default and extended buffers.
--- arch/x86/include/asm/fpu/types.h | 12 ++++++++++++ arch/x86/kernel/fpu/core.c | 6 ++++++ arch/x86/kernel/fpu/init.c | 9 +++++++++ arch/x86/kernel/fpu/xstate.c | 3 +++ 4 files changed, 30 insertions(+)
arch/x86/include/asm/fpu/types.h +12 -0
@@ -310,6 +310,18 @@ union fpregs_state { };struct fpstate { +/* @kernel_size: The size of the kernel register image */ +unsigned int size; + +/* @user_size: The size in non-compacted UABI format */ +unsigned int user_size; + +/* @xfeatures: xfeatures for which the storage is sized */ +u64 xfeatures; + +/* @user_xfeatures: xfeatures valid in UABI buffers */ +u64 user_xfeatures; +/* @regs: The register state union for all supported formats */ union fpregs_state regs;
arch/x86/kernel/fpu/core.c +6 -0
@@ -342,6 +342,12 @@ void fpstate_reset(struct fpu *fpu) {/* Set the fpstate pointer to the default fpstate */ fpu->fpstate = &fpu->__fpstate; + +/* Initialize sizes and feature masks */ + fpu->fpstate->size = fpu_kernel_xstate_size; + fpu->fpstate->user_size = fpu_user_xstate_size; + fpu->fpstate->xfeatures = xfeatures_mask_all; + fpu->fpstate->user_xfeatures =xfeatures_mask_uabi (); }#if IS_ENABLED (CONFIG_KVM )
arch/x86/kernel/fpu/init.c +9 -0
@@ -212,6 +212,14 @@ static void __init fpu__init_system_xstate_size_legacy(void) } fpu_user_xstate_size = fpu_kernel_xstate_size; +fpstate_reset (¤t->thread.fpu); +} + +static void __init fpu__init_init_fpstate (void ) +{ +/* Bring init_fpstate size and features up to date */ + init_fpstate.size = fpu_kernel_xstate_size; + init_fpstate.xfeatures = xfeatures_mask_all; }/*
@@ -233,4 +241,5 @@ void __init fpu__init_system(struct cpuinfo_x86 *c)fpu__init_system_xstate_size_legacy ();fpu__init_system_xstate ();fpu__init_task_struct_size (); +fpu__init_init_fpstate (); }
arch/x86/kernel/fpu/xstate.c +3 -0
@@ -720,6 +720,7 @@ static void __init fpu__init_disable_system_xstate(void) xfeatures_mask_all =0 ;cr4_clear_bits (X86_CR4_OSXSAVE );setup_clear_cpu_cap (X86_FEATURE_XSAVE ); +fpstate_reset (¤t->thread.fpu); }/*
@@ -792,6 +793,8 @@ void __init fpu__init_system_xstate(void)if (err) goto out_disable; +fpstate_reset (¤t->thread.fpu); +/* * Update info used for ptrace frames; use standard-format size and no * supervisor xstates:
PATCH 15/21 x86/fpu: Use fpstate::size
In reply to Thomas GleixnerMake use of fpstate::size in various places which require the buffer size information for sanity checks or memcpy() sizing.
--- arch/x86/kernel/fpu/core.c | 13 ++++++------- arch/x86/kernel/fpu/signal.c | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/core.c +6 -7
@@ -166,13 +166,12 @@ void fpu_swap_kvm_fpu(struct fpu *save,fpregs_lock ();if (save) { -if (test_thread_flag (TIF_NEED_FPU_LOAD )) { -memcpy (&save->fpstate->regs, - ¤t->thread.fpu.fpstate->regs, - fpu_kernel_xstate_size); - }else { +struct fpstate *fpcur = current->thread.fpu.fpstate; + +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +memcpy (&save->fpstate->regs, &fpcur->regs, fpcur->size); +else save_fpregs_to_fpstate (save); - } }if (rstor) {
@@ -398,7 +397,7 @@ int fpu_clone(struct task_struct *dst)fpregs_lock ();if (test_thread_flag (TIF_NEED_FPU_LOAD )) {memcpy (&dst_fpu->fpstate->regs, &src_fpu->fpstate->regs, - fpu_kernel_xstate_size); + dst_fpu->fpstate->size); }else {save_fpregs_to_fpstate (dst_fpu); }
arch/x86/kernel/fpu/signal.c +3 -4
@@ -313,15 +313,13 @@ static bool restore_fpregs_from_user(voistatic bool __fpu_restore_sig (void __user *buf,void __user *buf_fx,bool ia32_fxstate) { -int state_size = fpu_kernel_xstate_size;struct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;struct user_i387_ia32_struct env; +bool success, fx_only = false;union fpregs_state *fpregs; +unsigned int state_size;u64 user_xfeatures =0 ; -bool fx_only = false; -bool success; -if (use_xsave ()) {struct _fpx_sw_bytes fx_sw_user;
@@ -334,6 +332,7 @@ static bool __fpu_restore_sig(void __use user_xfeatures = fx_sw_user.xfeatures; }else { user_xfeatures =XFEATURE_MASK_FPSSE ; + state_size = fpu->fpstate->size; }if (likely (!ia32_fxstate)) {
[tip: x86/fpu] x86/fpu: Use fpstate::size
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: be31dfdfd75b172af3ddcfa7511cdc3bb7adb25e Gitweb: https://git.kernel.org/tip/be31dfdfd75b172af3ddcfa7511cdc3bb7adb25e Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:48 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:02:25 +02:00
x86/fpu: Use fpstate::size
Make use of fpstate::size in various places which require the buffer size information for sanity checks or memcpy() sizing.
--- arch/x86/kernel/fpu/core.c | 13 ++++++------- arch/x86/kernel/fpu/signal.c | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/core.c +6 -7
@@ -166,13 +166,12 @@ void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask)fpregs_lock ();if (save) { -if (test_thread_flag (TIF_NEED_FPU_LOAD )) { -memcpy (&save->fpstate->regs, - ¤t->thread.fpu.fpstate->regs, - fpu_kernel_xstate_size); - }else { +struct fpstate *fpcur = current->thread.fpu.fpstate; + +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +memcpy (&save->fpstate->regs, &fpcur->regs, fpcur->size); +else save_fpregs_to_fpstate (save); - } }if (rstor) {
@@ -398,7 +397,7 @@ int fpu_clone(struct task_struct *dst)fpregs_lock ();if (test_thread_flag (TIF_NEED_FPU_LOAD )) {memcpy (&dst_fpu->fpstate->regs, &src_fpu->fpstate->regs, - fpu_kernel_xstate_size); + dst_fpu->fpstate->size); }else {save_fpregs_to_fpstate (dst_fpu); }
arch/x86/kernel/fpu/signal.c +3 -4
@@ -313,15 +313,13 @@ retry:static bool __fpu_restore_sig (void __user *buf,void __user *buf_fx,bool ia32_fxstate) { -int state_size = fpu_kernel_xstate_size;struct task_struct *tsk = current;struct fpu *fpu = &tsk->thread.fpu;struct user_i387_ia32_struct env; +bool success, fx_only = false;union fpregs_state *fpregs; +unsigned int state_size;u64 user_xfeatures =0 ; -bool fx_only = false; -bool success; -if (use_xsave ()) {struct _fpx_sw_bytes fx_sw_user;
@@ -334,6 +332,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, user_xfeatures = fx_sw_user.xfeatures; }else { user_xfeatures =XFEATURE_MASK_FPSSE ; + state_size = fpu->fpstate->size; }if (likely (!ia32_fxstate)) {
PATCH 16/21 x86/fpu/xstate: Use fpstate for os_xsave()
In reply to Thomas GleixnerWith variable feature sets XSAVE[S} requires to know the feature set for which the buffer is valid. Retrieve it from fpstate.
--- arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/fpu/signal.c | 4 ++-- arch/x86/kernel/fpu/xstate.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/core.c +1 -1
@@ -105,7 +105,7 @@ EXPORT_SYMBOL(irq_fpu_usable);void save_fpregs_to_fpstate (struct fpu *fpu) {if (likely (use_xsave ())) { -os_xsave (&fpu->fpstate->regs.xsave); +os_xsave (fpu->fpstate);/* * AVX512 state is tracked here because its use is
arch/x86/kernel/fpu/signal.c +2 -2
@@ -349,7 +349,6 @@ static bool __fpu_restore_sig(void __useif (__copy_from_user (&env, buf,sizeof (env)))return false; - fpregs = &fpu->fpstate->regs;/* * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is * not modified on context switch and that the xstate is considered
@@ -367,13 +366,14 @@ static bool __fpu_restore_sig(void __use* the right place in memory. It's ia32 mode. Shrug. */ if (xfeatures_mask_supervisor ()) -os_xsave (&fpregs->xsave); +os_xsave (fpu->fpstate);set_thread_flag (TIF_NEED_FPU_LOAD ); }__fpu_invalidate_fpregs_state (fpu);__cpu_invalidate_fpregs_state ();fpregs_unlock (); + fpregs = &fpu->fpstate->regs;if (use_xsave () && !fx_only) {if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx))return false;
arch/x86/kernel/fpu/xstate.h +3 -3
@@ -101,16 +101,16 @@ extern void *get_xsave_addr(struct xregs* Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features * and command line options. The choice is permanent until the next reboot. */ -static inline void os_xsave (struct xregs_state *xstate) +static inline void os_xsave (struct fpstate *fpstate) { -u64 mask = xfeatures_mask_all; +u64 mask = fpstate->xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err;WARN_ON_FPU (!alternatives_patched); -XSTATE_XSAVE (xstate, lmask, hmask, err); +XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);/* We should never fault when copying to a kernel buffer: */ WARN_ON_FPU (err);
[tip: x86/fpu] x86/fpu/xstate: Use fpstate for os_xsave()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 073e627a4537e682c43a1e8df659ce24cbced40c Gitweb: https://git.kernel.org/tip/073e627a4537e682c43a1e8df659ce24cbced40c Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:49 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:03:36 +02:00
x86/fpu/xstate: Use fpstate for os_xsave()
With variable feature sets XSAVE[S] requires to know the feature set for which the buffer is valid. Retrieve it from fpstate.
--- arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/fpu/signal.c | 4 ++-- arch/x86/kernel/fpu/xstate.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/core.c +1 -1
@@ -105,7 +105,7 @@ EXPORT_SYMBOL(irq_fpu_usable);void save_fpregs_to_fpstate (struct fpu *fpu) {if (likely (use_xsave ())) { -os_xsave (&fpu->fpstate->regs.xsave); +os_xsave (fpu->fpstate);/* * AVX512 state is tracked here because its use is
arch/x86/kernel/fpu/signal.c +2 -2
@@ -349,7 +349,6 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,if (__copy_from_user (&env, buf,sizeof (env)))return false; - fpregs = &fpu->fpstate->regs;/* * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is * not modified on context switch and that the xstate is considered
@@ -367,13 +366,14 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* the right place in memory. It's ia32 mode. Shrug. */ if (xfeatures_mask_supervisor ()) -os_xsave (&fpregs->xsave); +os_xsave (fpu->fpstate);set_thread_flag (TIF_NEED_FPU_LOAD ); }__fpu_invalidate_fpregs_state (fpu);__cpu_invalidate_fpregs_state ();fpregs_unlock (); + fpregs = &fpu->fpstate->regs;if (use_xsave () && !fx_only) {if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx))return false;
arch/x86/kernel/fpu/xstate.h +3 -3
@@ -101,16 +101,16 @@ extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);* Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features * and command line options. The choice is permanent until the next reboot. */ -static inline void os_xsave (struct xregs_state *xstate) +static inline void os_xsave (struct fpstate *fpstate) { -u64 mask = xfeatures_mask_all; +u64 mask = fpstate->xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err;WARN_ON_FPU (!alternatives_patched); -XSTATE_XSAVE (xstate, lmask, hmask, err); +XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);/* We should never fault when copying to a kernel buffer: */ WARN_ON_FPU (err);
PATCH 17/21 x86/fpu/xstate: Use fpstate for xsave_to_user_sigframe()
In reply to Thomas GleixnerWith dynamically enabled features the sigframe code must know the features which are enabled for the task. Get them from fpstate.
--- arch/x86/kernel/fpu/xstate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.h +1 -1
@@ -149,7 +149,7 @@ static inline int xsave_to_user_sigframe* internally, e.g. PKRU. That's user space ABI and also required * to allow the signal handler to modify PKRU. */ -u64 mask =xfeatures_mask_uabi (); +u64 mask = current->thread.fpu.fpstate->user_xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err;
[tip: x86/fpu] x86/fpu/xstate: Use fpstate for xsave_to_user_sigframe()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 0b2d39aa03574eb401cdfaac2f483a6f68173355 Gitweb: https://git.kernel.org/tip/0b2d39aa03574eb401cdfaac2f483a6f68173355 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:51 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:04:46 +02:00
x86/fpu/xstate: Use fpstate for xsave_to_user_sigframe()
With dynamically enabled features the sigframe code must know the features which are enabled for the task. Get them from fpstate.
--- arch/x86/kernel/fpu/xstate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/xstate.h +1 -1
@@ -149,7 +149,7 @@ static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)* internally, e.g. PKRU. That's user space ABI and also required * to allow the signal handler to modify PKRU. */ -u64 mask =xfeatures_mask_uabi (); +u64 mask = current->thread.fpu.fpstate->user_xfeatures;u32 lmask = mask;u32 hmask = mask >>32 ;int err;
PATCH 18/21 x86/fpu: Use fpstate in fpu_copy_kvm_uabi_to_fpstate()
In reply to Thomas GleixnerStraight forward conversion. No functional change.
--- arch/x86/kernel/fpu/core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/core.c +6 -6
@@ -205,7 +205,7 @@ EXPORT_SYMBOL_GPL(fpu_copy_fpstate_to_kvint fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *vpkru) { -union fpregs_state *kstate = &fpu->fpstate->regs; +struct fpstate *kstate = fpu->fpstate;const union fpregs_state *ustate = buf;struct pkru_state *xpkru;int ret;
@@ -215,25 +215,25 @@ int fpu_copy_kvm_uabi_to_fpstate(structreturn -EINVAL ;if (ustate->fxsave.mxcsr & ~mxcsr_feature_mask)return -EINVAL ; -memcpy (&kstate->fxsave, &ustate->fxsave,sizeof (ustate->fxsave)); +memcpy (&kstate->regs.fxsave, &ustate->fxsave,sizeof (ustate->fxsave));return 0 ; }if (ustate->xsave.header.xfeatures & ~xcr0)return -EINVAL ; - ret =copy_uabi_from_kernel_to_xstate (&kstate->xsave, ustate); + ret =copy_uabi_from_kernel_to_xstate (&kstate->regs.xsave, ustate);if (ret)return ret;/* Retrieve PKRU if not in init state */ -if (kstate->xsave.header.xfeatures &XFEATURE_MASK_PKRU ) { - xpkru =get_xsave_addr (&kstate->xsave,XFEATURE_PKRU ); +if (kstate->regs.xsave.header.xfeatures &XFEATURE_MASK_PKRU ) { + xpkru =get_xsave_addr (&kstate->regs.xsave,XFEATURE_PKRU ); *vpkru = xpkru->pkru; }/* Ensure that XCOMP_BV is set up for XSAVES */ -xstate_init_xcomp_bv (&kstate->xsave,xfeatures_mask_uabi ()); +xstate_init_xcomp_bv (&kstate->regs.xsave,xfeatures_mask_uabi ());return 0 ; }EXPORT_SYMBOL_GPL (fpu_copy_kvm_uabi_to_fpstate);
[tip: x86/fpu] x86/fpu: Use fpstate in fpu_copy_kvm_uabi_to_fpstate()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: ad6ede407aae01d9617e172b27e179ce1046cbfc Gitweb: https://git.kernel.org/tip/ad6ede407aae01d9617e172b27e179ce1046cbfc Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:52 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:17:47 +02:00
x86/fpu: Use fpstate in fpu_copy_kvm_uabi_to_fpstate()
Straight forward conversion. No functional change.
--- arch/x86/kernel/fpu/core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/core.c +6 -6
@@ -205,7 +205,7 @@ EXPORT_SYMBOL_GPL(fpu_copy_fpstate_to_kvm_uabi);int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *vpkru) { -union fpregs_state *kstate = &fpu->fpstate->regs; +struct fpstate *kstate = fpu->fpstate;const union fpregs_state *ustate = buf;struct pkru_state *xpkru;int ret;
@@ -215,25 +215,25 @@ int fpu_copy_kvm_uabi_to_fpstate(struct fpu *fpu, const void *buf, u64 xcr0,return -EINVAL ;if (ustate->fxsave.mxcsr & ~mxcsr_feature_mask)return -EINVAL ; -memcpy (&kstate->fxsave, &ustate->fxsave,sizeof (ustate->fxsave)); +memcpy (&kstate->regs.fxsave, &ustate->fxsave,sizeof (ustate->fxsave));return 0 ; }if (ustate->xsave.header.xfeatures & ~xcr0)return -EINVAL ; - ret =copy_uabi_from_kernel_to_xstate (&kstate->xsave, ustate); + ret =copy_uabi_from_kernel_to_xstate (&kstate->regs.xsave, ustate);if (ret)return ret;/* Retrieve PKRU if not in init state */ -if (kstate->xsave.header.xfeatures &XFEATURE_MASK_PKRU ) { - xpkru =get_xsave_addr (&kstate->xsave,XFEATURE_PKRU ); +if (kstate->regs.xsave.header.xfeatures &XFEATURE_MASK_PKRU ) { + xpkru =get_xsave_addr (&kstate->regs.xsave,XFEATURE_PKRU ); *vpkru = xpkru->pkru; }/* Ensure that XCOMP_BV is set up for XSAVES */ -xstate_init_xcomp_bv (&kstate->xsave,xfeatures_mask_uabi ()); +xstate_init_xcomp_bv (&kstate->regs.xsave,xfeatures_mask_uabi ());return 0 ; }EXPORT_SYMBOL_GPL (fpu_copy_kvm_uabi_to_fpstate);
PATCH 19/21 x86/fpu: Use fpstate in __copy_xstate_to_uabi_buf()
In reply to Thomas GleixnerWith dynamically enabled features the copy function must know the features and the size which is valid for the task. Retrieve them from fpstate.
--- arch/x86/kernel/fpu/core.c | 8 ++++---- arch/x86/kernel/fpu/xstate.c | 11 ++++++----- arch/x86/kernel/fpu/xstate.h | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-)
arch/x86/kernel/fpu/core.c +4 -4
@@ -187,15 +187,15 @@ EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpu);void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru) { -union fpregs_state *kstate = &fpu->fpstate->regs; +struct fpstate *kstate = fpu->fpstate;union fpregs_state *ustate = buf;struct membuf mb = { .p = buf, .left = size };if (cpu_feature_enabled (X86_FEATURE_XSAVE )) { -__copy_xstate_to_uabi_buf (mb, &kstate->xsave, pkru, -XSTATE_COPY_XSAVE ); +__copy_xstate_to_uabi_buf (mb, kstate, pkru,XSTATE_COPY_XSAVE ); }else { -memcpy (&ustate->fxsave, &kstate->fxsave,sizeof (ustate->fxsave)); +memcpy (&ustate->fxsave, &kstate->regs.fxsave, +sizeof (ustate->fxsave));/* Make it restorable on a XSAVE enabled host */ ustate->xsave.header.xfeatures =XFEATURE_MASK_FPSSE ; }
arch/x86/kernel/fpu/xstate.c +6 -5
@@ -969,7 +969,7 @@ static void copy_feature(bool from_xstat/** * __copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer * @to: membuf descriptor -* @xsave: The xsave from which to copy +* @fpstate: The fpstate buffer from which to copy * @pkru_val: The PKRU value to store in the PKRU component * @copy_mode: The requested copy mode *
@@ -979,11 +979,12 @@ static void copy_feature(bool from_xstat* * It supports partial copy but @to.pos always starts from zero. */ -void __copy_xstate_to_uabi_buf (struct membuf to,struct xregs_state *xsave, +void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode) {const unsigned int off_mxcsr = offsetof(struct fxregs_state , mxcsr);struct xregs_state *xinit = &init_fpstate.regs.xsave; +struct xregs_state *xsave = &fpstate->regs.xsave;struct xstate_header header;unsigned int zerofrom;u64 mask;
@@ -1003,7 +1004,7 @@ void __copy_xstate_to_uabi_buf(struct mebreak ;case XSTATE_COPY_XSAVE : - header.xfeatures &=xfeatures_mask_uabi (); + header.xfeatures &= fpstate->user_xfeatures;break ; }
@@ -1046,7 +1047,7 @@ void __copy_xstate_to_uabi_buf(struct me* but there is no state to copy from in the compacted * init_fpstate. The gap tracking will zero these states. */ - mask =xfeatures_mask_uabi (); + mask = fpstate->user_xfeatures;for_each_extended_xfeature (i, mask) {/*
@@ -1097,7 +1098,7 @@ void __copy_xstate_to_uabi_buf(struct mevoid copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk,enum xstate_copy_mode copy_mode) { -__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.fpstate->regs.xsave, +__copy_xstate_to_uabi_buf (to, tsk->thread.fpu.fpstate, tsk->thread.pkru, copy_mode); }
arch/x86/kernel/fpu/xstate.h +1 -1
@@ -15,7 +15,7 @@ static inline void xstate_init_xcomp_bv( xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } -extern void __copy_xstate_to_uabi_buf (struct membuf to,struct xregs_state *xsave, +extern void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode);extern void fpu__init_cpu_xstate (void );
[tip: x86/fpu] x86/fpu: Use fpstate in __copy_xstate_to_uabi_buf()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 3ac8d75778fc8c1c22daad9bc674166b862f6f6e Gitweb: https://git.kernel.org/tip/3ac8d75778fc8c1c22daad9bc674166b862f6f6e Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:54 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:18:23 +02:00
x86/fpu: Use fpstate in __copy_xstate_to_uabi_buf()
With dynamically enabled features the copy function must know the features and the size which is valid for the task. Retrieve them from fpstate.
--- arch/x86/kernel/fpu/core.c | 8 ++++---- arch/x86/kernel/fpu/xstate.c | 11 ++++++----- arch/x86/kernel/fpu/xstate.h | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-)
arch/x86/kernel/fpu/core.c +4 -4
@@ -187,15 +187,15 @@ EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpu);void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru) { -union fpregs_state *kstate = &fpu->fpstate->regs; +struct fpstate *kstate = fpu->fpstate;union fpregs_state *ustate = buf;struct membuf mb = { .p = buf, .left = size };if (cpu_feature_enabled (X86_FEATURE_XSAVE )) { -__copy_xstate_to_uabi_buf (mb, &kstate->xsave, pkru, -XSTATE_COPY_XSAVE ); +__copy_xstate_to_uabi_buf (mb, kstate, pkru,XSTATE_COPY_XSAVE ); }else { -memcpy (&ustate->fxsave, &kstate->fxsave,sizeof (ustate->fxsave)); +memcpy (&ustate->fxsave, &kstate->regs.fxsave, +sizeof (ustate->fxsave));/* Make it restorable on a XSAVE enabled host */ ustate->xsave.header.xfeatures =XFEATURE_MASK_FPSSE ; }
arch/x86/kernel/fpu/xstate.c +6 -5
@@ -969,7 +969,7 @@ static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,/** * __copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer * @to: membuf descriptor -* @xsave: The xsave from which to copy +* @fpstate: The fpstate buffer from which to copy * @pkru_val: The PKRU value to store in the PKRU component * @copy_mode: The requested copy mode *
@@ -979,11 +979,12 @@ static void copy_feature(bool from_xstate, struct membuf *to, void *xstate,* * It supports partial copy but @to.pos always starts from zero. */ -void __copy_xstate_to_uabi_buf (struct membuf to,struct xregs_state *xsave, +void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode) {const unsigned int off_mxcsr = offsetof(struct fxregs_state , mxcsr);struct xregs_state *xinit = &init_fpstate.regs.xsave; +struct xregs_state *xsave = &fpstate->regs.xsave;struct xstate_header header;unsigned int zerofrom;u64 mask;
@@ -1003,7 +1004,7 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,break ;case XSTATE_COPY_XSAVE : - header.xfeatures &=xfeatures_mask_uabi (); + header.xfeatures &= fpstate->user_xfeatures;break ; }
@@ -1046,7 +1047,7 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,* but there is no state to copy from in the compacted * init_fpstate. The gap tracking will zero these states. */ - mask =xfeatures_mask_uabi (); + mask = fpstate->user_xfeatures;for_each_extended_xfeature (i, mask) {/*
@@ -1097,7 +1098,7 @@ out:void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk,enum xstate_copy_mode copy_mode) { -__copy_xstate_to_uabi_buf (to, &tsk->thread.fpu.fpstate->regs.xsave, +__copy_xstate_to_uabi_buf (to, tsk->thread.fpu.fpstate, tsk->thread.pkru, copy_mode); }
arch/x86/kernel/fpu/xstate.h +1 -1
@@ -15,7 +15,7 @@ static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask) xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } -extern void __copy_xstate_to_uabi_buf (struct membuf to,struct xregs_state *xsave, +extern void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode);extern void fpu__init_cpu_xstate (void );
PATCH 20/21 x86/fpu/xstate: Use fpstate for copy_uabi_to_xstate()
In reply to Thomas GleixnerPrepare for dynamically enabled states per task. The function needs to retrieve the features and sizes which are valid in a fpstate context. Retrieve them from fpstate.
Move the function declarations to the core header as they are not required anywhere else.
--- arch/x86/include/asm/fpu/xstate.h | 12 ------------ arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/fpu/regset.c | 5 ++--- arch/x86/kernel/fpu/signal.c | 2 +- arch/x86/kernel/fpu/xstate.c | 18 ++++++++++-------- arch/x86/kernel/fpu/xstate.h | 12 ++++++++++++ 6 files changed, 26 insertions(+), 25 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -12
@@ -129,20 +129,8 @@ extern void __init update_regset_xstate_ u64 xstate_mask);int xfeature_size (int xfeature_nr); -int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf); -int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave,const void __user *ubuf);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); -enum xstate_copy_mode { -XSTATE_COPY_FP , -XSTATE_COPY_FX , -XSTATE_COPY_XSAVE , -}; - -struct membuf ; -void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk, -enum xstate_copy_mode mode); -#endif
arch/x86/kernel/fpu/core.c +1 -1
@@ -222,7 +222,7 @@ int fpu_copy_kvm_uabi_to_fpstate(structif (ustate->xsave.header.xfeatures & ~xcr0)return -EINVAL ; - ret =copy_uabi_from_kernel_to_xstate (&kstate->regs.xsave, ustate); + ret =copy_uabi_from_kernel_to_xstate (kstate, ustate);if (ret)return ret;
arch/x86/kernel/fpu/regset.c +2 -3
@@ -8,11 +8,11 @@#include <asm/fpu/api.h> #include <asm/fpu/signal.h> #include <asm/fpu/regset.h> -#include <asm/fpu/xstate.h> #include "context.h" #include "internal.h" #include "legacy.h" +#include "xstate.h" /* * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
@@ -168,8 +168,7 @@ int xstateregs_set(struct task_struct *t }fpu_force_restore (fpu); - ret =copy_uabi_from_kernel_to_xstate (&fpu->fpstate->regs.xsave, - kbuf ?: tmpbuf); + ret =copy_uabi_from_kernel_to_xstate (fpu->fpstate, kbuf ?: tmpbuf); out:vfree (tmpbuf);
arch/x86/kernel/fpu/signal.c +1 -1
@@ -375,7 +375,7 @@ static bool __fpu_restore_sig(void __use fpregs = &fpu->fpstate->regs;if (use_xsave () && !fx_only) { -if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx)) +if (copy_sigframe_from_user_to_xstate (fpu->fpstate, buf_fx))return false; }else { if (__copy_from_user(&fpregs->fxsave, buf_fx,
arch/x86/kernel/fpu/xstate.c +10 -8
@@ -463,10 +463,11 @@ int xfeature_size(int xfeature_nr) }/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */ -static int validate_user_xstate_header (const struct xstate_header *hdr) +static int validate_user_xstate_header (const struct xstate_header *hdr, +struct fpstate *fpstate) {/* No unknown or supervisor features may be set */ -if (hdr->xfeatures & ~xfeatures_mask_uabi ()) +if (hdr->xfeatures & ~fpstate->user_xfeatures)return -EINVAL ;/* Userspace must use the uncompacted format */
@@ -1115,9 +1116,10 @@ static int copy_from_buffer(void *dst, u } -static int copy_uabi_to_xstate (struct xregs_state *xsave,const void *kbuf, +static int copy_uabi_to_xstate (struct fpstate *fpstate,const void *kbuf,const void __user *ubuf) { +struct xregs_state *xsave = &fpstate->regs.xsave;unsigned int offset, size;struct xstate_header hdr;u64 mask;
@@ -1127,7 +1129,7 @@ static int copy_uabi_to_xstate(struct xrif (copy_from_buffer (&hdr, offset,sizeof (hdr), kbuf, ubuf))return -EFAULT ; -if (validate_user_xstate_header (&hdr)) +if (validate_user_xstate_header (&hdr, fpstate))return -EINVAL ;/* Validate MXCSR when any of the related features is in use */
@@ -1182,9 +1184,9 @@ static int copy_uabi_to_xstate(struct xr* Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] * format and copy to the target thread. Used by ptrace and KVM. */ -int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf) +int copy_uabi_from_kernel_to_xstate (struct fpstate *fpstate,const void *kbuf) { -return copy_uabi_to_xstate (xsave, kbuf,NULL ); +return copy_uabi_to_xstate (fpstate, kbuf,NULL ); }/*
@@ -1192,10 +1194,10 @@ int copy_uabi_from_kernel_to_xstate(stru* XSAVE[S] format and copy to the target thread. This is called from the * sigreturn() and rt_sigreturn() system calls. */ -int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave, +int copy_sigframe_from_user_to_xstate (struct fpstate *fpstate,const void __user *ubuf) { -return copy_uabi_to_xstate (xsave,NULL , ubuf); +return copy_uabi_to_xstate (fpstate,NULL , ubuf); }static bool validate_independent_components (u64 mask)
arch/x86/kernel/fpu/xstate.h +12 -0
@@ -15,8 +15,20 @@ static inline void xstate_init_xcomp_bv( xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } +enum xstate_copy_mode { +XSTATE_COPY_FP , +XSTATE_COPY_FX , +XSTATE_COPY_XSAVE , +}; + +struct membuf ;extern void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode); +extern void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk, +enum xstate_copy_mode mode); +extern int copy_uabi_from_kernel_to_xstate (struct fpstate *fpstate,const void *kbuf); +extern int copy_sigframe_from_user_to_xstate (struct fpstate *fpstate,const void __user *ubuf); +extern void fpu__init_cpu_xstate (void );extern void fpu__init_system_xstate (void );
[tip: x86/fpu] x86/fpu/xstate: Use fpstate for copy_uabi_to_xstate()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 49e4eb4125d506937e52e10c34c8cafd93ab0ed6 Gitweb: https://git.kernel.org/tip/49e4eb4125d506937e52e10c34c8cafd93ab0ed6 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 13 Oct 2021 16:55:55 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:24:14 +02:00
x86/fpu/xstate: Use fpstate for copy_uabi_to_xstate()
Prepare for dynamically enabled states per task. The function needs to retrieve the features and sizes which are valid in a fpstate context. Retrieve them from fpstate.
Move the function declarations to the core header as they are not required anywhere else.
--- arch/x86/include/asm/fpu/xstate.h | 12 ------------ arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/fpu/regset.c | 5 ++--- arch/x86/kernel/fpu/signal.c | 2 +- arch/x86/kernel/fpu/xstate.c | 18 ++++++++++-------- arch/x86/kernel/fpu/xstate.h | 12 ++++++++++++ 6 files changed, 26 insertions(+), 25 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -12
@@ -129,20 +129,8 @@ extern void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask);int xfeature_size (int xfeature_nr); -int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf); -int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave,const void __user *ubuf);void xsaves (struct xregs_state *xsave,u64 mask);void xrstors (struct xregs_state *xsave,u64 mask); -enum xstate_copy_mode { -XSTATE_COPY_FP , -XSTATE_COPY_FX , -XSTATE_COPY_XSAVE , -}; - -struct membuf ; -void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk, -enum xstate_copy_mode mode); -#endif
arch/x86/kernel/fpu/core.c +1 -1
@@ -222,7 +222,7 @@ int fpu_copy_kvm_uabi_to_fpstate(struct fpu *fpu, const void *buf, u64 xcr0,if (ustate->xsave.header.xfeatures & ~xcr0)return -EINVAL ; - ret =copy_uabi_from_kernel_to_xstate (&kstate->regs.xsave, ustate); + ret =copy_uabi_from_kernel_to_xstate (kstate, ustate);if (ret)return ret;
arch/x86/kernel/fpu/regset.c +2 -3
@@ -8,11 +8,11 @@#include <asm/fpu/api.h> #include <asm/fpu/signal.h> #include <asm/fpu/regset.h> -#include <asm/fpu/xstate.h> #include "context.h" #include "internal.h" #include "legacy.h" +#include "xstate.h" /* * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
@@ -168,8 +168,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, }fpu_force_restore (fpu); - ret =copy_uabi_from_kernel_to_xstate (&fpu->fpstate->regs.xsave, - kbuf ?: tmpbuf); + ret =copy_uabi_from_kernel_to_xstate (fpu->fpstate, kbuf ?: tmpbuf); out:vfree (tmpbuf);
arch/x86/kernel/fpu/signal.c +1 -1
@@ -375,7 +375,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, fpregs = &fpu->fpstate->regs;if (use_xsave () && !fx_only) { -if (copy_sigframe_from_user_to_xstate (&fpregs->xsave, buf_fx)) +if (copy_sigframe_from_user_to_xstate (fpu->fpstate, buf_fx))return false; }else { if (__copy_from_user(&fpregs->fxsave, buf_fx,
arch/x86/kernel/fpu/xstate.c +10 -8
@@ -463,10 +463,11 @@ int xfeature_size(int xfeature_nr) }/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */ -static int validate_user_xstate_header (const struct xstate_header *hdr) +static int validate_user_xstate_header (const struct xstate_header *hdr, +struct fpstate *fpstate) {/* No unknown or supervisor features may be set */ -if (hdr->xfeatures & ~xfeatures_mask_uabi ()) +if (hdr->xfeatures & ~fpstate->user_xfeatures)return -EINVAL ;/* Userspace must use the uncompacted format */
@@ -1115,9 +1116,10 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, } -static int copy_uabi_to_xstate (struct xregs_state *xsave,const void *kbuf, +static int copy_uabi_to_xstate (struct fpstate *fpstate,const void *kbuf,const void __user *ubuf) { +struct xregs_state *xsave = &fpstate->regs.xsave;unsigned int offset, size;struct xstate_header hdr;u64 mask;
@@ -1127,7 +1129,7 @@ static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,if (copy_from_buffer (&hdr, offset,sizeof (hdr), kbuf, ubuf))return -EFAULT ; -if (validate_user_xstate_header (&hdr)) +if (validate_user_xstate_header (&hdr, fpstate))return -EINVAL ;/* Validate MXCSR when any of the related features is in use */
@@ -1182,9 +1184,9 @@ static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,* Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] * format and copy to the target thread. Used by ptrace and KVM. */ -int copy_uabi_from_kernel_to_xstate (struct xregs_state *xsave,const void *kbuf) +int copy_uabi_from_kernel_to_xstate (struct fpstate *fpstate,const void *kbuf) { -return copy_uabi_to_xstate (xsave, kbuf,NULL ); +return copy_uabi_to_xstate (fpstate, kbuf,NULL ); }/*
@@ -1192,10 +1194,10 @@ int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)* XSAVE[S] format and copy to the target thread. This is called from the * sigreturn() and rt_sigreturn() system calls. */ -int copy_sigframe_from_user_to_xstate (struct xregs_state *xsave, +int copy_sigframe_from_user_to_xstate (struct fpstate *fpstate,const void __user *ubuf) { -return copy_uabi_to_xstate (xsave,NULL , ubuf); +return copy_uabi_to_xstate (fpstate,NULL , ubuf); }static bool validate_independent_components (u64 mask)
arch/x86/kernel/fpu/xstate.h +12 -0
@@ -15,8 +15,20 @@ static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask) xsave->header.xcomp_bv = mask |XCOMP_BV_COMPACTED_FORMAT ; } +enum xstate_copy_mode { +XSTATE_COPY_FP , +XSTATE_COPY_FX , +XSTATE_COPY_XSAVE , +}; + +struct membuf ;extern void __copy_xstate_to_uabi_buf (struct membuf to,struct fpstate *fpstate,u32 pkru_val,enum xstate_copy_mode copy_mode); +extern void copy_xstate_to_uabi_buf (struct membuf to,struct task_struct *tsk, +enum xstate_copy_mode mode); +extern int copy_uabi_from_kernel_to_xstate (struct fpstate *fpstate,const void *kbuf); +extern int copy_sigframe_from_user_to_xstate (struct fpstate *fpstate,const void __user *ubuf); +extern void fpu__init_cpu_xstate (void );extern void fpu__init_system_xstate (void );
PATCH 21/21 x86/fpu/signal: Use fpstate for size and features
In reply to Thomas GleixnerFor dynamically enabled features it's required to get the features which are enabled for that context when restoring from sigframe.
The same applies for all signal frame size calculations.
--- arch/x86/kernel/fpu/signal.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
arch/x86/kernel/fpu/signal.c +19 -12
@@ -41,7 +41,7 @@ static inline bool check_xstate_in_sigfr/* Check for the first magic field and other error scenarios. */ if (fx_sw->magic1 !=FP_XSTATE_MAGIC1 || fx_sw->xstate_size < min_xstate_size || - fx_sw->xstate_size > fpu_user_xstate_size || + fx_sw->xstate_size > current->thread.fpu.fpstate->user_size || fx_sw->xstate_size > fx_sw->extended_size) goto setfx;
@@ -230,11 +230,11 @@ bool copy_fpstate_to_sigframe(void __usereturn true; } -static int __restore_fpregs_from_user (void __user *buf,u64 xrestore, -bool fx_only) +static int __restore_fpregs_from_user (void __user *buf,u64 ufeatures, +u64 xrestore,bool fx_only) {if (use_xsave ()) { -u64 init_bv =xfeatures_mask_uabi () & ~xrestore; +u64 init_bv = ufeatures & ~xrestore;int ret;if (likely (!fx_only))
@@ -265,7 +265,8 @@ static bool restore_fpregs_from_user(voi retry:fpregs_lock ();pagefault_disable (); - ret =__restore_fpregs_from_user (buf, xrestore, fx_only); + ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, + xrestore, fx_only);pagefault_enable ();if (unlikely (ret)) {
@@ -425,10 +426,11 @@ static bool __fpu_restore_sig(void __usereturn success; } -static inline int xstate_sigframe_size (void ) +static inline unsigned int xstate_sigframe_size (struct fpstate *fpstate) { -return use_xsave () ? fpu_user_xstate_size +FP_XSTATE_MAGIC2_SIZE : - fpu_user_xstate_size; +unsigned int size = fpstate->user_size; + +return use_xsave () ? size +FP_XSTATE_MAGIC2_SIZE : size; }/*
@@ -436,17 +438,19 @@ static inline int xstate_sigframe_size(v*/ bool fpu__restore_sig (void __user *buf,int ia32_frame) { -unsigned int size =xstate_sigframe_size ();struct fpu *fpu = ¤t->thread.fpu;void __user *buf_fx = buf;bool ia32_fxstate = false;bool success = false; +unsigned int size;if (unlikely (!buf)) {fpu__clear_user_states (fpu);return true; } + size =xstate_sigframe_size (fpu->fpstate); + ia32_frame &= (IS_ENABLED (CONFIG_X86_32 ) ||IS_ENABLED (CONFIG_IA32_EMULATION ));
@@ -481,7 +485,7 @@ unsigned long fpu__alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, unsigned long *size) { - unsigned long frame_size = xstate_sigframe_size(); + unsigned long frame_size = xstate_sigframe_size(current->thread.fpu.fpstate); *buf_fx = sp =round_down (sp - frame_size,64 );if (ia32_frame &&use_fxsr ()) {
@@ -494,9 +498,12 @@ fpu__alloc_mathframe(unsigned long sp, ireturn sp; } -unsigned long fpu__get_fpstate_size (void ) +unsigned long __init fpu__get_fpstate_size (void ) { -unsigned long ret =xstate_sigframe_size (); +unsigned long ret = fpu_user_xstate_size; + +if (use_xsave ()) + ret +=FP_XSTATE_MAGIC2_SIZE ;/* * This space is needed on (most) 32-bit kernels, or when a 32-bit
PATCH v2 21/21 x86/fpu/signal: Use fpstate for size and features
For dynamically enabled features it's required to get the features which are enabled for that context when restoring from sigframe.
The same applies for all signal frame size calculations.
---
V2: Added a missing conversion and folded back a conversion which
was hidden in part 3
--- arch/x86/kernel/fpu/signal.c | 44 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-)
arch/x86/kernel/fpu/signal.c +27 -17
@@ -41,7 +41,7 @@ static inline bool check_xstate_in_sigfr/* Check for the first magic field and other error scenarios. */ if (fx_sw->magic1 !=FP_XSTATE_MAGIC1 || fx_sw->xstate_size < min_xstate_size || - fx_sw->xstate_size > fpu_user_xstate_size || + fx_sw->xstate_size > current->thread.fpu.fpstate->user_size || fx_sw->xstate_size > fx_sw->extended_size) goto setfx;
@@ -98,7 +98,8 @@ static inline bool save_fsave_header(strreturn true; } -static inline bool save_xstate_epilog (void __user *buf,int ia32_frame) +static inline bool save_xstate_epilog (void __user *buf,int ia32_frame, +unsigned int usize) {struct xregs_state __user *x = buf;struct _fpx_sw_bytes *sw_bytes;
@@ -113,7 +114,7 @@ static inline bool save_xstate_epilog(voreturn !err; err |=__put_user (FP_XSTATE_MAGIC2 , - (__u32 __user *)(buf + fpu_user_xstate_size)); + (__u32 __user *)(buf + usize));/* * Read the xfeatures which we copied (directly from the cpu or
@@ -171,6 +172,7 @@ static inline int copy_fpregs_to_sigframbool copy_fpstate_to_sigframe (void __user *buf,void __user *buf_fx,int size) {struct task_struct *tsk = current; +struct fpstate *fpstate = tsk->thread.fpu.fpstate;int ia32_fxstate = (buf != buf_fx);int ret;
@@ -215,7 +217,7 @@ bool copy_fpstate_to_sigframe(void __usefpregs_unlock ();if (ret) { -if (!__clear_user (buf_fx, fpu_user_xstate_size)) +if (!__clear_user (buf_fx, fpstate->user_size)) goto retry;return false; }
@@ -224,17 +226,18 @@ bool copy_fpstate_to_sigframe(void __useif ((ia32_fxstate || !use_fxsr ()) && !save_fsave_header (tsk, buf))return false; -if (use_fxsr () && !save_xstate_epilog (buf_fx, ia32_fxstate)) +if (use_fxsr () && + !save_xstate_epilog (buf_fx, ia32_fxstate, fpstate->user_size))return false;return true; } -static int __restore_fpregs_from_user (void __user *buf,u64 xrestore, -bool fx_only) +static int __restore_fpregs_from_user (void __user *buf,u64 ufeatures, +u64 xrestore,bool fx_only) {if (use_xsave ()) { -u64 init_bv =xfeatures_mask_uabi () & ~xrestore; +u64 init_bv = ufeatures & ~xrestore;int ret;if (likely (!fx_only))
@@ -265,7 +268,8 @@ static bool restore_fpregs_from_user(voi retry:fpregs_lock ();pagefault_disable (); - ret =__restore_fpregs_from_user (buf, xrestore, fx_only); + ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, + xrestore, fx_only);pagefault_enable ();if (unlikely (ret)) {
@@ -332,7 +336,7 @@ static bool __fpu_restore_sig(void __use user_xfeatures = fx_sw_user.xfeatures; }else { user_xfeatures =XFEATURE_MASK_FPSSE ; - state_size = fpu->fpstate->size; + state_size = fpu->fpstate->user_size; }if (likely (!ia32_fxstate)) {
@@ -425,10 +429,11 @@ static bool __fpu_restore_sig(void __usereturn success; } -static inline int xstate_sigframe_size (void ) +static inline unsigned int xstate_sigframe_size (struct fpstate *fpstate) { -return use_xsave () ? fpu_user_xstate_size +FP_XSTATE_MAGIC2_SIZE : - fpu_user_xstate_size; +unsigned int size = fpstate->user_size; + +return use_xsave () ? size +FP_XSTATE_MAGIC2_SIZE : size; }/*
@@ -436,17 +441,19 @@ static inline int xstate_sigframe_size(v*/ bool fpu__restore_sig (void __user *buf,int ia32_frame) { -unsigned int size =xstate_sigframe_size ();struct fpu *fpu = ¤t->thread.fpu;void __user *buf_fx = buf;bool ia32_fxstate = false;bool success = false; +unsigned int size;if (unlikely (!buf)) {fpu__clear_user_states (fpu);return true; } + size =xstate_sigframe_size (fpu->fpstate); + ia32_frame &= (IS_ENABLED (CONFIG_X86_32 ) ||IS_ENABLED (CONFIG_IA32_EMULATION ));
@@ -481,7 +488,7 @@ unsigned long fpu__alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, unsigned long *size) { - unsigned long frame_size = xstate_sigframe_size(); + unsigned long frame_size = xstate_sigframe_size(current->thread.fpu.fpstate); *buf_fx = sp =round_down (sp - frame_size,64 );if (ia32_frame &&use_fxsr ()) {
@@ -494,9 +501,12 @@ fpu__alloc_mathframe(unsigned long sp, ireturn sp; } -unsigned long fpu__get_fpstate_size (void ) +unsigned long __init fpu__get_fpstate_size (void ) { -unsigned long ret =xstate_sigframe_size (); +unsigned long ret = fpu_user_xstate_size; + +if (use_xsave ()) + ret +=FP_XSTATE_MAGIC2_SIZE ;/* * This space is needed on (most) 32-bit kernels, or when a 32-bit
[tip: x86/fpu] x86/fpu/signal: Use fpstate for size and features
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 5509cc78080d29b23706dbf076d51691b69f3c79 Gitweb: https://git.kernel.org/tip/5509cc78080d29b23706dbf076d51691b69f3c79 Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 15 Oct 2021 00:51:51 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Thu, 21 Oct 2021 14:24:47 +02:00
x86/fpu/signal: Use fpstate for size and features
For dynamically enabled features it's required to get the features which are enabled for that context when restoring from sigframe.
The same applies for all signal frame size calculations.
--- arch/x86/kernel/fpu/signal.c | 44 +++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 17 deletions(-)
arch/x86/kernel/fpu/signal.c +27 -17
@@ -41,7 +41,7 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf,/* Check for the first magic field and other error scenarios. */ if (fx_sw->magic1 !=FP_XSTATE_MAGIC1 || fx_sw->xstate_size < min_xstate_size || - fx_sw->xstate_size > fpu_user_xstate_size || + fx_sw->xstate_size > current->thread.fpu.fpstate->user_size || fx_sw->xstate_size > fx_sw->extended_size) goto setfx;
@@ -98,7 +98,8 @@ static inline bool save_fsave_header(struct task_struct *tsk, void __user *buf)return true; } -static inline bool save_xstate_epilog (void __user *buf,int ia32_frame) +static inline bool save_xstate_epilog (void __user *buf,int ia32_frame, +unsigned int usize) {struct xregs_state __user *x = buf;struct _fpx_sw_bytes *sw_bytes;
@@ -113,7 +114,7 @@ static inline bool save_xstate_epilog(void __user *buf, int ia32_frame)return !err; err |=__put_user (FP_XSTATE_MAGIC2 , - (__u32 __user *)(buf + fpu_user_xstate_size)); + (__u32 __user *)(buf + usize));/* * Read the xfeatures which we copied (directly from the cpu or
@@ -171,6 +172,7 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)bool copy_fpstate_to_sigframe (void __user *buf,void __user *buf_fx,int size) {struct task_struct *tsk = current; +struct fpstate *fpstate = tsk->thread.fpu.fpstate;int ia32_fxstate = (buf != buf_fx);int ret;
@@ -215,7 +217,7 @@ retry:fpregs_unlock ();if (ret) { -if (!__clear_user (buf_fx, fpu_user_xstate_size)) +if (!__clear_user (buf_fx, fpstate->user_size)) goto retry;return false; }
@@ -224,17 +226,18 @@ retry:if ((ia32_fxstate || !use_fxsr ()) && !save_fsave_header (tsk, buf))return false; -if (use_fxsr () && !save_xstate_epilog (buf_fx, ia32_fxstate)) +if (use_fxsr () && + !save_xstate_epilog (buf_fx, ia32_fxstate, fpstate->user_size))return false;return true; } -static int __restore_fpregs_from_user (void __user *buf,u64 xrestore, -bool fx_only) +static int __restore_fpregs_from_user (void __user *buf,u64 ufeatures, +u64 xrestore,bool fx_only) {if (use_xsave ()) { -u64 init_bv =xfeatures_mask_uabi () & ~xrestore; +u64 init_bv = ufeatures & ~xrestore;int ret;if (likely (!fx_only))
@@ -265,7 +268,8 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, retry:fpregs_lock ();pagefault_disable (); - ret =__restore_fpregs_from_user (buf, xrestore, fx_only); + ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, + xrestore, fx_only);pagefault_enable ();if (unlikely (ret)) {
@@ -332,7 +336,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, user_xfeatures = fx_sw_user.xfeatures; }else { user_xfeatures =XFEATURE_MASK_FPSSE ; - state_size = fpu->fpstate->size; + state_size = fpu->fpstate->user_size; }if (likely (!ia32_fxstate)) {
@@ -425,10 +429,11 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,return success; } -static inline int xstate_sigframe_size (void ) +static inline unsigned int xstate_sigframe_size (struct fpstate *fpstate) { -return use_xsave () ? fpu_user_xstate_size +FP_XSTATE_MAGIC2_SIZE : - fpu_user_xstate_size; +unsigned int size = fpstate->user_size; + +return use_xsave () ? size +FP_XSTATE_MAGIC2_SIZE : size; }/*
@@ -436,17 +441,19 @@ static inline int xstate_sigframe_size(void)*/ bool fpu__restore_sig (void __user *buf,int ia32_frame) { -unsigned int size =xstate_sigframe_size ();struct fpu *fpu = ¤t->thread.fpu;void __user *buf_fx = buf;bool ia32_fxstate = false;bool success = false; +unsigned int size;if (unlikely (!buf)) {fpu__clear_user_states (fpu);return true; } + size =xstate_sigframe_size (fpu->fpstate); + ia32_frame &= (IS_ENABLED (CONFIG_X86_32 ) ||IS_ENABLED (CONFIG_IA32_EMULATION ));
@@ -481,7 +488,7 @@ unsigned long fpu__alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx, unsigned long *size) { - unsigned long frame_size = xstate_sigframe_size(); + unsigned long frame_size = xstate_sigframe_size(current->thread.fpu.fpstate); *buf_fx = sp =round_down (sp - frame_size,64 );if (ia32_frame &&use_fxsr ()) {
@@ -494,9 +501,12 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame,return sp; } -unsigned long fpu__get_fpstate_size (void ) +unsigned long __init fpu__get_fpstate_size (void ) { -unsigned long ret =xstate_sigframe_size (); +unsigned long ret = fpu_user_xstate_size; + +if (use_xsave ()) + ret +=FP_XSTATE_MAGIC2_SIZE ;/* * This space is needed on (most) 32-bit kernels, or when a 32-bit