Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: adiantum - Eliminate slab memory allocation in setkey
@ 2026-11-25  1:35 Eric Biggers
  2026-11-27  1:53 ` Laveesh Bansal
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Biggers @ 2026-11-25  1:35 UTC (permalink / raw)
  To: linux-crypto, Herbert Xu; +Cc: Laveesh Bansal, Eric Biggers

Instances of the adiantum template are some of the only skcipher
algorithms that allocate slab memory in their ->setkey() function.

This is causing a problem for blk-crypto, which sets a key while holding
a lock that can be taken during direct reclaim.

That case will be fixed by using memalloc_noio_save() to make the
allocation inherit GFP_NOIO
(https://lore.kernel.org/linux-block/20261123181408.62972-1-laveeshb@laveeshbansal.com/).
Regardless, there's actually no need for adiantum_setkey() to allocate
memory.  The only reason it did so originally was because "nhpoly1305"
was a separate crypto_shash algorithm, so the big NH key array was
hidden behind an opaque pointer.  However, that's now been fixed.

Eliminate the slab memory allocation in adiantum_setkey() by:

   (1) Making it generate the derived key material in adiantum_tfm_ctx,
       in the same memory where the final converted keys end up anyway.

   (2) Moving the skcipher_request, scatterlist, and IV to the stack.
       For safety, ensure the CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE flag is
       not set (though xchacha12 and xchacha20 don't set it anyway).

   (3) Deleting the crypto_wait object, which was unnecessary.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 crypto/adiantum.c | 81 +++++++++++++++++++++++++----------------------
 1 file changed, 43 insertions(+), 38 deletions(-)

diff --git a/crypto/adiantum.c b/crypto/adiantum.c
index 5ddf585abb666..825958c6e4769 100644
--- a/crypto/adiantum.c
+++ b/crypto/adiantum.c
@@ -60,9 +60,15 @@ struct adiantum_instance_ctx {
 struct adiantum_tfm_ctx {
 	struct crypto_skcipher *streamcipher;
 	struct crypto_cipher *blockcipher;
-	struct poly1305_core_key header_hash_key;
-	struct poly1305_core_key msg_poly_key;
-	u32 nh_key[NH_KEY_WORDS];
+	union {
+		struct {
+			struct poly1305_core_key header_hash_key;
+			struct poly1305_core_key msg_poly_key;
+			u32 nh_key[NH_KEY_WORDS];
+		};
+		/* Used only during adiantum_setkey() */
+		u8 derived_keys[BLOCKCIPHER_KEY_SIZE + HASH_KEY_SIZE];
+	};
 };
 
 struct nhpoly1305_ctx {
@@ -106,14 +112,11 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
 			   unsigned int keylen)
 {
 	struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
-	struct {
-		u8 iv[XCHACHA_IV_SIZE];
-		u8 derived_keys[BLOCKCIPHER_KEY_SIZE + HASH_KEY_SIZE];
-		struct scatterlist sg;
-		struct crypto_wait wait;
-		struct skcipher_request req; /* must be last */
-	} *data;
-	u8 *keyp;
+	SYNC_SKCIPHER_REQUEST_ON_STACK(
+		req, (struct crypto_sync_skcipher *)tctx->streamcipher);
+	u8 poly_keys[2 * POLY1305_BLOCK_SIZE];
+	u8 iv[XCHACHA_IV_SIZE] = { 1 };
+	struct scatterlist sg;
 	int err;
 
 	/* Set the stream cipher key (K_S) */
@@ -126,46 +129,42 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
 		return err;
 
 	/* Derive the subkeys */
-	data = kzalloc(sizeof(*data) +
-		       crypto_skcipher_reqsize(tctx->streamcipher), GFP_KERNEL);
-	if (!data)
-		return -ENOMEM;
-	data->iv[0] = 1;
-	sg_init_one(&data->sg, data->derived_keys, sizeof(data->derived_keys));
-	crypto_init_wait(&data->wait);
-	skcipher_request_set_tfm(&data->req, tctx->streamcipher);
-	skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
-						  CRYPTO_TFM_REQ_MAY_BACKLOG,
-				      crypto_req_done, &data->wait);
-	skcipher_request_set_crypt(&data->req, &data->sg, &data->sg,
-				   sizeof(data->derived_keys), data->iv);
-	err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), &data->wait);
+	memset(tctx->derived_keys, 0, sizeof(tctx->derived_keys));
+	sg_init_one(&sg, tctx->derived_keys, sizeof(tctx->derived_keys));
+	skcipher_request_set_callback(req, 0, NULL, NULL);
+	skcipher_request_set_crypt(req, &sg, &sg, sizeof(tctx->derived_keys),
+				   iv);
+	err = crypto_skcipher_encrypt(req);
 	if (err)
 		goto out;
-	keyp = data->derived_keys;
 
 	/* Set the block cipher key (K_E) */
 	crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
 	crypto_cipher_set_flags(tctx->blockcipher,
 				crypto_skcipher_get_flags(tfm) &
 				CRYPTO_TFM_REQ_MASK);
-	err = crypto_cipher_setkey(tctx->blockcipher, keyp,
+	err = crypto_cipher_setkey(tctx->blockcipher, tctx->derived_keys,
 				   BLOCKCIPHER_KEY_SIZE);
 	if (err)
 		goto out;
-	keyp += BLOCKCIPHER_KEY_SIZE;
 
 	/* Set the hash key (K_H) */
-	poly1305_core_setkey(&tctx->header_hash_key, keyp);
-	keyp += POLY1305_BLOCK_SIZE;
-	poly1305_core_setkey(&tctx->msg_poly_key, keyp);
-	keyp += POLY1305_BLOCK_SIZE;
-	for (int i = 0; i < NH_KEY_WORDS; i++)
-		tctx->nh_key[i] = get_unaligned_le32(&keyp[i * 4]);
-	keyp += NH_KEY_BYTES;
-	WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]);
+	memcpy(poly_keys, &tctx->derived_keys[BLOCKCIPHER_KEY_SIZE],
+	       2 * POLY1305_BLOCK_SIZE);
+	memmove(tctx->nh_key,
+		&tctx->derived_keys[BLOCKCIPHER_KEY_SIZE +
+				    2 * POLY1305_BLOCK_SIZE],
+		NH_KEY_BYTES);
+	poly1305_core_setkey(&tctx->header_hash_key, &poly_keys[0]);
+	poly1305_core_setkey(&tctx->msg_poly_key,
+			     &poly_keys[POLY1305_BLOCK_SIZE]);
+	le32_to_cpu_array(tctx->nh_key, NH_KEY_WORDS);
+	memzero_explicit(poly_keys, sizeof(poly_keys));
 out:
-	kfree_sensitive(data);
+	if (err)
+		memzero_explicit(tctx->derived_keys,
+				 sizeof(tctx->derived_keys));
+	skcipher_request_zero(req);
 	return err;
 }
 
@@ -506,6 +505,11 @@ static int adiantum_init_tfm(struct crypto_skcipher *tfm)
 	streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
 	if (IS_ERR(streamcipher))
 		return PTR_ERR(streamcipher);
+	if (WARN_ON(crypto_skcipher_reqsize(streamcipher) >
+		    MAX_SYNC_SKCIPHER_REQSIZE)) {
+		err = -EINVAL;
+		goto err_free_streamcipher;
+	}
 
 	blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
 	if (IS_ERR(blockcipher)) {
@@ -590,7 +594,8 @@ static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
 	err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
 				   skcipher_crypto_instance(inst),
 				   crypto_attr_alg_name(tb[1]), 0,
-				   mask | CRYPTO_ALG_ASYNC /* sync only */);
+				   mask | CRYPTO_ALG_ASYNC /* sync only */ |
+					   CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE);
 	if (err)
 		goto err_free_inst;
 	streamcipher_alg = crypto_spawn_skcipher_alg_common(&ictx->streamcipher_spawn);

base-commit: 04cd6899aaaeeab82afb97e58eb8619fde35b47d
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: adiantum - Eliminate slab memory allocation in setkey
  2026-11-25  1:35 [PATCH] crypto: adiantum - Eliminate slab memory allocation in setkey Eric Biggers
@ 2026-11-27  1:53 ` Laveesh Bansal
  2026-11-27 18:44   ` Eric Biggers
  0 siblings, 1 reply; 3+ messages in thread
From: Laveesh Bansal @ 2026-11-27  1:53 UTC (permalink / raw)
  To: Eric Biggers; +Cc: Herbert Xu, linux-crypto

I tested this on x86_64, arm64 and s390x (big-endian, emulated in QEMU) --
all on top of mainline c44104f3642d.

On Tue, Nov 24, 2026 at 05:35:53PM -0800, Eric Biggers wrote:
> +	memmove(tctx->nh_key,
> +		&tctx->derived_keys[BLOCKCIPHER_KEY_SIZE +
> +				    2 * POLY1305_BLOCK_SIZE],
> +		NH_KEY_BYTES);

I checked that this gives the same ciphertext as before. On each machine
I compared it with and without the patch using 400 seeded random keys,
tweaks and plaintexts for each of adiantum(xchacha12,aes) and
adiantum(xchacha20,aes), including re-keying one tfm against a fresh one.
The digests were identical with and without the patch, and the same on
all three machines. I saw no round trip or re-key mismatches.

> +	SYNC_SKCIPHER_REQUEST_ON_STACK(
> +		req, (struct crypto_sync_skcipher *)tctx->streamcipher);

I measured the stack cost. With -fstack-usage, the frame of
adiantum_setkey() grows from 64 to 640 bytes on arm64 (gcc 13.3.0; 96 to
896 with KASAN) and from 72 to 872 bytes on x86_64 with KASAN (gcc
15.2.0), under the 2048 byte frame warning limit.

I also ran the stack tracer on arm64 (KASAN, 32 KiB stacks,
with v3 of my blk-crypto patch also applied:
https://lore.kernel.org/all/20261125081417.77674-1-laveeshb@laveeshbansal.com/)
under the f2fs/adiantum workload with memory pressure.
The deepest stack at any adiantum, chacha or blk-crypto function was
7784 bytes, going through adiantum_setkey() via inode eviction in
reclaim. That was one run and I did not run the tracer on x86_64.

I think this is an acceptable trade. The old kzalloc() could enter direct
reclaim from setkey, and an allocation in the same fallback I/O path (the
bio encryption) used about 4 KiB of stack in reclaim in the same
workload. The new frame adds 576 to 800 bytes, mostly the on-stack
request (a fixed 384 bytes plus header).

A couple of other results:

- The lockdep cycle from the blk-crypto report is gone with this patch.
  In my f2fs/adiantum workload under memory pressure the base kernel hit
  it in all 3 runs on arm64, and lockdep recorded the &profile->lock to
  fs_reclaim dependency. With only this patch I got none and lockdep no
  longer recorded that dependency. I also got none with
  CRYPTO_SELFTESTS_FULL.
  On x86_64 the base hit the cycle in all runs - 6 across 2 hosts.
  With only this patch I got none,
  and also none with v3 of my patch on top. I saw no hung task or
  KASAN reports in any of the runs with the patch.

- CRYPTO_SELFTESTS: I did not get any failures, with and without
  CRYPTO_SELFTESTS_FULL. The WARN_ON in
  adiantum_init_tfm() did not fire in any of my runs.

Tested-by: Laveesh Bansal <laveeshb@laveeshbansal.com>

Thanks
Laveesh

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: adiantum - Eliminate slab memory allocation in setkey
  2026-11-27  1:53 ` Laveesh Bansal
@ 2026-11-27 18:44   ` Eric Biggers
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-11-27 18:44 UTC (permalink / raw)
  To: Laveesh Bansal; +Cc: Herbert Xu, linux-crypto

On Thu, Nov 26, 2026 at 05:53:45PM -0800, Laveesh Bansal wrote:
> I tested this on x86_64, arm64 and s390x (big-endian, emulated in QEMU) --
> all on top of mainline c44104f3642d.
> 
> On Tue, Nov 24, 2026 at 05:35:53PM -0800, Eric Biggers wrote:
> > +	memmove(tctx->nh_key,
> > +		&tctx->derived_keys[BLOCKCIPHER_KEY_SIZE +
> > +				    2 * POLY1305_BLOCK_SIZE],
> > +		NH_KEY_BYTES);
> 
> I checked that this gives the same ciphertext as before. On each machine
> I compared it with and without the patch using 400 seeded random keys,
> tweaks and plaintexts for each of adiantum(xchacha12,aes) and
> adiantum(xchacha20,aes), including re-keying one tfm against a fresh one.
> The digests were identical with and without the patch, and the same on
> all three machines. I saw no round trip or re-key mismatches.
> 
> > +	SYNC_SKCIPHER_REQUEST_ON_STACK(
> > +		req, (struct crypto_sync_skcipher *)tctx->streamcipher);
> 
> I measured the stack cost. With -fstack-usage, the frame of
> adiantum_setkey() grows from 64 to 640 bytes on arm64 (gcc 13.3.0; 96 to
> 896 with KASAN) and from 72 to 872 bytes on x86_64 with KASAN (gcc
> 15.2.0), under the 2048 byte frame warning limit.
> 
> I also ran the stack tracer on arm64 (KASAN, 32 KiB stacks,
> with v3 of my blk-crypto patch also applied:
> https://lore.kernel.org/all/20261125081417.77674-1-laveeshb@laveeshbansal.com/)
> under the f2fs/adiantum workload with memory pressure.
> The deepest stack at any adiantum, chacha or blk-crypto function was
> 7784 bytes, going through adiantum_setkey() via inode eviction in
> reclaim. That was one run and I did not run the tracer on x86_64.
> 
> I think this is an acceptable trade. The old kzalloc() could enter direct
> reclaim from setkey, and an allocation in the same fallback I/O path (the
> bio encryption) used about 4 KiB of stack in reclaim in the same
> workload. The new frame adds 576 to 800 bytes, mostly the on-stack
> request (a fixed 384 bytes plus header).
> 
> A couple of other results:
> 
> - The lockdep cycle from the blk-crypto report is gone with this patch.
>   In my f2fs/adiantum workload under memory pressure the base kernel hit
>   it in all 3 runs on arm64, and lockdep recorded the &profile->lock to
>   fs_reclaim dependency. With only this patch I got none and lockdep no
>   longer recorded that dependency. I also got none with
>   CRYPTO_SELFTESTS_FULL.
>   On x86_64 the base hit the cycle in all runs - 6 across 2 hosts.
>   With only this patch I got none,
>   and also none with v3 of my patch on top. I saw no hung task or
>   KASAN reports in any of the runs with the patch.
> 
> - CRYPTO_SELFTESTS: I did not get any failures, with and without
>   CRYPTO_SELFTESTS_FULL. The WARN_ON in
>   adiantum_init_tfm() did not fire in any of my runs.
> 
> Tested-by: Laveesh Bansal <laveeshb@laveeshbansal.com>

Thanks.  Later we should shrink the required stack space by switching
from crypto_skcipher to the ChaCha library.
(SYNC_SKCIPHER_REQUEST_ON_STACK() uses way more stack space than
needed.)  But that would be a separate cleanup, and the stack usage from
SYNC_SKCIPHER_REQUEST_ON_STACK() is tolerable for now.

- Eric

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27 18:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  1:35 [PATCH] crypto: adiantum - Eliminate slab memory allocation in setkey Eric Biggers
2026-11-27  1:53 ` Laveesh Bansal
2026-11-27 18:44   ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox