Skip to content

Commit 005f78c

Browse files
author
Ard Biesheuvel
committed
arm64: defer reloading a task's FPSIMD state to userland resume
If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time. This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task returns to userland. If a task does not return to userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU. This patch adds two more functions to abstract away from straight FPSIMD register file saves and restores: - fpsimd_restore_current_state -> ensure current's FPSIMD state is loaded - fpsimd_flush_task_state -> invalidate live copies of a task's FPSIMD state Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
1 parent c51f926 commit 005f78c

6 files changed

Lines changed: 143 additions & 18 deletions

File tree

‎arch/arm64/include/asm/fpsimd.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct fpsimd_state {
3737
u32 fpcr;
3838
};
3939
};
40+
/* the id of the last cpu to have restored this state */
41+
unsigned int cpu;
4042
};
4143

4244
#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
@@ -59,8 +61,11 @@ extern void fpsimd_thread_switch(struct task_struct *next);
5961
extern void fpsimd_flush_thread(void);
6062

6163
extern void fpsimd_preserve_current_state(void);
64+
extern void fpsimd_restore_current_state(void);
6265
extern void fpsimd_update_current_state(struct fpsimd_state *state);
6366

67+
extern void fpsimd_flush_task_state(struct task_struct *target);
68+
6469
#endif
6570

6671
#endif

‎arch/arm64/include/asm/thread_info.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static inline struct thread_info *current_thread_info(void)
100100
#define TIF_SIGPENDING 0
101101
#define TIF_NEED_RESCHED 1
102102
#define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
103+
#define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
103104
#define TIF_SYSCALL_TRACE 8
104105
#define TIF_POLLING_NRFLAG 16
105106
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
@@ -112,10 +113,11 @@ static inline struct thread_info *current_thread_info(void)
112113
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
113114
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
114115
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
116+
#define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
115117
#define _TIF_32BIT (1 << TIF_32BIT)
116118

117119
#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
118-
_TIF_NOTIFY_RESUME)
120+
_TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
119121

120122
#endif /* __KERNEL__ */
121123
#endif /* __ASM_THREAD_INFO_H */

‎arch/arm64/kernel/entry.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ fast_work_pending:
576576
str x0, [sp, #S_X0] // returned x0
577577
work_pending:
578578
tbnz x1, #TIF_NEED_RESCHED, work_resched
579-
/* TIF_SIGPENDING or TIF_NOTIFY_RESUME case */
579+
/* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
580580
ldr x2, [sp, #S_PSTATE]
581581
mov x0, sp // 'regs'
582582
tst x2, #PSR_MODE_MASK // user mode regs?

‎arch/arm64/kernel/fpsimd.c‎

Lines changed: 128 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,60 @@
3434
#define FPEXC_IXF (1 << 4)
3535
#define FPEXC_IDF (1 << 7)
3636

37+
/*
38+
* In order to reduce the number of times the FPSIMD state is needlessly saved
39+
* and restored, we need to keep track of two things:
40+
* (a) for each task, we need to remember which CPU was the last one to have
41+
* the task's FPSIMD state loaded into its FPSIMD registers;
42+
* (b) for each CPU, we need to remember which task's userland FPSIMD state has
43+
* been loaded into its FPSIMD registers most recently, or whether it has
44+
* been used to perform kernel mode NEON in the meantime.
45+
*
46+
* For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
47+
* the id of the current CPU everytime the state is loaded onto a CPU. For (b),
48+
* we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
49+
* address of the userland FPSIMD state of the task that was loaded onto the CPU
50+
* the most recently, or NULL if kernel mode NEON has been performed after that.
51+
*
52+
* With this in place, we no longer have to restore the next FPSIMD state right
53+
* when switching between tasks. Instead, we can defer this check to userland
54+
* resume, at which time we verify whether the CPU's fpsimd_last_state and the
55+
* task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
56+
* can omit the FPSIMD restore.
57+
*
58+
* As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
59+
* indicate whether or not the userland FPSIMD state of the current task is
60+
* present in the registers. The flag is set unless the FPSIMD registers of this
61+
* CPU currently contain the most recent userland FPSIMD state of the current
62+
* task.
63+
*
64+
* For a certain task, the sequence may look something like this:
65+
* - the task gets scheduled in; if both the task's fpsimd_state.cpu field
66+
* contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
67+
* variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
68+
* cleared, otherwise it is set;
69+
*
70+
* - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
71+
* userland FPSIMD state is copied from memory to the registers, the task's
72+
* fpsimd_state.cpu field is set to the id of the current CPU, the current
73+
* CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
74+
* TIF_FOREIGN_FPSTATE flag is cleared;
75+
*
76+
* - the task executes an ordinary syscall; upon return to userland, the
77+
* TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
78+
* restored;
79+
*
80+
* - the task executes a syscall which executes some NEON instructions; this is
81+
* preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
82+
* register contents to memory, clears the fpsimd_last_state per-cpu variable
83+
* and sets the TIF_FOREIGN_FPSTATE flag;
84+
*
85+
* - the task gets preempted after kernel_neon_end() is called; as we have not
86+
* returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
87+
* whatever is in the FPSIMD registers is not saved to memory, but discarded.
88+
*/
89+
static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
90+
3791
/*
3892
* Trapped FP/ASIMD access.
3993
*/
@@ -72,41 +126,96 @@ void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
72126

73127
void fpsimd_thread_switch(struct task_struct *next)
74128
{
75-
/* check if not kernel threads */
76-
if (current->mm)
129+
/*
130+
* Save the current FPSIMD state to memory, but only if whatever is in
131+
* the registers is in fact the most recent userland FPSIMD state of
132+
* 'current'.
133+
*/
134+
if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
77135
fpsimd_save_state(&current->thread.fpsimd_state);
78-
if (next->mm)
79-
fpsimd_load_state(&next->thread.fpsimd_state);
136+
137+
if (next->mm) {
138+
/*
139+
* If we are switching to a task whose most recent userland
140+
* FPSIMD state is already in the registers of *this* cpu,
141+
* we can skip loading the state from memory. Otherwise, set
142+
* the TIF_FOREIGN_FPSTATE flag so the state will be loaded
143+
* upon the next return to userland.
144+
*/
145+
struct fpsimd_state *st = &next->thread.fpsimd_state;
146+
147+
if (__this_cpu_read(fpsimd_last_state) == st
148+
&& st->cpu == smp_processor_id())
149+
clear_ti_thread_flag(task_thread_info(next),
150+
TIF_FOREIGN_FPSTATE);
151+
else
152+
set_ti_thread_flag(task_thread_info(next),
153+
TIF_FOREIGN_FPSTATE);
154+
}
80155
}
81156

82157
void fpsimd_flush_thread(void)
83158
{
84-
preempt_disable();
85159
memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
86-
fpsimd_load_state(&current->thread.fpsimd_state);
87-
preempt_enable();
160+
set_thread_flag(TIF_FOREIGN_FPSTATE);
88161
}
89162

90163
/*
91-
* Save the userland FPSIMD state of 'current' to memory
164+
* Save the userland FPSIMD state of 'current' to memory, but only if the state
165+
* currently held in the registers does in fact belong to 'current'
92166
*/
93167
void fpsimd_preserve_current_state(void)
94168
{
95169
preempt_disable();
96-
fpsimd_save_state(&current->thread.fpsimd_state);
170+
if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
171+
fpsimd_save_state(&current->thread.fpsimd_state);
97172
preempt_enable();
98173
}
99174

100175
/*
101-
* Load an updated userland FPSIMD state for 'current' from memory
176+
* Load the userland FPSIMD state of 'current' from memory, but only if the
177+
* FPSIMD state already held in the registers is /not/ the most recent FPSIMD
178+
* state of 'current'
179+
*/
180+
void fpsimd_restore_current_state(void)
181+
{
182+
preempt_disable();
183+
if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
184+
struct fpsimd_state *st = &current->thread.fpsimd_state;
185+
186+
fpsimd_load_state(st);
187+
this_cpu_write(fpsimd_last_state, st);
188+
st->cpu = smp_processor_id();
189+
}
190+
preempt_enable();
191+
}
192+
193+
/*
194+
* Load an updated userland FPSIMD state for 'current' from memory and set the
195+
* flag that indicates that the FPSIMD register contents are the most recent
196+
* FPSIMD state of 'current'
102197
*/
103198
void fpsimd_update_current_state(struct fpsimd_state *state)
104199
{
105200
preempt_disable();
106201
fpsimd_load_state(state);
202+
if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
203+
struct fpsimd_state *st = &current->thread.fpsimd_state;
204+
205+
this_cpu_write(fpsimd_last_state, st);
206+
st->cpu = smp_processor_id();
207+
}
107208
preempt_enable();
108209
}
109210

211+
/*
212+
* Invalidate live CPU copies of task t's FPSIMD state
213+
*/
214+
void fpsimd_flush_task_state(struct task_struct *t)
215+
{
216+
t->thread.fpsimd_state.cpu = NR_CPUS;
217+
}
218+
110219
#ifdef CONFIG_KERNEL_MODE_NEON
111220

112221
/*
@@ -118,16 +227,19 @@ void kernel_neon_begin(void)
118227
BUG_ON(in_interrupt());
119228
preempt_disable();
120229

121-
if (current->mm)
230+
/*
231+
* Save the userland FPSIMD state if we have one and if we haven't done
232+
* so already. Clear fpsimd_last_state to indicate that there is no
233+
* longer userland FPSIMD state in the registers.
234+
*/
235+
if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
122236
fpsimd_save_state(&current->thread.fpsimd_state);
237+
this_cpu_write(fpsimd_last_state, NULL);
123238
}
124239
EXPORT_SYMBOL(kernel_neon_begin);
125240

126241
void kernel_neon_end(void)
127242
{
128-
if (current->mm)
129-
fpsimd_load_state(&current->thread.fpsimd_state);
130-
131243
preempt_enable();
132244
}
133245
EXPORT_SYMBOL(kernel_neon_end);
@@ -140,12 +252,12 @@ static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
140252
{
141253
switch (cmd) {
142254
case CPU_PM_ENTER:
143-
if (current->mm)
255+
if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
144256
fpsimd_save_state(&current->thread.fpsimd_state);
145257
break;
146258
case CPU_PM_EXIT:
147259
if (current->mm)
148-
fpsimd_load_state(&current->thread.fpsimd_state);
260+
set_thread_flag(TIF_FOREIGN_FPSTATE);
149261
break;
150262
case CPU_PM_ENTER_FAILED:
151263
default:

‎arch/arm64/kernel/ptrace.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ static int fpr_set(struct task_struct *target, const struct user_regset *regset,
517517
return ret;
518518

519519
target->thread.fpsimd_state.user_fpsimd = newstate;
520+
fpsimd_flush_task_state(target);
520521
return ret;
521522
}
522523

@@ -764,6 +765,7 @@ static int compat_vfp_set(struct task_struct *target,
764765
uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
765766
}
766767

768+
fpsimd_flush_task_state(target);
767769
return ret;
768770
}
769771

‎arch/arm64/kernel/signal.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,8 @@ asmlinkage void do_notify_resume(struct pt_regs *regs,
413413
clear_thread_flag(TIF_NOTIFY_RESUME);
414414
tracehook_notify_resume(regs);
415415
}
416+
417+
if (thread_flags & _TIF_FOREIGN_FPSTATE)
418+
fpsimd_restore_current_state();
419+
416420
}

0 commit comments

Comments
 (0)