Merge tag 'perf-urgent-2022-06-05' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / arch / x86 / include / asm / fpu / sched.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_FPU_SCHED_H
3 #define _ASM_X86_FPU_SCHED_H
4
5 #include <linux/sched.h>
6
7 #include <asm/cpufeature.h>
8 #include <asm/fpu/types.h>
9
10 #include <asm/trace/fpu.h>
11
12 extern void save_fpregs_to_fpstate(struct fpu *fpu);
13 extern void fpu__drop(struct fpu *fpu);
14 extern int  fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal);
15 extern void fpu_flush_thread(void);
16
17 /*
18  * FPU state switching for scheduling.
19  *
20  * This is a two-stage process:
21  *
22  *  - switch_fpu_prepare() saves the old state.
23  *    This is done within the context of the old process.
24  *
25  *  - switch_fpu_finish() sets TIF_NEED_FPU_LOAD; the floating point state
26  *    will get loaded on return to userspace, or when the kernel needs it.
27  *
28  * If TIF_NEED_FPU_LOAD is cleared then the CPU's FPU registers
29  * are saved in the current thread's FPU register state.
30  *
31  * If TIF_NEED_FPU_LOAD is set then CPU's FPU registers may not
32  * hold current()'s FPU registers. It is required to load the
33  * registers before returning to userland or using the content
34  * otherwise.
35  *
36  * The FPU context is only stored/restored for a user task and
37  * PF_KTHREAD is used to distinguish between kernel and user threads.
38  */
39 static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
40 {
41         if (cpu_feature_enabled(X86_FEATURE_FPU) &&
42             !(current->flags & PF_KTHREAD)) {
43                 save_fpregs_to_fpstate(old_fpu);
44                 /*
45                  * The save operation preserved register state, so the
46                  * fpu_fpregs_owner_ctx is still @old_fpu. Store the
47                  * current CPU number in @old_fpu, so the next return
48                  * to user space can avoid the FPU register restore
49                  * when is returns on the same CPU and still owns the
50                  * context.
51                  */
52                 old_fpu->last_cpu = cpu;
53
54                 trace_x86_fpu_regs_deactivated(old_fpu);
55         }
56 }
57
58 /*
59  * Delay loading of the complete FPU state until the return to userland.
60  * PKRU is handled separately.
61  */
62 static inline void switch_fpu_finish(void)
63 {
64         if (cpu_feature_enabled(X86_FEATURE_FPU))
65                 set_thread_flag(TIF_NEED_FPU_LOAD);
66 }
67
68 #endif /* _ASM_X86_FPU_SCHED_H */