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

Kees Cook kees at kernel.org
Thu Oct 2 09:22:38 PDT 2025


On Wed, Oct 01, 2025 at 11:02:22PM +0200, Ard Biesheuvel wrote:
> [...]
> diff --git a/arch/arm64/include/asm/simd.h b/arch/arm64/include/asm/simd.h
> index d9f83c478736..7ddb25df5c98 100644
> --- a/arch/arm64/include/asm/simd.h
> +++ b/arch/arm64/include/asm/simd.h
> @@ -43,8 +43,11 @@ static __must_check inline bool may_use_simd(void) {
>  
>  #endif /* ! CONFIG_KERNEL_MODE_NEON */
>  
> -DEFINE_LOCK_GUARD_0(ksimd, kernel_neon_begin(), kernel_neon_end())
> +DEFINE_LOCK_GUARD_1(ksimd,
> +		    struct user_fpsimd_state,
> +		    kernel_neon_begin(_T->lock),
> +		    kernel_neon_end(_T->lock))
>  
> -#define scoped_ksimd()	scoped_guard(ksimd)
> +#define scoped_ksimd()	scoped_guard(ksimd, &(struct user_fpsimd_state){})

I love it!

> [...]
> -void kernel_neon_end(void)
> +void kernel_neon_end(struct user_fpsimd_state *s)
>  {
>  	if (!system_supports_fpsimd())
>  		return;
> @@ -1899,8 +1910,9 @@ void kernel_neon_end(void)
>  	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && in_serving_softirq() &&
>  	    test_thread_flag(TIF_KERNEL_FPSTATE))
>  		fpsimd_load_kernel_state(current);
> -	else
> -		clear_thread_flag(TIF_KERNEL_FPSTATE);
> +	else if (test_and_clear_thread_flag(TIF_KERNEL_FPSTATE))
> +		if (cmpxchg(&current->thread.kernel_fpsimd_state, s, NULL) != s)
> +			BUG();

I always question BUG() uses -- is there a recoverable way to deal with
a mismatch here? I assume not and that this is the best we can do, but I
thought I'd just explicitly ask. :)

-Kees

-- 
Kees Cook



More information about the linux-arm-kernel mailing list