Skip to content

Commit

Permalink
x86/fpu: Move xstate size to fpu_*_cfg
Browse files Browse the repository at this point in the history
Use the new kernel and user space config storage to store and retrieve the
XSTATE buffer sizes. The default and the maximum size are the same for now,
but will change when support for dynamically enabled features is added.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20211014230739.296830097@linutronix.de
  • Loading branch information
Thomas Gleixner authored and Borislav Petkov committed Oct 21, 2021
1 parent cd9ae76 commit 2bd264b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
8 changes: 4 additions & 4 deletions arch/x86/kernel/fpu/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void fpu_sync_fpstate(struct fpu *fpu)
static inline unsigned int init_fpstate_copy_size(void)
{
if (!use_xsave())
return fpu_kernel_xstate_size;
return fpu_kernel_cfg.default_size;

/* XSAVE(S) just needs the legacy and the xstate header part */
return sizeof(init_fpstate.regs.xsave);
Expand Down Expand Up @@ -347,8 +347,8 @@ void fpstate_reset(struct fpu *fpu)
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->size = fpu_kernel_cfg.default_size;
fpu->fpstate->user_size = fpu_user_cfg.default_size;
fpu->fpstate->xfeatures = xfeatures_mask_all;
fpu->fpstate->user_xfeatures = xfeatures_mask_uabi();
}
Expand Down Expand Up @@ -420,7 +420,7 @@ int fpu_clone(struct task_struct *dst)
void fpu_thread_struct_whitelist(unsigned long *offset, unsigned long *size)
{
*offset = offsetof(struct thread_struct, fpu.__fpstate.regs);
*size = fpu_kernel_xstate_size;
*size = fpu_kernel_cfg.default_size;
}

/*
Expand Down
31 changes: 14 additions & 17 deletions arch/x86/kernel/fpu/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ static void __init fpu__init_system_generic(void)
fpu__init_system_mxcsr();
}

/*
* Size of the FPU context state. All tasks in the system use the
* same context size, regardless of what portion they use.
* This is inherent to the XSAVE architecture which puts all state
* components into a single, continuous memory block:
*/
unsigned int fpu_kernel_xstate_size __ro_after_init;

/* Get alignment of the TYPE. */
#define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)

Expand Down Expand Up @@ -171,7 +163,7 @@ static void __init fpu__init_task_struct_size(void)
* Add back the dynamically-calculated register state
* size.
*/
task_size += fpu_kernel_xstate_size;
task_size += fpu_kernel_cfg.default_size;

/*
* We dynamically size 'struct fpu', so we require that
Expand All @@ -195,25 +187,30 @@ static void __init fpu__init_task_struct_size(void)
*/
static void __init fpu__init_system_xstate_size_legacy(void)
{
unsigned int size;

/*
* Note that xstate sizes might be overwritten later during
* fpu__init_system_xstate().
* Note that the size configuration might be overwritten later
* during fpu__init_system_xstate().
*/
if (!cpu_feature_enabled(X86_FEATURE_FPU))
fpu_kernel_xstate_size = sizeof(struct swregs_state);
size = sizeof(struct swregs_state);
else if (cpu_feature_enabled(X86_FEATURE_FXSR))
fpu_kernel_xstate_size = sizeof(struct fxregs_state);
size = sizeof(struct fxregs_state);
else
fpu_kernel_xstate_size = sizeof(struct fregs_state);
size = sizeof(struct fregs_state);

fpu_user_xstate_size = fpu_kernel_xstate_size;
fpu_kernel_cfg.max_size = size;
fpu_kernel_cfg.default_size = size;
fpu_user_cfg.max_size = size;
fpu_user_cfg.default_size = size;
fpstate_reset(&current->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.size = fpu_kernel_cfg.max_size;
init_fpstate.xfeatures = xfeatures_mask_all;
}

Expand All @@ -234,7 +231,7 @@ void __init fpu__init_system(struct cpuinfo_x86 *c)

fpu__init_system_generic();
fpu__init_system_xstate_size_legacy();
fpu__init_system_xstate();
fpu__init_system_xstate(fpu_kernel_cfg.max_size);
fpu__init_task_struct_size();
fpu__init_init_fpstate();
}
2 changes: 0 additions & 2 deletions arch/x86/kernel/fpu/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#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 */
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/fpu/regset.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
/*
* A whole standard-format XSAVE buffer is needed:
*/
if (pos != 0 || count != fpu_user_xstate_size)
if (pos != 0 || count != fpu_user_cfg.max_size)
return -EFAULT;

if (!kbuf) {
Expand Down
6 changes: 3 additions & 3 deletions arch/x86/kernel/fpu/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame,

unsigned long __init fpu__get_fpstate_size(void)
{
unsigned long ret = fpu_user_xstate_size;
unsigned long ret = fpu_user_cfg.max_size;

if (use_xsave())
ret += FP_XSTATE_MAGIC2_SIZE;
Expand Down Expand Up @@ -531,12 +531,12 @@ unsigned long __init fpu__get_fpstate_size(void)
*/
void __init fpu__init_prepare_fx_sw_frame(void)
{
int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
int size = fpu_user_cfg.default_size + FP_XSTATE_MAGIC2_SIZE;

fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
fx_sw_reserved.extended_size = size;
fx_sw_reserved.xfeatures = xfeatures_mask_uabi();
fx_sw_reserved.xstate_size = fpu_user_xstate_size;
fx_sw_reserved.xstate_size = fpu_user_cfg.default_size;

if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
IS_ENABLED(CONFIG_X86_32)) {
Expand Down
32 changes: 18 additions & 14 deletions arch/x86/kernel/fpu/xstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ static unsigned int xstate_comp_offsets[XFEATURE_MAX] __ro_after_init =
static unsigned int xstate_supervisor_only_offsets[XFEATURE_MAX] __ro_after_init =
{ [ 0 ... XFEATURE_MAX - 1] = -1};

/*
* The XSAVE area of kernel can be in standard or compacted format;
* it is always in standard format for user mode. This is the user
* mode standard format size used for signal and ptrace frames.
*/
unsigned int fpu_user_xstate_size __ro_after_init;

/*
* Return whether the system supports a given xfeature.
*
Expand Down Expand Up @@ -716,8 +709,11 @@ static int __init init_xstate_size(void)
if (!paranoid_xstate_size_valid(kernel_size))
return -EINVAL;

fpu_kernel_xstate_size = kernel_size;
fpu_user_xstate_size = user_size;
/* Keep it the same for now */
fpu_kernel_cfg.max_size = kernel_size;
fpu_kernel_cfg.default_size = kernel_size;
fpu_user_cfg.max_size = user_size;
fpu_user_cfg.default_size = user_size;

return 0;
}
Expand All @@ -726,19 +722,26 @@ static int __init init_xstate_size(void)
* We enabled the XSAVE hardware, but something went wrong and
* we can not use it. Disable it.
*/
static void __init fpu__init_disable_system_xstate(void)
static void __init fpu__init_disable_system_xstate(unsigned int legacy_size)
{
xfeatures_mask_all = 0;
cr4_clear_bits(X86_CR4_OSXSAVE);
setup_clear_cpu_cap(X86_FEATURE_XSAVE);

/* Restore the legacy size.*/
fpu_kernel_cfg.max_size = legacy_size;
fpu_kernel_cfg.default_size = legacy_size;
fpu_user_cfg.max_size = legacy_size;
fpu_user_cfg.default_size = legacy_size;

fpstate_reset(&current->thread.fpu);
}

/*
* Enable and initialize the xsave feature.
* Called once per system bootup.
*/
void __init fpu__init_system_xstate(void)
void __init fpu__init_system_xstate(unsigned int legacy_size)
{
unsigned int eax, ebx, ecx, edx;
u64 xfeatures;
Expand Down Expand Up @@ -810,7 +813,8 @@ void __init fpu__init_system_xstate(void)
* Update info used for ptrace frames; use standard-format size and no
* supervisor xstates:
*/
update_regset_xstate_info(fpu_user_xstate_size, xfeatures_mask_uabi());
update_regset_xstate_info(fpu_user_cfg.max_size,
xfeatures_mask_uabi());

fpu__init_prepare_fx_sw_frame();
setup_init_fpu_buf();
Expand All @@ -830,13 +834,13 @@ void __init fpu__init_system_xstate(void)
print_xstate_offset_size();
pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
xfeatures_mask_all,
fpu_kernel_xstate_size,
fpu_kernel_cfg.max_size,
boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard");
return;

out_disable:
/* something went wrong, try to boot without any XSAVE support */
fpu__init_disable_system_xstate();
fpu__init_disable_system_xstate(legacy_size);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/fpu/xstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void


extern void fpu__init_cpu_xstate(void);
extern void fpu__init_system_xstate(void);
extern void fpu__init_system_xstate(unsigned int legacy_size);

extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);

Expand Down

0 comments on commit 2bd264b

Please sign in to comment.