Skip to contentSkip to conversation

blk-crypto: use a NOIO scope while holding profile->lock

PATCH Superseded Laveesh Bansal 3 participants 5 messages Nov 23 – Nov 27

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 blk-crypto: use a NOIO scope while holding profile->lock

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")
Reported-by [email protected]

---
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/[email protected]

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(+)
block/blk-crypto-profile.c +7 -0
index 53126c091b0b..395829fe7bcc 100644
@@ -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);
include/linux/blk-crypto-profile.h +1 -0
index 4f39e9cd7576..9209b6a14b98 100644
@@ -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;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 1 line · Laveesh Bansal

setkey allocates with GFP_KERNEL (Adiantum)

We should just fix that one:
https://lore.kernel.org/linux-crypto/[email protected]/

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.

Quoted 2 lines
Fixes 488f6682c832 ("block: blk-crypto-fallback for Inline Encryption")

The v7.3+ should not be there.

block/blk-crypto-profile.c static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile) · line 59

Quoted 7 lines
block/blk-crypto-profile.c +2 -0
@@ -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 */

block/blk-crypto-profile.c static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile) · line 64

Quoted 4 lines
block/blk-crypto-profile.c +1 -0
 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

#4 lore raw

Hi Christoph

I sent v3 to address Eric's comments on v2.

blk_crypto_hw_enter() now returns the saved flags and blk_crypto_hw_exit() takes them, instead of keeping them in the profile. The comment is reworded and the version is gone from the stable tag:

https://lore.kernel.org/r/[email protected]

I did not carry over your Reviewed-by since the code changed. If you are ok with v3, please re-add it.

Thanks
Laveesh