[RFC PATCH 4/7] crypto: x86/sm3 - limit FPU preemption

From: Robert Elliott
Date: Thu Oct 06 2022 - 18:32:41 EST


As done by the ECB and CBC helpers in arch/x86/crypt/ecb_cbc_helpers.h,
limit the number of bytes processed between kernel_fpu_begin() and
kernel_fpu_end() calls.

Those functions call preempt_disable() and preempt_enable(), so
the CPU core is unavailable for scheduling while running, causing:
rcu: INFO: rcu_preempt detected expedited stalls on CPUs/tasks: {12-... } 22 jiffies s: 277 root: 0x1/.

Fixes: 930ab34d906d ("crypto: x86/sm3 - add AVX assembly implementation")
Suggested-by: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Robert Elliott <elliott@xxxxxxx>
---
arch/x86/crypto/sm3_avx_glue.c | 28 +++++++++++++++++++++++-----
1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/arch/x86/crypto/sm3_avx_glue.c b/arch/x86/crypto/sm3_avx_glue.c
index bfd11da956a4..adb9e55e2a16 100644
--- a/arch/x86/crypto/sm3_avx_glue.c
+++ b/arch/x86/crypto/sm3_avx_glue.c
@@ -18,6 +18,8 @@
#include <asm/cpu_device_id.h>
#include <asm/simd.h>

+#define FPU_BYTES 4096U /* avoid kernel_fpu_begin/end scheduler/rcu stalls */
+
asmlinkage void sm3_transform_avx(struct sm3_state *state,
const u8 *data, int nblocks);

@@ -25,6 +27,7 @@ static int sm3_avx_update(struct shash_desc *desc, const u8 *data,
unsigned int len)
{
struct sm3_state *sctx = shash_desc_ctx(desc);
+ unsigned int chunk;

if (!crypto_simd_usable() ||
(sctx->count % SM3_BLOCK_SIZE) + len < SM3_BLOCK_SIZE) {
@@ -38,9 +41,14 @@ static int sm3_avx_update(struct shash_desc *desc, const u8 *data,
*/
BUILD_BUG_ON(offsetof(struct sm3_state, state) != 0);

- kernel_fpu_begin();
- sm3_base_do_update(desc, data, len, sm3_transform_avx);
- kernel_fpu_end();
+ do {
+ chunk = min(len, FPU_BYTES);
+ len -= chunk;
+ kernel_fpu_begin();
+ sm3_base_do_update(desc, data, chunk, sm3_transform_avx);
+ kernel_fpu_end();
+ data += chunk;
+ } while (len);

return 0;
}
@@ -48,6 +56,8 @@ static int sm3_avx_update(struct shash_desc *desc, const u8 *data,
static int sm3_avx_finup(struct shash_desc *desc, const u8 *data,
unsigned int len, u8 *out)
{
+ unsigned int chunk;
+
if (!crypto_simd_usable()) {
struct sm3_state *sctx = shash_desc_ctx(desc);

@@ -58,9 +68,17 @@ static int sm3_avx_finup(struct shash_desc *desc, const u8 *data,
return 0;
}

+ do {
+ chunk = min(len, FPU_BYTES);
+ len -= chunk;
+ if (chunk) {
+ kernel_fpu_begin();
+ sm3_base_do_update(desc, data, chunk, sm3_transform_avx);
+ kernel_fpu_end();
+ }
+ data += chunk;
+ } while (len);
kernel_fpu_begin();
- if (len)
- sm3_base_do_update(desc, data, len, sm3_transform_avx);
sm3_base_do_finalize(desc, sm3_transform_avx);
kernel_fpu_end();

--
2.37.3