Skip to contentSkip to conversation

arm64/simd: Avoid pointless clearing of FP/SIMD buffer

PATCH Superseded Ard Biesheuvel 2 participants 6 messages Dec 4, 2025 – Dec 8, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/simd: Avoid pointless clearing of FP/SIMD buffer

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +2 -1
index 0941f6f58a14..825b7fe94003 100644
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
 		    kernel_neon_begin(_T->lock),
 		    kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()	scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()	\
+	scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))
 
 #endif
Signature · 1 line
2.47.3
#1 lore raw
Quoted 36 lines · Ard Biesheuvel

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +0 -0
index 0941f6f58a14..825b7fe94003 100644

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 52

Quoted 6 lines
arch/arm64/include/asm/simd.h +2 -1
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
 		    kernel_neon_begin(_T->lock),
 		    kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()	scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()	\
+	scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))

Ick. I should have looked at the generated code more closely.

It's actually worse than you describe, because the zeroing is there even without CONFIG_INIT_STACK_ALL_ZERO=y, simply because the user_fpsimd_state struct is declared using a compound literal.

I'm afraid that this patch probably isn't a good idea, as it relies on undefined behavior. Before this patch, the user_fpsimd_state is declared using a compound literal, which takes on its enclosing scope, i.e. the 'for' statement generated by scoped_guard(). After this patch, it's in a new inner scope, and the pointer to it escapes from it.

Unfortunately I don't think there's any way to solve this while keeping the scoped_ksimd() API as-is.

Best I can come up with is to leave it to the callers to allocate the state, and then use scoped_guard() similar to a regular lock:

        struct user_fpsimd_state __uninitialized fpsimd_state;                   
                                                                                 
        scoped_guard(ksimd, &fpsimd_state)                                       
                foo_neon(...)

Maybe wrap the state declaration with a macro:
DECLARE_FPSIMD_STATE_ONSTACK(fpsimd_state);

- Eric

#2 lore raw
Quoted 51 lines · Eric Biggers
Quoted 36 lines · Ard Biesheuvel

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +0 -0
index 0941f6f58a14..825b7fe94003 100644

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 52

Quoted 6 lines
arch/arm64/include/asm/simd.h +2 -1
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
                  kernel_neon_begin(_T->lock),
                  kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()       scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()       \
+     scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))

Ick. I should have looked at the generated code more closely.

It's actually worse than you describe, because the zeroing is there even without CONFIG_INIT_STACK_ALL_ZERO=y, simply because the user_fpsimd_state struct is declared using a compound literal.

Quoted 9 lines

I'm afraid that this patch probably isn't a good idea, as it relies on undefined behavior. Before this patch, the user_fpsimd_state is declared using a compound literal, which takes on its enclosing scope, i.e. the 'for' statement generated by scoped_guard(). After this patch, it's in a new inner scope, and the pointer to it escapes from it.

Unfortunately I don't think there's any way to solve this while keeping the scoped_ksimd() API as-is.

How about

arch/arm64/include/asm/simd.h +3 -1
@@ -48,6 +48,8 @@ DEFINE_LOCK_GUARD_1(ksimd,
                    kernel_neon_begin(_T->lock),
                    kernel_neon_end(_T->lock))
 
-#define scoped_ksimd() scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()         __scoped_ksimd(__UNIQUE_ID(fpsimd_state))
+#define __scoped_ksimd(id)     struct user_fpsimd_state __uninitialized id; \
+                               scoped_guard(ksimd, &id)
 
 #endif
#3 lore raw
Quoted 67 lines · Ard Biesheuvel
Quoted 51 lines · Eric Biggers
Quoted 36 lines · Ard Biesheuvel

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +0 -0
index 0941f6f58a14..825b7fe94003 100644

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 52

Quoted 6 lines
arch/arm64/include/asm/simd.h +2 -1
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
                  kernel_neon_begin(_T->lock),
                  kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()       scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()       \
+     scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))

Ick. I should have looked at the generated code more closely.

It's actually worse than you describe, because the zeroing is there even without CONFIG_INIT_STACK_ALL_ZERO=y, simply because the user_fpsimd_state struct is declared using a compound literal.

Quoted 9 lines

I'm afraid that this patch probably isn't a good idea, as it relies on undefined behavior. Before this patch, the user_fpsimd_state is declared using a compound literal, which takes on its enclosing scope, i.e. the 'for' statement generated by scoped_guard(). After this patch, it's in a new inner scope, and the pointer to it escapes from it.

Unfortunately I don't think there's any way to solve this while keeping the scoped_ksimd() API as-is.

How about

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 53

Quoted 7 lines
arch/arm64/include/asm/simd.h +3 -1
@@ -48,6 +48,8 @@ DEFINE_LOCK_GUARD_1(ksimd,
                    kernel_neon_begin(_T->lock),
                    kernel_neon_end(_T->lock))
 
-#define scoped_ksimd() scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()         __scoped_ksimd(__UNIQUE_ID(fpsimd_state))
+#define __scoped_ksimd(id)     struct user_fpsimd_state __uninitialized id; \
+                               scoped_guard(ksimd, &id)

I guess that will work. It's not great that it will make scoped_ksimd() expand into more than one statement, which is error-prone and not normally allowed in macros. But it looks okay for all the current users of it.

- Eric

#4 lore raw
Quoted 72 lines · Eric Biggers
Quoted 70 lines · Ard Biesheuvel
Quoted 51 lines · Eric Biggers
Quoted 36 lines · Ard Biesheuvel

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +0 -0
index 0941f6f58a14..825b7fe94003 100644

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 52

Quoted 6 lines
arch/arm64/include/asm/simd.h +2 -1
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
                  kernel_neon_begin(_T->lock),
                  kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()       scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()       \
+     scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))

Ick. I should have looked at the generated code more closely.

It's actually worse than you describe, because the zeroing is there even without CONFIG_INIT_STACK_ALL_ZERO=y, simply because the user_fpsimd_state struct is declared using a compound literal.

Quoted 9 lines

I'm afraid that this patch probably isn't a good idea, as it relies on undefined behavior. Before this patch, the user_fpsimd_state is declared using a compound literal, which takes on its enclosing scope, i.e. the 'for' statement generated by scoped_guard(). After this patch, it's in a new inner scope, and the pointer to it escapes from it.

Unfortunately I don't think there's any way to solve this while keeping the scoped_ksimd() API as-is.

How about

arch/arm64/include/asm/simd.h +0 -0
@@ -48,6 +48,8 @@ DEFINE_LOCK_GUARD_1(ksimd,
                    kernel_neon_begin(_T->lock),
                    kernel_neon_end(_T->lock))
 
Quoted 10 lines

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 53

Quoted 4 lines
arch/arm64/include/asm/simd.h +3 -1
-#define scoped_ksimd() scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()         __scoped_ksimd(__UNIQUE_ID(fpsimd_state))
+#define __scoped_ksimd(id)     struct user_fpsimd_state __uninitialized id; \
+                               scoped_guard(ksimd, &id)

I guess that will work. It's not great that it will make scoped_ksimd() expand into more than one statement, which is error-prone and not normally allowed in macros. But it looks okay for all the current users of it.

We could always repeat the 'for()' trick that the cleanup helpers use, e.g.,

for (struct user_fpsimd_state __uninitialized __st; true; ({ goto label; }))

    if (0) {
label:    break;
    } else scoped_guard(ksimd, &__st)

Would you prefer that?

#5 lore raw
Quoted 84 lines · Ard Biesheuvel
Quoted 72 lines · Eric Biggers
Quoted 70 lines · Ard Biesheuvel
Quoted 51 lines · Eric Biggers
Quoted 36 lines · Ard Biesheuvel

The buffer provided to kernel_neon_begin() is only used if the task is scheduled out while the FP/SIMD is in use by the kernel, or when such a section is interrupted by a softirq that also uses the FP/SIMD.

IOW, this happens rarely, and even if it happened often, there is still no reason for this buffer to be cleared beforehand, which happens by default when using a compiler that supports -ftrivial-auto-var-init.

So mark the buffer as __uninitialized. Given that this is a variable attribute not a type attribute, this requires that the expression is tweaked a bit.

Cc Will Deacon <[email protected]>,
Cc Catalin Marinas <[email protected]>,
Cc Kees Cook <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/arm64/include/asm/simd.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

The issue here is that returning a pointer to an automatic variable as it goes out of scope is slightly dodgy, especially in the context of __attribute__((cleanup())), on which the scoped guard API relies heavily. However, in this case it should be safe, given that this expression is the input to the guarded variable type's constructor.

It is definitely not pretty, though, so hopefully here is a better way to attach this.

arch/arm64/include/asm/simd.h +0 -0
index 0941f6f58a14..825b7fe94003 100644

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 52

Quoted 6 lines
arch/arm64/include/asm/simd.h +2 -1
@@ -48,6 +48,7 @@ DEFINE_LOCK_GUARD_1(ksimd,
                  kernel_neon_begin(_T->lock),
                  kernel_neon_end(_T->lock))
 
-#define scoped_ksimd()       scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()       \
+     scoped_guard(ksimd, ({ struct user_fpsimd_state __uninitialized s; &s; }))

Ick. I should have looked at the generated code more closely.

It's actually worse than you describe, because the zeroing is there even without CONFIG_INIT_STACK_ALL_ZERO=y, simply because the user_fpsimd_state struct is declared using a compound literal.

Quoted 9 lines

I'm afraid that this patch probably isn't a good idea, as it relies on undefined behavior. Before this patch, the user_fpsimd_state is declared using a compound literal, which takes on its enclosing scope, i.e. the 'for' statement generated by scoped_guard(). After this patch, it's in a new inner scope, and the pointer to it escapes from it.

Unfortunately I don't think there's any way to solve this while keeping the scoped_ksimd() API as-is.

How about

arch/arm64/include/asm/simd.h +0 -0
@@ -48,6 +48,8 @@ DEFINE_LOCK_GUARD_1(ksimd,
                    kernel_neon_begin(_T->lock),
                    kernel_neon_end(_T->lock))
 
Quoted 10 lines

arch/arm64/include/asm/simd.h DEFINE_LOCK_GUARD_1(ksimd, · line 53

Quoted 4 lines
arch/arm64/include/asm/simd.h +3 -1
-#define scoped_ksimd() scoped_guard(ksimd, &(struct user_fpsimd_state){})
+#define scoped_ksimd()         __scoped_ksimd(__UNIQUE_ID(fpsimd_state))
+#define __scoped_ksimd(id)     struct user_fpsimd_state __uninitialized id; \
+                               scoped_guard(ksimd, &id)

I guess that will work. It's not great that it will make scoped_ksimd() expand into more than one statement, which is error-prone and not normally allowed in macros. But it looks okay for all the current users of it.

Quoted 8 lines

We could always repeat the 'for()' trick that the cleanup helpers use, e.g.,

for (struct user_fpsimd_state __uninitialized __st; true; ({ goto label; }))

    if (0) {
label:    break;
    } else scoped_guard(ksimd, &__st)

Would you prefer that?

Hmm, I didn't consider using nested 'for' statements. It looks like that should solve the problem. It makes the implementation a bit harder to understand, but at least the 'for' statement trick isn't new... Could you send a patch? Preferably with a clear comment that documents why it's done this way.

- Eric