dm era: a metadata device that never comes back
Subsystems (2)
v3 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.
v2: https://lore.kernel.org/dm-devel/[email protected]/
v1: https://lore.kernel.org/dm-devel/[email protected]/
Changes since v2:
- patch 1 releases the block allocated for the copy on the paths that fail, which v2 left leaked. v2 argued that releasing it is wrong while the space map is in bootstrap mode, where blocks are handed out without a count to decrement. That was wrong: sm_ll_extend() calls dm_tm_new_block() in exactly that mode, and its error path already decrements the block it allocated, so the two neighbours differed for no reason. Reported by the Sashiko AI review.
- the unwind label that takes the original's reference back is named for what it does rather than for releasing one.
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.
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 | 13 ++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
PATCH v3 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. The block allocated for the copy is lost on those same paths.
metadata_ll_commit() shows what the dropped reference 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 and release the copy's block, the way dm_tm_new_block() above already does.
The decrement of the original stays where it was. Moving it past the copy 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.
--- drivers/md/persistent-data/dm-transaction-manager.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/persistent-data/dm-transaction-manager.c +10 -3
@@ -299,11 +299,11 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, r =dm_sm_dec_block (tm->sm, orig);if (r <0 ) -return r; + goto out_free_new; r =dm_bm_read_lock (tm->bm, orig, v, &orig_block);if (r <0 ) -return r; + goto out_inc_orig;/* * It would be tempting to use dm_bm_unlock_move here, but some
@@ -315,13 +315,20 @@ 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_inc_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_inc_orig: +/* The caller goes on using orig when the shadow fails. */ +dm_sm_inc_block (tm->sm, orig); +out_free_new: +dm_sm_dec_block (tm->sm, new);return r; }
Signature · 1 line
2.55.0
PATCH v3 2/3 dm era: refuse metadata rpcs once the target is suspended
In reply to Denis V. LunevFrom: 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.
--- drivers/md/dm-era-target.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/md/dm-era-target.c +8 -1
@@ -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
PATCH v3 3/3 dm era: construct the target suspended
In reply to Denis V. LunevFrom: 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.
--- drivers/md/dm-era-target.c | 1 + 1 file changed, 1 insertion(+)
drivers/md/dm-era-target.c +1 -0
@@ -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
Re: [PATCH v3 2/3] dm era: refuse metadata rpcs once the target is suspended
In reply to Denis V. LunevQuoted 29 lines · 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 indm_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 afterdrain_workqueue()has returned. The worker then commits the metadata beside the commitera_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 failedAdd 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.
Quoted 7 lines
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")Signed-off-by Denis V. Lunev <[email protected]>
Re: [PATCH v3 3/3] dm era: construct the target suspended
In reply to Denis V. LunevQuoted 24 lines · 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, soera->suspendedreads 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 failedEvery 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.
Quoted 8 lines
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+Signed-off-by Denis V. Lunev <[email protected]>