arm64: Move kernel mode FPSIMD buffer to the stack
Subsystems (7)
v4 cover arm64: Move kernel mode FPSIMD buffer to the stack
From: Ard Biesheuvel <[email protected]>
Move the buffer for preserving/restoring the kernel mode FPSIMD state on a context switch out of struct thread_struct, and onto the stack, so that the memory cost is not imposed needlessly on all tasks in the system.
Changes since v3:
- Fix sloppy editing errors in ARM CRC code.
- Add more comments about the state argument to kernel_neon_begin/end and the associated field in thread_struct
- Fix bug in generic kernel mode FPU change
- Add Rbs from Eric
Changes since v2:
- Fix generic kernel mode FPU api instead of removing it.
- Rebase onto v6.18-rc0 and fix the fallout
- Prefer WARN() over BUG() in kernel_neon_begin/end
- Avoid unnecessary cmpxchg() calls
- When invoked in softirq context, use the caller provided buffer rather than the one stored in the task struct - this permits callers from task context (including users of the generic kernel mode FPU api) to pass NULL as the buffer when running with preemption disabled.
- Add acks from Kees and Eric; Mark's was dropped along with the patch in question.
- Fix new occurrence of kernel_neon_begin/end in Mellanox driver.
Changes since v1:
- Add a patch reverting the arm64 support for the generic kernel_fpu_begin()/end() API, which is problematic on arm64.
- Introduce a new 'ksimd' scoped guard that encapsulates the calls the kernel_neon_begin() and kernel_neon_end() at a higher level of abstraction. This makes it straight-forward to plumb in the stack buffer without complicating the callers.
- Move all kernel mode NEON users on arm64 (and some on ARM) over to the new API.
- Add Mark's ack to patches #6 - #8
Ard Biesheuvel (21):
crypto/arm64: aes-ce-ccm - Avoid pointless yield of the NEON unit crypto/arm64: sm4-ce-ccm - Avoid pointless yield of the NEON unit crypto/arm64: sm4-ce-gcm - Avoid pointless yield of the NEON unit arm64/simd: Add scoped guard API for kernel mode SIMD ARM/simd: Add scoped guard API for kernel mode SIMD
crypto: aegis128-neon - Move to more abstract 'ksimd' guard API
raid6: Move to more abstract 'ksimd' guard API lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API lib/crypto: Switch ARM and arm64 to 'ksimd' scoped guard API crypto/arm64: aes-ccm - Switch to 'ksimd' scoped guard API crypto/arm64: aes-blk - Switch to 'ksimd' scoped guard API crypto/arm64: aes-gcm - Switch to 'ksimd' scoped guard API crypto/arm64: nhpoly1305 - Switch to 'ksimd' scoped guard API crypto/arm64: polyval - Switch to 'ksimd' scoped guard API crypto/arm64: sha3 - Switch to 'ksimd' scoped guard API crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API crypto/arm64: sm4 - Switch to 'ksimd' scoped guard API arm64/xorblocks: Switch to 'ksimd' scoped guard API net/mlx5: Switch to more abstract scoped ksimd guard API on arm64 arm64/fpu: Enforce task-context only for generic kernel mode FPU arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack
arch/arm/include/asm/simd.h | 7 + arch/arm64/crypto/aes-ce-ccm-glue.c | 116 +++++------ arch/arm64/crypto/aes-ce-glue.c | 87 ++++---- arch/arm64/crypto/aes-glue.c | 139 ++++++------- arch/arm64/crypto/aes-neonbs-glue.c | 150 +++++++------- arch/arm64/crypto/ghash-ce-glue.c | 27 ++- arch/arm64/crypto/nhpoly1305-neon-glue.c | 5 +- arch/arm64/crypto/polyval-ce-glue.c | 12 +- arch/arm64/crypto/sha3-ce-glue.c | 10 +- arch/arm64/crypto/sm3-ce-glue.c | 15 +- arch/arm64/crypto/sm3-neon-glue.c | 16 +- arch/arm64/crypto/sm4-ce-ccm-glue.c | 49 ++--- arch/arm64/crypto/sm4-ce-cipher-glue.c | 10 +- arch/arm64/crypto/sm4-ce-gcm-glue.c | 62 ++---- arch/arm64/crypto/sm4-ce-glue.c | 214 +++++++++----------- arch/arm64/crypto/sm4-neon-glue.c | 25 +-- arch/arm64/include/asm/fpu.h | 16 +- arch/arm64/include/asm/neon.h | 4 +- arch/arm64/include/asm/processor.h | 7 +- arch/arm64/include/asm/simd.h | 10 + arch/arm64/include/asm/xor.h | 22 +- arch/arm64/kernel/fpsimd.c | 53 +++-- crypto/aegis128-neon.c | 33 ++- drivers/net/ethernet/mellanox/mlx5/core/wc.c | 19 +- lib/crc/arm/crc-t10dif.h | 16 +- lib/crc/arm/crc32.h | 11 +- lib/crc/arm64/crc-t10dif.h | 16 +- lib/crc/arm64/crc32.h | 16 +- lib/crypto/arm/chacha.h | 6 +- lib/crypto/arm/poly1305.h | 6 +- lib/crypto/arm/sha1.h | 13 +- lib/crypto/arm/sha256.h | 12 +- lib/crypto/arm/sha512.h | 5 +- lib/crypto/arm64/chacha.h | 11 +- lib/crypto/arm64/poly1305.h | 6 +- lib/crypto/arm64/sha1.h | 7 +- lib/crypto/arm64/sha256.h | 19 +- lib/crypto/arm64/sha512.h | 8 +- lib/raid6/neon.c | 17 +- lib/raid6/recov_neon.c | 15 +- 40 files changed, 601 insertions(+), 691 deletions(-)
base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 1/21 crypto/arm64: aes-ce-ccm - Avoid pointless yield of the NEON unit
From: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it explicitly in order to prevent scheduling latency spikes.
--- arch/arm64/crypto/aes-ce-ccm-glue.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm64/crypto/aes-ce-ccm-glue.c +1 -4
@@ -114,11 +114,8 @@ static u32 ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes, in += adv; abytes -= adv; -if (unlikely (rem)) { -kernel_neon_end (); -kernel_neon_begin (); +if (unlikely (rem)) macp =0 ; - } }else {u32 l =min (AES_BLOCK_SIZE - macp, abytes);
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 01/21] crypto/arm64: aes-ce-ccm - Avoid pointless yield of the NEON unit
Quoted 11 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it explicitly in order to prevent scheduling latency spikes.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/crypto/aes-ce-ccm-glue.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH v4 2/21 crypto/arm64: sm4-ce-ccm - Avoid pointless yield of the NEON unit
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it when calling APIs that may sleep.
Also, move the calls to kernel_neon_end() to the same scope as kernel_neon_begin(). This is needed for a subsequent change where a stack buffer is allocated transparently and passed to kernel_neon_begin().
--- arch/arm64/crypto/sm4-ce-ccm-glue.c | 25 +++++--------------- 1 file changed, 6 insertions(+), 19 deletions(-)
arch/arm64/crypto/sm4-ce-ccm-glue.c +6 -19
@@ -172,35 +172,22 @@ static int ccm_crypt(struct aead_request *req, struct skcipher_walk *walk,if (req->assoclen)ccm_calculate_auth_mac (req, mac); -while (walk->nbytes && walk->nbytes != walk->total) { +while (walk->nbytes) {unsigned int tail = walk->nbytes %SM4_BLOCK_SIZE ; +if (walk->nbytes == walk->total) + tail =0 ; +sm4_ce_ccm_crypt (rkey_enc, walk->dst.virt.addr, walk->src.virt.addr, walk->iv, walk->nbytes - tail, mac); -kernel_neon_end (); - err =skcipher_walk_done (walk, tail); - -kernel_neon_begin (); } -if (walk->nbytes) { -sm4_ce_ccm_crypt (rkey_enc, walk->dst.virt.addr, - walk->src.virt.addr, walk->iv, - walk->nbytes, mac); - -sm4_ce_ccm_final (rkey_enc, ctr0, mac); +sm4_ce_ccm_final (rkey_enc, ctr0, mac); -kernel_neon_end (); - - err =skcipher_walk_done (walk,0 ); - }else { -sm4_ce_ccm_final (rkey_enc, ctr0, mac); - -kernel_neon_end (); - } +kernel_neon_end ();return err; }
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 02/21] crypto/arm64: sm4-ce-ccm - Avoid pointless yield of the NEON unit
Quoted 10 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it when calling APIs that may sleep.
Also, move the calls to
kernel_neon_end()to the same scope askernel_neon_begin(). This is needed for a subsequent change where a stack buffer is allocated transparently and passed tokernel_neon_begin().
Quoted 5 lines
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/crypto/sm4-ce-ccm-glue.c | 25 +++++--------------- 1 file changed, 6 insertions(+), 19 deletions(-)
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH v4 3/21 crypto/arm64: sm4-ce-gcm - Avoid pointless yield of the NEON unit
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it when calling APIs that may sleep.
Also, move the calls to kernel_neon_end() to the same scope as kernel_neon_begin(). This is needed for a subsequent change where a stack buffer is allocated transparently and passed to kernel_neon_begin().
While at it, simplify the logic.
--- arch/arm64/crypto/sm4-ce-gcm-glue.c | 25 +++++--------------- 1 file changed, 6 insertions(+), 19 deletions(-)
arch/arm64/crypto/sm4-ce-gcm-glue.c +6 -19
@@ -154,36 +154,23 @@ static int gcm_crypt(struct aead_request *req, struct skcipher_walk *walk,if (req->assoclen)gcm_calculate_auth_mac (req, ghash); -while (walk->nbytes) { +do {unsigned int tail = walk->nbytes %SM4_BLOCK_SIZE ;const u8 *src = walk->src.virt.addr;u8 *dst = walk->dst.virt.addr; +const u8 *l =NULL ;if (walk->nbytes == walk->total) { -sm4_ce_pmull_gcm_crypt (ctx->key.rkey_enc, dst, src, iv, - walk->nbytes, ghash, - ctx->ghash_table, - (const u8 *)&lengths); - -kernel_neon_end (); - -return skcipher_walk_done (walk,0 ); + l = (const u8 *)&lengths; + tail =0 ; }sm4_ce_pmull_gcm_crypt (ctx->key.rkey_enc, dst, src, iv, walk->nbytes - tail, ghash, - ctx->ghash_table,NULL ); - -kernel_neon_end (); + ctx->ghash_table, l); err =skcipher_walk_done (walk, tail); - -kernel_neon_begin (); - } - -sm4_ce_pmull_gcm_crypt (ctx->key.rkey_enc,NULL ,NULL , iv, - walk->nbytes, ghash, ctx->ghash_table, - (const u8 *)&lengths); + }while (walk->nbytes);kernel_neon_end ();
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 03/21] crypto/arm64: sm4-ce-gcm - Avoid pointless yield of the NEON unit
Quoted 10 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Kernel mode NEON sections are now preemptible on arm64, and so there is no need to yield it when calling APIs that may sleep.
Also, move the calls to
kernel_neon_end()to the same scope askernel_neon_begin(). This is needed for a subsequent change where a stack buffer is allocated transparently and passed tokernel_neon_begin().
Quoted 7 lines
While at it, simplify the logic.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/crypto/sm4-ce-gcm-glue.c | 25 +++++--------------- 1 file changed, 6 insertions(+), 19 deletions(-)
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH v4 4/21 arm64/simd: Add scoped guard API for kernel mode SIMD
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Encapsulate kernel_neon_begin() and kernel_neon_end() using a 'ksimd' cleanup guard. This hides the prototype of those functions, allowing them to be changed for arm64 but not ARM, without breaking code that is shared between those architectures (RAID6, AEGIS-128)
It probably makes sense to expose this API more widely across architectures, as it affords more flexibility to the arch code to plumb it in, while imposing more rigid rules regarding the start/end bookends appearing in matched pairs.
--- arch/arm64/include/asm/simd.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/include/asm/simd.h +7 -0
@@ -6,12 +6,15 @@#ifndef __ASM_SIMD_H#define __ASM_SIMD_H +#include <linux/cleanup.h> #include <linux/compiler.h> #include <linux/irqflags.h> #include <linux/percpu.h> #include <linux/preempt.h> #include <linux/types.h> +#include <asm/neon.h> +#ifdef CONFIG_KERNEL_MODE_NEON /*
@@ -40,4 +43,8 @@ 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 scoped_ksimd () scoped_guard(ksimd) +#endif
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 04/21] arm64/simd: Add scoped guard API for kernel mode SIMD
Quoted 7 lines · On Fri, 31 Oct 2025 11:39:03 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Encapsulate
kernel_neon_begin()andkernel_neon_end()using a 'ksimd' cleanup guard. This hides the prototype of those functions, allowing them to be changed for arm64 but not ARM, without breaking code that is shared between those architectures (RAID6, AEGIS-128)
Quoted 8 lines
It probably makes sense to expose this API more widely across architectures, as it affords more flexibility to the arch code to plumb it in, while imposing more rigid rules regarding the start/end bookends appearing in matched pairs.
Reviewed-by Kees Cook <[email protected]>Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Very nice.
FWIW I looked at all the usecases and other than a couple of trivial comments on individual patches they look good to me.
For patches 4-19
Quoted 33 lines
--- arch/arm64/include/asm/simd.h | 7 +++++++ 1 file changed, 7 insertions(+)arch/arm64/include/asm/simd.h +7
-0@@ -6,12 +6,15 @@#ifndef __ASM_SIMD_H#define __ASM_SIMD_H +#include <linux/cleanup.h> #include <linux/compiler.h> #include <linux/irqflags.h> #include <linux/percpu.h> #include <linux/preempt.h> #include <linux/types.h> +#include <asm/neon.h> +#ifdef CONFIG_KERNEL_MODE_NEON /* @@ -40,4 +43,8 @@ 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 scoped_ksimd () scoped_guard(ksimd) +#endif
Ard Biesheuvel
maintainerRe: [PATCH v4 04/21] arm64/simd: Add scoped guard API for kernel mode SIMD
Quoted 19 lines · Jonathan Cameron
Quoted 7 lines · On Fri, 31 Oct 2025 11:39:03 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Encapsulate
kernel_neon_begin()andkernel_neon_end()using a 'ksimd' cleanup guard. This hides the prototype of those functions, allowing them to be changed for arm64 but not ARM, without breaking code that is shared between those architectures (RAID6, AEGIS-128)Quoted 8 lines
It probably makes sense to expose this API more widely across architectures, as it affords more flexibility to the arch code to plumb it in, while imposing more rigid rules regarding the start/end bookends appearing in matched pairs.
Reviewed-by Kees Cook <[email protected]>Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Quoted 8 lines
Very nice.
FWIW I looked at all the usecases and other than a couple of trivial comments on individual patches they look good to me.
Reviewed-by Jonathan Cameron <[email protected]>For patches 4-19
Thanks!
PATCH v4 5/21 ARM/simd: Add scoped guard API for kernel mode SIMD
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Implement the ksimd scoped guard API so that it can be used by code that supports both ARM and arm64.
--- arch/arm/include/asm/simd.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm/include/asm/simd.h +7 -0
@@ -2,14 +2,21 @@#ifndef _ASM_SIMD_H#define _ASM_SIMD_H +#include <linux/cleanup.h> #include <linux/compiler_attributes.h> #include <linux/preempt.h> #include <linux/types.h> +#include <asm/neon.h> +static __must_check inline bool may_use_simd (void ) {return IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) && !in_hardirq () && !irqs_disabled (); } +DEFINE_LOCK_GUARD_0(ksimd, kernel_neon_begin(), kernel_neon_end()) + +#define scoped_ksimd () scoped_guard(ksimd) +#endif /* _ASM_SIMD_H */
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 6/21 crypto: aegis128-neon - Move to more abstract 'ksimd' guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move away from calling kernel_neon_begin() and kernel_neon_end() directly, and instead, use the newly introduced scoped_ksimd() API. This permits arm64 to modify the kernel mode NEON API without affecting code that is shared between ARM and arm64.
--- crypto/aegis128-neon.c | 33 +++++++------------- 1 file changed, 12 insertions(+), 21 deletions(-)
crypto/aegis128-neon.c +12 -21
@@ -4,7 +4,7 @@*/ #include <asm/cpufeature.h> -#include <asm/neon.h> +#include <asm/simd.h> #include "aegis.h" #include "aegis-neon.h"
@@ -24,32 +24,28 @@ void crypto_aegis128_init_simd(struct aegis_state *state, const union aegis_block *key, const u8 *iv) { -kernel_neon_begin (); -crypto_aegis128_init_neon (state, key, iv); -kernel_neon_end (); + scoped_ksimd() + crypto_aegis128_init_neon(state, key, iv); }void crypto_aegis128_update_simd (struct aegis_state *state,const void *msg) { -kernel_neon_begin (); -crypto_aegis128_update_neon (state, msg); -kernel_neon_end (); + scoped_ksimd() +crypto_aegis128_update_neon (state, msg); }void crypto_aegis128_encrypt_chunk_simd (struct aegis_state *state,u8 *dst,const u8 *src,unsigned int size) { -kernel_neon_begin (); -crypto_aegis128_encrypt_chunk_neon (state, dst, src, size); -kernel_neon_end (); + scoped_ksimd() +crypto_aegis128_encrypt_chunk_neon (state, dst, src, size); }void crypto_aegis128_decrypt_chunk_simd (struct aegis_state *state,u8 *dst,const u8 *src,unsigned int size) { -kernel_neon_begin (); -crypto_aegis128_decrypt_chunk_neon (state, dst, src, size); -kernel_neon_end (); + scoped_ksimd() +crypto_aegis128_decrypt_chunk_neon (state, dst, src, size); } int crypto_aegis128_final_simd(struct aegis_state *state,
@@ -58,12 +54,7 @@ int crypto_aegis128_final_simd(struct aegis_state *state, unsigned int cryptlen, unsigned int authsize) { - int ret; - -kernel_neon_begin (); - ret =crypto_aegis128_final_neon (state, tag_xor, assoclen, cryptlen, - authsize); -kernel_neon_end (); - -return ret; + scoped_ksimd() + return crypto_aegis128_final_neon(state, tag_xor, assoclen, + cryptlen, authsize); }
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 7/21 raid6: Move to more abstract 'ksimd' guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Move away from calling kernel_neon_begin() and kernel_neon_end() directly, and instead, use the newly introduced scoped_ksimd() API. This permits arm64 to modify the kernel mode NEON API without affecting code that is shared between ARM and arm64.
--- lib/raid6/neon.c | 17 +++++++---------- lib/raid6/recov_neon.c | 15 ++++++--------- 2 files changed, 13 insertions(+), 19 deletions(-)
lib/raid6/neon.c +7 -10
@@ -8,10 +8,9 @@#include <linux/raid/pq.h> #ifdef __KERNEL__ -#include <asm/neon.h> +#include <asm/simd.h> #else -#define kernel_neon_begin () -#define kernel_neon_end () +#define scoped_ksimd ()#define cpu_has_neon () (1)#endif
@@ -32,10 +31,9 @@ { \ void raid6_neon ## _n ## _gen_syndrome_real(int, \unsigned long ,void **); \ -kernel_neon_begin (); \ - raid6_neon ## _n ## _gen_syndrome_real(disks, \ + scoped_ksimd() \ + raid6_neon ## _n ## _gen_syndrome_real(disks, \ (unsigned long )bytes, ptrs); \ -kernel_neon_end (); \ } \ static void raid6_neon ## _n ## _xor_syndrome(int disks, \ int start, int stop, \
@@ -43,10 +41,9 @@ { \ void raid6_neon ## _n ## _xor_syndrome_real(int, \int ,int ,unsigned long ,void **); \ -kernel_neon_begin (); \ - raid6_neon ## _n ## _xor_syndrome_real(disks, \ - start, stop, (unsigned long)bytes, ptrs); \ -kernel_neon_end (); \ + scoped_ksimd() \ + raid6_neon ## _n ## _xor_syndrome_real(disks, \ + start, stop, (unsigned long )bytes, ptrs);\ } \ struct raid6_calls const raid6_neonx ## _n = { \ raid6_neon ## _n ## _gen_syndrome, \
lib/raid6/recov_neon.c +6 -9
@@ -7,11 +7,10 @@#include <linux/raid/pq.h> #ifdef __KERNEL__ -#include <asm/neon.h> +#include <asm/simd.h> #include "neon.h" #else -#define kernel_neon_begin () -#define kernel_neon_end () +#define scoped_ksimd ()#define cpu_has_neon () (1)#endif
@@ -55,9 +54,8 @@ static void raid6_2data_recov_neon(int disks, size_t bytes, int faila, qmul = raid6_vgfmul[raid6_gfinv[raid6_gfexp[faila] ^ raid6_gfexp[failb]]]; -kernel_neon_begin (); -__raid6_2data_recov_neon (bytes, p, q, dp, dq, pbmul, qmul); -kernel_neon_end (); + scoped_ksimd() +__raid6_2data_recov_neon (bytes, p, q, dp, dq, pbmul, qmul); } static void raid6_datap_recov_neon(int disks, size_t bytes, int faila,
@@ -86,9 +84,8 @@ static void raid6_datap_recov_neon(int disks, size_t bytes, int faila,/* Now, pick the proper data tables */ qmul = raid6_vgfmul[raid6_gfinv[raid6_gfexp[faila]]]; -kernel_neon_begin (); -__raid6_datap_recov_neon (bytes, p, q, dq, qmul); -kernel_neon_end (); + scoped_ksimd() +__raid6_datap_recov_neon (bytes, p, q, dq, qmul); }const struct raid6_recov_calls raid6_recov_neon = {
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 8/21 lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of kernel_neon_begin() and kernel_neon_end() to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.
For symmetry, do the same for 32-bit ARM too.
--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)
lib/crc/arm/crc-t10dif.h +5 -11
@@ -5,7 +5,6 @@* Copyright (C) 2016 Linaro Ltd <[email protected]> */ -#include <asm/neon.h> #include <asm/simd.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);
@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length); -kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {u8 buf[16 ]__aligned(16) ; -kernel_neon_begin (); -crc_t10dif_pmull8 (crc, data, length, buf); -kernel_neon_end (); + scoped_ksimd() +crc_t10dif_pmull8 (crc, data, length, buf);return crc_t10dif_generic (0 , buf,sizeof (buf)); }
lib/crc/arm/crc32.h +4 -7
@@ -8,7 +8,6 @@#include <linux/cpufeature.h> #include <asm/hwcap.h> -#include <asm/neon.h> #include <asm/simd.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_crc32);
@@ -42,9 +41,8 @@ static inline u32 crc32_le_arch(u32 crc, const u8 *p, size_t len) len -= n; } n =round_down (len,16 ); -kernel_neon_begin (); - crc =crc32_pmull_le (p, n, crc); -kernel_neon_end (); +scoped_ksimd () + crc = crc32_pmull_le(p, n, crc); p += n; len -= n; }
@@ -71,9 +69,8 @@ static inline u32 crc32c_arch(u32 crc, const u8 *p, size_t len) len -= n; } n =round_down (len,16 ); -kernel_neon_begin (); - crc =crc32c_pmull_le (p, n, crc); -kernel_neon_end (); +scoped_ksimd () + crc = crc32c_pmull_le(p, n, crc); p += n; len -= n; }
lib/crc/arm64/crc-t10dif.h +5 -11
@@ -7,7 +7,6 @@#include <linux/cpufeature.h> -#include <asm/neon.h> #include <asm/simd.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_asimd);
@@ -22,21 +21,16 @@ asmlinkage u16 crc_t10dif_pmull_p64(u16 init_crc, const u8 *buf, size_t len);static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull_p64 (crc, data, length); -kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull_p64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_asimd) &&likely (may_use_simd ())) {u8 buf[16 ]; -kernel_neon_begin (); -crc_t10dif_pmull_p8 (crc, data, length, buf); -kernel_neon_end (); + scoped_ksimd() +crc_t10dif_pmull_p8 (crc, data, length, buf);return crc_t10dif_generic (0 , buf,sizeof (buf)); }
lib/crc/arm64/crc32.h +6 -10
@@ -2,7 +2,6 @@#include <asm/alternative.h> #include <asm/cpufeature.h> -#include <asm/neon.h> #include <asm/simd.h> // The minimum input length to consider the 4-way interleaved code path
@@ -23,9 +22,8 @@ static inline u32 crc32_le_arch(u32 crc, const u8 *p, size_t len)if (len >= min_len &&cpu_have_named_feature (PMULL ) &&likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc32_le_arm64_4way (crc, p, len); -kernel_neon_end (); +scoped_ksimd () + crc = crc32_le_arm64_4way(crc, p, len); p +=round_down (len,64 ); len %=64 ;
@@ -44,9 +42,8 @@ static inline u32 crc32c_arch(u32 crc, const u8 *p, size_t len)if (len >= min_len &&cpu_have_named_feature (PMULL ) &&likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc32c_le_arm64_4way (crc, p, len); -kernel_neon_end (); +scoped_ksimd () + crc = crc32c_le_arm64_4way(crc, p, len); p +=round_down (len,64 ); len %=64 ;
@@ -65,9 +62,8 @@ static inline u32 crc32_be_arch(u32 crc, const u8 *p, size_t len)if (len >= min_len &&cpu_have_named_feature (PMULL ) &&likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc32_be_arm64_4way (crc, p, len); -kernel_neon_end (); +scoped_ksimd () + crc = crc32_be_arm64_4way(crc, p, len); p +=round_down (len,64 ); len %=64 ;
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);
lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {
I briefly thought this was a functional change but it's not because of may_use_simd() being something that isn't going to change between the two evaluations.
Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) {
if (static_branch_likely(&have_pmull)) {
scoped_ksimd()
return crc_t10dif_pmull64(crc, data, length); } else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE &&
static_branch_likely(&have_neon)) {
...?
Ard Biesheuvel
maintainerRe: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 61 lines · Jonathan Cameron
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {I briefly thought this was a functional change but it's not because of
may_use_simd()being something that isn't going to change between the two evaluations.Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) { if (static_branch_likely(&have_pmull)) { scoped_ksimd() return crc_t10dif_pmull64(crc, data, length); } else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE && static_branch_likely(&have_neon)) { ...
Quoted 2 lines
?
Yeah that would be a reasonable cleanup, I guess.
Ard Biesheuvel
maintainerRe: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 60 lines · Ard Biesheuvel
Quoted 57 lines · Jonathan Cameron
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {I briefly thought this was a functional change but it's not because of
may_use_simd()being something that isn't going to change between the two evaluations.Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) { if (static_branch_likely(&have_pmull)) { scoped_ksimd() return crc_t10dif_pmull64(crc, data, length);
Quoted 8 lines
Quoted 6 lines
} else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE && static_branch_likely(&have_neon)) { ...?
Yeah that would be a reasonable cleanup, I guess.
Actually, looking more closely, that would result in may_use_simd() being evaluated even when the static keys are set to false, given that the compiler is unlikely to be able to figure out by itself that may_use_simd() has no side effects.
Re: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 68 lines · On Fri, 31 Oct 2025 15:05:19 +0100 Ard Biesheuvel <[email protected]> wrote:
Quoted 67 lines · Ard Biesheuvel
Quoted 63 lines · Jonathan Cameron
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {I briefly thought this was a functional change but it's not because of
may_use_simd()being something that isn't going to change between the two evaluations.Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) { if (static_branch_likely(&have_pmull)) { scoped_ksimd() return crc_t10dif_pmull64(crc, data, length); } else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE && static_branch_likely(&have_neon)) { ...?
Quoted 6 lines
Quoted 1 line
Yeah that would be a reasonable cleanup, I guess.
Actually, looking more closely, that would result in
may_use_simd()being evaluated even when the static keys are set to false, given that the compiler is unlikely to be able to figure out by itself thatmay_use_simd()has no side effects.
Yeah. That was why it was a question :)
Given everything is marked as likely I wasn't sure if we cared about when the static keys aren't set.
Jonathan
Quoted 1 line
Ard Biesheuvel
maintainerRe: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 74 lines · Jonathan Cameron
Quoted 70 lines · On Fri, 31 Oct 2025 15:05:19 +0100 Ard Biesheuvel <[email protected]> wrote:
Quoted 68 lines · Ard Biesheuvel
Quoted 61 lines · Jonathan Cameron
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {I briefly thought this was a functional change but it's not because of
may_use_simd()being something that isn't going to change between the two evaluations.Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) { if (static_branch_likely(&have_pmull)) { scoped_ksimd() return crc_t10dif_pmull64(crc, data, length); } else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE && static_branch_likely(&have_neon)) { ...Quoted 2 lines
?
Yeah that would be a reasonable cleanup, I guess.
Quoted 8 lines
Quoted 4 lines
Actually, looking more closely, that would result in
may_use_simd()being evaluated even when the static keys are set to false, given that the compiler is unlikely to be able to figure out by itself thatmay_use_simd()has no side effects.Yeah. That was why it was a question :)
Given everything is marked as likely I wasn't sure if we cared about when the static keys aren't set.
Yeah, it is rather doubtful that those annotations (or the use of static keys, for that matter) make a meaningful difference here.
Eric Biggers
maintainerRe: [PATCH v4 08/21] lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API
Quoted 80 lines · Ard Biesheuvel
Quoted 78 lines · Jonathan Cameron
Quoted 74 lines · On Fri, 31 Oct 2025 15:05:19 +0100 Ard Biesheuvel <[email protected]> wrote:
Quoted 60 lines · Ard Biesheuvel
Quoted 57 lines · Jonathan Cameron
Quoted 32 lines · On Fri, 31 Oct 2025 11:39:07 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of
kernel_neon_begin()andkernel_neon_end()to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.For symmetry, do the same for 32-bit ARM too.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>--- lib/crc/arm/crc-t10dif.h | 16 +++++----------- lib/crc/arm/crc32.h | 11 ++++------- lib/crc/arm64/crc-t10dif.h | 16 +++++----------- lib/crc/arm64/crc32.h | 16 ++++++---------- 4 files changed, 20 insertions(+), 39 deletions(-)lib/crc/arm/crc-t10dif.h +0
-4static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);@@ -20,21 +19,16 @@ asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len,static inline u16 crc_t10dif_arch (u16 crc,const u8 *data,size_t length) {if (length >=CRC_T10DIF_PMULL_CHUNK_SIZE ) { -if (static_branch_likely (&have_pmull)) { -if (likely (may_use_simd ())) { -kernel_neon_begin (); - crc =crc_t10dif_pmull64 (crc, data, length);lib/crc/arm/crc-t10dif.h asmlinkage void crc_t10dif_pmull8(u16 init_crc, const u8 *buf, size_t len, · line 28
Quoted 10 lines
lib/crc/arm/crc-t10dif.h +3
-3-kernel_neon_end (); -return crc; - } +if (static_branch_likely (&have_pmull) &&likely (may_use_simd ())) { + scoped_ksimd() +return crc_t10dif_pmull64 (crc, data, length); }else if (length >CRC_T10DIF_PMULL_CHUNK_SIZE &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) {I briefly thought this was a functional change but it's not because of
may_use_simd()being something that isn't going to change between the two evaluations.Would it hurt at all to pull that up to be
if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && likely(may_use_simd())) { if (static_branch_likely(&have_pmull)) { scoped_ksimd() return crc_t10dif_pmull64(crc, data, length);Quoted 8 lines
Quoted 6 lines
} else if (length > CRC_T10DIF_PMULL_CHUNK_SIZE && static_branch_likely(&have_neon)) { ...?
Yeah that would be a reasonable cleanup, I guess.
Actually, looking more closely, that would result in
may_use_simd()being evaluated even when the static keys are set to false, given that the compiler is unlikely to be able to figure out by itself thatmay_use_simd()has no side effects.
Quoted 7 lines
Quoted 4 lines
Yeah. That was why it was a question :)
Given everything is marked as likely I wasn't sure if we cared about when the static keys aren't set.Yeah, it is rather doubtful that those annotations (or the use of static keys, for that matter) make a meaningful difference here.
Well, this change makes crc_t10dif_update() not usable during early boot on arm64, as it will start hitting the WARN_ON(!system_capabilities_finalized() in may_use_simd().
I doubt there are any current callers where that matters, but I've been trying to avoid this sort of unnecessary pitfall in the CRC functions.
Checking the static key first eliminates this pitfall and is also more efficient on CPUs that don't support the relevant CPU feature.
- Eric
PATCH v4 9/21 lib/crypto: Switch ARM and arm64 to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Before modifying the prototypes of kernel_neon_begin() and kernel_neon_end() to accommodate kernel mode FP/SIMD state buffers allocated on the stack, move arm64 to the new 'ksimd' scoped guard API, which encapsulates the calls to those functions.
For symmetry, do the same for 32-bit ARM too.
--- lib/crypto/arm/chacha.h | 6 ++---- lib/crypto/arm/poly1305.h | 6 ++---- lib/crypto/arm/sha1.h | 13 ++++++------- lib/crypto/arm/sha256.h | 12 ++++++------ lib/crypto/arm/sha512.h | 5 ++--- lib/crypto/arm64/chacha.h | 11 ++++------- lib/crypto/arm64/poly1305.h | 6 ++---- lib/crypto/arm64/sha1.h | 7 +++---- lib/crypto/arm64/sha256.h | 19 ++++++++----------- lib/crypto/arm64/sha512.h | 8 ++++---- 10 files changed, 39 insertions(+), 54 deletions(-)
lib/crypto/arm/chacha.h +2 -4
@@ -12,7 +12,6 @@#include <asm/cputype.h> #include <asm/hwcap.h> -#include <asm/neon.h> #include <asm/simd.h> asmlinkage void chacha_block_xor_neon(const struct chacha_state *state,
@@ -87,9 +86,8 @@ static void chacha_crypt_arch(struct chacha_state *state, u8 *dst, do { unsigned int todo = min_t(unsigned int, bytes, SZ_4K); - kernel_neon_begin(); - chacha_doneon(state, dst, src, todo, nrounds); - kernel_neon_end(); + scoped_ksimd() + chacha_doneon(state, dst, src, todo, nrounds); bytes -= todo; src += todo;
lib/crypto/arm/poly1305.h +2 -4
@@ -6,7 +6,6 @@*/ #include <asm/hwcap.h> -#include <asm/neon.h> #include <asm/simd.h> #include <linux/cpufeature.h> #include <linux/jump_label.h>
@@ -32,9 +31,8 @@ static void poly1305_blocks(struct poly1305_block_state *state, const u8 *src, do { unsigned int todo = min_t(unsigned int, len, SZ_4K); - kernel_neon_begin(); - poly1305_blocks_neon(state, src, todo, padbit); - kernel_neon_end(); + scoped_ksimd() + poly1305_blocks_neon(state, src, todo, padbit); len -= todo; src += todo;
lib/crypto/arm/sha1.h +6 -7
@@ -4,7 +4,6 @@* * Copyright 2025 Google LLC */ -#include <asm/neon.h> #include <asm/simd.h> static __ro_after_init DEFINE_STATIC_KEY_FALSE (have_neon);
@@ -22,12 +21,12 @@ static void sha1_blocks(struct sha1_block_state *state, {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) { -kernel_neon_begin (); -if (static_branch_likely (&have_ce)) -sha1_ce_transform (state, data, nblocks); -else -sha1_transform_neon (state, data, nblocks); -kernel_neon_end (); + scoped_ksimd() { +if (static_branch_likely (&have_ce)) +sha1_ce_transform (state, data, nblocks); +else +sha1_transform_neon (state, data, nblocks); + } }else {sha1_block_data_order (state, data, nblocks); }
lib/crypto/arm/sha256.h +6 -6
@@ -22,12 +22,12 @@ static void sha256_blocks(struct sha256_block_state *state, {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) { -kernel_neon_begin (); -if (static_branch_likely (&have_ce)) -sha256_ce_transform (state, data, nblocks); -else -sha256_block_data_order_neon (state, data, nblocks); -kernel_neon_end (); + scoped_ksimd() { +if (static_branch_likely (&have_ce)) +sha256_ce_transform (state, data, nblocks); +else +sha256_block_data_order_neon (state, data, nblocks); + } }else {sha256_block_data_order (state, data, nblocks); }
lib/crypto/arm/sha512.h +2 -3
@@ -19,9 +19,8 @@ static void sha512_blocks(struct sha512_block_state *state, {if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_neon) &&likely (may_use_simd ())) { -kernel_neon_begin (); -sha512_block_data_order_neon (state, data, nblocks); -kernel_neon_end (); + scoped_ksimd() +sha512_block_data_order_neon (state, data, nblocks); }else {sha512_block_data_order (state, data, nblocks); }
lib/crypto/arm64/chacha.h +4 -7
@@ -23,7 +23,6 @@#include <linux/kernel.h> #include <asm/hwcap.h> -#include <asm/neon.h> #include <asm/simd.h> asmlinkage void chacha_block_xor_neon(const struct chacha_state *state,
@@ -65,9 +64,8 @@ static void hchacha_block_arch(const struct chacha_state *state,if (!static_branch_likely (&have_neon) || !crypto_simd_usable ()) {hchacha_block_generic (state, out, nrounds); }else { -kernel_neon_begin (); -hchacha_block_neon (state, out, nrounds); -kernel_neon_end (); + scoped_ksimd() +hchacha_block_neon (state, out, nrounds); } }
@@ -81,9 +79,8 @@ static void chacha_crypt_arch(struct chacha_state *state, u8 *dst, do { unsigned int todo = min_t(unsigned int, bytes, SZ_4K); - kernel_neon_begin(); - chacha_doneon(state, dst, src, todo, nrounds); - kernel_neon_end(); + scoped_ksimd() + chacha_doneon(state, dst, src, todo, nrounds); bytes -= todo; src += todo;
lib/crypto/arm64/poly1305.h +2 -4
@@ -6,7 +6,6 @@*/ #include <asm/hwcap.h> -#include <asm/neon.h> #include <asm/simd.h> #include <linux/cpufeature.h> #include <linux/jump_label.h>
@@ -31,9 +30,8 @@ static void poly1305_blocks(struct poly1305_block_state *state, const u8 *src, do { unsigned int todo = min_t(unsigned int, len, SZ_4K); - kernel_neon_begin(); - poly1305_blocks_neon(state, src, todo, padbit); - kernel_neon_end(); + scoped_ksimd() + poly1305_blocks_neon(state, src, todo, padbit); len -= todo; src += todo;
lib/crypto/arm64/sha1.h +3 -4
@@ -4,7 +4,6 @@* * Copyright 2025 Google LLC */ -#include <asm/neon.h> #include <asm/simd.h> #include <linux/cpufeature.h>
@@ -20,9 +19,9 @@ static void sha1_blocks(struct sha1_block_state *state,do {size_t rem; -kernel_neon_begin (); - rem =__sha1_ce_transform (state, data, nblocks); -kernel_neon_end (); +scoped_ksimd () + rem = __sha1_ce_transform(state, data, nblocks); + data += (nblocks - rem) *SHA1_BLOCK_SIZE ; nblocks = rem; }while (nblocks);
lib/crypto/arm64/sha256.h +8 -11
@@ -4,7 +4,6 @@* * Copyright 2025 Google LLC */ -#include <asm/neon.h> #include <asm/simd.h> #include <linux/cpufeature.h>
@@ -27,17 +26,16 @@ static void sha256_blocks(struct sha256_block_state *state,do {size_t rem; -kernel_neon_begin (); - rem =__sha256_ce_transform (state, - data, nblocks); -kernel_neon_end (); +scoped_ksimd () + rem = __sha256_ce_transform(state, data, + nblocks); + data += (nblocks - rem) *SHA256_BLOCK_SIZE ; nblocks = rem; }while (nblocks); }else { -kernel_neon_begin (); -sha256_block_neon (state, data, nblocks); -kernel_neon_end (); + scoped_ksimd() +sha256_block_neon (state, data, nblocks); } }else {sha256_block_data_order (state, data, nblocks);
@@ -66,9 +64,8 @@ static bool sha256_finup_2x_arch(const struct __sha256_ctx *ctx,if (IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&static_branch_likely (&have_ce) && len >=SHA256_BLOCK_SIZE && len <=65536 &&likely (may_use_simd ())) { -kernel_neon_begin (); -sha256_ce_finup2x (ctx, data1, data2, len, out1, out2); -kernel_neon_end (); + scoped_ksimd() +sha256_ce_finup2x (ctx, data1, data2, len, out1, out2);kmsan_unpoison_memory (out1,SHA256_DIGEST_SIZE );kmsan_unpoison_memory (out2,SHA256_DIGEST_SIZE );return true;
lib/crypto/arm64/sha512.h +4 -4
@@ -4,7 +4,7 @@* * Copyright 2025 Google LLC */ -#include <asm/neon.h> +#include <asm/simd.h> #include <linux/cpufeature.h>
@@ -24,9 +24,9 @@ static void sha512_blocks(struct sha512_block_state *state,do {size_t rem; -kernel_neon_begin (); - rem =__sha512_ce_transform (state, data, nblocks); -kernel_neon_end (); +scoped_ksimd () + rem = __sha512_ce_transform(state, data, nblocks); + data += (nblocks - rem) *SHA512_BLOCK_SIZE ; nblocks = rem; }while (nblocks);
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 10/21 crypto/arm64: aes-ccm - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/aes-ce-ccm-glue.c | 135 ++++++++++---------- 1 file changed, 66 insertions(+), 69 deletions(-)
arch/arm64/crypto/aes-ce-ccm-glue.c +66 -69
@@ -8,7 +8,6 @@* Author: Ard Biesheuvel <[email protected]> */ -#include <asm/neon.h> #include <linux/unaligned.h> #include <crypto/aes.h> #include <crypto/scatterwalk.h>
@@ -16,6 +15,8 @@#include <crypto/internal/skcipher.h> #include <linux/module.h> +#include <asm/simd.h> +#include "aes-ce-setkey.h" MODULE_IMPORT_NS ("CRYPTO_INTERNAL" );
@@ -184,40 +185,38 @@ static int ccm_encrypt(struct aead_request *req)if (unlikely (err))return err; -kernel_neon_begin (); - -if (req->assoclen) -ccm_calculate_auth_mac (req, mac); - -do { -u32 tail = walk.nbytes %AES_BLOCK_SIZE ; -const u8 *src = walk.src.virt.addr; -u8 *dst = walk.dst.virt.addr; -u8 buf[AES_BLOCK_SIZE ]; -u8 *final_iv =NULL ; - -if (walk.nbytes == walk.total) { - tail =0 ; - final_iv = orig_iv; - } - -if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) - src = dst =memcpy (&buf[sizeof (buf) - walk.nbytes], - src, walk.nbytes); - -ce_aes_ccm_encrypt (dst, src, walk.nbytes - tail, - ctx->key_enc,num_rounds (ctx), - mac, walk.iv, final_iv); - -if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) -memcpy (walk.dst.virt.addr, dst, walk.nbytes); - -if (walk.nbytes) { - err =skcipher_walk_done (&walk, tail); - } - }while (walk.nbytes); - -kernel_neon_end (); + scoped_ksimd() { +if (req->assoclen) +ccm_calculate_auth_mac (req, mac); + +do { +u32 tail = walk.nbytes %AES_BLOCK_SIZE ; +const u8 *src = walk.src.virt.addr; +u8 *dst = walk.dst.virt.addr; +u8 buf[AES_BLOCK_SIZE ]; +u8 *final_iv =NULL ; + +if (walk.nbytes == walk.total) { + tail =0 ; + final_iv = orig_iv; + } + +if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) + src = dst =memcpy (&buf[sizeof (buf) - walk.nbytes], + src, walk.nbytes); + +ce_aes_ccm_encrypt (dst, src, walk.nbytes - tail, + ctx->key_enc,num_rounds (ctx), + mac, walk.iv, final_iv); + +if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) +memcpy (walk.dst.virt.addr, dst, walk.nbytes); + +if (walk.nbytes) { + err =skcipher_walk_done (&walk, tail); + } + }while (walk.nbytes); + }if (unlikely (err))return err;
@@ -251,40 +250,38 @@ static int ccm_decrypt(struct aead_request *req)if (unlikely (err))return err; -kernel_neon_begin (); - -if (req->assoclen) -ccm_calculate_auth_mac (req, mac); - -do { -u32 tail = walk.nbytes %AES_BLOCK_SIZE ; -const u8 *src = walk.src.virt.addr; -u8 *dst = walk.dst.virt.addr; -u8 buf[AES_BLOCK_SIZE ]; -u8 *final_iv =NULL ; - -if (walk.nbytes == walk.total) { - tail =0 ; - final_iv = orig_iv; - } - -if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) - src = dst =memcpy (&buf[sizeof (buf) - walk.nbytes], - src, walk.nbytes); - -ce_aes_ccm_decrypt (dst, src, walk.nbytes - tail, - ctx->key_enc,num_rounds (ctx), - mac, walk.iv, final_iv); - -if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) -memcpy (walk.dst.virt.addr, dst, walk.nbytes); - -if (walk.nbytes) { - err =skcipher_walk_done (&walk, tail); - } - }while (walk.nbytes); - -kernel_neon_end (); + scoped_ksimd() { +if (req->assoclen) +ccm_calculate_auth_mac (req, mac); + +do { +u32 tail = walk.nbytes %AES_BLOCK_SIZE ; +const u8 *src = walk.src.virt.addr; +u8 *dst = walk.dst.virt.addr; +u8 buf[AES_BLOCK_SIZE ]; +u8 *final_iv =NULL ; + +if (walk.nbytes == walk.total) { + tail =0 ; + final_iv = orig_iv; + } + +if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) + src = dst =memcpy (&buf[sizeof (buf) - walk.nbytes], + src, walk.nbytes); + +ce_aes_ccm_decrypt (dst, src, walk.nbytes - tail, + ctx->key_enc,num_rounds (ctx), + mac, walk.iv, final_iv); + +if (unlikely (walk.nbytes <AES_BLOCK_SIZE )) +memcpy (walk.dst.virt.addr, dst, walk.nbytes); + +if (walk.nbytes) { + err =skcipher_walk_done (&walk, tail); + } + }while (walk.nbytes); + }if (unlikely (err))return err;
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 11/21 crypto/arm64: aes-blk - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/aes-ce-glue.c | 87 ++++++------ arch/arm64/crypto/aes-glue.c | 139 ++++++++---------- arch/arm64/crypto/aes-neonbs-glue.c | 150 ++++++++++---------- 3 files changed, 181 insertions(+), 195 deletions(-)
arch/arm64/crypto/aes-ce-glue.c +43 -44
@@ -52,9 +52,8 @@ static void aes_cipher_encrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])return ; } -kernel_neon_begin (); -__aes_ce_encrypt (ctx->key_enc, dst, src,num_rounds (ctx)); -kernel_neon_end (); + scoped_ksimd() +__aes_ce_encrypt (ctx->key_enc, dst, src,num_rounds (ctx)); }static void aes_cipher_decrypt (struct crypto_tfm *tfm,u8 dst[],u8 const src[])
@@ -66,9 +65,8 @@ static void aes_cipher_decrypt(struct crypto_tfm *tfm, u8 dst[], u8 const src[])return ; } -kernel_neon_begin (); -__aes_ce_decrypt (ctx->key_dec, dst, src,num_rounds (ctx)); -kernel_neon_end (); + scoped_ksimd() +__aes_ce_decrypt (ctx->key_dec, dst, src,num_rounds (ctx)); } int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
@@ -94,47 +92,48 @@ int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,for (i =0 ; i < kwords; i++) ctx->key_enc[i] =get_unaligned_le32 (in_key + i *sizeof (u32)); -kernel_neon_begin (); -for (i =0 ; i <sizeof (rcon); i++) { -u32 *rki = ctx->key_enc + (i * kwords); -u32 *rko = rki + kwords; - - rko[0 ] =ror32 (__aes_ce_sub (rki[kwords -1 ]),8 ) ^ rcon[i] ^ rki[0 ]; - rko[1 ] = rko[0 ] ^ rki[1 ]; - rko[2 ] = rko[1 ] ^ rki[2 ]; - rko[3 ] = rko[2 ] ^ rki[3 ]; - -if (key_len ==AES_KEYSIZE_192 ) { -if (i >=7 ) -break ; - rko[4 ] = rko[3 ] ^ rki[4 ]; - rko[5 ] = rko[4 ] ^ rki[5 ]; - }else if (key_len ==AES_KEYSIZE_256 ) { -if (i >=6 ) -break ; - rko[4 ] =__aes_ce_sub (rko[3 ]) ^ rki[4 ]; - rko[5 ] = rko[4 ] ^ rki[5 ]; - rko[6 ] = rko[5 ] ^ rki[6 ]; - rko[7 ] = rko[6 ] ^ rki[7 ]; + scoped_ksimd() { +for (i =0 ; i <sizeof (rcon); i++) { +u32 *rki = ctx->key_enc + (i * kwords); +u32 *rko = rki + kwords; + + rko[0 ] =ror32 (__aes_ce_sub (rki[kwords -1 ]),8 ) ^ + rcon[i] ^ rki[0 ]; + rko[1 ] = rko[0 ] ^ rki[1 ]; + rko[2 ] = rko[1 ] ^ rki[2 ]; + rko[3 ] = rko[2 ] ^ rki[3 ]; + +if (key_len ==AES_KEYSIZE_192 ) { +if (i >=7 ) +break ; + rko[4 ] = rko[3 ] ^ rki[4 ]; + rko[5 ] = rko[4 ] ^ rki[5 ]; + }else if (key_len ==AES_KEYSIZE_256 ) { +if (i >=6 ) +break ; + rko[4 ] =__aes_ce_sub (rko[3 ]) ^ rki[4 ]; + rko[5 ] = rko[4 ] ^ rki[5 ]; + rko[6 ] = rko[5 ] ^ rki[6 ]; + rko[7 ] = rko[6 ] ^ rki[7 ]; + } } - } -/* -* Generate the decryption keys for the Equivalent Inverse Cipher. -* This involves reversing the order of the round keys, and applying -* the Inverse Mix Columns transformation on all but the first and -* the last one. -*/ - key_enc = (struct aes_block *)ctx->key_enc; - key_dec = (struct aes_block *)ctx->key_dec; - j =num_rounds (ctx); - - key_dec[0 ] = key_enc[j]; -for (i =1 , j--; j >0 ; i++, j--) -__aes_ce_invert (key_dec + i, key_enc + j); - key_dec[i] = key_enc[0 ]; +/* +* Generate the decryption keys for the Equivalent Inverse +* Cipher. This involves reversing the order of the round +* keys, and applying the Inverse Mix Columns transformation on +* all but the first and the last one. +*/ + key_enc = (struct aes_block *)ctx->key_enc; + key_dec = (struct aes_block *)ctx->key_dec; + j =num_rounds (ctx); + + key_dec[0 ] = key_enc[j]; +for (i =1 , j--; j >0 ; i++, j--) +__aes_ce_invert (key_dec + i, key_enc + j); + key_dec[i] = key_enc[0 ]; + } -kernel_neon_end ();return 0 ; }EXPORT_SYMBOL (ce_aes_expandkey);
arch/arm64/crypto/aes-glue.c +63 -76
253 lines not on this page · raw message
arch/arm64/crypto/aes-neonbs-glue.c +75 -75
202 lines not on this page · raw message
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 12/21 crypto/arm64: aes-gcm - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/ghash-ce-glue.c | 27 ++++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-)
arch/arm64/crypto/ghash-ce-glue.c +13 -14
@@ -5,7 +5,6 @@* Copyright (C) 2014 - 2018 Linaro Ltd. <[email protected]> */ -#include <asm/neon.h> #include <crypto/aes.h> #include <crypto/b128ops.h> #include <crypto/gcm.h>
@@ -22,6 +21,8 @@#include <linux/string.h> #include <linux/unaligned.h> +#include <asm/simd.h> +MODULE_DESCRIPTION ("GHASH and AES-GCM using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );
@@ -74,9 +75,8 @@ void ghash_do_simd_update(int blocks, u64 dg[], const char *src, u64 const h[][2], const char *head)) { -kernel_neon_begin (); -simd_update (blocks, dg, src, key->h, head); -kernel_neon_end (); + scoped_ksimd() + simd_update(blocks, dg, src, key->h, head); }/* avoid hogging the CPU for too long */
@@ -329,11 +329,10 @@ static int gcm_encrypt(struct aead_request *req, char *iv, int assoclen) tag =NULL ; } -kernel_neon_begin (); -pmull_gcm_encrypt (nbytes, dst, src, ctx->ghash_key.h, - dg, iv, ctx->aes_key.key_enc, nrounds, - tag); -kernel_neon_end (); + scoped_ksimd() +pmull_gcm_encrypt (nbytes, dst, src, ctx->ghash_key.h, + dg, iv, ctx->aes_key.key_enc, nrounds, + tag);if (unlikely (!nbytes))break ;
@@ -399,11 +398,11 @@ static int gcm_decrypt(struct aead_request *req, char *iv, int assoclen) tag =NULL ; } -kernel_neon_begin (); - ret =pmull_gcm_decrypt (nbytes, dst, src, ctx->ghash_key.h, - dg, iv, ctx->aes_key.key_enc, - nrounds, tag, otag, authsize); -kernel_neon_end (); +scoped_ksimd () + ret = pmull_gcm_decrypt(nbytes, dst, src, + ctx->ghash_key.h, + dg, iv, ctx->aes_key.key_enc, + nrounds, tag, otag, authsize);if (unlikely (!nbytes))break ;
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 13/21 crypto/arm64: nhpoly1305 - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/nhpoly1305-neon-glue.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/crypto/nhpoly1305-neon-glue.c +2 -3
@@ -25,9 +25,8 @@ static int nhpoly1305_neon_update(struct shash_desc *desc,do { unsigned int n = min_t(unsigned int, srclen, SZ_4K); -kernel_neon_begin (); -crypto_nhpoly1305_update_helper (desc, src, n, nh_neon); -kernel_neon_end (); + scoped_ksimd() +crypto_nhpoly1305_update_helper (desc, src, n, nh_neon); src += n; srclen -= n; }while (srclen);
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 14/21 crypto/arm64: polyval - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/polyval-ce-glue.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
arch/arm64/crypto/polyval-ce-glue.c +5 -7
@@ -15,7 +15,7 @@* ARMv8 Crypto Extensions instructions to implement the finite field operations. */ -#include <asm/neon.h> +#include <asm/simd.h> #include <crypto/internal/hash.h> #include <crypto/polyval.h> #include <crypto/utils.h>
@@ -45,16 +45,14 @@ asmlinkage void pmull_polyval_mul(u8 *op1, const u8 *op2);static void internal_polyval_update (const struct polyval_tfm_ctx *keys,const u8 *in,size_t nblocks,u8 *accumulator) { -kernel_neon_begin (); -pmull_polyval_update (keys, in, nblocks, accumulator); -kernel_neon_end (); + scoped_ksimd() +pmull_polyval_update (keys, in, nblocks, accumulator); }static void internal_polyval_mul (u8 *op1,const u8 *op2) { -kernel_neon_begin (); -pmull_polyval_mul (op1, op2); -kernel_neon_end (); + scoped_ksimd() +pmull_polyval_mul (op1, op2); } static int polyval_arm64_setkey(struct crypto_shash *tfm,
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 15/21 crypto/arm64: sha3 - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/sha3-ce-glue.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
arch/arm64/crypto/sha3-ce-glue.c +4 -6
@@ -46,9 +46,8 @@ static int sha3_update(struct shash_desc *desc, const u8 *data,do {int rem; -kernel_neon_begin (); - rem =sha3_ce_transform (sctx->st, data, blocks, ds); -kernel_neon_end (); +scoped_ksimd () + rem = sha3_ce_transform(sctx->st, data, blocks, ds); data += (blocks - rem) * bs; blocks = rem; }while (blocks);
@@ -73,9 +72,8 @@ static int sha3_finup(struct shash_desc *desc, const u8 *src, unsigned int len,memset (block + len,0 , bs - len); block[bs -1 ] |=0x80 ; -kernel_neon_begin (); -sha3_ce_transform (sctx->st, block,1 , ds); -kernel_neon_end (); + scoped_ksimd() +sha3_ce_transform (sctx->st, block,1 , ds);memzero_explicit (block ,sizeof (block));for (i =0 ; i < ds /8 ; i++)
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 16/21 crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/sm3-ce-glue.c | 15 ++++++++------- arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-)
arch/arm64/crypto/sm3-ce-glue.c +8 -7
@@ -5,7 +5,6 @@* Copyright (C) 2018 Linaro Ltd <[email protected]> */ -#include <asm/neon.h> #include <crypto/internal/hash.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h>
@@ -13,6 +12,8 @@#include <linux/kernel.h> #include <linux/module.h> +#include <asm/simd.h> +MODULE_DESCRIPTION ("SM3 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );
@@ -25,18 +26,18 @@ static int sm3_ce_update(struct shash_desc *desc, const u8 *data, {int remain; -kernel_neon_begin (); - remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform); -kernel_neon_end (); + scoped_ksimd() { + remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform); + }return remain; }static int sm3_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -kernel_neon_begin (); -sm3_base_do_finup (desc, data, len, sm3_ce_transform); -kernel_neon_end (); + scoped_ksimd() { +sm3_base_do_finup (desc, data, len, sm3_ce_transform); + }return sm3_base_finish (desc, out); }
arch/arm64/crypto/sm3-neon-glue.c +6 -10
@@ -5,7 +5,7 @@* Copyright (C) 2022 Tianjia Zhang <[email protected]> */ -#include <asm/neon.h> +#include <asm/simd.h> #include <crypto/internal/hash.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h>
@@ -20,20 +20,16 @@ asmlinkage void sm3_neon_transform(struct sm3_state *sst, u8 const *src,static int sm3_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -int remain; - -kernel_neon_begin (); - remain =sm3_base_do_update_blocks (desc, data, len, sm3_neon_transform); -kernel_neon_end (); -return remain; + scoped_ksimd() +return sm3_base_do_update_blocks (desc, data, len, + sm3_neon_transform); }static int sm3_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -kernel_neon_begin (); -sm3_base_do_finup (desc, data, len, sm3_neon_transform); -kernel_neon_end (); + scoped_ksimd() +sm3_base_do_finup (desc, data, len, sm3_neon_transform);return sm3_base_finish (desc, out); }
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
Quoted 10 lines · On Fri, 31 Oct 2025 11:39:15 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Switch to the more abstract '
scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of structtask_struct.Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>
Hi Ard,
Trivial comment inline.
Quoted 27 lines
--- arch/arm64/crypto/sm3-ce-glue.c | 15 ++++++++------- arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-)arch/arm64/crypto/sm3-ce-glue.c +2
-1@@ -5,7 +5,6 @@* Copyright (C) 2018 Linaro Ltd <[email protected]> */ -#include <asm/neon.h> #include <crypto/internal/hash.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> @@ -13,6 +12,8 @@#include <linux/kernel.h> #include <linux/module.h> +#include <asm/simd.h> +MODULE_DESCRIPTION ("SM3 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );
arch/arm64/crypto/sm3-ce-glue.c static int sm3_ce_update(struct shash_desc *desc, const u8 *data, · line 29
Quoted 7 lines
arch/arm64/crypto/sm3-ce-glue.c +1
-3@@ -25,18 +26,18 @@ static int sm3_ce_update(struct shash_desc *desc, const u8 *data, {int remain; -kernel_neon_begin (); - remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform); -kernel_neon_end (); + scoped_ksimd() {
Why does this get brackets unlike other cases?
Quoted 56 lines
arch/arm64/crypto/sm3-ce-glue.c +5
-3+ remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform); + }return remain; }static int sm3_ce_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -kernel_neon_begin (); -sm3_base_do_finup (desc, data, len, sm3_ce_transform); -kernel_neon_end (); + scoped_ksimd() { +sm3_base_do_finup (desc, data, len, sm3_ce_transform); + }return sm3_base_finish (desc, out); }arch/arm64/crypto/sm3-neon-glue.c +6
-10@@ -5,7 +5,7 @@* Copyright (C) 2022 Tianjia Zhang <[email protected]> */ -#include <asm/neon.h> +#include <asm/simd.h> #include <crypto/internal/hash.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> @@ -20,20 +20,16 @@ asmlinkage void sm3_neon_transform(struct sm3_state *sst, u8 const *src,static int sm3_neon_update (struct shash_desc *desc,const u8 *data,unsigned int len) { -int remain; - -kernel_neon_begin (); - remain =sm3_base_do_update_blocks (desc, data, len, sm3_neon_transform); -kernel_neon_end (); -return remain; + scoped_ksimd() +return sm3_base_do_update_blocks (desc, data, len, + sm3_neon_transform); }static int sm3_neon_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *out) { -kernel_neon_begin (); -sm3_base_do_finup (desc, data, len, sm3_neon_transform); -kernel_neon_end (); + scoped_ksimd() +sm3_base_do_finup (desc, data, len, sm3_neon_transform);return sm3_base_finish (desc, out); }
Ard Biesheuvel
maintainerRe: [PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
Quoted 48 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 31 Oct 2025 11:39:15 +0100 Ard Biesheuvel <[email protected]> wrote:
From: Ard Biesheuvel <[email protected]>
Switch to the more abstract '
scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of structtask_struct.Reviewed-by Eric Biggers <[email protected]>Signed-off-by Ard Biesheuvel <[email protected]>Hi Ard,
Trivial comment inline.
Quoted 30 lines
--- arch/arm64/crypto/sm3-ce-glue.c | 15 ++++++++------- arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-)arch/arm64/crypto/sm3-ce-glue.c +2
-1@@ -5,7 +5,6 @@* Copyright (C) 2018 Linaro Ltd <[email protected]> */ -#include <asm/neon.h> #include <crypto/internal/hash.h> #include <crypto/sm3.h> #include <crypto/sm3_base.h> @@ -13,6 +12,8 @@#include <linux/kernel.h> #include <linux/module.h> +#include <asm/simd.h> +MODULE_DESCRIPTION ("SM3 secure hash using ARMv8 Crypto Extensions" );MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_LICENSE ("GPL v2" );@@ -25,18 +26,18 @@ static int sm3_ce_update(struct shash_desc *desc, const u8 *data, {int remain;
Quoted 7 lines
arch/arm64/crypto/sm3-ce-glue.c static int sm3_ce_update(struct shash_desc *desc, const u8 *data, · line 29
Quoted 4 lines
arch/arm64/crypto/sm3-ce-glue.c +1
-3-kernel_neon_begin (); - remain =sm3_base_do_update_blocks (desc, data, len, sm3_ce_transform); -kernel_neon_end (); + scoped_ksimd() {Why does this get brackets unlike other cases?
No reason other than the fact that some time passed between writing the other patches and these ones, and there is no functional difference.
PATCH v4 17/21 crypto/arm64: sm4 - Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/crypto/sm4-ce-ccm-glue.c | 38 ++-- arch/arm64/crypto/sm4-ce-cipher-glue.c | 10 +- arch/arm64/crypto/sm4-ce-gcm-glue.c | 53 +++-- arch/arm64/crypto/sm4-ce-glue.c | 214 +++++++++----------- arch/arm64/crypto/sm4-neon-glue.c | 25 +-- 5 files changed, 149 insertions(+), 191 deletions(-)
arch/arm64/crypto/sm4-ce-ccm-glue.c +17 -21
@@ -11,7 +11,7 @@#include <linux/crypto.h> #include <linux/kernel.h> #include <linux/cpufeature.h> -#include <asm/neon.h> +#include <asm/simd.h> #include <crypto/scatterwalk.h> #include <crypto/internal/aead.h> #include <crypto/internal/skcipher.h>
@@ -35,10 +35,9 @@ static int ccm_setkey(struct crypto_aead *tfm, const u8 *key,if (key_len !=SM4_KEY_SIZE )return -EINVAL ; -kernel_neon_begin (); -sm4_ce_expand_key (key, ctx->rkey_enc, ctx->rkey_dec, - crypto_sm4_fk, crypto_sm4_ck); -kernel_neon_end (); + scoped_ksimd() +sm4_ce_expand_key (key, ctx->rkey_enc, ctx->rkey_dec, + crypto_sm4_fk, crypto_sm4_ck);return 0 ; }
@@ -167,28 +166,25 @@ static int ccm_crypt(struct aead_request *req, struct skcipher_walk *walk,memcpy (ctr0, walk->iv,SM4_BLOCK_SIZE );crypto_inc (walk->iv,SM4_BLOCK_SIZE ); -kernel_neon_begin (); + scoped_ksimd() { +if (req->assoclen) +ccm_calculate_auth_mac (req, mac); -if (req->assoclen) -ccm_calculate_auth_mac (req, mac); - -while (walk->nbytes) { -unsigned int tail = walk->nbytes %SM4_BLOCK_SIZE ; +while (walk->nbytes) { +unsigned int tail = walk->nbytes %SM4_BLOCK_SIZE ; -if (walk->nbytes == walk->total) - tail =0 ; +if (walk->nbytes == walk->total) + tail =0 ; -sm4_ce_ccm_crypt (rkey_enc, walk->dst.virt.addr, - walk->src.virt.addr, walk->iv, - walk->nbytes - tail, mac); +sm4_ce_ccm_crypt (rkey_enc, walk->dst.virt.addr, + walk->src.virt.addr, walk->iv, + walk->nbytes - tail, mac); - err =skcipher_walk_done (walk, tail); + err =skcipher_walk_done (walk, tail); + } +sm4_ce_ccm_final (rkey_enc, ctr0, mac); } -sm4_ce_ccm_final (rkey_enc, ctr0, mac); - -kernel_neon_end (); -return err; }
arch/arm64/crypto/sm4-ce-cipher-glue.c +4 -6
@@ -32,9 +32,8 @@ static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)if (!crypto_simd_usable ()) {sm4_crypt_block (ctx->rkey_enc, out, in); }else { -kernel_neon_begin (); -sm4_ce_do_crypt (ctx->rkey_enc, out, in); -kernel_neon_end (); + scoped_ksimd() +sm4_ce_do_crypt (ctx->rkey_enc, out, in); } }
@@ -45,9 +44,8 @@ static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)if (!crypto_simd_usable ()) {sm4_crypt_block (ctx->rkey_dec, out, in); }else { -kernel_neon_begin (); -sm4_ce_do_crypt (ctx->rkey_dec, out, in); -kernel_neon_end (); + scoped_ksimd() +sm4_ce_do_crypt (ctx->rkey_dec, out, in); } }
arch/arm64/crypto/sm4-ce-gcm-glue.c +24 -29
75 lines not on this page · raw message
arch/arm64/crypto/sm4-ce-glue.c +96 -118
311 lines not on this page · raw message
arch/arm64/crypto/sm4-neon-glue.c +8 -17
@@ -48,11 +48,8 @@ static int sm4_ecb_do_crypt(struct skcipher_request *req, const u32 *rkey) nblocks = nbytes /SM4_BLOCK_SIZE ;if (nblocks) { -kernel_neon_begin (); - -sm4_neon_crypt (rkey, dst, src, nblocks); - -kernel_neon_end (); + scoped_ksimd() +sm4_neon_crypt (rkey, dst, src, nblocks); } err =skcipher_walk_done (&walk, nbytes %SM4_BLOCK_SIZE );
@@ -126,12 +123,9 @@ static int sm4_cbc_decrypt(struct skcipher_request *req) nblocks = nbytes /SM4_BLOCK_SIZE ;if (nblocks) { -kernel_neon_begin (); - -sm4_neon_cbc_dec (ctx->rkey_dec, dst, src, - walk.iv, nblocks); - -kernel_neon_end (); + scoped_ksimd() +sm4_neon_cbc_dec (ctx->rkey_dec, dst, src, + walk.iv, nblocks); } err =skcipher_walk_done (&walk, nbytes %SM4_BLOCK_SIZE );
@@ -157,12 +151,9 @@ static int sm4_ctr_crypt(struct skcipher_request *req) nblocks = nbytes /SM4_BLOCK_SIZE ;if (nblocks) { -kernel_neon_begin (); - -sm4_neon_ctr_crypt (ctx->rkey_enc, dst, src, - walk.iv, nblocks); - -kernel_neon_end (); + scoped_ksimd() +sm4_neon_ctr_crypt (ctx->rkey_enc, dst, src, + walk.iv, nblocks); dst += nblocks *SM4_BLOCK_SIZE ; src += nblocks *SM4_BLOCK_SIZE ;
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 18/21 arm64/xorblocks: Switch to 'ksimd' scoped guard API
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Switch to the more abstract 'scoped_ksimd()' API, which will be modified in a future patch to transparently allocate a kernel mode FP/SIMD state buffer on the stack, so that kernel mode FP/SIMD code remains preemptible in principe, but without the memory overhead that adds 528 bytes to the size of struct task_struct.
--- arch/arm64/include/asm/xor.h | 22 ++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-)
arch/arm64/include/asm/xor.h +9 -13
@@ -9,7 +9,7 @@#include <linux/hardirq.h> #include <asm-generic/xor.h> #include <asm/hwcap.h> -#include <asm/neon.h> +#include <asm/simd.h> #ifdef CONFIG_KERNEL_MODE_NEON
@@ -19,9 +19,8 @@ static void xor_neon_2(unsigned long bytes, unsigned long * __restrict p1, const unsigned long * __restrict p2) { -kernel_neon_begin (); - xor_block_inner_neon.do_2 (bytes, p1, p2); -kernel_neon_end (); + scoped_ksimd() + xor_block_inner_neon.do_2(bytes, p1, p2); } static void
@@ -29,9 +28,8 @@ xor_neon_3(unsigned long bytes, unsigned long * __restrict p1, const unsigned long * __restrict p2, const unsigned long * __restrict p3) { -kernel_neon_begin (); - xor_block_inner_neon.do_3 (bytes, p1, p2, p3); -kernel_neon_end (); + scoped_ksimd() + xor_block_inner_neon.do_3(bytes, p1, p2, p3); } static void
@@ -40,9 +38,8 @@ xor_neon_4(unsigned long bytes, unsigned long * __restrict p1, const unsigned long * __restrict p3, const unsigned long * __restrict p4) { -kernel_neon_begin (); - xor_block_inner_neon.do_4 (bytes, p1, p2, p3, p4); -kernel_neon_end (); + scoped_ksimd() + xor_block_inner_neon.do_4(bytes, p1, p2, p3, p4); } static void
@@ -52,9 +49,8 @@ xor_neon_5(unsigned long bytes, unsigned long * __restrict p1, const unsigned long * __restrict p4, const unsigned long * __restrict p5) { -kernel_neon_begin (); - xor_block_inner_neon.do_5 (bytes, p1, p2, p3, p4, p5); -kernel_neon_end (); + scoped_ksimd() + xor_block_inner_neon.do_5(bytes, p1, p2, p3, p4, p5); } static struct xor_block_template xor_block_arm64 = {
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 19/21 net/mlx5: Switch to more abstract scoped ksimd guard API on arm64
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Instead of calling kernel_neon_begin/end directly, switch to the scoped guard API which encapsulates those calls. This is needed because the prototypes of those APIs are going to be modified and will require a kernel mode FP/SIMD buffer to be provided, which the scoped guard API will do transparently.
--- drivers/net/ethernet/mellanox/mlx5/core/wc.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/wc.c +10 -9
@@ -9,6 +9,7 @@#if IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&IS_ENABLED (CONFIG_ARM64 )#include <asm/neon.h> +#include <asm/simd.h> #endif #define TEST_WC_NUM_WQES 255
@@ -264,15 +265,15 @@ static void mlx5_iowrite64_copy(struct mlx5_wc_sq *sq, __be32 mmio_wqe[16], {#if IS_ENABLED (CONFIG_KERNEL_MODE_NEON ) &&IS_ENABLED (CONFIG_ARM64 )if (cpu_has_neon ()) { -kernel_neon_begin (); - asmvolatile - (".arch_extension simd\n\t" -"ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [%0]\n\t" -"st1 {v0.16b, v1.16b, v2.16b, v3.16b}, [%1]" - : - :"r" (mmio_wqe),"r" (sq->bfreg.map + offset) - :"memory" ,"v0" ,"v1" ,"v2" ,"v3" ); -kernel_neon_end (); + scoped_ksimd() { + asmvolatile ( +".arch_extension simd\n\t" +"ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [%0]\n\t" +"st1 {v0.16b, v1.16b, v2.16b, v3.16b}, [%1]" + : + :"r" (mmio_wqe),"r" (sq->bfreg.map + offset) + :"memory" ,"v0" ,"v1" ,"v2" ,"v3" ); + }return ; }#endif
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 20/21 arm64/fpu: Enforce task-context only for generic kernel mode FPU
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The generic kernel mode FPU API, which is used by the AMDGPU driver to perform floating point calculations, is modeled after the most restrictive architecture that supports it. This means it doesn't support preemption, and can only be used from task context.
The arm64 implementation is a bit more flexible, but supporting that in the generic API complicates matters slightly, and for no good reason, given that the only user does not need it.
So enforce that kernel_fpu_begin() can only be called from task context, and [redundantly] disable preemption. This removes the need for users of this API to provide a kernel mode FP/SIMD state after a future patch that makes that compulsory for preemptible task context.
--- arch/arm64/include/asm/fpu.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
arch/arm64/include/asm/fpu.h +14 -2
@@ -6,10 +6,22 @@#ifndef __ASM_FPU_H#define __ASM_FPU_H +#include <linux/preempt.h> #include <asm/neon.h> #define kernel_fpu_available () cpu_has_neon() -#define kernel_fpu_begin () kernel_neon_begin() -#define kernel_fpu_end () kernel_neon_end() + +static inline void kernel_fpu_begin (void ) +{ +BUG_ON (!in_task ()); +preempt_disable (); +kernel_neon_begin (); +} + +static inline void kernel_fpu_end (void ) +{ +kernel_neon_end (); +preempt_enable (); +}#endif /* ! __ASM_FPU_H */
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
PATCH v4 21/21 arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Commit aefbab8e77eb16b5
("arm64: fpsimd: Preserve/restore kernel mode NEON at context switch")
added a 'kernel_fpsimd_state' field to struct thread_struct, which is the arch-specific portion of struct task_struct, and is allocated for each task in the system. The size of this field is 528 bytes, resulting in non-negligible bloat of task_struct, and the resulting memory overhead may impact performance on systems with many processes.
This allocation is only used if the task is scheduled out or interrupted by a softirq while using the FP/SIMD unit in kernel mode, and so it is possible to transparently allocate this buffer on the caller's stack instead.
So tweak the 'ksimd' scoped guard implementation so that a stack buffer is allocated and passed to both kernel_neon_begin() and kernel_neon_end(), and either record it in the task struct, or use it directly to preserve the task mode kernel FP/SIMD when running in softirq context. Passing the address to both functions, and checking the addresses for consistency ensures that callers of the updated bare begin/end API use it in a manner that is consistent with the new context switch semantics.
--- arch/arm64/include/asm/fpu.h | 4 +- arch/arm64/include/asm/neon.h | 4 +- arch/arm64/include/asm/processor.h | 7 ++- arch/arm64/include/asm/simd.h | 7 ++- arch/arm64/kernel/fpsimd.c | 53 ++++++++++++++------ 5 files changed, 54 insertions(+), 21 deletions(-)
arch/arm64/include/asm/fpu.h +2 -2
@@ -15,12 +15,12 @@ static inline void kernel_fpu_begin(void) {BUG_ON (!in_task ());preempt_disable (); -kernel_neon_begin (); +kernel_neon_begin (NULL ); }static inline void kernel_fpu_end (void ) { -kernel_neon_end (); +kernel_neon_end (NULL );preempt_enable (); }
arch/arm64/include/asm/neon.h +2 -2
@@ -13,7 +13,7 @@#define cpu_has_neon () system_supports_fpsimd() -void kernel_neon_begin (void ); -void kernel_neon_end (void ); +void kernel_neon_begin (struct user_fpsimd_state *); +void kernel_neon_end (struct user_fpsimd_state *);#endif /* ! __ASM_NEON_H */
arch/arm64/include/asm/processor.h +6 -1
@@ -172,7 +172,12 @@ struct thread_struct {unsigned long fault_code;/* ESR_EL1 value */ struct debug_info debug;/* debugging */ -struct user_fpsimd_state kernel_fpsimd_state; +/* +* Set [cleared] by kernel_neon_begin() [kernel_neon_end()] to the +* address of a caller provided buffer that will be used to preserve a +* task's kernel mode FPSIMD state while it is scheduled out. +*/ +struct user_fpsimd_state *kernel_fpsimd_state;unsigned int kernel_fpsimd_cpu;#ifdef CONFIG_ARM64_PTR_AUTH struct ptrauth_keys_user keys_user;
arch/arm64/include/asm/simd.h +5 -2
@@ -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){})#endif
arch/arm64/kernel/fpsimd.c +39 -14
125 lines not on this page · raw message
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Ard Biesheuvel
maintainerRe: [PATCH v4 21/21] arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack
Quoted 172 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Commit
aefbab8e77eb16b5("arm64: fpsimd: Preserve/restore kernel mode NEON at context switch")
added a '
kernel_fpsimd_state' field to structthread_struct, which is the arch-specific portion of structtask_struct, and is allocated for each task in the system. The size of this field is 528 bytes, resulting in non-negligible bloat oftask_struct, and the resulting memory overhead may impact performance on systems with many processes.This allocation is only used if the task is scheduled out or interrupted by a softirq while using the FP/SIMD unit in kernel mode, and so it is possible to transparently allocate this buffer on the caller's stack instead.
So tweak the 'ksimd' scoped guard implementation so that a stack buffer is allocated and passed to both
kernel_neon_begin()andkernel_neon_end(), and either record it in the task struct, or use it directly to preserve the task mode kernel FP/SIMD when running in softirq context. Passing the address to both functions, and checking the addresses for consistency ensures that callers of the updated bare begin/end API use it in a manner that is consistent with the new context switch semantics.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/arm64/include/asm/fpu.h | 4 +- arch/arm64/include/asm/neon.h | 4 +- arch/arm64/include/asm/processor.h | 7 ++- arch/arm64/include/asm/simd.h | 7 ++- arch/arm64/kernel/fpsimd.c | 53 ++++++++++++++------ 5 files changed, 54 insertions(+), 21 deletions(-)arch/arm64/include/asm/fpu.h +2
-2@@ -15,12 +15,12 @@ static inline void kernel_fpu_begin(void) {BUG_ON (!in_task ());preempt_disable (); -kernel_neon_begin (); +kernel_neon_begin (NULL ); }static inline void kernel_fpu_end (void ) { -kernel_neon_end (); +kernel_neon_end (NULL );preempt_enable (); }arch/arm64/include/asm/neon.h +2
-2@@ -13,7 +13,7 @@#define cpu_has_neon () system_supports_fpsimd() -void kernel_neon_begin (void ); -void kernel_neon_end (void ); +void kernel_neon_begin (struct user_fpsimd_state *); +void kernel_neon_end (struct user_fpsimd_state *);#endif /* ! __ASM_NEON_H */ arch/arm64/include/asm/processor.h +6
-1@@ -172,7 +172,12 @@ struct thread_struct {unsigned long fault_code;/* ESR_EL1 value */ struct debug_info debug;/* debugging */ -struct user_fpsimd_state kernel_fpsimd_state; +/* +* Set [cleared] by kernel_neon_begin() [kernel_neon_end()] to the +* address of a caller provided buffer that will be used to preserve a +* task's kernel mode FPSIMD state while it is scheduled out. +*/ +struct user_fpsimd_state *kernel_fpsimd_state;unsigned int kernel_fpsimd_cpu;#ifdef CONFIG_ARM64_PTR_AUTH struct ptrauth_keys_user keys_user;arch/arm64/include/asm/simd.h +5
-2@@ -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){})#endif arch/arm64/kernel/fpsimd.c +14
-6@@ -1489,21 +1489,23 @@ static void fpsimd_load_kernel_state(struct task_struct *task)* Elide the load if this CPU holds the most recent kernel mode * FPSIMD context of the current task. */ -if (last->st == &task->thread.kernel_fpsimd_state && +if (last->st == task->thread.kernel_fpsimd_state && task->thread.kernel_fpsimd_cpu ==smp_processor_id ())return ; -fpsimd_load_state (&task->thread.kernel_fpsimd_state); +fpsimd_load_state (task->thread.kernel_fpsimd_state); }static void fpsimd_save_kernel_state (struct task_struct *task) {struct cpu_fp_state cpu_fp_state = { - .st = &task->thread.kernel_fpsimd_state, + .st = task->thread.kernel_fpsimd_state, .to_save =FP_STATE_FPSIMD , }; -fpsimd_save_state (&task->thread.kernel_fpsimd_state); +BUG_ON (!cpu_fp_state.st); + +fpsimd_save_state (task->thread.kernel_fpsimd_state);fpsimd_bind_state_to_cpu (&cpu_fp_state); task->thread.kernel_fpsimd_cpu =smp_processor_id ();@@ -1774,6 +1776,7 @@ void fpsimd_update_current_state(struct user_fpsimd_state const *state)void fpsimd_flush_task_state (struct task_struct *t) { t->thread.fpsimd_cpu =NR_CPUS ; + t->thread.kernel_fpsimd_state =NULL ;/* * If we don't support fpsimd, bail out after we have * reset the fpsimd_cpu for this task and clear the @@ -1833,8 +1836,13 @@ void fpsimd_save_and_flush_cpu_state(void)* * The caller may freely use the FPSIMD registers until kernel_neon_end() is * called. +* +* Unless called from non-preemptible task context, @state must point to a +* caller provided buffer that will be used to preserve the task's kernel mode +* FPSIMD context when it is scheduled out, or if it is interrupted by kernel +* mode FPSIMD occurring in softirq context. May be %NULL otherwise. */ -void kernel_neon_begin (void ) +void kernel_neon_begin (struct user_fpsimd_state *state) {if (WARN_ON (!system_supports_fpsimd ()))return ;@@ -1846,7 +1854,7 @@ void kernel_neon_begin(void)/* Save unsaved fpsimd state, if any: */ if (test_thread_flag (TIF_KERNEL_FPSTATE )) {BUG_ON (IS_ENABLED (CONFIG_PREEMPT_RT ) || !in_serving_softirq ()); -fpsimd_save_kernel_state (current); +fpsimd_save_state (state); }else {fpsimd_save_user_state ();@@ -1867,8 +1875,17 @@ void kernel_neon_begin(void)* mode in task context. So in this case, setting the flag here * is always appropriate.
arch/arm64/kernel/fpsimd.c void kernel_neon_begin(void) · line 1885
Quoted 10 lines
arch/arm64/kernel/fpsimd.c +8
-1*/ -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. +*/ +WARN_ON (current->thread.kernel_fpsimd_state !=NULL ); +WARN_ON (preemptible () && !state);
This is in the wrong place: we are never preemptible here, even when called from preemptible context.
Will fix for the next rev.
Quoted 14 lines · Ard Biesheuvel
Ard Biesheuvel (21):
crypto/arm64: aes-ce-ccm - Avoid pointless yield of the NEON unitcrypto/arm64: sm4-ce-ccm - Avoid pointless yield of the NEON unitcrypto/arm64: sm4-ce-gcm - Avoid pointless yield of the NEON unit arm64/simd: Add scoped guard API for kernel mode SIMD ARM/simd: Add scoped guard API for kernel mode SIMD
crypto: aegis128-neon - Move to more abstract 'ksimd' guard API
raid6: Move to more abstract 'ksimd' guard API lib/crc: Switch ARM and arm64 to 'ksimd' scoped guard API lib/crypto: Switch ARM and arm64 to 'ksimd' scoped guard APIcrypto/arm64: aes-ccm - Switch to 'ksimd' scoped guard APIcrypto/arm64: aes-blk - Switch to 'ksimd' scoped guard APIcrypto/arm64: aes-gcm - Switch to 'ksimd' scoped guard APIcrypto/arm64: nhpoly1305 - Switch to 'ksimd' scoped guard API
Quoted 8 lines
crypto/arm64: polyval - Switch to 'ksimd' scoped guard APIcrypto/arm64: sha3 - Switch to 'ksimd' scoped guard APIcrypto/arm64: sm3 - Switch to 'ksimd' scoped guard APIcrypto/arm64: sm4 - Switch to 'ksimd' scoped guard API arm64/xorblocks: Switch to 'ksimd' scoped guard API net/mlx5: Switch to more abstract scoped ksimd guard API on arm64 arm64/fpu: Enforce task-context only for generic kernel mode FPU arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack
For the series, especially the arm64 bits:
Since most of this is crypto library changes, I'm fine for it to go upstream via your tree but please keep it on a stable branch in case we need to solve any conflicts.
Thanks.
Signature · 1 line
Catalin