Skip to contentSkip to conversation

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

PATCH New Laveesh Bansal 1 participant 1 message Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 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 I/O or fs reclaim. 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 v3:
- blk_crypto_hw_enter() now returns the saved flags and blk_crypto_hw_exit() takes them, instead of keeping them in the profile; comment reworded (Eric).
- Dropped the version from the stable tag (Eric).
- Did not carry Christoph's Reviewed-by from v2, since the code changed; Christoph, please re-add it if you are ok with this version.
v2: https://lore.kernel.org/r/[email protected]

Tested on arm64 and x86_64 (QEMU, base c44104f3642d, KASAN and lockdep):
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      v3
  lockdep cycle warning, arm64                2 of 2    0 of 8
  lockdep cycle warning, x86_64               6 of 6    0 of 6
  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 6
  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 13

No KASAN reports. With the hog using up memory, one of the 6 x86 runs stalled and never finished, and one of the 2 uid 65534 runs had a hung-task report but finished. In both, the task holding profile->lock was in reclaim from the setkey allocation, with kswapd waiting on the lock in key eviction, 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. The deadlock did not reproduce on ext4; fstests not run on x86_64.

 block/blk-crypto-profile.c | 53 ++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 19 deletions(-)
block/blk-crypto-profile.c +34 -19
index 53126c091b0b..62d3eaa4e186 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>
@@ -42,7 +43,8 @@ struct blk_crypto_keyslot {
 	struct blk_crypto_profile *profile;
 };
 
-static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
+static inline unsigned int
+blk_crypto_hw_enter(struct blk_crypto_profile *profile)
 	__acquires(&profile->lock)
 {
 	/*
@@ -53,11 +55,15 @@ 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);
+	/* Avoid recursing into I/O or fs reclaim while holding profile->lock */
+	return memalloc_noio_save();
 }
 
-static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
+static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile,
+				      unsigned int memflags)
 	__releases(&profile->lock)
 {
+	memalloc_noio_restore(memflags);
 	up_write(&profile->lock);
 	if (profile->dev)
 		pm_runtime_put_sync(profile->dev);
@@ -252,6 +258,7 @@ blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
 	struct blk_crypto_keyslot *slot;
 	int slot_idx;
 	int err;
+	unsigned int memflags;
 
 	*slot_ptr = NULL;
 
@@ -269,10 +276,10 @@ blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
 		goto success;
 
 	for (;;) {
-		blk_crypto_hw_enter(profile);
+		memflags = blk_crypto_hw_enter(profile);
 		slot = blk_crypto_find_and_grab_keyslot(profile, key);
 		if (slot) {
-			blk_crypto_hw_exit(profile);
+			blk_crypto_hw_exit(profile, memflags);
 			goto success;
 		}
 
@@ -283,7 +290,7 @@ blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
 		if (!list_empty(&profile->idle_slots))
 			break;
 
-		blk_crypto_hw_exit(profile);
+		blk_crypto_hw_exit(profile, memflags);
 		wait_event(profile->idle_slots_wait_queue,
 			   !list_empty(&profile->idle_slots));
 	}
@@ -295,7 +302,7 @@ blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
 	err = profile->ll_ops.keyslot_program(profile, key, slot_idx);
 	if (err) {
 		wake_up(&profile->idle_slots_wait_queue);
-		blk_crypto_hw_exit(profile);
+		blk_crypto_hw_exit(profile, memflags);
 		return errno_to_blk_status(err);
 	}
 
@@ -310,7 +317,7 @@ blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
 
 	blk_crypto_remove_slot_from_lru_list(slot);
 
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 success:
 	*slot_ptr = slot;
 	return BLK_STS_OK;
@@ -345,18 +352,19 @@ int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
 {
 	struct blk_crypto_keyslot *slot;
 	int err;
+	unsigned int memflags;
 
 	if (profile->num_slots == 0) {
 		if (profile->ll_ops.keyslot_evict) {
-			blk_crypto_hw_enter(profile);
+			memflags = blk_crypto_hw_enter(profile);
 			err = profile->ll_ops.keyslot_evict(profile, key, -1);
-			blk_crypto_hw_exit(profile);
+			blk_crypto_hw_exit(profile, memflags);
 			return err;
 		}
 		return 0;
 	}
 
-	blk_crypto_hw_enter(profile);
+	memflags = blk_crypto_hw_enter(profile);
 	slot = blk_crypto_find_keyslot(profile, key);
 	if (!slot) {
 		/*
@@ -382,7 +390,7 @@ int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
 	hlist_del(&slot->hash_node);
 	slot->key = NULL;
 out:
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 	return err;
 }
 
@@ -397,6 +405,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 +413,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 +424,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);
@@ -466,6 +477,7 @@ int blk_crypto_derive_sw_secret(struct block_device *bdev,
 	struct blk_crypto_profile *profile =
 		bdev_get_queue(bdev)->crypto_profile;
 	int err;
+	unsigned int memflags;
 
 	if (!profile)
 		return -EOPNOTSUPP;
@@ -473,10 +485,10 @@ int blk_crypto_derive_sw_secret(struct block_device *bdev,
 		return -EOPNOTSUPP;
 	if (!profile->ll_ops.derive_sw_secret)
 		return -EOPNOTSUPP;
-	blk_crypto_hw_enter(profile);
+	memflags = blk_crypto_hw_enter(profile);
 	err = profile->ll_ops.derive_sw_secret(profile, eph_key, eph_key_size,
 					       sw_secret);
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 	return err;
 }
 EXPORT_SYMBOL_GPL(blk_crypto_derive_sw_secret);
@@ -486,6 +498,7 @@ int blk_crypto_import_key(struct blk_crypto_profile *profile,
 			  u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
 {
 	int ret;
+	unsigned int memflags;
 
 	if (!profile)
 		return -EOPNOTSUPP;
@@ -493,10 +506,10 @@ int blk_crypto_import_key(struct blk_crypto_profile *profile,
 		return -EOPNOTSUPP;
 	if (!profile->ll_ops.import_key)
 		return -EOPNOTSUPP;
-	blk_crypto_hw_enter(profile);
+	memflags = blk_crypto_hw_enter(profile);
 	ret = profile->ll_ops.import_key(profile, raw_key, raw_key_size,
 					 lt_key);
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(blk_crypto_import_key);
@@ -505,6 +518,7 @@ int blk_crypto_generate_key(struct blk_crypto_profile *profile,
 			    u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
 {
 	int ret;
+	unsigned int memflags;
 
 	if (!profile)
 		return -EOPNOTSUPP;
@@ -512,9 +526,9 @@ int blk_crypto_generate_key(struct blk_crypto_profile *profile,
 		return -EOPNOTSUPP;
 	if (!profile->ll_ops.generate_key)
 		return -EOPNOTSUPP;
-	blk_crypto_hw_enter(profile);
+	memflags = blk_crypto_hw_enter(profile);
 	ret = profile->ll_ops.generate_key(profile, lt_key);
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(blk_crypto_generate_key);
@@ -524,6 +538,7 @@ int blk_crypto_prepare_key(struct blk_crypto_profile *profile,
 			   u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
 {
 	int ret;
+	unsigned int memflags;
 
 	if (!profile)
 		return -EOPNOTSUPP;
@@ -531,10 +546,10 @@ int blk_crypto_prepare_key(struct blk_crypto_profile *profile,
 		return -EOPNOTSUPP;
 	if (!profile->ll_ops.prepare_key)
 		return -EOPNOTSUPP;
-	blk_crypto_hw_enter(profile);
+	memflags = blk_crypto_hw_enter(profile);
 	ret = profile->ll_ops.prepare_key(profile, lt_key, lt_key_size,
 					  eph_key);
-	blk_crypto_hw_exit(profile);
+	blk_crypto_hw_exit(profile, memflags);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(blk_crypto_prepare_key);
Signature · 1 line
2.43.0