Skip to content

Commit

Permalink
x86/fpu: Add an __fpregs_load_activate() internal helper
Browse files Browse the repository at this point in the history
Add a helper function that ensures the floating point registers for the
current task are active. Use with preemption disabled.

While at it, add fpregs_lock/unlock() helpers too, to be used in later
patches.

 [ bp: Add a comment about its intended usage. ]

Signed-off-by: Rik van Riel <[email protected]>
Signed-off-by: Sebastian Andrzej Siewior <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Reviewed-by: Dave Hansen <[email protected]>
Reviewed-by: Thomas Gleixner <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Aubrey Li <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: "Jason A. Donenfeld" <[email protected]>
Cc: kvm ML <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Radim Krčmář <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: x86-ml <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
  • Loading branch information
rikvanriel authored and suryasaimadhu committed Apr 10, 2019
1 parent 0169f53 commit 4ee9151
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 11 additions & 0 deletions arch/x86/include/asm/fpu/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#ifndef _ASM_X86_FPU_API_H
#define _ASM_X86_FPU_API_H
#include <linux/preempt.h>

/*
* Use kernel_fpu_begin/end() if you intend to use FPU in kernel context. It
Expand All @@ -22,6 +23,16 @@ extern void kernel_fpu_begin(void);
extern void kernel_fpu_end(void);
extern bool irq_fpu_usable(void);

static inline void fpregs_lock(void)
{
preempt_disable();
}

static inline void fpregs_unlock(void)
{
preempt_enable();
}

/*
* Query the presence of one or more xfeatures. Works on any legacy CPU as well.
*
Expand Down
22 changes: 14 additions & 8 deletions arch/x86/include/asm/fpu/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,18 @@ static inline void fpregs_activate(struct fpu *fpu)
trace_x86_fpu_regs_activated(fpu);
}

/*
* Internal helper, do not use directly. Use switch_fpu_return() instead.
*/
static inline void __fpregs_load_activate(struct fpu *fpu, int cpu)
{
if (!fpregs_state_valid(fpu, cpu)) {
if (current->mm)
copy_kernel_to_fpregs(&fpu->state);
fpregs_activate(fpu);
}
}

/*
* FPU state switching for scheduling.
*
Expand Down Expand Up @@ -522,14 +534,8 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
*/
static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
{
if (static_cpu_has(X86_FEATURE_FPU)) {
if (!fpregs_state_valid(new_fpu, cpu)) {
if (current->mm)
copy_kernel_to_fpregs(&new_fpu->state);
}

fpregs_activate(new_fpu);
}
if (static_cpu_has(X86_FEATURE_FPU))
__fpregs_load_activate(new_fpu, cpu);
}

/*
Expand Down

0 comments on commit 4ee9151

Please sign in to comment.