Commit 37a7a2a1 authored by Jisheng Zhang's avatar Jisheng Zhang Committed by Palmer Dabbelt

riscv: Turn has_fpu into a static key if FPU=y

The has_fpu check sits at hot code path: switch_to(). Currently, has_fpu
is a bool variable if FPU=y, switch_to() checks it each time, we can
optimize out this check by turning the has_fpu into a static key.
Signed-off-by: default avatarJisheng Zhang <jszhang@kernel.org>
Signed-off-by: default avatarPalmer Dabbelt <palmerdabbelt@google.com>
parent 8237c524
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#ifndef _ASM_RISCV_SWITCH_TO_H #ifndef _ASM_RISCV_SWITCH_TO_H
#define _ASM_RISCV_SWITCH_TO_H #define _ASM_RISCV_SWITCH_TO_H
#include <linux/jump_label.h>
#include <linux/sched/task_stack.h> #include <linux/sched/task_stack.h>
#include <asm/processor.h> #include <asm/processor.h>
#include <asm/ptrace.h> #include <asm/ptrace.h>
...@@ -55,9 +56,13 @@ static inline void __switch_to_aux(struct task_struct *prev, ...@@ -55,9 +56,13 @@ static inline void __switch_to_aux(struct task_struct *prev,
fstate_restore(next, task_pt_regs(next)); fstate_restore(next, task_pt_regs(next));
} }
extern bool has_fpu; extern struct static_key_false cpu_hwcap_fpu;
static __always_inline bool has_fpu(void)
{
return static_branch_likely(&cpu_hwcap_fpu);
}
#else #else
#define has_fpu false static __always_inline bool has_fpu(void) { return false; }
#define fstate_save(task, regs) do { } while (0) #define fstate_save(task, regs) do { } while (0)
#define fstate_restore(task, regs) do { } while (0) #define fstate_restore(task, regs) do { } while (0)
#define __switch_to_aux(__prev, __next) do { } while (0) #define __switch_to_aux(__prev, __next) do { } while (0)
...@@ -70,7 +75,7 @@ extern struct task_struct *__switch_to(struct task_struct *, ...@@ -70,7 +75,7 @@ extern struct task_struct *__switch_to(struct task_struct *,
do { \ do { \
struct task_struct *__prev = (prev); \ struct task_struct *__prev = (prev); \
struct task_struct *__next = (next); \ struct task_struct *__next = (next); \
if (has_fpu) \ if (has_fpu()) \
__switch_to_aux(__prev, __next); \ __switch_to_aux(__prev, __next); \
((last) = __switch_to(__prev, __next)); \ ((last) = __switch_to(__prev, __next)); \
} while (0) } while (0)
......
...@@ -19,7 +19,7 @@ unsigned long elf_hwcap __read_mostly; ...@@ -19,7 +19,7 @@ unsigned long elf_hwcap __read_mostly;
static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
#ifdef CONFIG_FPU #ifdef CONFIG_FPU
bool has_fpu __read_mostly; __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu);
#endif #endif
/** /**
...@@ -146,6 +146,6 @@ void riscv_fill_hwcap(void) ...@@ -146,6 +146,6 @@ void riscv_fill_hwcap(void)
#ifdef CONFIG_FPU #ifdef CONFIG_FPU
if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)) if (elf_hwcap & (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D))
has_fpu = true; static_branch_enable(&cpu_hwcap_fpu);
#endif #endif
} }
...@@ -87,7 +87,7 @@ void start_thread(struct pt_regs *regs, unsigned long pc, ...@@ -87,7 +87,7 @@ void start_thread(struct pt_regs *regs, unsigned long pc,
unsigned long sp) unsigned long sp)
{ {
regs->status = SR_PIE; regs->status = SR_PIE;
if (has_fpu) { if (has_fpu()) {
regs->status |= SR_FS_INITIAL; regs->status |= SR_FS_INITIAL;
/* /*
* Restore the initial value to the FP register * Restore the initial value to the FP register
......
...@@ -90,7 +90,7 @@ static long restore_sigcontext(struct pt_regs *regs, ...@@ -90,7 +90,7 @@ static long restore_sigcontext(struct pt_regs *regs,
/* sc_regs is structured the same as the start of pt_regs */ /* sc_regs is structured the same as the start of pt_regs */
err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
/* Restore the floating-point state. */ /* Restore the floating-point state. */
if (has_fpu) if (has_fpu())
err |= restore_fp_state(regs, &sc->sc_fpregs); err |= restore_fp_state(regs, &sc->sc_fpregs);
return err; return err;
} }
...@@ -143,7 +143,7 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, ...@@ -143,7 +143,7 @@ static long setup_sigcontext(struct rt_sigframe __user *frame,
/* sc_regs is structured the same as the start of pt_regs */ /* sc_regs is structured the same as the start of pt_regs */
err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
/* Save the floating-point state. */ /* Save the floating-point state. */
if (has_fpu) if (has_fpu())
err |= save_fp_state(regs, &sc->sc_fpregs); err |= save_fp_state(regs, &sc->sc_fpregs);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment