[PATCH v2] blk-crypto: use a NOIO scope while holding profile->lock

From: Laveesh Bansal

Date: Mon Nov 23 2026 - 13:14:54 EST


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@xxxxxxxxxxxxxxx # v7.3+
Reported-by: syzbot+20d60aafd9a0d1f2a380@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/r/6a9b25c3.b5d4176b.238c3e.0004.GAE@xxxxxxxxxx
Signed-off-by: Laveesh Bansal <laveeshb@xxxxxxxxxxxxxxxxx>
---
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@xxxxxxxxxxxxxxxxx

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