* [PATCH v2] blk-crypto: use a NOIO scope while holding profile->lock
@ 2026-11-23 18:14 Laveesh Bansal
2026-11-25 1:44 ` Eric Biggers
0 siblings, 1 reply; 2+ messages in thread
From: Laveesh Bansal @ 2026-11-23 18:14 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.
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] blk-crypto: use a NOIO scope while holding profile->lock
2026-11-23 18:14 [PATCH v2] blk-crypto: use a NOIO scope while holding profile->lock Laveesh Bansal
@ 2026-11-25 1:44 ` Eric Biggers
0 siblings, 0 replies; 2+ messages in thread
From: Eric Biggers @ 2026-11-25 1:44 UTC (permalink / raw)
To: Laveesh Bansal
Cc: Jens Axboe, Christoph Hellwig, Theodore Y. Ts'o, Jaegeuk Kim,
syzbot+20d60aafd9a0d1f2a380, linux-block, linux-fscrypt,
linux-f2fs-devel, linux-crypto, linux-kernel
On Mon, Nov 23, 2026 at 10:14:08AM -0800, Laveesh Bansal wrote:
> setkey allocates with GFP_KERNEL (Adiantum)
We should just fix that one:
https://lore.kernel.org/linux-crypto/20261125013553.94480-1-ebiggers@kernel.org/
We should still proceed with this patch, but there is really no need for
the crypto code to allocate memory in random places. I've been working
on getting rid of cases like this.
> Fixes: 488f6682c832 ("block: blk-crypto-fallback for Inline Encryption")
> Cc: stable@vger.kernel.org # v7.3+
The v7.3+ should not be there.
> @@ -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();
> }
"don't recurse into the fs" describes memalloc_nofs_save(), but the code
actually uses memalloc_noio_save(). The code is right, so a better
comment might be:
/* Avoid recursing into I/O or fs reclaim while holding profile->lock */
> static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
> __releases(&profile->lock)
> {
> + memalloc_noio_restore(profile->memflags);
Storing the flags in blk_crypto_profile isn't necessary. It should just
be a stack variable, returned by blk_crypto_hw_enter() and consumed by
blk_crypto_hw_exit().
- Eric
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 1:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 18:14 [PATCH v2] blk-crypto: use a NOIO scope while holding profile->lock Laveesh Bansal
2026-11-25 1: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