[PATCH v2 20/20] arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack

Eric Biggers ebiggers at kernel.org
Fri Oct 3 13:18:18 PDT 2025


On Wed, Oct 01, 2025 at 11:02:22PM +0200, Ard Biesheuvel wrote:
> diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h
> index 4f8d677b73ee..93bca4d454d7 100644
> --- a/arch/arm64/include/asm/processor.h
> +++ b/arch/arm64/include/asm/processor.h
> @@ -172,7 +172,7 @@ struct thread_struct {
>  	unsigned long		fault_code;	/* ESR_EL1 value */
>  	struct debug_info	debug;		/* debugging */
>  
> -	struct user_fpsimd_state	kernel_fpsimd_state;
> +	struct user_fpsimd_state	*kernel_fpsimd_state;

Is TIF_KERNEL_FPSTATE redundant with kernel_fpsimd_state != NULL?  If
so, should we have both?

> +void kernel_neon_begin(struct user_fpsimd_state *s)
>  {
>  	if (WARN_ON(!system_supports_fpsimd()))
>  		return;
> @@ -1866,8 +1869,16 @@ void kernel_neon_begin(void)
>  		 * mode in task context. So in this case, setting the flag here
>  		 * is always appropriate.
>  		 */
> -		if (IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq())
> +		if (IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq()) {
> +			/*
> +			 * Record the caller provided buffer as the kernel mode
> +			 * FP/SIMD buffer for this task, so that the state can
> +			 * be preserved and restored on a context switch.
> +			 */
> +			if (cmpxchg(&current->thread.kernel_fpsimd_state, NULL, s))
> +				BUG();

Does this really need to be a cmpxchg, vs. a regular load and store?
It's just operating on current.

> +	else if (test_and_clear_thread_flag(TIF_KERNEL_FPSTATE))
> +		if (cmpxchg(&current->thread.kernel_fpsimd_state, s, NULL) != s)
> +			BUG();

Likewise.

- Eric



More information about the linux-arm-kernel mailing list