[PATCH 006/208] x86/fpu: Split an fpstate_alloc_init() function out of init_fpu()

From: Ingo Molnar
Date: Tue May 05 2015 - 13:08:22 EST


Most init_fpu() users don't want the register-saving aspect of the
function, they are calling it for 'current' and when FPU registers
are not allocated and initialized yet.

Split out a simplified API that does just that (and add debug-checks
for these conditions): fpstate_alloc_init().

Use it where appropriate.

Reviewed-by: Borislav Petkov <bp@xxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Fenghua Yu <fenghua.yu@xxxxxxxxx>
Cc: H. Peter Anvin <hpa@xxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Oleg Nesterov <oleg@xxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
---
arch/x86/include/asm/i387.h | 3 +++
arch/x86/kernel/i387.c | 31 +++++++++++++++++++++++++++++++
arch/x86/kernel/process.c | 2 +-
arch/x86/kernel/traps.c | 2 +-
arch/x86/kernel/xsave.c | 2 +-
arch/x86/kvm/x86.c | 2 +-
arch/x86/math-emu/fpu_entry.c | 2 +-
7 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/i387.h b/arch/x86/include/asm/i387.h
index d4419da9b210..1a896b4533c4 100644
--- a/arch/x86/include/asm/i387.h
+++ b/arch/x86/include/asm/i387.h
@@ -18,7 +18,10 @@
struct pt_regs;
struct user_i387_struct;

+extern int fpstate_alloc_init(struct task_struct *curr);
+
extern int init_fpu(struct task_struct *child);
+
extern void fpu_finit(struct fpu *fpu);
extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
extern void math_state_restore(void);
diff --git a/arch/x86/kernel/i387.c b/arch/x86/kernel/i387.c
index 29251f5668b1..56b6e726fb60 100644
--- a/arch/x86/kernel/i387.c
+++ b/arch/x86/kernel/i387.c
@@ -247,6 +247,37 @@ void fpu_finit(struct fpu *fpu)
EXPORT_SYMBOL_GPL(fpu_finit);

/*
+ * Allocate the backing store for the current task's FPU registers
+ * and initialize the registers themselves as well.
+ *
+ * Can fail.
+ */
+int fpstate_alloc_init(struct task_struct *curr)
+{
+ int ret;
+
+ if (WARN_ON_ONCE(curr != current))
+ return -EINVAL;
+ if (WARN_ON_ONCE(curr->flags & PF_USED_MATH))
+ return -EINVAL;
+
+ /*
+ * Memory allocation at the first usage of the FPU and other state.
+ */
+ ret = fpu_alloc(&curr->thread.fpu);
+ if (ret)
+ return ret;
+
+ fpu_finit(&curr->thread.fpu);
+
+ /* Safe to do for the current task: */
+ curr->flags |= PF_USED_MATH;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(fpstate_alloc_init);
+
+/*
* The _current_ task is using the FPU for the first time
* so initialize it and set the mxcsr to its default
* value at reset if we support XMM instructions and then
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 8213da62b1b7..e2220e38ff6d 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -158,7 +158,7 @@ void flush_thread(void)
free_thread_xstate(tsk);
} else if (!used_math()) {
/* kthread execs. TODO: cleanup this horror. */
- if (WARN_ON(init_fpu(tsk)))
+ if (WARN_ON(fpstate_alloc_init(tsk)))
force_sig(SIGKILL, tsk);
user_fpu_begin();
restore_init_xstate();
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 12f29f9907cd..cf9c9627be19 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -846,7 +846,7 @@ void math_state_restore(void)
/*
* does a slab alloc which can sleep
*/
- if (init_fpu(tsk)) {
+ if (fpstate_alloc_init(tsk)) {
/*
* ran out of memory!
*/
diff --git a/arch/x86/kernel/xsave.c b/arch/x86/kernel/xsave.c
index 87a815b85f3e..a977cdd03825 100644
--- a/arch/x86/kernel/xsave.c
+++ b/arch/x86/kernel/xsave.c
@@ -349,7 +349,7 @@ int __restore_xstate_sig(void __user *buf, void __user *buf_fx, int size)
if (!access_ok(VERIFY_READ, buf, size))
return -EACCES;

- if (!used_math() && init_fpu(tsk))
+ if (!used_math() && fpstate_alloc_init(tsk))
return -1;

if (!static_cpu_has(X86_FEATURE_FPU))
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c73efcd03e29..bfc396632ee8 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6600,7 +6600,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
int r;
sigset_t sigsaved;

- if (!tsk_used_math(current) && init_fpu(current))
+ if (!tsk_used_math(current) && fpstate_alloc_init(current))
return -ENOMEM;

if (vcpu->sigset_active)
diff --git a/arch/x86/math-emu/fpu_entry.c b/arch/x86/math-emu/fpu_entry.c
index 9b868124128d..c9ff09a02385 100644
--- a/arch/x86/math-emu/fpu_entry.c
+++ b/arch/x86/math-emu/fpu_entry.c
@@ -149,7 +149,7 @@ void math_emulate(struct math_emu_info *info)
struct desc_struct code_descriptor;

if (!used_math()) {
- if (init_fpu(current)) {
+ if (fpstate_alloc_init(current)) {
do_group_exit(SIGKILL);
return;
}
--
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/