Skip to content

Commit e92bee9

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
arm64/fpsimd: Avoid erroneous elide of user state reload
TIF_FOREIGN_FPSTATE is a 'convenience' flag that should reflect whether the current CPU holds the most recent user mode FP/SIMD state of the current task. It combines two conditions: - whether the current CPU's FP/SIMD state belongs to the task; - whether that state is the most recent associated with the task (as a task may have executed on other CPUs as well). When a task is scheduled in and TIF_KERNEL_FPSTATE is set, it means the task was in a kernel mode NEON section when it was scheduled out, and so the kernel mode FP/SIMD state is restored. Since this implies that the current CPU is *not* holding the most recent user mode FP/SIMD state of the current task, the TIF_FOREIGN_FPSTATE flag is set too, so that the user mode FP/SIMD state is reloaded from memory when returning to userland. However, the task may be scheduled out after completing the kernel mode NEON section, but before returning to userland. When this happens, the TIF_FOREIGN_FPSTATE flag will not be preserved, but will be set as usual the next time the task is scheduled in, and will be based on the above conditions. This means that, rather than setting TIF_FOREIGN_FPSTATE when scheduling in a task with TIF_KERNEL_FPSTATE set, the underlying state should be updated so that TIF_FOREIGN_FPSTATE will assume the expected value as a result. So instead, call fpsimd_flush_cpu_state(), which takes care of this. Closes: https://lore.kernel.org/all/[email protected] Reported-by: Johannes Nixdorf <[email protected]> Fixes: aefbab8 ("arm64: fpsimd: Preserve/restore kernel mode NEON at context switch") Cc: Mark Brown <[email protected]> Cc: Dave Martin <[email protected]> Cc: Janne Grunau <[email protected]> Cc: [email protected] Signed-off-by: Ard Biesheuvel <[email protected]> Tested-by: Janne Grunau <[email protected]> Tested-by: Johannes Nixdorf <[email protected]> Reviewed-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent f481bb3 commit e92bee9

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

arch/arm64/kernel/fpsimd.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,27 @@ static void fpsimd_save_kernel_state(struct task_struct *task)
15351535
task->thread.kernel_fpsimd_cpu = smp_processor_id();
15361536
}
15371537

1538+
/*
1539+
* Invalidate any task's FPSIMD state that is present on this cpu.
1540+
* The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1541+
* before calling this function.
1542+
*/
1543+
static void fpsimd_flush_cpu_state(void)
1544+
{
1545+
WARN_ON(!system_supports_fpsimd());
1546+
__this_cpu_write(fpsimd_last_state.st, NULL);
1547+
1548+
/*
1549+
* Leaving streaming mode enabled will cause issues for any kernel
1550+
* NEON and leaving streaming mode or ZA enabled may increase power
1551+
* consumption.
1552+
*/
1553+
if (system_supports_sme())
1554+
sme_smstop();
1555+
1556+
set_thread_flag(TIF_FOREIGN_FPSTATE);
1557+
}
1558+
15381559
void fpsimd_thread_switch(struct task_struct *next)
15391560
{
15401561
bool wrong_task, wrong_cpu;
@@ -1552,7 +1573,7 @@ void fpsimd_thread_switch(struct task_struct *next)
15521573

15531574
if (test_tsk_thread_flag(next, TIF_KERNEL_FPSTATE)) {
15541575
fpsimd_load_kernel_state(next);
1555-
set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
1576+
fpsimd_flush_cpu_state();
15561577
} else {
15571578
/*
15581579
* Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
@@ -1842,27 +1863,6 @@ void fpsimd_flush_task_state(struct task_struct *t)
18421863
barrier();
18431864
}
18441865

1845-
/*
1846-
* Invalidate any task's FPSIMD state that is present on this cpu.
1847-
* The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1848-
* before calling this function.
1849-
*/
1850-
static void fpsimd_flush_cpu_state(void)
1851-
{
1852-
WARN_ON(!system_supports_fpsimd());
1853-
__this_cpu_write(fpsimd_last_state.st, NULL);
1854-
1855-
/*
1856-
* Leaving streaming mode enabled will cause issues for any kernel
1857-
* NEON and leaving streaming mode or ZA enabled may increase power
1858-
* consumption.
1859-
*/
1860-
if (system_supports_sme())
1861-
sme_smstop();
1862-
1863-
set_thread_flag(TIF_FOREIGN_FPSTATE);
1864-
}
1865-
18661866
/*
18671867
* Save the FPSIMD state to memory and invalidate cpu view.
18681868
* This function must be called with preemption disabled.

0 commit comments

Comments
 (0)