* [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback
@ 2026-11-22 21:31 Laveesh Bansal
2026-11-22 21:44 ` sashiko-bot
2026-11-23 9:31 ` Christoph Hellwig
0 siblings, 2 replies; 3+ messages 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 related [flat|nested] 3+ messages in thread* Re: [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback
2026-11-22 21:31 [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback Laveesh Bansal
@ 2026-11-22 21:44 ` sashiko-bot
2026-11-23 9:31 ` Christoph Hellwig
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-22 21:44 UTC (permalink / raw)
To: Laveesh Bansal, Jens Axboe, Eric Biggers
Cc: Christoph Hellwig, Theodore Y. Ts'o, Jaegeuk Kim,
syzbot+20d60aafd9a0d1f2a380, linux-fscrypt, linux-crypto
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] block: NULL deref and OOB read in blk-crypto-fallback with
num_keyslots=0 [1]
--
commit 7d0d16db31e8a22572290b726bc65b670e7fa101
Author: Laveesh Bansal <laveeshb@laveeshbansal.com>
blk-crypto: avoid fs reclaim under profile->lock in the fallback
This commit wraps the crypto_sync_skcipher_setkey() calls in the blk-crypto
fallback with memalloc_noio_save() to prevent direct reclaim from recursing
into filesystem key eviction while holding profile->lock.
> diff --git a/block/blk-crypto-fallback.c b/block/blk-crypto-fallback.c
> index 12e0b487e23ab..2df418f169328 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];
[Severity: High]
This problem wasn't introduced by this patch, but can setting the
num_keyslots module parameter to 0 cause an out-of-bounds read here and a
NULL pointer dereference in blk_crypto_keyslot_index() [1]?
When profile->num_slots is 0, __blk_crypto_evict_key() invokes
keyslot_evict() with slot set to -1, which blk_crypto_fallback_evict_keyslot()
uses as an unsigned index into the zero-length blk_crypto_keyslots[] array:
block/blk-crypto-profile.c:__blk_crypto_evict_key() {
if (profile->num_slots == 0) {
if (profile->ll_ops.keyslot_evict) {
blk_crypto_hw_enter(profile);
err = profile->ll_ops.keyslot_evict(profile, key, -1);
blk_crypto_hw_exit(profile);
return err;
}
return 0;
}
}
Similarly, during encryption with num_slots == 0, blk_crypto_get_keyslot()
returns BLK_STS_OK with *slot_ptr = NULL, and
blk_crypto_fallback_encrypt_bio() -> blk_crypto_fallback_tfm() ->
blk_crypto_keyslot_index() dereferences slot->profile.
[1] https://sashiko.dev/#/bug/linux-3e1346dd-cfa2-4a1f-89e7-2cad97d10dce
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122213120.20974-1-laveeshb@laveeshbansal.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback
2026-11-22 21:31 [PATCH] blk-crypto: avoid fs reclaim under profile->lock in the fallback Laveesh Bansal
2026-11-22 21:44 ` sashiko-bot
@ 2026-11-23 9:31 ` Christoph Hellwig
1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-23 9:31 UTC (permalink / raw)
To: Laveesh Bansal
Cc: Jens Axboe, Eric Biggers, Christoph Hellwig, Theodore Y. Ts'o,
Jaegeuk Kim, syzbot+20d60aafd9a0d1f2a380, linux-block,
linux-fscrypt, linux-f2fs-devel, linux-crypto, linux-kernel
On Sun, Nov 22, 2026 at 01:31:20PM -0800, Laveesh Bansal wrote:
> 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.
Pleawe move these into blk_crypto_hw_enter/exit so that it is tied
to the lock for which you enforce the NOIO context instead of hacking
around the issue for specific allocations.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-23 9:31 UTC | newest]
Thread overview: 3+ messages (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
2026-11-22 21:44 ` sashiko-bot
2026-11-23 9:31 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox