2012-02-21 21:19:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1994 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Pentium III FXSR, SSE support
|
|
|
|
* General FPU state handling cleanups
|
|
|
|
* Gareth Hughes <gareth@valinux.com>, May 2000
|
|
|
|
* x86-64 work by Andi Kleen 2002
|
|
|
|
*/
|
|
|
|
|
2015-04-24 00:54:44 +00:00
|
|
|
#ifndef _ASM_X86_FPU_INTERNAL_H
|
|
|
|
#define _ASM_X86_FPU_INTERNAL_H
|
2012-02-21 21:19:22 +00:00
|
|
|
|
2012-07-24 23:05:27 +00:00
|
|
|
#include <linux/compat.h>
|
2015-04-26 14:56:05 +00:00
|
|
|
#include <linux/sched.h>
|
2012-02-21 21:19:22 +00:00
|
|
|
#include <linux/slab.h>
|
2015-04-22 08:58:10 +00:00
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
#include <asm/user.h>
|
2015-04-24 00:46:00 +00:00
|
|
|
#include <asm/fpu/api.h>
|
2015-04-28 06:41:33 +00:00
|
|
|
#include <asm/fpu/xstate.h>
|
2012-02-21 21:19:22 +00:00
|
|
|
|
2015-04-29 18:24:14 +00:00
|
|
|
/*
|
|
|
|
* High level FPU state handling functions:
|
|
|
|
*/
|
2015-04-30 10:59:30 +00:00
|
|
|
extern void fpu__activate_curr(struct fpu *fpu);
|
2015-05-27 10:22:29 +00:00
|
|
|
extern void fpu__activate_fpstate_read(struct fpu *fpu);
|
2015-05-27 10:22:29 +00:00
|
|
|
extern void fpu__activate_fpstate_write(struct fpu *fpu);
|
2015-04-29 18:24:14 +00:00
|
|
|
extern void fpu__save(struct fpu *fpu);
|
2015-05-04 09:49:58 +00:00
|
|
|
extern void fpu__restore(struct fpu *fpu);
|
2015-04-29 19:09:18 +00:00
|
|
|
extern int fpu__restore_sig(void __user *buf, int ia32_frame);
|
2015-04-29 18:24:14 +00:00
|
|
|
extern void fpu__drop(struct fpu *fpu);
|
|
|
|
extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
|
2015-04-29 18:35:33 +00:00
|
|
|
extern void fpu__clear(struct fpu *fpu);
|
2015-05-05 13:56:33 +00:00
|
|
|
extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
|
|
|
|
extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
|
2015-04-29 18:24:14 +00:00
|
|
|
|
2015-05-05 13:56:33 +00:00
|
|
|
/*
|
|
|
|
* Boot time FPU initialization functions:
|
|
|
|
*/
|
|
|
|
extern void fpu__init_cpu(void);
|
|
|
|
extern void fpu__init_system_xstate(void);
|
|
|
|
extern void fpu__init_cpu_xstate(void);
|
|
|
|
extern void fpu__init_system(struct cpuinfo_x86 *c);
|
2015-04-26 14:56:05 +00:00
|
|
|
extern void fpu__init_check_bugs(void);
|
|
|
|
extern void fpu__resume_cpu(void);
|
|
|
|
|
2015-05-05 09:34:49 +00:00
|
|
|
/*
|
|
|
|
* Debugging facility:
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_X86_DEBUG_FPU
|
|
|
|
# define WARN_ON_FPU(x) WARN_ON_ONCE(x)
|
|
|
|
#else
|
|
|
|
# define WARN_ON_FPU(x) ({ 0; })
|
|
|
|
#endif
|
|
|
|
|
2015-02-06 20:02:01 +00:00
|
|
|
/*
|
2015-05-05 13:56:33 +00:00
|
|
|
* FPU related CPU feature flag helper routines:
|
2015-02-06 20:02:01 +00:00
|
|
|
*/
|
2012-09-06 21:58:52 +00:00
|
|
|
static __always_inline __pure bool use_eager_fpu(void)
|
|
|
|
{
|
2014-03-27 22:10:40 +00:00
|
|
|
return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
|
2012-09-06 21:58:52 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
static __always_inline __pure bool use_xsaveopt(void)
|
|
|
|
{
|
2014-03-27 22:10:40 +00:00
|
|
|
return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline __pure bool use_xsave(void)
|
|
|
|
{
|
2014-03-27 22:10:40 +00:00
|
|
|
return static_cpu_has_safe(X86_FEATURE_XSAVE);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline __pure bool use_fxsr(void)
|
|
|
|
{
|
2014-03-27 22:10:40 +00:00
|
|
|
return static_cpu_has_safe(X86_FEATURE_FXSR);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 13:56:33 +00:00
|
|
|
/*
|
|
|
|
* fpstate handling functions:
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern union fpregs_state init_fpstate;
|
|
|
|
|
|
|
|
extern void fpstate_init(union fpregs_state *state);
|
|
|
|
#ifdef CONFIG_MATH_EMULATION
|
|
|
|
extern void fpstate_init_soft(struct swregs_state *soft);
|
|
|
|
#else
|
|
|
|
static inline void fpstate_init_soft(struct swregs_state *soft) {}
|
|
|
|
#endif
|
|
|
|
static inline void fpstate_init_fxstate(struct fxregs_state *fx)
|
|
|
|
{
|
|
|
|
fx->cwd = 0x37f;
|
|
|
|
fx->mxcsr = MXCSR_DEFAULT;
|
|
|
|
}
|
2015-04-28 09:25:02 +00:00
|
|
|
extern void fpstate_sanitize_xstate(struct fpu *fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
|
2012-09-22 00:18:44 +00:00
|
|
|
#define user_insn(insn, output, input...) \
|
|
|
|
({ \
|
|
|
|
int err; \
|
|
|
|
asm volatile(ASM_STAC "\n" \
|
|
|
|
"1:" #insn "\n\t" \
|
|
|
|
"2: " ASM_CLAC "\n" \
|
|
|
|
".section .fixup,\"ax\"\n" \
|
|
|
|
"3: movl $-1,%[err]\n" \
|
|
|
|
" jmp 2b\n" \
|
|
|
|
".previous\n" \
|
|
|
|
_ASM_EXTABLE(1b, 3b) \
|
|
|
|
: [err] "=r" (err), output \
|
|
|
|
: "0"(0), input); \
|
|
|
|
err; \
|
|
|
|
})
|
|
|
|
|
2012-07-24 23:05:28 +00:00
|
|
|
#define check_insn(insn, output, input...) \
|
|
|
|
({ \
|
|
|
|
int err; \
|
|
|
|
asm volatile("1:" #insn "\n\t" \
|
|
|
|
"2:\n" \
|
|
|
|
".section .fixup,\"ax\"\n" \
|
|
|
|
"3: movl $-1,%[err]\n" \
|
|
|
|
" jmp 2b\n" \
|
|
|
|
".previous\n" \
|
|
|
|
_ASM_EXTABLE(1b, 3b) \
|
|
|
|
: [err] "=r" (err), output \
|
|
|
|
: "0"(0), input); \
|
|
|
|
err; \
|
|
|
|
})
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_fregs_to_user(struct fregs_state __user *fx)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2012-09-22 00:18:44 +00:00
|
|
|
return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2012-07-24 23:05:28 +00:00
|
|
|
if (config_enabled(CONFIG_X86_32))
|
2012-09-22 00:18:44 +00:00
|
|
|
return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
|
2012-07-24 23:05:28 +00:00
|
|
|
else if (config_enabled(CONFIG_AS_FXSAVEQ))
|
2012-09-22 00:18:44 +00:00
|
|
|
return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
|
2012-02-21 21:19:22 +00:00
|
|
|
|
2015-04-30 09:34:09 +00:00
|
|
|
/* See comment in copy_fxregs_to_kernel() below. */
|
2012-09-22 00:18:44 +00:00
|
|
|
return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-05-25 08:59:31 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (config_enabled(CONFIG_X86_32)) {
|
|
|
|
err = check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
|
|
|
|
} else {
|
|
|
|
if (config_enabled(CONFIG_AS_FXSAVEQ)) {
|
|
|
|
err = check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
|
|
|
|
} else {
|
|
|
|
/* See comment in copy_fxregs_to_kernel() below. */
|
|
|
|
err = check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx), "m" (*fx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Copying from a kernel buffer to FPU registers should never fail: */
|
|
|
|
WARN_ON_FPU(err);
|
|
|
|
|
|
|
|
return err;
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
|
2012-09-25 22:42:18 +00:00
|
|
|
{
|
|
|
|
if (config_enabled(CONFIG_X86_32))
|
|
|
|
return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
|
|
|
|
else if (config_enabled(CONFIG_AS_FXSAVEQ))
|
|
|
|
return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
|
|
|
|
|
2015-04-30 09:34:09 +00:00
|
|
|
/* See comment in copy_fxregs_to_kernel() below. */
|
2012-09-25 22:42:18 +00:00
|
|
|
return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
|
|
|
|
"m" (*fx));
|
|
|
|
}
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_kernel_to_fregs(struct fregs_state *fx)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-05-25 08:59:31 +00:00
|
|
|
int err = check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
|
|
|
|
|
|
|
|
WARN_ON_FPU(err);
|
|
|
|
|
|
|
|
return err;
|
2012-09-25 22:42:18 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:15:32 +00:00
|
|
|
static inline int copy_user_to_fregs(struct fregs_state __user *fx)
|
2012-09-25 22:42:18 +00:00
|
|
|
{
|
|
|
|
return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 09:34:09 +00:00
|
|
|
static inline void copy_fxregs_to_kernel(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2012-07-24 23:05:28 +00:00
|
|
|
if (config_enabled(CONFIG_X86_32))
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
|
2012-07-24 23:05:28 +00:00
|
|
|
else if (config_enabled(CONFIG_AS_FXSAVEQ))
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
|
2012-07-24 23:05:28 +00:00
|
|
|
else {
|
|
|
|
/* Using "rex64; fxsave %0" is broken because, if the memory
|
|
|
|
* operand uses any extended registers for addressing, a second
|
|
|
|
* REX prefix will be generated (to the assembler, rex64
|
|
|
|
* followed by semicolon is a separate instruction), and hence
|
|
|
|
* the 64-bitness is lost.
|
|
|
|
*
|
|
|
|
* Using "fxsaveq %0" would be the ideal choice, but is only
|
|
|
|
* supported starting with gas 2.16.
|
|
|
|
*
|
|
|
|
* Using, as a workaround, the properly prefixed form below
|
|
|
|
* isn't accepted by any binutils version so far released,
|
|
|
|
* complaining that the same type of prefix is used twice if
|
|
|
|
* an extended register is needed for addressing (fix submitted
|
|
|
|
* to mainline 2005-11-21).
|
|
|
|
*
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
* asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
|
2012-07-24 23:05:28 +00:00
|
|
|
*
|
|
|
|
* This, however, we can work around by forcing the compiler to
|
|
|
|
* select an addressing mode that doesn't require extended
|
|
|
|
* registers.
|
|
|
|
*/
|
|
|
|
asm volatile( "rex64/fxsave (%[fx])"
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
: "=m" (fpu->state.fxsave)
|
|
|
|
: [fx] "R" (&fpu->state.fxsave));
|
2012-07-24 23:05:28 +00:00
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 07:55:39 +00:00
|
|
|
/* These macros all use (%edi)/(%rdi) as the single memory argument. */
|
|
|
|
#define XSAVE ".byte " REX_PREFIX "0x0f,0xae,0x27"
|
|
|
|
#define XSAVEOPT ".byte " REX_PREFIX "0x0f,0xae,0x37"
|
|
|
|
#define XSAVES ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
|
|
|
|
#define XRSTOR ".byte " REX_PREFIX "0x0f,0xae,0x2f"
|
|
|
|
#define XRSTORS ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
|
|
|
|
|
|
|
|
/* xstate instruction fault handler: */
|
|
|
|
#define xstate_fault(__err) \
|
|
|
|
\
|
|
|
|
".section .fixup,\"ax\"\n" \
|
|
|
|
\
|
2015-05-25 09:27:46 +00:00
|
|
|
"3: movl $-2,%[_err]\n" \
|
2015-05-25 07:55:39 +00:00
|
|
|
" jmp 2b\n" \
|
|
|
|
\
|
|
|
|
".previous\n" \
|
|
|
|
\
|
|
|
|
_ASM_EXTABLE(1b, 3b) \
|
2015-05-25 09:27:46 +00:00
|
|
|
: [_err] "=r" (__err)
|
2015-05-25 07:55:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called only during boot time when x86 caps are not set
|
|
|
|
* up and alternative can not be used yet.
|
|
|
|
*/
|
2015-05-24 07:23:25 +00:00
|
|
|
static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
|
2015-05-25 07:55:39 +00:00
|
|
|
{
|
|
|
|
u64 mask = -1;
|
|
|
|
u32 lmask = mask;
|
|
|
|
u32 hmask = mask >> 32;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
WARN_ON(system_state != SYSTEM_BOOTING);
|
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_XSAVES))
|
|
|
|
asm volatile("1:"XSAVES"\n\t"
|
|
|
|
"2:\n\t"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:27:46 +00:00
|
|
|
: "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
|
|
|
|
: "memory");
|
2015-05-25 07:55:39 +00:00
|
|
|
else
|
|
|
|
asm volatile("1:"XSAVE"\n\t"
|
|
|
|
"2:\n\t"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:27:46 +00:00
|
|
|
: "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
|
|
|
|
: "memory");
|
2015-05-24 07:23:25 +00:00
|
|
|
|
|
|
|
/* We should never fault when copying to a kernel buffer: */
|
|
|
|
WARN_ON_FPU(err);
|
2015-05-25 07:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called only during boot time when x86 caps are not set
|
|
|
|
* up and alternative can not be used yet.
|
|
|
|
*/
|
2015-05-24 07:23:25 +00:00
|
|
|
static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate, u64 mask)
|
2015-05-25 07:55:39 +00:00
|
|
|
{
|
|
|
|
u32 lmask = mask;
|
|
|
|
u32 hmask = mask >> 32;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
WARN_ON(system_state != SYSTEM_BOOTING);
|
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_XSAVES))
|
|
|
|
asm volatile("1:"XRSTORS"\n\t"
|
|
|
|
"2:\n\t"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:27:46 +00:00
|
|
|
: "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
|
|
|
|
: "memory");
|
2015-05-25 07:55:39 +00:00
|
|
|
else
|
|
|
|
asm volatile("1:"XRSTOR"\n\t"
|
|
|
|
"2:\n\t"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:27:46 +00:00
|
|
|
: "D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask), "0" (err)
|
|
|
|
: "memory");
|
2015-05-24 07:23:25 +00:00
|
|
|
|
|
|
|
/* We should never fault when copying from a kernel buffer: */
|
|
|
|
WARN_ON_FPU(err);
|
2015-05-25 07:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save processor xstate to xsave area.
|
|
|
|
*/
|
2015-05-24 07:23:25 +00:00
|
|
|
static inline void copy_xregs_to_kernel(struct xregs_state *xstate)
|
2015-05-25 07:55:39 +00:00
|
|
|
{
|
|
|
|
u64 mask = -1;
|
|
|
|
u32 lmask = mask;
|
|
|
|
u32 hmask = mask >> 32;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
WARN_ON(!alternatives_patched);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If xsaves is enabled, xsaves replaces xsaveopt because
|
|
|
|
* it supports compact format and supervisor states in addition to
|
|
|
|
* modified optimization in xsaveopt.
|
|
|
|
*
|
|
|
|
* Otherwise, if xsaveopt is enabled, xsaveopt replaces xsave
|
|
|
|
* because xsaveopt supports modified optimization which is not
|
|
|
|
* supported by xsave.
|
|
|
|
*
|
|
|
|
* If none of xsaves and xsaveopt is enabled, use xsave.
|
|
|
|
*/
|
|
|
|
alternative_input_2(
|
|
|
|
"1:"XSAVE,
|
|
|
|
XSAVEOPT,
|
|
|
|
X86_FEATURE_XSAVEOPT,
|
|
|
|
XSAVES,
|
|
|
|
X86_FEATURE_XSAVES,
|
2015-05-25 08:57:06 +00:00
|
|
|
[xstate] "D" (xstate), "a" (lmask), "d" (hmask) :
|
2015-05-25 07:55:39 +00:00
|
|
|
"memory");
|
|
|
|
asm volatile("2:\n\t"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:59:35 +00:00
|
|
|
: "0" (err)
|
2015-05-25 07:55:39 +00:00
|
|
|
: "memory");
|
|
|
|
|
2015-05-24 07:23:25 +00:00
|
|
|
/* We should never fault when copying to a kernel buffer: */
|
|
|
|
WARN_ON_FPU(err);
|
2015-05-25 07:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore processor xstate from xsave area.
|
|
|
|
*/
|
2015-05-24 07:23:25 +00:00
|
|
|
static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
|
2015-05-25 07:55:39 +00:00
|
|
|
{
|
|
|
|
u32 lmask = mask;
|
|
|
|
u32 hmask = mask >> 32;
|
2015-05-25 09:59:35 +00:00
|
|
|
int err = 0;
|
2015-05-25 07:55:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use xrstors to restore context if it is enabled. xrstors supports
|
|
|
|
* compacted format of xsave area which is not supported by xrstor.
|
|
|
|
*/
|
|
|
|
alternative_input(
|
|
|
|
"1: " XRSTOR,
|
|
|
|
XRSTORS,
|
|
|
|
X86_FEATURE_XSAVES,
|
2015-05-25 08:57:06 +00:00
|
|
|
"D" (xstate), "m" (*xstate), "a" (lmask), "d" (hmask)
|
2015-05-25 07:55:39 +00:00
|
|
|
: "memory");
|
|
|
|
|
|
|
|
asm volatile("2:\n"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:59:35 +00:00
|
|
|
: "0" (err)
|
2015-05-25 07:55:39 +00:00
|
|
|
: "memory");
|
|
|
|
|
2015-05-24 07:23:25 +00:00
|
|
|
/* We should never fault when copying from a kernel buffer: */
|
|
|
|
WARN_ON_FPU(err);
|
2015-05-25 07:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save xstate to user space xsave area.
|
|
|
|
*
|
|
|
|
* We don't use modified optimization because xrstor/xrstors might track
|
|
|
|
* a different application.
|
|
|
|
*
|
|
|
|
* We don't use compacted format xsave area for
|
|
|
|
* backward compatibility for old applications which don't understand
|
|
|
|
* compacted format of xsave area.
|
|
|
|
*/
|
|
|
|
static inline int copy_xregs_to_user(struct xregs_state __user *buf)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear the xsave header first, so that reserved fields are
|
|
|
|
* initialized to zero.
|
|
|
|
*/
|
|
|
|
err = __clear_user(&buf->header, sizeof(buf->header));
|
|
|
|
if (unlikely(err))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
__asm__ __volatile__(ASM_STAC "\n"
|
|
|
|
"1:"XSAVE"\n"
|
|
|
|
"2: " ASM_CLAC "\n"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:59:35 +00:00
|
|
|
: "D" (buf), "a" (-1), "d" (-1), "0" (err)
|
2015-05-25 07:55:39 +00:00
|
|
|
: "memory");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore xstate from user space xsave area.
|
|
|
|
*/
|
|
|
|
static inline int copy_user_to_xregs(struct xregs_state __user *buf, u64 mask)
|
|
|
|
{
|
|
|
|
struct xregs_state *xstate = ((__force struct xregs_state *)buf);
|
|
|
|
u32 lmask = mask;
|
|
|
|
u32 hmask = mask >> 32;
|
2015-05-25 09:59:35 +00:00
|
|
|
int err = 0;
|
2015-05-25 07:55:39 +00:00
|
|
|
|
|
|
|
__asm__ __volatile__(ASM_STAC "\n"
|
|
|
|
"1:"XRSTOR"\n"
|
|
|
|
"2: " ASM_CLAC "\n"
|
|
|
|
xstate_fault(err)
|
2015-05-25 09:59:35 +00:00
|
|
|
: "D" (xstate), "a" (lmask), "d" (hmask), "0" (err)
|
2015-05-25 07:55:39 +00:00
|
|
|
: "memory"); /* memory required? */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
/*
|
|
|
|
* These must be called with preempt disabled. Returns
|
x86/fpu: Rename fpu_save_init() to copy_fpregs_to_fpstate()
So fpu_save_init() is a historic name that got its name when the only
way the FPU state was FNSAVE, which cleared (well, destroyed) the FPU
state after saving it.
Nowadays the name is misleading, because ever since the introduction of
FXSAVE (and more modern FPU saving instructions) the 'we need to reload
the FPU state' part is only true if there's a pending FPU exception [*],
which is almost never the case.
So rename it to copy_fpregs_to_fpstate() to make it clear what's
happening. Also add a few comments about why we cannot keep registers
in certain cases.
Also clean up the control flow a bit, to make it more apparent when
we are dropping/keeping FP registers, and to optimize the common
case (of keeping fpregs) some more.
[*] Probably not true anymore, modern instructions always leave the FPU
state intact, even if exceptions are pending: because pending FP
exceptions are posted on the next FP instruction, not asynchronously.
They were truly asynchronous back in the IRQ13 case, and we had to
synchronize with them, but that code is not working anymore: we don't
have IRQ13 mapped in the IDT anymore.
But a cleanup patch is obviously not the place to change subtle behavior.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 00:53:16 +00:00
|
|
|
* 'true' if the FPU state is still intact and we can
|
|
|
|
* keep registers active.
|
|
|
|
*
|
|
|
|
* The legacy FNSAVE instruction cleared all FPU state
|
|
|
|
* unconditionally, so registers are essentially destroyed.
|
|
|
|
* Modern FPU state can be kept in registers, if there are
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
* no pending FP exceptions.
|
2012-02-21 21:19:22 +00:00
|
|
|
*/
|
x86/fpu: Rename fpu_save_init() to copy_fpregs_to_fpstate()
So fpu_save_init() is a historic name that got its name when the only
way the FPU state was FNSAVE, which cleared (well, destroyed) the FPU
state after saving it.
Nowadays the name is misleading, because ever since the introduction of
FXSAVE (and more modern FPU saving instructions) the 'we need to reload
the FPU state' part is only true if there's a pending FPU exception [*],
which is almost never the case.
So rename it to copy_fpregs_to_fpstate() to make it clear what's
happening. Also add a few comments about why we cannot keep registers
in certain cases.
Also clean up the control flow a bit, to make it more apparent when
we are dropping/keeping FP registers, and to optimize the common
case (of keeping fpregs) some more.
[*] Probably not true anymore, modern instructions always leave the FPU
state intact, even if exceptions are pending: because pending FP
exceptions are posted on the next FP instruction, not asynchronously.
They were truly asynchronous back in the IRQ13 case, and we had to
synchronize with them, but that code is not working anymore: we don't
have IRQ13 mapped in the IDT anymore.
But a cleanup patch is obviously not the place to change subtle behavior.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 00:53:16 +00:00
|
|
|
static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
if (likely(use_xsave())) {
|
2015-04-30 09:34:09 +00:00
|
|
|
copy_xregs_to_kernel(&fpu->state.xsave);
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
if (likely(use_fxsr())) {
|
2015-04-30 09:34:09 +00:00
|
|
|
copy_fxregs_to_kernel(fpu);
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
return 1;
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
x86/fpu: Optimize copy_fpregs_to_fpstate() by removing the FNCLEX synchronization with FP exceptions
So we have the following ancient code in copy_fpregs_to_fpstate():
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
asm volatile("fnclex");
goto drop_fpregs;
}
which clears pending FPU exceptions and then drops registers, which
causes the next FP instruction of the saved context to re-load the
saved FPU state, with all pending exceptions marked properly, and
will re-start the exception handling mechanism in the hardware.
Since FPU exceptions are always issued on instruction boundaries,
in particular on the next FP instruction following the exception
generating instruction, there's no fear of getting an FP exception
asynchronously.
They were truly asynchronous back in the IRQ13 days, when the FPU was
a weird and expensive co-processor that did its own processing, and we
had to synchronize with them, but that code is not working anymore:
we don't have IRQ13 mapped in the IDT anymore.
With the introduction of optimized XSAVE support there's a new
complication: if the xstate features bit indicates that a particular
state component is unused (in 'init state'), then the hardware does
not guarantee that the XSAVE (et al) instruction keeps the underlying
FPU state image in memory valid and current. In practice this means
that the hardware won't write it, and the exceptions flag in the
state might be an older version, with it still being set. This
meant that we had to check the xfeatures flag as well, adding
another memory load and branch to a critical hot path of the scheduler.
So optimize all this by removing both the old quirk and the new check,
and straight-line optimizing the most common cases with likely()
hints. Quite a bit of code gets removed this way:
arch/x86/kernel/process_64.o:
text data bss dec filename
5484 8 0 5492 process_64.o.before
5416 8 0 5424 process_64.o.after
Now there's also a chance that some weird behavior or erratum was
masked by our IRQ13 handling quirk (or that I misunderstood the
nature of the quirk), and that this change triggers some badness.
There's no real good way to protect against that possibility other
than keeping this change well isolated, well commented and well
bisectable. If you bisect a weird (or not so weird) breakage to
this commit then please let us know!
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 01:32:18 +00:00
|
|
|
* Legacy FPU register saving, FNSAVE always clears FPU registers,
|
|
|
|
* so we have to mark them inactive:
|
2012-02-21 21:19:22 +00:00
|
|
|
*/
|
2015-05-25 08:57:06 +00:00
|
|
|
asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
|
x86/fpu: Rename fpu_save_init() to copy_fpregs_to_fpstate()
So fpu_save_init() is a historic name that got its name when the only
way the FPU state was FNSAVE, which cleared (well, destroyed) the FPU
state after saving it.
Nowadays the name is misleading, because ever since the introduction of
FXSAVE (and more modern FPU saving instructions) the 'we need to reload
the FPU state' part is only true if there's a pending FPU exception [*],
which is almost never the case.
So rename it to copy_fpregs_to_fpstate() to make it clear what's
happening. Also add a few comments about why we cannot keep registers
in certain cases.
Also clean up the control flow a bit, to make it more apparent when
we are dropping/keeping FP registers, and to optimize the common
case (of keeping fpregs) some more.
[*] Probably not true anymore, modern instructions always leave the FPU
state intact, even if exceptions are pending: because pending FP
exceptions are posted on the next FP instruction, not asynchronously.
They were truly asynchronous back in the IRQ13 case, and we had to
synchronize with them, but that code is not working anymore: we don't
have IRQ13 mapped in the IDT anymore.
But a cleanup patch is obviously not the place to change subtle behavior.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 00:53:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 11:47:01 +00:00
|
|
|
static inline int __copy_kernel_to_fpregs(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-05-24 07:23:25 +00:00
|
|
|
if (use_xsave()) {
|
|
|
|
copy_kernel_to_xregs(&fpu->state.xsave, -1);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (use_fxsr())
|
|
|
|
return copy_kernel_to_fxregs(&fpu->state.fxsave);
|
|
|
|
else
|
|
|
|
return copy_kernel_to_fregs(&fpu->state.fsave);
|
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 11:47:01 +00:00
|
|
|
static inline int copy_kernel_to_fpregs(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2014-12-21 14:02:23 +00:00
|
|
|
/*
|
|
|
|
* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
|
|
|
|
* pending. Clear the x87 state here by setting it to fixed values.
|
|
|
|
* "m" is a random variable that should be in L1.
|
|
|
|
*/
|
2014-06-17 22:06:23 +00:00
|
|
|
if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
|
2014-01-12 03:15:52 +00:00
|
|
|
asm volatile(
|
|
|
|
"fnclex\n\t"
|
|
|
|
"emms\n\t"
|
|
|
|
"fildl %P[addr]" /* set F?P to defined value */
|
2015-04-24 12:19:26 +00:00
|
|
|
: : [addr] "m" (fpu->fpregs_active));
|
2014-01-12 03:15:52 +00:00
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
|
2015-05-27 11:47:01 +00:00
|
|
|
return __copy_kernel_to_fpregs(fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:57:06 +00:00
|
|
|
extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size);
|
2015-05-05 13:56:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FPU context switch related helper methods:
|
|
|
|
*/
|
|
|
|
|
|
|
|
DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
|
|
|
|
* on this CPU.
|
|
|
|
*
|
|
|
|
* This will disable any lazy FPU state restore of the current FPU state,
|
|
|
|
* but if the current thread owns the FPU, it will still be saved by.
|
|
|
|
*/
|
|
|
|
static inline void __cpu_disable_lazy_restore(unsigned int cpu)
|
|
|
|
{
|
|
|
|
per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
|
|
|
|
{
|
|
|
|
return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-27 06:58:45 +00:00
|
|
|
/*
|
|
|
|
* Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
|
|
|
|
* idiom, which is then paired with the sw-flag (fpregs_active) later on:
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void __fpregs_activate_hw(void)
|
|
|
|
{
|
|
|
|
if (!use_eager_fpu())
|
|
|
|
clts();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __fpregs_deactivate_hw(void)
|
|
|
|
{
|
|
|
|
if (!use_eager_fpu())
|
|
|
|
stts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
|
2015-04-24 12:28:01 +00:00
|
|
|
static inline void __fpregs_deactivate(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-05-05 09:34:49 +00:00
|
|
|
WARN_ON_FPU(!fpu->fpregs_active);
|
|
|
|
|
2015-04-24 12:19:26 +00:00
|
|
|
fpu->fpregs_active = 0;
|
2015-04-23 10:18:28 +00:00
|
|
|
this_cpu_write(fpu_fpregs_owner_ctx, NULL);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 06:58:45 +00:00
|
|
|
/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
|
2015-04-24 12:26:47 +00:00
|
|
|
static inline void __fpregs_activate(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-05-05 09:34:49 +00:00
|
|
|
WARN_ON_FPU(fpu->fpregs_active);
|
|
|
|
|
2015-04-24 12:19:26 +00:00
|
|
|
fpu->fpregs_active = 1;
|
2015-04-23 10:24:59 +00:00
|
|
|
this_cpu_write(fpu_fpregs_owner_ctx, fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 14:56:05 +00:00
|
|
|
/*
|
|
|
|
* The question "does this thread have fpu access?"
|
|
|
|
* is slightly racy, since preemption could come in
|
|
|
|
* and revoke it immediately after the test.
|
|
|
|
*
|
|
|
|
* However, even in that very unlikely scenario,
|
|
|
|
* we can just assume we have FPU access - typically
|
|
|
|
* to save the FP state - we'll just take a #NM
|
|
|
|
* fault and get the FPU access back.
|
|
|
|
*/
|
2015-04-28 10:28:08 +00:00
|
|
|
static inline int fpregs_active(void)
|
2015-04-26 14:56:05 +00:00
|
|
|
{
|
|
|
|
return current->thread.fpu.fpregs_active;
|
|
|
|
}
|
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
/*
|
|
|
|
* Encapsulate the CR0.TS handling together with the
|
|
|
|
* software flag.
|
|
|
|
*
|
|
|
|
* These generally need preemption protection to work,
|
|
|
|
* do try to avoid using these on their own.
|
|
|
|
*/
|
2015-04-24 12:31:27 +00:00
|
|
|
static inline void fpregs_activate(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-04-27 06:58:45 +00:00
|
|
|
__fpregs_activate_hw();
|
2015-04-24 12:31:27 +00:00
|
|
|
__fpregs_activate(fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 12:31:27 +00:00
|
|
|
static inline void fpregs_deactivate(struct fpu *fpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-04-24 12:31:27 +00:00
|
|
|
__fpregs_deactivate(fpu);
|
2015-04-27 06:58:45 +00:00
|
|
|
__fpregs_deactivate_hw();
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FPU state switching for scheduling.
|
|
|
|
*
|
|
|
|
* This is a two-stage process:
|
|
|
|
*
|
|
|
|
* - switch_fpu_prepare() saves the old state and
|
|
|
|
* sets the new state of the CR0.TS bit. This is
|
|
|
|
* done within the context of the old process.
|
|
|
|
*
|
|
|
|
* - switch_fpu_finish() restores the new state as
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
typedef struct { int preload; } fpu_switch_t;
|
|
|
|
|
2015-04-23 15:39:04 +00:00
|
|
|
static inline fpu_switch_t
|
|
|
|
switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
|
|
|
fpu_switch_t fpu;
|
|
|
|
|
2012-08-24 21:13:02 +00:00
|
|
|
/*
|
|
|
|
* If the task has used the math, pre-load the FPU on xsave processors
|
|
|
|
* or if the past 5 consecutive context-switches used math.
|
|
|
|
*/
|
2015-04-23 10:49:20 +00:00
|
|
|
fpu.preload = new_fpu->fpstate_active &&
|
2015-04-23 15:39:04 +00:00
|
|
|
(use_eager_fpu() || new_fpu->counter > 5);
|
2015-02-06 20:02:03 +00:00
|
|
|
|
2015-04-24 12:19:26 +00:00
|
|
|
if (old_fpu->fpregs_active) {
|
x86/fpu: Rename fpu_save_init() to copy_fpregs_to_fpstate()
So fpu_save_init() is a historic name that got its name when the only
way the FPU state was FNSAVE, which cleared (well, destroyed) the FPU
state after saving it.
Nowadays the name is misleading, because ever since the introduction of
FXSAVE (and more modern FPU saving instructions) the 'we need to reload
the FPU state' part is only true if there's a pending FPU exception [*],
which is almost never the case.
So rename it to copy_fpregs_to_fpstate() to make it clear what's
happening. Also add a few comments about why we cannot keep registers
in certain cases.
Also clean up the control flow a bit, to make it more apparent when
we are dropping/keeping FP registers, and to optimize the common
case (of keeping fpregs) some more.
[*] Probably not true anymore, modern instructions always leave the FPU
state intact, even if exceptions are pending: because pending FP
exceptions are posted on the next FP instruction, not asynchronously.
They were truly asynchronous back in the IRQ13 case, and we had to
synchronize with them, but that code is not working anymore: we don't
have IRQ13 mapped in the IDT anymore.
But a cleanup patch is obviously not the place to change subtle behavior.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 00:53:16 +00:00
|
|
|
if (!copy_fpregs_to_fpstate(old_fpu))
|
2015-04-23 15:39:04 +00:00
|
|
|
old_fpu->last_cpu = -1;
|
2015-02-06 20:02:03 +00:00
|
|
|
else
|
2015-04-23 15:39:04 +00:00
|
|
|
old_fpu->last_cpu = cpu;
|
2015-02-06 20:02:03 +00:00
|
|
|
|
2015-04-23 10:18:28 +00:00
|
|
|
/* But leave fpu_fpregs_owner_ctx! */
|
2015-04-24 12:19:26 +00:00
|
|
|
old_fpu->fpregs_active = 0;
|
2012-02-21 21:19:22 +00:00
|
|
|
|
|
|
|
/* Don't change CR0.TS if we just switch! */
|
|
|
|
if (fpu.preload) {
|
2015-04-23 15:39:04 +00:00
|
|
|
new_fpu->counter++;
|
2015-04-24 12:26:47 +00:00
|
|
|
__fpregs_activate(new_fpu);
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
prefetch(&new_fpu->state);
|
2015-04-27 06:58:45 +00:00
|
|
|
} else {
|
|
|
|
__fpregs_deactivate_hw();
|
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
} else {
|
2015-04-23 15:39:04 +00:00
|
|
|
old_fpu->counter = 0;
|
|
|
|
old_fpu->last_cpu = -1;
|
2012-02-21 21:19:22 +00:00
|
|
|
if (fpu.preload) {
|
2015-04-23 15:39:04 +00:00
|
|
|
new_fpu->counter++;
|
2015-04-23 15:25:44 +00:00
|
|
|
if (fpu_want_lazy_restore(new_fpu, cpu))
|
2012-02-21 21:19:22 +00:00
|
|
|
fpu.preload = 0;
|
|
|
|
else
|
x86/fpu: Simplify FPU handling by embedding the fpstate in task_struct (again)
So 6 years ago we made the FPU fpstate dynamically allocated:
aa283f49276e ("x86, fpu: lazy allocation of FPU area - v5")
61c4628b5386 ("x86, fpu: split FPU state from task struct - v5")
In hindsight this was a mistake:
- it complicated context allocation failure handling, such as:
/* kthread execs. TODO: cleanup this horror. */
if (WARN_ON(fpstate_alloc_init(fpu)))
force_sig(SIGKILL, tsk);
- it caused us to enable irqs in fpu__restore():
local_irq_enable();
/*
* does a slab alloc which can sleep
*/
if (fpstate_alloc_init(fpu)) {
/*
* ran out of memory!
*/
do_group_exit(SIGKILL);
return;
}
local_irq_disable();
- it (slightly) slowed down task creation/destruction by adding
slab allocation/free pattens.
- it made access to context contents (slightly) slower by adding
one more pointer dereference.
The motivation for the dynamic allocation was two-fold:
- reduce memory consumption by non-FPU tasks
- allocate and handle only the necessary amount of context for
various XSAVE processors that have varying hardware frame
sizes.
These days, with glibc using SSE memcpy by default and GCC optimizing
for SSE/AVX by default, the scope of FPU using apps on an x86 system is
much larger than it was 6 years ago.
For example on a freshly installed Fedora 21 desktop system, with a
recent kernel, all non-kthread tasks have used the FPU shortly after
bootup.
Also, even modern embedded x86 CPUs try to support the latest vector
instruction set - so they'll too often use the larger xstate frame
sizes.
So remove the dynamic allocation complication by embedding the FPU
fpstate in task_struct again. This should make the FPU a lot more
accessible to all sorts of atomic contexts.
We could still optimize for the xstate frame size in the future,
by moving the state structure to the last element of task_struct,
and allocating only a part of that.
This change is kept minimal by still keeping the ctx_alloc()/free()
routines (that now do nothing substantial) - we'll remove them in
the following patches.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 02:19:39 +00:00
|
|
|
prefetch(&new_fpu->state);
|
2015-04-24 12:30:38 +00:00
|
|
|
fpregs_activate(new_fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fpu;
|
|
|
|
}
|
|
|
|
|
2015-05-05 13:56:33 +00:00
|
|
|
/*
|
|
|
|
* Misc helper functions:
|
|
|
|
*/
|
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
/*
|
|
|
|
* By the time this gets called, we've already cleared CR0.TS and
|
|
|
|
* given the process the FPU if we are going to preload the FPU
|
|
|
|
* state - all we need to do is to conditionally restore the register
|
|
|
|
* state itself.
|
|
|
|
*/
|
2015-04-23 15:43:27 +00:00
|
|
|
static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
|
2012-02-21 21:19:22 +00:00
|
|
|
{
|
2015-04-23 15:43:27 +00:00
|
|
|
if (fpu_switch.preload) {
|
2015-05-27 11:47:01 +00:00
|
|
|
if (unlikely(copy_kernel_to_fpregs(new_fpu))) {
|
2015-05-05 09:34:49 +00:00
|
|
|
WARN_ON_FPU(1);
|
2015-04-30 05:12:46 +00:00
|
|
|
fpu__clear(new_fpu);
|
2015-05-05 09:34:49 +00:00
|
|
|
}
|
2012-02-21 21:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-11 17:34:09 +00:00
|
|
|
* Needs to be preemption-safe.
|
2012-02-21 21:19:22 +00:00
|
|
|
*
|
2012-08-24 21:12:58 +00:00
|
|
|
* NOTE! user_fpu_begin() must be used only immediately before restoring
|
2015-03-11 17:34:09 +00:00
|
|
|
* the save state. It does not do any saving/restoring on its own. In
|
|
|
|
* lazy FPU mode, it is just an optimization to avoid a #NM exception,
|
|
|
|
* the task can lose the FPU right after preempt_enable().
|
2012-02-21 21:19:22 +00:00
|
|
|
*/
|
|
|
|
static inline void user_fpu_begin(void)
|
|
|
|
{
|
2015-04-23 10:31:17 +00:00
|
|
|
struct fpu *fpu = ¤t->thread.fpu;
|
|
|
|
|
2012-02-21 21:19:22 +00:00
|
|
|
preempt_disable();
|
2015-04-28 10:28:08 +00:00
|
|
|
if (!fpregs_active())
|
2015-04-24 12:30:38 +00:00
|
|
|
fpregs_activate(fpu);
|
2012-02-21 21:19:22 +00:00
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2015-05-05 13:56:33 +00:00
|
|
|
/*
|
|
|
|
* MXCSR and XCR definitions:
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern unsigned int mxcsr_feature_mask;
|
|
|
|
|
|
|
|
#define XCR_XFEATURE_ENABLED_MASK 0x00000000
|
|
|
|
|
|
|
|
static inline u64 xgetbv(u32 index)
|
|
|
|
{
|
|
|
|
u32 eax, edx;
|
|
|
|
|
|
|
|
asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
|
|
|
|
: "=a" (eax), "=d" (edx)
|
|
|
|
: "c" (index));
|
|
|
|
return eax + ((u64)edx << 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void xsetbv(u32 index, u64 value)
|
|
|
|
{
|
|
|
|
u32 eax = value;
|
|
|
|
u32 edx = value >> 32;
|
|
|
|
|
|
|
|
asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
|
|
|
|
: : "a" (eax), "d" (edx), "c" (index));
|
|
|
|
}
|
|
|
|
|
2015-04-24 00:54:44 +00:00
|
|
|
#endif /* _ASM_X86_FPU_INTERNAL_H */
|