[PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API

Ard Biesheuvel posted 21 patches 1 month, 2 weeks ago
[PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
Posted by Ard Biesheuvel 1 month, 2 weeks ago
From: Ard Biesheuvel <ardb@kernel.org>

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.

Reviewed-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/arm64/crypto/sm3-ce-glue.c   | 15 ++++++++-------
 arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++----------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/arch/arm64/crypto/sm3-ce-glue.c b/arch/arm64/crypto/sm3-ce-glue.c
index eac6f5fa0abe..24c1fcfae072 100644
--- a/arch/arm64/crypto/sm3-ce-glue.c
+++ b/arch/arm64/crypto/sm3-ce-glue.c
@@ -5,7 +5,6 @@
  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
  */
 
-#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 <ard.biesheuvel@linaro.org>");
 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);
 }
 
diff --git a/arch/arm64/crypto/sm3-neon-glue.c b/arch/arm64/crypto/sm3-neon-glue.c
index 6c4611a503a3..15f30cc24f32 100644
--- a/arch/arm64/crypto/sm3-neon-glue.c
+++ b/arch/arm64/crypto/sm3-neon-glue.c
@@ -5,7 +5,7 @@
  * Copyright (C) 2022 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
  */
 
-#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);
 }
 
-- 
2.51.1.930.gacf6e81ea2-goog
Re: [PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
Posted by Jonathan Cameron 1 month, 2 weeks ago
On Fri, 31 Oct 2025 11:39:15 +0100
Ard Biesheuvel <ardb+git@google.com> wrote:

> From: Ard Biesheuvel <ardb@kernel.org>
> 
> 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.
> 
> Reviewed-by: Eric Biggers <ebiggers@kernel.org>
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Hi Ard,

Trivial comment inline.

> ---
>  arch/arm64/crypto/sm3-ce-glue.c   | 15 ++++++++-------
>  arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++----------
>  2 files changed, 14 insertions(+), 17 deletions(-)
> 
> diff --git a/arch/arm64/crypto/sm3-ce-glue.c b/arch/arm64/crypto/sm3-ce-glue.c
> index eac6f5fa0abe..24c1fcfae072 100644
> --- a/arch/arm64/crypto/sm3-ce-glue.c
> +++ b/arch/arm64/crypto/sm3-ce-glue.c
> @@ -5,7 +5,6 @@
>   * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
>   */
>  
> -#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 <ard.biesheuvel@linaro.org>");
>  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() {

Why does this get brackets unlike other cases?

> +		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);
>  }
>  
> diff --git a/arch/arm64/crypto/sm3-neon-glue.c b/arch/arm64/crypto/sm3-neon-glue.c
> index 6c4611a503a3..15f30cc24f32 100644
> --- a/arch/arm64/crypto/sm3-neon-glue.c
> +++ b/arch/arm64/crypto/sm3-neon-glue.c
> @@ -5,7 +5,7 @@
>   * Copyright (C) 2022 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
>   */
>  
> -#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);
>  }
>
Re: [PATCH v4 16/21] crypto/arm64: sm3 - Switch to 'ksimd' scoped guard API
Posted by Ard Biesheuvel 1 month, 2 weeks ago
On Fri, 31 Oct 2025 at 14:52, Jonathan Cameron
<jonathan.cameron@huawei.com> wrote:
>
> On Fri, 31 Oct 2025 11:39:15 +0100
> Ard Biesheuvel <ardb+git@google.com> wrote:
>
> > From: Ard Biesheuvel <ardb@kernel.org>
> >
> > 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.
> >
> > Reviewed-by: Eric Biggers <ebiggers@kernel.org>
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> Hi Ard,
>
> Trivial comment inline.
>
> > ---
> >  arch/arm64/crypto/sm3-ce-glue.c   | 15 ++++++++-------
> >  arch/arm64/crypto/sm3-neon-glue.c | 16 ++++++----------
> >  2 files changed, 14 insertions(+), 17 deletions(-)
> >
> > diff --git a/arch/arm64/crypto/sm3-ce-glue.c b/arch/arm64/crypto/sm3-ce-glue.c
> > index eac6f5fa0abe..24c1fcfae072 100644
> > --- a/arch/arm64/crypto/sm3-ce-glue.c
> > +++ b/arch/arm64/crypto/sm3-ce-glue.c
> > @@ -5,7 +5,6 @@
> >   * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
> >   */
> >
> > -#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 <ard.biesheuvel@linaro.org>");
> >  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() {
>
> 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.