[PATCH v2 27/30] crypto: sa2ul - use synchronous fallbacks and fixed request sizes

From: Manorit Chawdhry

Date: Wed Nov 04 2026 - 00:50:03 EST


To move away from crypto_*_set_reqsize(), use synchronous skcipher
fallbacks with stack requests to avoid reserving fallback storage in
each hardware request.

On arm64, native HMAC authenc requests need 448 bytes for SHA1 and 488
bytes for SHA256, exceeding crypto_alloc_sync_aead()'s 384-byte limit.
Since the sa2ul driver registers these authenc requests, keep ordinary
AEAD fallbacks with dynamically sized request storage.

For the other requests, start using cra_reqsize, keeping its embedded
fallback request for incremental hashing.

Also, while at it, switch hash transform callbacks to init_tfm and
exit_tfm.

Signed-off-by: Manorit Chawdhry <m-chawdhry@xxxxxx>
---
drivers/crypto/sa2ul.c | 131 ++++++++++++++++++++++++-------------------------
drivers/crypto/sa2ul.h | 2 +-
2 files changed, 65 insertions(+), 68 deletions(-)

diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index ae3889695dfe..19caaa41ba03 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -760,7 +760,7 @@ static int sa_init_ctx_info(struct sa_ctx_info *ctx,
return err;
}

-static void sa_cipher_cra_exit(struct crypto_skcipher *tfm)
+static void sa_cipher_exit_tfm(struct crypto_skcipher *tfm)
{
struct sa_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
@@ -772,15 +772,15 @@ static void sa_cipher_cra_exit(struct crypto_skcipher *tfm)
sa_free_ctx_info(&ctx->enc, data);
sa_free_ctx_info(&ctx->dec, data);

- crypto_free_skcipher(ctx->fallback.skcipher);
+ crypto_free_sync_skcipher(ctx->fallback.skcipher);
}

-static int sa_cipher_cra_init(struct crypto_skcipher *tfm)
+static int sa_cipher_init_tfm(struct crypto_skcipher *tfm)
{
struct sa_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
const char *name = crypto_tfm_alg_name(&tfm->base);
- struct crypto_skcipher *child;
+ struct crypto_sync_skcipher *child;
int ret;

memzero_explicit(ctx, sizeof(*ctx));
@@ -793,7 +793,7 @@ static int sa_cipher_cra_init(struct crypto_skcipher *tfm)
if (ret)
goto ctx_dec_err;

- child = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
+ child = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(child)) {
dev_err(sa_k3_dev, "Error allocating fallback algo %s\n", name);
ret = PTR_ERR(child);
@@ -801,8 +801,6 @@ static int sa_cipher_cra_init(struct crypto_skcipher *tfm)
}

ctx->fallback.skcipher = child;
- crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
- sizeof(struct skcipher_request));

dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
__func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
@@ -819,7 +817,7 @@ static int sa_cipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
unsigned int keylen, struct algo_data *ad)
{
struct sa_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- struct crypto_skcipher *child = ctx->fallback.skcipher;
+ struct crypto_sync_skcipher *child = ctx->fallback.skcipher;
int cmdl_len;
struct sa_cmdl_cfg cfg;
int ret;
@@ -835,10 +833,10 @@ static int sa_cipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
cfg.enc_eng_id = ad->enc_eng.eng_id;
cfg.iv_size = crypto_skcipher_ivsize(tfm);

- crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
- crypto_skcipher_set_flags(child, tfm->base.crt_flags &
+ crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
+ crypto_sync_skcipher_set_flags(child, tfm->base.crt_flags &
CRYPTO_TFM_REQ_MASK);
- ret = crypto_skcipher_setkey(child, key, keylen);
+ ret = crypto_sync_skcipher_setkey(child, key, keylen);
if (ret)
return ret;

@@ -1248,18 +1246,16 @@ static int sa_cipher_run(struct skcipher_request *req, u8 *iv, int enc)
if (req->cryptlen > SA_MAX_DATA_SZ ||
(req->cryptlen >= SA_UNSAFE_DATA_SZ_MIN &&
req->cryptlen <= SA_UNSAFE_DATA_SZ_MAX)) {
- struct skcipher_request *subreq = skcipher_request_ctx(req);
+ SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback.skcipher);
+ int ret;

- skcipher_request_set_tfm(subreq, ctx->fallback.skcipher);
- skcipher_request_set_callback(subreq, req->base.flags,
- req->base.complete,
- req->base.data);
+ skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
skcipher_request_set_crypt(subreq, req->src, req->dst,
req->cryptlen, req->iv);
- if (enc)
- return crypto_skcipher_encrypt(subreq);
- else
- return crypto_skcipher_decrypt(subreq);
+ ret = enc ? crypto_skcipher_encrypt(subreq) :
+ crypto_skcipher_decrypt(subreq);
+ skcipher_request_zero(subreq);
+ return ret;
}

sa_req.size = req->cryptlen;
@@ -1415,9 +1411,9 @@ static int sa_sha_setup(struct sa_tfm_ctx *ctx, struct algo_data *ad)
return -EINVAL;
}

-static int sa_sha_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
+static int sa_sha_init_tfm(struct crypto_ahash *tfm, const char *alg_base)
{
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
+ struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
int ret;

@@ -1444,10 +1440,6 @@ static int sa_sha_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
__func__, tfm, ctx->enc.sc_id, &ctx->enc.sc_phys,
ctx->dec.sc_id, &ctx->dec.sc_phys);

- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct sa_sha_req_ctx) +
- crypto_ahash_reqsize(ctx->fallback.ahash));
-
return 0;

err_free_ctx:
@@ -1455,7 +1447,7 @@ static int sa_sha_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
return ret;
}

-static void sa_sha_cra_exit(struct crypto_tfm *tfm);
+static void sa_sha_exit_tfm(struct crypto_ahash *tfm);

static int sa_sha_init(struct ahash_request *req)
{
@@ -1525,13 +1517,13 @@ static int sa_sha_export(struct ahash_request *req, void *out)
return crypto_ahash_export(subreq, out);
}

-static int sa_sha1_cra_init(struct crypto_tfm *tfm)
+static int sa_sha1_init_tfm(struct crypto_ahash *tfm)
{
struct algo_data ad = { 0 };
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
+ struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
int ret = 0;

- ret = sa_sha_cra_init_alg(tfm, "sha1");
+ ret = sa_sha_init_tfm(tfm, "sha1");
if (ret)
return ret;

@@ -1540,18 +1532,18 @@ static int sa_sha1_cra_init(struct crypto_tfm *tfm)

ret = sa_sha_setup(ctx, &ad);
if (ret)
- sa_sha_cra_exit(tfm);
+ sa_sha_exit_tfm(tfm);

return ret;
}

-static int sa_sha256_cra_init(struct crypto_tfm *tfm)
+static int sa_sha256_init_tfm(struct crypto_ahash *tfm)
{
struct algo_data ad = { 0 };
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
+ struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
int ret = 0;

- ret = sa_sha_cra_init_alg(tfm, "sha256");
+ ret = sa_sha_init_tfm(tfm, "sha256");
if (ret)
return ret;

@@ -1560,18 +1552,18 @@ static int sa_sha256_cra_init(struct crypto_tfm *tfm)

ret = sa_sha_setup(ctx, &ad);
if (ret)
- sa_sha_cra_exit(tfm);
+ sa_sha_exit_tfm(tfm);

return ret;
}

-static int sa_sha512_cra_init(struct crypto_tfm *tfm)
+static int sa_sha512_init_tfm(struct crypto_ahash *tfm)
{
struct algo_data ad = { 0 };
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
+ struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
int ret = 0;

- ret = sa_sha_cra_init_alg(tfm, "sha512");
+ ret = sa_sha_init_tfm(tfm, "sha512");
if (ret)
return ret;

@@ -1580,14 +1572,14 @@ static int sa_sha512_cra_init(struct crypto_tfm *tfm)

ret = sa_sha_setup(ctx, &ad);
if (ret)
- sa_sha_cra_exit(tfm);
+ sa_sha_exit_tfm(tfm);

return ret;
}

-static void sa_sha_cra_exit(struct crypto_tfm *tfm)
+static void sa_sha_exit_tfm(struct crypto_ahash *tfm)
{
- struct sa_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
+ struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);

dev_dbg(sa_k3_dev, "%s(0x%p) sc-ids(0x%x(0x%pad), 0x%x(0x%pad))\n",
@@ -1596,8 +1588,7 @@ static void sa_sha_cra_exit(struct crypto_tfm *tfm)

crypto_free_ahash(ctx->fallback.ahash);

- if (crypto_tfm_alg_type(tfm) == CRYPTO_ALG_TYPE_AHASH)
- sa_free_ctx_info(&ctx->enc, data);
+ sa_free_ctx_info(&ctx->enc, data);
}

static void sa_aead_dma_in_callback(void *data,
@@ -1643,7 +1634,7 @@ static void sa_aead_dma_in_callback(void *data,
aead_request_complete(req, err);
}

-static int sa_cra_init_aead(struct crypto_aead *tfm, const char *fallback)
+static int sa_aead_init_tfm(struct crypto_aead *tfm, const char *fallback)
{
struct sa_tfm_ctx *ctx = crypto_aead_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
@@ -1685,17 +1676,17 @@ static int sa_cra_init_aead(struct crypto_aead *tfm, const char *fallback)
return ret;
}

-static int sa_cra_init_aead_sha1(struct crypto_aead *tfm)
+static int sa_aead_sha1_init_tfm(struct crypto_aead *tfm)
{
- return sa_cra_init_aead(tfm, "authenc(hmac(sha1),cbc(aes))");
+ return sa_aead_init_tfm(tfm, "authenc(hmac(sha1),cbc(aes))");
}

-static int sa_cra_init_aead_sha256(struct crypto_aead *tfm)
+static int sa_aead_sha256_init_tfm(struct crypto_aead *tfm)
{
- return sa_cra_init_aead(tfm, "authenc(hmac(sha256),cbc(aes))");
+ return sa_aead_init_tfm(tfm, "authenc(hmac(sha256),cbc(aes))");
}

-static void sa_exit_tfm_aead(struct crypto_aead *tfm)
+static void sa_aead_exit_tfm(struct crypto_aead *tfm)
{
struct sa_tfm_ctx *ctx = crypto_aead_ctx(tfm);
struct sa_crypto_data *data = dev_get_drvdata(sa_k3_dev);
@@ -1884,8 +1875,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.base.cra_blocksize = AES_BLOCK_SIZE,
.base.cra_ctxsize = sizeof(struct sa_tfm_ctx),
.base.cra_module = THIS_MODULE,
- .init = sa_cipher_cra_init,
- .exit = sa_cipher_cra_exit,
+ .init = sa_cipher_init_tfm,
+ .exit = sa_cipher_exit_tfm,
.min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE,
.ivsize = AES_BLOCK_SIZE,
@@ -1907,8 +1898,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.base.cra_blocksize = AES_BLOCK_SIZE,
.base.cra_ctxsize = sizeof(struct sa_tfm_ctx),
.base.cra_module = THIS_MODULE,
- .init = sa_cipher_cra_init,
- .exit = sa_cipher_cra_exit,
+ .init = sa_cipher_init_tfm,
+ .exit = sa_cipher_exit_tfm,
.min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE,
.setkey = sa_aes_ecb_setkey,
@@ -1929,8 +1920,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.base.cra_blocksize = DES_BLOCK_SIZE,
.base.cra_ctxsize = sizeof(struct sa_tfm_ctx),
.base.cra_module = THIS_MODULE,
- .init = sa_cipher_cra_init,
- .exit = sa_cipher_cra_exit,
+ .init = sa_cipher_init_tfm,
+ .exit = sa_cipher_exit_tfm,
.min_keysize = 3 * DES_KEY_SIZE,
.max_keysize = 3 * DES_KEY_SIZE,
.ivsize = DES_BLOCK_SIZE,
@@ -1952,8 +1943,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.base.cra_blocksize = DES_BLOCK_SIZE,
.base.cra_ctxsize = sizeof(struct sa_tfm_ctx),
.base.cra_module = THIS_MODULE,
- .init = sa_cipher_cra_init,
- .exit = sa_cipher_cra_exit,
+ .init = sa_cipher_init_tfm,
+ .exit = sa_cipher_exit_tfm,
.min_keysize = 3 * DES_KEY_SIZE,
.max_keysize = 3 * DES_KEY_SIZE,
.setkey = sa_3des_ecb_setkey,
@@ -1974,13 +1965,15 @@ static struct sa_alg_tmpl sa_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = SHA1_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct sa_tfm_ctx),
+ .cra_reqsize = sizeof(struct sa_sha_req_ctx) +
+ MAX_SYNC_HASH_REQSIZE,
.cra_module = THIS_MODULE,
- .cra_init = sa_sha1_cra_init,
- .cra_exit = sa_sha_cra_exit,
},
.halg.digestsize = SHA1_DIGEST_SIZE,
.halg.statesize = sizeof(struct sa_sha_req_ctx) +
sizeof(struct sha1_state),
+ .init_tfm = sa_sha1_init_tfm,
+ .exit_tfm = sa_sha_exit_tfm,
.init = sa_sha_init,
.update = sa_sha_update,
.final = sa_sha_final,
@@ -2003,13 +1996,15 @@ static struct sa_alg_tmpl sa_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = SHA256_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct sa_tfm_ctx),
+ .cra_reqsize = sizeof(struct sa_sha_req_ctx) +
+ MAX_SYNC_HASH_REQSIZE,
.cra_module = THIS_MODULE,
- .cra_init = sa_sha256_cra_init,
- .cra_exit = sa_sha_cra_exit,
},
.halg.digestsize = SHA256_DIGEST_SIZE,
.halg.statesize = sizeof(struct sa_sha_req_ctx) +
sizeof(struct sha256_state),
+ .init_tfm = sa_sha256_init_tfm,
+ .exit_tfm = sa_sha_exit_tfm,
.init = sa_sha_init,
.update = sa_sha_update,
.final = sa_sha_final,
@@ -2032,13 +2027,15 @@ static struct sa_alg_tmpl sa_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = SHA512_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct sa_tfm_ctx),
+ .cra_reqsize = sizeof(struct sa_sha_req_ctx) +
+ MAX_SYNC_HASH_REQSIZE,
.cra_module = THIS_MODULE,
- .cra_init = sa_sha512_cra_init,
- .cra_exit = sa_sha_cra_exit,
},
.halg.digestsize = SHA512_DIGEST_SIZE,
.halg.statesize = sizeof(struct sa_sha_req_ctx) +
sizeof(struct sha512_state),
+ .init_tfm = sa_sha512_init_tfm,
+ .exit_tfm = sa_sha_exit_tfm,
.init = sa_sha_init,
.update = sa_sha_update,
.final = sa_sha_final,
@@ -2067,8 +2064,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.ivsize = AES_BLOCK_SIZE,
.maxauthsize = SHA1_DIGEST_SIZE,

- .init = sa_cra_init_aead_sha1,
- .exit = sa_exit_tfm_aead,
+ .init = sa_aead_sha1_init_tfm,
+ .exit = sa_aead_exit_tfm,
.setkey = sa_aead_cbc_sha1_setkey,
.setauthsize = sa_aead_setauthsize,
.encrypt = sa_aead_encrypt,
@@ -2095,8 +2092,8 @@ static struct sa_alg_tmpl sa_algs[] = {
.ivsize = AES_BLOCK_SIZE,
.maxauthsize = SHA256_DIGEST_SIZE,

- .init = sa_cra_init_aead_sha256,
- .exit = sa_exit_tfm_aead,
+ .init = sa_aead_sha256_init_tfm,
+ .exit = sa_aead_exit_tfm,
.setkey = sa_aead_cbc_sha256_setkey,
.setauthsize = sa_aead_setauthsize,
.encrypt = sa_aead_encrypt,
diff --git a/drivers/crypto/sa2ul.h b/drivers/crypto/sa2ul.h
index 775f3939be68..5152c497858f 100644
--- a/drivers/crypto/sa2ul.h
+++ b/drivers/crypto/sa2ul.h
@@ -242,7 +242,7 @@ struct sa_tfm_ctx {
int iv_idx;
/* for fallback */
union {
- struct crypto_skcipher *skcipher;
+ struct crypto_sync_skcipher *skcipher;
struct crypto_ahash *ahash;
struct crypto_aead *aead;
} fallback;

--
2.43.0