dm era: a metadata device that never comes back
Subsystems (2)
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.
Denis V. Lunev (3):
dm persistent data: drop the original block only once it is copied 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, 17 insertions(+), 6 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/3 dm persistent data: drop the original block only once it is copied
From: Denis V. Lunev <[email protected]>
__shadow_block() releases the original block's reference before it reads the block, so every error return in between 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 btree caller ends up in a different state. dm_btree_insert() publishes the new root only on success, so the caller keeps a root whose count has dropped to zero, and the next dm_tm_shadow_block() of it finds dm_sm_count_is_more_than_one() false and writes the block in place instead of copying it.
Decrement the original once the copy holds its contents. A read of a metadata block can fail for a transient reason, and a single failure is enough to get here.
--- drivers/md/persistent-data/dm-transaction-manager.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/md/persistent-data/dm-transaction-manager.c +8 -5
@@ -297,10 +297,6 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,if (r <0 )return r; - r =dm_sm_dec_block (tm->sm, orig); -if (r <0 ) -return r; - r =dm_bm_read_lock (tm->bm, orig, v, &orig_block);if (r <0 )return r;
@@ -322,7 +318,14 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, dm_bm_block_size(tm->bm));dm_bm_unlock (orig_block); -return r; + + r =dm_sm_dec_block (tm->sm, orig); +if (r <0 ) { +dm_bm_unlock (*result); +return r; + } + +return 0 ; } int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 1/3] dm persistent data: drop the original block only once it is copied
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] dm persistent data: __shadow_block() exceeds max concurrent locks - [Medium] dm persistent data…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] dm persistent data: __shadow_block() exceeds max concurrent locks
- [Medium] dm persistent data: space map block leak on error in __shadow_block()
--
commit f51a9340a7ed4ff57bea163d1f75e930e3c7038c
Author: Denis V. Lunev <[email protected]>
dm persistent data: drop the original block only once it is copied
This commit moves the dm_sm_dec_block() call in __shadow_block() after the original block is read, copied into the newly allocated block, and unlocked.
Quoted 19 lines
drivers/md/persistent-data/dm-transaction-manager.c +0
-4@@ -297,10 +297,6 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,if (r <0 )return r; - r =dm_sm_dec_block (tm->sm, orig); -if (r <0 ) -return r; - r =dm_bm_read_lock (tm->bm, orig, v, &orig_block);if (r <0 )return r;@@ -322,7 +318,14 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, dm_bm_block_size(tm->bm));dm_bm_unlock (orig_block);
drivers/md/persistent-data/dm-transaction-manager.c static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig, · line 329
Quoted 10 lines
drivers/md/persistent-data/dm-transaction-manager.c +8
-1-return r; + + r =dm_sm_dec_block (tm->sm, orig); +if (r <0 ) { +dm_bm_unlock (*result); +return r; + } + +return 0 ; }
[Severity: High]
Can calling dm_sm_dec_block() while *result is still write-locked exceed the maximum concurrent block locks reserved by dm-bufio and deadlock under memory pressure?
Previously, __shadow_block() ran in three sequential phases that each held at most two locks internally: dm_sm_new_block(), dm_sm_dec_block(), and dm_bm_read_lock(orig) + dm_bm_write_lock_zero(new). Commit 490ae017f54e ("dm thin metadata: THIN_MAX_CONCURRENT_LOCKS should be 6") sized THIN_MAX_CONCURRENT_LOCKS to 6 for rebalance3(), which holds 4 locks (2 in the shadow spine plus left and center children) when init_child() calls dm_tm_shadow_block() for the right child (4 + 2 = 6).
With dm_sm_dec_block(tm->sm, orig) moved after dm_bm_write_lock_zero(), *result is already held when dm_sm_dec_block() runs. When smm->recursion_count is 0 and orig's bitmap block has not been shadowed yet in the current transaction, sm_metadata_dec_blocks() -> sm_ll_dec() -> __sm_ll_dec() -> shadow_bitmap() recursively enters __shadow_block() for the bitmap block and takes dm_bm_read_lock() and dm_bm_write_lock_zero(). That brings the peak lock count to caller_locks + 3, reaching 7 concurrent locks in rebalance3() (exceeding THIN_MAX_CONCURRENT_LOCKS = 6) and 6 in rebalance2() (exceeding ERA_MAX_CONCURRENT_LOCKS = 5, CACHE_MAX_CONCURRENT_LOCKS = 5, and DM_CLONE_MAX_CONCURRENT_LOCKS = 5). When dm-bufio falls back to its reserved_buffers pool under memory pressure, a single metadata thread holding all reserved buffers cannot evict its own held buffers and blocks forever in __alloc_buffer_wait_no_callback() -> __wait_for_free_buffer().
[Severity: Medium]
Does __shadow_block() also leak the newly allocated block new in the space map when dm_bm_read_lock(), dm_bm_write_lock_zero(), or dm_sm_dec_block() fails?
At the start of __shadow_block(), dm_sm_new_block(tm->sm, &new) allocates new with a reference count of 1. If a transient read error in dm_bm_read_lock() (or a failure in dm_bm_write_lock_zero() or dm_sm_dec_block(tm->sm, orig)) aborts the shadow and a subsequent commit succeeds, new is never released with dm_sm_dec_block(tm->sm, new) (unlike dm_tm_new_block() right above, which decrements new_block on error) and remains permanently allocated in the space map with no metadata pointing to it. On the dm_sm_dec_block(tm->sm, orig) failure path, dm_bm_unlock(*result) also marks the buffer for new dirty in dm-bufio.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 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 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 refused 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