Skip to contentSkip to conversation

dm era: a metadata device that never comes back

PATCH New Denis V. Lunev 2 participants 5 messages Nov 24

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

v2 cover dm era: a metadata device that never comes back

We move running containers between machines without restarting them, and their block volumes travel with them, so dm-era tells us which blocks changed between copy passes.

About one move in three ended with the volume dead rather than moved:
the metadata broke, every commit after it failed, the target reported "era Error", and the writes it tracked began to fail, which takes the workload's filesystem read-only. Rebuilding the device recovers the tracking, not the filesystem.

Three unrelated causes, all of which the kernel log blames on the space map, which is the victim. Two need no bad luck: multipathd asks every target that appears for its status, so one is asked exactly while it resumes, and again while it suspends. The third is older and wider - a transient read failure in the persistent-data library leaves the space map short, and the next commit makes that permanent. dm-thin and dm-cache reach it too, where it costs a transaction rather than the device, because they can abort and dm-era cannot.

Each patch is reproduced on a kernel whose only modification is the fix under test. Patch 3 must not be backported before patch 2, nor before cca2c6aebe86, where the resize goes through the worker the two would refuse.

v1: https://lore.kernel.org/dm-devel/[email protected]/

Changes since v1:

- patch 1 no longer moves the decrement past the copy. Holding the copy's write lock across dm_sm_dec_block() raises the number of blocks one task holds, because the decrement starts by shadowing a bitmap block of its own, and the targets size their dm-bufio reserve for the peak without that. The reference is put back on the paths that fail instead, which leaves the order, and the peak, as they were. Reported by the Sashiko AI review.
- patch 1 no longer releases the block allocated for the copy. That block is leaked on the error paths, as it was before the patch, but releasing it is wrong while the space map is in bootstrap mode, where blocks are handed out without a count to decrement.

Denis V. Lunev (3):
dm persistent data: put back the reference a failed shadow dropped dm era: refuse metadata rpcs once the target is suspended dm era: construct the target suspended

 drivers/md/dm-era-target.c                          | 10 +++++++++-
 drivers/md/persistent-data/dm-transaction-manager.c |  9 +++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/3 dm persistent data: put back the reference a failed shadow dropped

From: Denis V. Lunev <[email protected]>

__shadow_block() drops the original block's reference before it reads the block, and every error return after that leaves the caller pointing at a block the space map has given away.

metadata_ll_commit() shows what that costs. It stores the new block in ll->bitmap_root only when the shadow succeeds, so a failed read leaves the space map's index root at a block whose count has already reached zero. Every commit after that shadows the same block, fails to decrement it, and fails:

device-mapper: space map common: unable to decrement block
device-mapper: era: metadata_commit: pre commit failed

The metadata never recovers. dm-era reports "era Error" and fails the writes it was tracking, which takes the workload's filesystem read-only. A read of a metadata block can fail for a transient reason, and one failure is enough to get here.

Take the reference back where the shadow fails, rather than moving the decrement past the copy. Dropping it later would hold the copy's write lock across dm_sm_dec_block(), which starts by shadowing a bitmap block of its own and so takes two more locks, and the targets size their dm-bufio reserve for the peak without them. commit 490ae017f54e ("dm thin metadata: THIN_MAX_CONCURRENT_LOCKS should be 6") set thin's budget to exactly what rebalance3() reaches today.

Fixes 3241b1d3e0aa ("dm: add persistent data library")
---
 drivers/md/persistent-data/dm-transaction-manager.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/persistent-data/dm-transaction-manager.c +7 -2
index 7401add01d12..cf203ebb5110 100644
@@ -303,7 +303,7 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
 
 	r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
 	if (r < 0)
-		return r;
+		goto out_put_orig;
 
 	/*
 	 * It would be tempting to use dm_bm_unlock_move here, but some
@@ -315,13 +315,18 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
 	r = dm_bm_write_lock_zero(tm->bm, new, v, result);
 	if (r) {
 		dm_bm_unlock(orig_block);
-		return r;
+		goto out_put_orig;
 	}
 
 	memcpy(dm_block_data(*result), dm_block_data(orig_block),
 	       dm_bm_block_size(tm->bm));
 
 	dm_bm_unlock(orig_block);
+	return 0;
+
+out_put_orig:
+	/* The caller goes on using orig when the shadow fails. */
+	dm_sm_inc_block(tm->sm, orig);
 	return r;
 }
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/3] dm persistent data: put back the reference a failed shadow dropped

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dm persistent data: leaked new block in __shadow_block() on error --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dm persistent data: leaked new block in __shadow_block() on error
--

commit 03aa675fd4998658022986a6e963e9c3d3429569
Author: Denis V. Lunev <[email protected]>

dm persistent data: put back the reference a failed shadow dropped

This commit restores the original block's reference count via dm_sm_inc_block() when __shadow_block() fails to read the original block or write-lock the new block.

Quoted 22 lines
drivers/md/persistent-data/dm-transaction-manager.c +2 -2
index 7401add01d12e..cf203ebb5110e 100644
@@ -303,7 +303,7 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
 
 	r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
 	if (r < 0)
-		return r;
+		goto out_put_orig;
 
 	/*
 	 * It would be tempting to use dm_bm_unlock_move here, but some
@@ -315,13 +315,18 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
 	r = dm_bm_write_lock_zero(tm->bm, new, v, result);
 	if (r) {
 		dm_bm_unlock(orig_block);
-		return r;
+		goto out_put_orig;
 	}
 
 	memcpy(dm_block_data(*result), dm_block_data(orig_block),

drivers/md/persistent-data/dm-transaction-manager.c static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, · line 331

Quoted 10 lines
drivers/md/persistent-data/dm-transaction-manager.c +5 -0
 	       dm_bm_block_size(tm->bm));
 
 	dm_bm_unlock(orig_block);
+	return 0;
+
+out_put_orig:
+	/* The caller goes on using orig when the shadow fails. */
+	dm_sm_inc_block(tm->sm, orig);
 	return r;
 }

[Severity: Medium]
Does this leak the newly allocated block new in the space map when __shadow_block() fails?

__shadow_block() allocates new via dm_sm_new_block(tm->sm, &new) with a reference count of 1 before decrementing orig:

drivers/md/persistent-data/dm-transaction-manager.c:__shadow_block() {
	r = dm_sm_new_block(tm->sm, &new);
	if (r < 0)
		return r;

	r = dm_sm_dec_block(tm->sm, orig);
	if (r < 0)
		return r;
    ...
}

Unlike dm_tm_new_block(), which calls dm_sm_dec_block(tm->sm, new_block) on failure, neither out_put_orig nor the dm_sm_dec_block(tm->sm, orig) error path releases new. Once the target recovers from the transient failure and commits a later transaction, new remains permanently allocated in the on-disk space map with nothing referencing it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/3 dm era: refuse metadata rpcs once the target is suspended

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

An rpc reaches the worker in two steps: the caller puts it on the rpc list, then wake_worker() queues the work unless the target is suspended. stop_worker() raises that flag and drains the workqueue with no ordering against either step, so an rpc can land on both sides of the drain.

An rpc queued before the flag is raised, whose wake_worker() runs after it, is never served. The caller waits for a completion that only the next resume can deliver, and it waits holding the table srcu, so a reload blocks in dm_swap_table() and the device can no longer be reloaded or removed.

An rpc whose wake_worker() read the flag as clear can instead queue the work after drain_workqueue() has returned. The worker then commits the metadata beside the commit era_postsuspend() runs in its own thread. Both commits allocate from the same space map and take the same block, which corrupts it, and every commit after that fails:

device-mapper: space map common: unable to decrement block
device-mapper: era: metadata_commit: pre commit failed

Add the rpc and queue the work under the rpc lock, take that lock to raise the flag, and refuse an rpc that arrives once it is raised. An rpc that made it onto the list is then queued before the drain waits for it.

Userspace sees one change. A status query on a suspended era device used to block until the device was resumed, and now reports the target in error.

Fixes eec40579d848 ("dm: add era target")
---
 drivers/md/dm-era-target.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/md/dm-era-target.c +8 -1
index ea499adca4ce..29b0602e7849 100644
@@ -1389,10 +1389,14 @@ static int perform_rpc(struct era *era, struct rpc *rpc)
 	init_completion(&rpc->complete);
 
 	spin_lock(&era->rpc_lock);
+	if (atomic_read(&era->suspended)) {
+		spin_unlock(&era->rpc_lock);
+		return -EBUSY;
+	}
 	list_add(&rpc->list, &era->rpc_calls);
+	queue_work(era->wq, &era->worker);
 	spin_unlock(&era->rpc_lock);
 
-	wake_worker(era);
 	wait_for_completion(&rpc->complete);
 
 	return rpc->result;
@@ -1427,7 +1431,10 @@ static void start_worker(struct era *era)
 
 static void stop_worker(struct era *era)
 {
+	spin_lock(&era->rpc_lock);
 	atomic_set(&era->suspended, 1);
+	spin_unlock(&era->rpc_lock);
+
 	drain_workqueue(era->wq);
 }
 
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 3/3 dm era: construct the target suspended

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

era_preresume() resizes the era array and commits the metadata in the thread that resumes the device, and starts the worker only once that is done. That is safe only while the worker cannot run, and nothing makes it so: struct era is zeroed, so era->suspended reads as running from the moment the target is constructed, and an rpc arriving before the resume wakes the worker alongside the resize. Both allocate from the same space map and take the same metadata block, which corrupts it:

device-mapper: block manager: validator mismatch (old=array vs new=index)
device-mapper: space map common: unable to decrement block
device-mapper: era: metadata_commit: pre commit failed

Every commit after that fails the same way, the target reports "era Error", and the writes it tracks fail, which takes the workload's filesystem read-only.

Userspace gets there without trying. multipathd asks every device-mapper target appearing on the node for its status, over its own uevent socket, to learn whether the target is a multipath map, so a target is asked exactly while it resumes.

Mark the target suspended when it is constructed, which is the state start_worker() already assumes it is leaving. The resize and the commit then run with the worker held back, as they must, and a status query in that window is reported busy rather than served beside them.

Fixes cca2c6aebe86 ("dm era: only resize metadata in preresume")
Cc [email protected] # v5.12+
---
 drivers/md/dm-era-target.c | 1 +
 1 file changed, 1 insertion(+)
drivers/md/dm-era-target.c +1 -0
index 29b0602e7849..d40a98169b04 100644
@@ -1557,6 +1557,7 @@ static int era_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
 	spin_lock_init(&era->rpc_lock);
 	INIT_LIST_HEAD(&era->rpc_calls);
+	atomic_set(&era->suspended, 1);
 
 	ti->private = era;
 	ti->num_flush_bios = 1;
Signature · 1 line
2.55.0