mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback
@ 2026-11-22 21:31 Laveesh Bansal
  0 siblings, 0 replies; only message in thread
From: Laveesh Bansal @ 2026-11-22 21:31 UTC (permalink / raw)
  To: Jens Axboe, Eric Biggers
  Cc: Christoph Hellwig, Theodore Y. Ts'o, Jaegeuk Kim,
	syzbot+20d60aafd9a0d1f2a380, linux-block, linux-fscrypt,
	linux-f2fs-devel, linux-crypto, linux-kernel

Setting a key in blk-crypto-fallback can deadlock on profile->lock.
blk_crypto_get_keyslot() holds it across keyslot_program, whose setkey
allocates with GFP_KERNEL (Adiantum); direct reclaim can evict an fscrypt
inode, and blk_crypto_evict_key() then takes profile->lock again. syzbot
reports it as an fs_reclaim vs profile->lock cycle. Since
commit 5acc6f649a68 ("fscrypt: Always use blk-crypto for contents on
block-based filesystems") hitting it no longer needs -o inlinecrypt.

Use memalloc_noio_save() around both setkey calls (keyslot_program and
the blank-key clear in evict_keyslot), as blk_crypto_alloc_enc_bio()
does.

Fixes: 488f6682c832 ("block: blk-crypto-fallback for Inline Encryption")
Cc: stable@vger.kernel.org # v7.3+
Reported-by: syzbot+20d60aafd9a0d1f2a380@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/r/6a9b25c3.b5d4176b.238c3e.0004.GAE@google.com
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---
I used an LLM for the analysis, the patch and the test runs.

A scope in blk_crypto_hw_enter() would also cover hardware drivers (qcom
ICE allocates under the lock); I kept this to the fallback.

Tested on arm64 and x86_64 (QEMU, base c44104f3642d, KASAN and lockdep;
patched runs and fstests include comment-only revisions): f2fs with an
Adiantum policy, creating encrypted files and reading them back with
O_DIRECT under a memory hog, about a minute per run.
Runs showing each symptom:

                                              base      patched
  lockdep cycle warning, arm64                2 of 2    0 of 6
  lockdep cycle warning, x86_64               6 of 6    0 of 16
  task deadlocked on its own lock, arm64      0 of 2    0 of 6
  task deadlocked on its own lock, x86_64     5 of 6    0 of 16
  lockdep cycle warning, x86_64 as uid 65534  2 of 2    0 of 2
  task deadlocked, x86_64 as uid 65534        1 of 2    0 of 2
  profile->lock -> fs_reclaim edge in
    lockdep's dump (runs that got to it)      3 of 3    0 of 21

No KASAN reports. Some runs, base and patched, hit unrelated stalls under the
hog (f2fs and page allocation); none has a blk-crypto frame. fstests -g
encrypt on arm64: f2fs passes; ext4 passes except generic/435, which ran out
of space on a 1 GB scratch device and passes on 2 GB. Could not reproduce on
ext4; fstests not run on x86_64.

 block/blk-crypto-fallback.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/block/blk-crypto-fallback.c b/block/blk-crypto-fallback.c
index 12e0b487e23a..2df418f16932 100644
--- a/block/blk-crypto-fallback.c
+++ b/block/blk-crypto-fallback.c
@@ -89,17 +89,24 @@ static struct bio_set enc_bio_set;
  */
 static u8 blank_key[BLK_CRYPTO_MAX_RAW_KEY_SIZE];
 
+/*
+ * setkey can allocate while profile->lock is held, and reclaim takes that lock
+ * to evict a key.  Use NOIO so those allocations can't recurse into the fs.
+ */
 static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
 {
 	struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
 	enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode;
+	unsigned int memflags;
 	int err;
 
 	WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID);
 
 	/* Clear the key in the skcipher */
+	memflags = memalloc_noio_save();
 	err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], blank_key,
 				     blk_crypto_modes[crypto_mode].keysize);
+	memalloc_noio_restore(memflags);
 	WARN_ON(err);
 	slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID;
 }
@@ -112,6 +119,7 @@ blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
 	struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
 	const enum blk_crypto_mode_num crypto_mode =
 						key->crypto_cfg.crypto_mode;
+	unsigned int memflags;
 	int err;
 
 	if (crypto_mode != slotp->crypto_mode &&
@@ -119,8 +127,10 @@ blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
 		blk_crypto_fallback_evict_keyslot(slot);
 
 	slotp->crypto_mode = crypto_mode;
+	memflags = memalloc_noio_save();
 	err = crypto_sync_skcipher_setkey(slotp->tfms[crypto_mode], key->bytes,
 				     key->size);
+	memalloc_noio_restore(memflags);
 	if (err) {
 		blk_crypto_fallback_evict_keyslot(slot);
 		return err;
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-22 21:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 21:31 [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback Laveesh Bansal

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

all inboxes | Powered by JetHome®