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
73127void 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
82157void 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 */
93167void 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 */
103198void 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}
124239EXPORT_SYMBOL (kernel_neon_begin );
125240
126241void kernel_neon_end (void )
127242{
128- if (current -> mm )
129- fpsimd_load_state (& current -> thread .fpsimd_state );
130-
131243 preempt_enable ();
132244}
133245EXPORT_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 :
0 commit comments