Skip to content

Commit d3a1815

Browse files
mrutland-armctmarinas
authored andcommitted
arm64/fpsimd: Add fpsimd_save_and_flush_current_state()
When the current task's FPSIMD/SVE/SME state may be live on *any* CPU in the system, special care must be taken when manipulating that state, as this manipulation can race with preemption and/or asynchronous usage of FPSIMD/SVE/SME (e.g. kernel-mode NEON in softirq handlers). Even when manipulation is is protected with get_cpu_fpsimd_context() and get_cpu_fpsimd_context(), the logic necessary when the state is live on the current CPU can be wildly different from the logic necessary when the state is not live on the current CPU. A number of historical and extant issues result from failing to handle these cases consistetntly and/or correctly. To make it easier to get such manipulation correct, add a new fpsimd_save_and_flush_current_state() helper function, which ensures that the current task's state has been saved to memory and any stale state on any CPU has been "flushed" such that is not live on any CPU in the system. This will allow code to safely manipulate the saved state without risk of races. Subsequent patches will use the new function. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Marc Zyngier <maz@kernel.org> Cc: Mark Brown <broonie@kernel.org> Cc: Will Deacon <will@kernel.org> Reviewed-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20250409164010.3480271-11-mark.rutland@arm.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent c94f2f3 commit d3a1815

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

arch/arm64/include/asm/fpsimd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct cpu_fp_state {
9696
extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
9797

9898
extern void fpsimd_flush_task_state(struct task_struct *target);
99+
extern void fpsimd_save_and_flush_current_state(void);
99100
extern void fpsimd_save_and_flush_cpu_state(void);
100101

101102
static inline bool thread_sm_enabled(struct thread_struct *thread)

arch/arm64/kernel/fpsimd.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,17 @@ void fpsimd_flush_task_state(struct task_struct *t)
18191819
barrier();
18201820
}
18211821

1822+
void fpsimd_save_and_flush_current_state(void)
1823+
{
1824+
if (!system_supports_fpsimd())
1825+
return;
1826+
1827+
get_cpu_fpsimd_context();
1828+
fpsimd_save_user_state();
1829+
fpsimd_flush_task_state(current);
1830+
put_cpu_fpsimd_context();
1831+
}
1832+
18221833
/*
18231834
* Save the FPSIMD state to memory and invalidate cpu view.
18241835
* This function must be called with preemption disabled.

0 commit comments

Comments
 (0)