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 v2] blk-crypto: use a NOIO scope while holding profile->lock
Date: Mon, 23 Nov 2026 10:14:08 -0800	[thread overview]
Message-ID: <20261123181408.62972-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.

Enter a NOIO scope in blk_crypto_hw_enter() and leave it in
blk_crypto_hw_exit(), so allocations made under the lock, by the
fallback or a hardware driver, can't recurse into the filesystem. The
saved flags live in the profile. blk_crypto_reprogram_all_keys() takes
the lock directly, so it gets the same scope.

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.

Changes in v2:
 - Moved the NOIO scope from the two setkey calls into
   blk_crypto_hw_enter()/blk_crypto_hw_exit(), as Christoph suggested,
   and added it to blk_crypto_reprogram_all_keys().
 - The scope now also covers the hardware drivers' keyslot callbacks. I
   could not test those; it only narrows reclaim for them.
v1: https://lore.kernel.org/r/20261122213120.20974-1-laveeshb@laveeshbansal.com

Tested on arm64 and x86_64 (QEMU, base c44104f3642d, KASAN and lockdep;
patched runs 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      v2
  lockdep cycle warning, arm64                2 of 2    0 of 8
  lockdep cycle warning, x86_64               6 of 6    0 of 12
  task deadlocked on its own lock, arm64      0 of 2    0 of 8
  task deadlocked on its own lock, x86_64     5 of 6    0 of 12
  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 18

No KASAN reports. Under the hog memory runs out, and 3 of the 12 x86 runs
stalled in page allocation, two without finishing; base stalls the same
way. In one, the task holding profile->lock was waiting in the setkey
allocation with kswapd queued behind it. That stall began earlier, in the
RAM-backed disk's own allocation, and lockdep reported nothing: NOIO stops
the allocation recursing into key eviction, not a wait for free memory.

fstests -g encrypt on arm64 passes on f2fs and ext4. Could not reproduce
on ext4; fstests not run on x86_64.

 block/blk-crypto-profile.c         | 7 +++++++
 include/linux/blk-crypto-profile.h | 1 +
 2 files changed, 8 insertions(+)

diff --git a/block/blk-crypto-profile.c b/block/blk-crypto-profile.c
index 53126c091b0b..395829fe7bcc 100644
--- a/block/blk-crypto-profile.c
+++ b/block/blk-crypto-profile.c
@@ -29,6 +29,7 @@
 #include <linux/atomic.h>
 #include <linux/mutex.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/mm.h>
 #include <linux/wait.h>
 #include <linux/blkdev.h>
 #include <linux/blk-integrity.h>
@@ -53,11 +54,14 @@ static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
 	if (profile->dev)
 		pm_runtime_get_sync(profile->dev);
 	down_write(&profile->lock);
+	/* Reclaim evicts keys under this lock; don't recurse into the fs. */
+	profile->memflags = memalloc_noio_save();
 }
 
 static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
 	__releases(&profile->lock)
 {
+	memalloc_noio_restore(profile->memflags);
 	up_write(&profile->lock);
 	if (profile->dev)
 		pm_runtime_put_sync(profile->dev);
@@ -397,6 +401,7 @@ int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
  */
 void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
 {
+	unsigned int memflags;
 	unsigned int slot;
 
 	if (profile->num_slots == 0)
@@ -404,6 +409,7 @@ void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
 
 	/* This is for device initialization, so don't resume the device */
 	down_write(&profile->lock);
+	memflags = memalloc_noio_save();
 	for (slot = 0; slot < profile->num_slots; slot++) {
 		const struct blk_crypto_key *key = profile->slots[slot].key;
 		int err;
@@ -414,6 +420,7 @@ void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
 		err = profile->ll_ops.keyslot_program(profile, key, slot);
 		WARN_ON(err);
 	}
+	memalloc_noio_restore(memflags);
 	up_write(&profile->lock);
 }
 EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys);
diff --git a/include/linux/blk-crypto-profile.h b/include/linux/blk-crypto-profile.h
index 4f39e9cd7576..9209b6a14b98 100644
--- a/include/linux/blk-crypto-profile.h
+++ b/include/linux/blk-crypto-profile.h
@@ -174,6 +174,7 @@ struct blk_crypto_profile {
 	 */
 	struct rw_semaphore lock;
 	struct lock_class_key lockdep_key;
+	unsigned int memflags;
 
 	/* List of idle slots, with least recently used slot at front */
 	wait_queue_head_t idle_slots_wait_queue;
-- 
2.43.0


                 reply	other threads:[~2026-11-23 18:14 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=20261123181408.62972-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®