mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Laveesh Bansal <laveeshb@laveeshbansal.com>
To: Jens Axboe <axboe@kernel.dk>, Eric Biggers <ebiggers@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>,
	"Theodore Y. Ts'o" <tytso@mit.edu>,
	Jaegeuk Kim <jaegeuk@kernel.org>,
	syzbot+20d60aafd9a0d1f2a380@syzkaller.appspotmail.com,
	linux-block@vger.kernel.org, linux-fscrypt@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback
Date: Sun, 22 Nov 2026 13:31:20 -0800	[thread overview]
Message-ID: <20261122213120.20974-1-laveeshb@laveeshbansal.com> (raw)

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


                 reply	other threads:[~2026-11-22 21:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122213120.20974-1-laveeshb@laveeshbansal.com \
    --to=laveeshb@laveeshbansal.com \
    --cc=axboe@kernel.dk \
    --cc=ebiggers@kernel.org \
    --cc=hch@lst.de \
    --cc=jaegeuk@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-fscrypt@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+20d60aafd9a0d1f2a380@syzkaller.appspotmail.com \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®