dm era: read the metadata again at the first resume
Subsystems (2)
PATCH 1/1 dm era: read the metadata again at the first resume
When a table is loaded for a device that already has an active era table, the new table's constructor opens the metadata device and reads the superblock into its own struct era_metadata, while the old table keeps using and committing the metadata until it is suspended. The new table's view is then stale, and its first commit, in preresume, writes it over what the old table committed after the load:
dmsetup reload era --table "..." dd if=/dev/zero of=/dev/mapper/era bs=64k seek=500 count=1 oflag=direct dmsetup suspend era dmsetup resume era
The write to block 500 is missing from the era metadata afterwards. The stale view also refers to metadata blocks that the old table may have freed and reused since. A reload is how an era device is resized, so this is not an unusual thing to do.
A status request for the inactive table commits the stale view too, while the old table is still active.
Read the metadata again at the first preresume, when the old table has been suspended and has done its final commit. This is done in the worker, which is the only user of the metadata. Until then the target doesn't write to the metadata device: status doesn't commit, the worker doesn't digest writesets, and postsuspend does nothing.
The resize that follows is moved into the worker as well. It was done directly from preresume while the worker could be running, so a status request at that time could start digesting writesets into the era array while it was being resized, and commit while preresume was committing.
---
An AI coding assistant (Claude) was used to review this patch, to move the resize into the worker, and to help with the commit message.
Tested on 7.4-rc4 in a VM with lockdep and PROVE_RCU enabled: writes between reload and resume, status and clear of an inactive table, and shrinking and growing by reload, with the metadata checked by era_check and era_invalidate afterwards.
drivers/md/dm-era-target.c | 117 ++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 35 deletions(-)
drivers/md/dm-era-target.c +82 -35
@@ -1187,6 +1187,13 @@ struct era {int sectors_per_block_shift;struct era_metadata *md; +/* +* Set by the first preresume, once the metadata has been read again +* and resized. Until then another table may still be using the +* metadata device, and nothing may be written to it. +*/ +bool activated; +struct workqueue_struct *wq;struct work_struct worker;
@@ -1205,7 +1212,9 @@ struct rpc {int (*fn0)(struct era_metadata *md);int (*fn1)(struct era_metadata *md,void *ref); +int (*fn_era)(struct era *era);void *arg; +bool no_commit;int result;struct completion complete;
@@ -1338,8 +1347,15 @@ static void process_rpc_calls(struct era *era)spin_unlock (&era->rpc_lock);list_for_each_entry_safe (rpc, tmp, &calls, list) { - rpc->result = rpc->fn0 ? rpc->fn0 (era->md) : rpc->fn1 (era->md, rpc->arg); - need_commit = true; +if (rpc->fn_era) + rpc->result = rpc->fn_era (era); +else if (rpc->fn0) + rpc->result = rpc->fn0 (era->md); +else + rpc->result = rpc->fn1 (era->md, rpc->arg); + +if (!rpc->no_commit) + need_commit = true; }if (need_commit) {
@@ -1365,8 +1381,10 @@ static void do_work(struct work_struct *ws) { struct era *era = container_of(ws, struct era, worker); -kick_off_digest (era); -process_old_eras (era); +if (era->activated) { +kick_off_digest (era); +process_old_eras (era); + }process_deferred_bios (era);process_rpc_calls (era); }
@@ -1400,22 +1418,7 @@ static int perform_rpc(struct era *era, struct rpc *rpc)static int in_worker0 (struct era *era,int (*fn)(struct era_metadata *md)) { -struct rpc rpc; - - rpc.fn0 = fn; - rpc.fn1 =NULL ; - -return perform_rpc (era, &rpc); -} - -static int in_worker1 (struct era *era, -int (*fn)(struct era_metadata *md,void *ref),void *arg) -{ -struct rpc rpc; - - rpc.fn0 =NULL ; - rpc.fn1 = fn; - rpc.arg = arg; +struct rpc rpc = { .fn0 = fn };return perform_rpc (era, &rpc); }
@@ -1596,6 +1599,10 @@ static void era_postsuspend(struct dm_target *ti)int r;struct era *era = ti->private; +/* Never resumed, so our view of the metadata may be stale. */ +if (!era->activated) +return ; + r =in_worker0 (era, metadata_era_archive);if (r) {DMERR ("%s: couldn't archive current era" , __func__);
@@ -1611,30 +1618,64 @@ static void era_postsuspend(struct dm_target *ti) } } -static int era_preresume (struct dm_target *ti) +/* +* The metadata was opened by the constructor, but if this table replaces +* another one then that table kept changing the metadata until it was +* suspended. So read it again now that we are the only user. +* +* Runs in the worker, and commits by itself so that nothing is written if +* the metadata can't be read. +*/ +static int era_activate (struct era *era) {int r; -struct era *era = ti->private; +struct era_metadata *md;dm_block_t new_size =calc_nr_blocks (era); -if (era->nr_blocks != new_size) { - r =metadata_resize (era->md, &new_size); -if (r) { -DMERR ("%s: metadata_resize failed" , __func__); -return r; - } + md =metadata_open (era->metadata_dev->bdev, era->sectors_per_block, + false); +if (IS_ERR (md)) { +DMERR ("%s: couldn't reopen metadata" , __func__); +return PTR_ERR (md); + } - r =metadata_commit (era->md); -if (r) { -DMERR ("%s: metadata_commit failed" , __func__); -return r; - } +metadata_close (era->md); + era->md = md; +memset (&era->digest,0 ,sizeof (era->digest)); + + r =metadata_resize (md, &new_size); +if (r) { +DMERR ("%s: metadata_resize failed" , __func__); +return r; + } - era->nr_blocks = new_size; + r =metadata_commit (md); +if (r) { +DMERR ("%s: metadata_commit failed" , __func__); +return r; } + era->nr_blocks = new_size; +WRITE_ONCE (era->activated, true); + +return 0 ; +} + +static int era_preresume (struct dm_target *ti) +{ +int r; +struct era *era = ti->private; +start_worker (era); +if (!era->activated) { +struct rpc rpc = { .fn_era = era_activate, .no_commit = true }; + + r =perform_rpc (era, &rpc); +if (r) +return r; + } + r =in_worker0 (era, metadata_era_rollover);if (r) {DMERR ("%s: metadata_era_rollover failed" , __func__);
@@ -1658,10 +1699,16 @@ static void era_status(struct dm_target *ti, status_type_t type,ssize_t sz =0 ;struct metadata_stats stats;char buf[BDEVNAME_SIZE ]; +struct rpc rpc = { .fn1 = metadata_get_stats, .arg = &stats };switch (type) {case STATUSTYPE_INFO : - r =in_worker1 (era, metadata_get_stats, &stats); +/* +* Before the first resume our view of the metadata may be +* stale and must not be written. +*/ + rpc.no_commit = !READ_ONCE (era->activated); + r =perform_rpc (era, &rpc);if (r) goto err;
base-commit: ebb7a8dc1e74c705daffe11c2faf38ec2628f838
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] dm era: missing activation check in era_message() causes NULL deref - [High] dm era: era_activate()…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] dm era: missing activation check in era_message() causes NULL deref
- [High] dm era: era_activate() reopen loses uncommitted space map index
- [Medium] dm era: era_preresume() starts digest before rollover and leaks worker
- [Low] dm era: unused nr_blocks field in struct era after activation refactor
--
commit a2950a4dea8dcd63e94b69dde9b3323092c96aec
Author: Ronnie Sahlberg <[email protected]>
dm era: read the metadata again at the first resume
This commit re-reads and resizes the era metadata in the worker at the first preresume, and suppresses metadata writes and writeset digestion until the target has been activated.
Quoted 4 lines
drivers/md/dm-era-target.c +0
-0
[ ... ]
Quoted 30 lines
drivers/md/dm-era-target.c +14
-8@@ -1611,30 +1618,64 @@ static void era_postsuspend(struct dm_target *ti) } } -static int era_preresume (struct dm_target *ti) +/* +* The metadata was opened by the constructor, but if this table replaces +* another one then that table kept changing the metadata until it was +* suspended. So read it again now that we are the only user. +* +* Runs in the worker, and commits by itself so that nothing is written if +* the metadata can't be read. +*/ +static int era_activate (struct era *era) {int r; -struct era *era = ti->private; +struct era_metadata *md;dm_block_t new_size =calc_nr_blocks (era); -if (era->nr_blocks != new_size) { - r =metadata_resize (era->md, &new_size); -if (r) { -DMERR ("%s: metadata_resize failed" , __func__); -return r; - } + md =metadata_open (era->metadata_dev->bdev, era->sectors_per_block, + false); +if (IS_ERR (md)) { +DMERR ("%s: couldn't reopen metadata" , __func__);
drivers/md/dm-era-target.c static void era_postsuspend(struct dm_target *ti) · line 1643
Quoted 10 lines
drivers/md/dm-era-target.c +4
-5+return PTR_ERR (md); + } - r =metadata_commit (era->md); -if (r) { -DMERR ("%s: metadata_commit failed" , __func__); -return r; - } +metadata_close (era->md); + era->md = md;
[Severity: High]
When a fresh metadata device is formatted during era_ctr(), does closing era->md and reopening it from disk here discard uncommitted in-memory space map index updates?
In create_fresh_metadata(), dm_tm_create_with_sm() commits the initial space map, and then dm_btree_empty() and dm_array_empty() allocate two blocks for writeset_tree_root and era_array_root. Those allocations update smm->ll.mi_le.index[0] in memory, but write_superblock() calls save_sm_root() and dm_tm_commit() without calling dm_tm_pre_commit() first:
drivers/md/dm-era-target.c:create_fresh_metadata() {
...
r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
...
r = dm_array_empty(&md->era_array_info, &md->era_array_root);
...
r = write_superblock(md);
}Because dm_tm_pre_commit() was not called, sm_metadata_commit() never wrote the updated smm->ll.mi_le to the on-disk bitmap_root block. Previously the first metadata_commit() in preresume flushed the in-memory era->md created by era_ctr(); now metadata_close(era->md) discards it and metadata_open() loads the stale bitmap_root from disk, leaving index[0]'s nr_free count permanently off by 2 blocks.
Quoted 6 lines
drivers/md/dm-era-target.c +6
-0+memset (&era->digest,0 ,sizeof (era->digest)); + + r =metadata_resize (md, &new_size); +if (r) { +DMERR ("%s: metadata_resize failed" , __func__); +return r;
drivers/md/dm-era-target.c static void era_postsuspend(struct dm_target *ti) · line 1658
Quoted 10 lines
drivers/md/dm-era-target.c +6
-1+ } - era->nr_blocks = new_size; + r =metadata_commit (md); +if (r) { +DMERR ("%s: metadata_commit failed" , __func__); +return r; } + era->nr_blocks = new_size;
[Severity: Low]
With the era->nr_blocks != new_size check removed from era_preresume(), era->nr_blocks is now only ever written to and never read (all other places use era->md->nr_blocks). Can nr_blocks be removed from struct era?
Quoted 23 lines
drivers/md/dm-era-target.c +18
-0+WRITE_ONCE (era->activated, true); + +return 0 ; +} + +static int era_preresume (struct dm_target *ti) +{ +int r; +struct era *era = ti->private; +start_worker (era); +if (!era->activated) { +struct rpc rpc = { .fn_era = era_activate, .no_commit = true }; + + r =perform_rpc (era, &rpc); +if (r) +return r; + } + r =in_worker0 (era, metadata_era_rollover);if (r) {DMERR ("%s: metadata_era_rollover failed" , __func__);
drivers/md/dm-era-target.c
Quoted 2 lines
drivers/md/dm-era-target.c +0
-0return r; }
[Severity: Medium]
If in_worker0(era, metadata_era_rollover) fails here (or a later target in the table fails preresume), does era_preresume() leave the worker running and digesting writesets on a still-suspended device?
era_activate() sets era->activated = true before the rollover RPC is queued, and open_metadata() sets md->archived_writesets = true. When in_worker0() wakes do_work() for the rollover RPC, do_work() sees era->activated == true and runs kick_off_digest() and process_old_eras() before process_rpc_calls(), which re-queues era->worker via wake_worker(). Because the error paths in era_preresume() do not call stop_worker(era), the worker continues running and mutating era->md in the background while the device remains suspended, and era_dtr() can later destroy the failed table without era_postsuspend() having stopped the worker.
Quoted 10 lines
drivers/md/dm-era-target.c +1
-0return 0 ; }@@ -1658,10 +1699,16 @@ static void era_status(struct dm_target *ti, status_type_t type,ssize_t sz =0 ;struct metadata_stats stats;char buf[BDEVNAME_SIZE ]; +struct rpc rpc = { .fn1 = metadata_get_stats, .arg = &stats };switch (type) {
drivers/md/dm-era-target.c static void era_status(struct dm_target *ti, status_type_t type, · line 1713
Quoted 10 lines
drivers/md/dm-era-target.c +6
-1case STATUSTYPE_INFO : - r =in_worker1 (era, metadata_get_stats, &stats); +/* +* Before the first resume our view of the metadata may be +* stale and must not be written. +*/ + rpc.no_commit = !READ_ONCE (era->activated); + r =perform_rpc (era, &rpc);if (r) goto err;
[Severity: High]
Should era_message() also reject messages while !READ_ONCE(era->activated)?
In do_resume(), dm_swap_table() installs the new table as live and drops md->suspend_lock before dm_resume() calls era_preresume(). If era_activate() fails (or a message arrives after dm_swap_table() before era_activate() completes), the table is live with era->activated == false, era->suspended == 0, and era->md->writesets[0..1].bits == NULL (since writeset bitmaps are only allocated by metadata_resize() in era_activate()). A "checkpoint" or "take_metadata_snap" message then runs metadata_new_era() -> writeset_init() in the worker and dereferences a NULL ws->bits pointer when md->nr_blocks > 0, while "drop_metadata_snap" commits the stale era->md to disk via in_worker0().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Please disregard this version. The Sashiko review is right about the space map index being lost when freshly formatted metadata is reopened, and the patch overlaps with Denis V. Lunev's series "dm era: a metadata device that never comes back". I will send a v2 based on top of that series.
Quoted 194 lines · On Thu, 26 Nov 2026 at 12:22, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] dm era: missing activation check inera_message()causes NULL deref
- [High] dm era:era_activate()reopen loses uncommitted space map index
- [Medium] dm era:era_preresume()starts digest before rollover and leaks worker
- [Low] dm era: unusednr_blocksfield in struct era after activation refactor
--commit
a2950a4dea8dcd63e94b69dde9b3323092c96aec
Author: Ronnie Sahlberg <[email protected]>dm era: read the metadata again at the first resume
This commit re-reads and resizes the era metadata in the worker at the first preresume, and suppresses metadata writes and writeset digestion until the target has been activated.
Quoted 4 lines
drivers/md/dm-era-target.c +0
-0[ ... ]
Quoted 30 lines
drivers/md/dm-era-target.c +14
-8@@ -1611,30 +1618,64 @@ static void era_postsuspend(struct dm_target *ti) } } -static int era_preresume (struct dm_target *ti) +/* +* The metadata was opened by the constructor, but if this table replaces +* another one then that table kept changing the metadata until it was +* suspended. So read it again now that we are the only user. +* +* Runs in the worker, and commits by itself so that nothing is written if +* the metadata can't be read. +*/ +static int era_activate (struct era *era) {int r; -struct era *era = ti->private; +struct era_metadata *md;dm_block_t new_size =calc_nr_blocks (era); -if (era->nr_blocks != new_size) { - r =metadata_resize (era->md, &new_size); -if (r) { -DMERR ("%s: metadata_resize failed" , __func__); -return r; - } + md =metadata_open (era->metadata_dev->bdev, era->sectors_per_block, + false); +if (IS_ERR (md)) { +DMERR ("%s: couldn't reopen metadata" , __func__);drivers/md/dm-era-target.c static void era_postsuspend(struct dm_target *ti) · line 1643
Quoted 10 lines
drivers/md/dm-era-target.c +4
-5+return PTR_ERR (md); + } - r =metadata_commit (era->md); -if (r) { -DMERR ("%s: metadata_commit failed" , __func__); -return r; - } +metadata_close (era->md); + era->md = md;[Severity: High]
When a fresh metadata device is formatted duringera_ctr(), does closingera->mdand reopening it from disk here discard uncommitted in-memory space map index updates?In
create_fresh_metadata(),dm_tm_create_with_sm()commits the initial space map, and thendm_btree_empty()anddm_array_empty()allocate two blocks forwriteset_tree_rootandera_array_root. Those allocations update smm->ll.mi_le.index[0] in memory, butwrite_superblock()callssave_sm_root()anddm_tm_commit()without callingdm_tm_pre_commit()first:drivers/md/dm-era-target.c:create_fresh_metadata() { ... r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root); ... r = dm_array_empty(&md->era_array_info, &md->era_array_root); ... r = write_superblock(md); }Because
dm_tm_pre_commit()was not called,sm_metadata_commit()never wrote the updatedsmm->ll.mi_leto the on-diskbitmap_rootblock. Previously the firstmetadata_commit()in preresume flushed the in-memoryera->mdcreated byera_ctr(); nowmetadata_close(era->md)discards it andmetadata_open()loads the stalebitmap_rootfrom disk, leaving index[0]'snr_freecount permanently off by 2 blocks.Quoted 6 lines
drivers/md/dm-era-target.c +6
-0+memset (&era->digest,0 ,sizeof (era->digest)); + + r =metadata_resize (md, &new_size); +if (r) { +DMERR ("%s: metadata_resize failed" , __func__); +return r;drivers/md/dm-era-target.c static void era_postsuspend(struct dm_target *ti) · line 1658
Quoted 10 lines
drivers/md/dm-era-target.c +6
-1+ } - era->nr_blocks = new_size; + r =metadata_commit (md); +if (r) { +DMERR ("%s: metadata_commit failed" , __func__); +return r; } + era->nr_blocks = new_size;[Severity: Low]
With theera->nr_blocks!=new_sizecheck removed fromera_preresume(),era->nr_blocksis now only ever written to and never read (all other places useera->md->nr_blocks). Cannr_blocksbe removed from struct era?Quoted 23 lines
drivers/md/dm-era-target.c +18
-0+WRITE_ONCE (era->activated, true); + +return 0 ; +} + +static int era_preresume (struct dm_target *ti) +{ +int r; +struct era *era = ti->private; +start_worker (era); +if (!era->activated) { +struct rpc rpc = { .fn_era = era_activate, .no_commit = true }; + + r =perform_rpc (era, &rpc); +if (r) +return r; + } + r =in_worker0 (era, metadata_era_rollover);if (r) {DMERR ("%s: metadata_era_rollover failed" , __func__);Quoted 2 lines
return r; }[Severity: Medium]
Ifin_worker0(era, metadata_era_rollover)fails here (or a later target in the table fails preresume), doesera_preresume()leave the worker running and digesting writesets on a still-suspended device?
era_activate()setsera->activated= true before the rollover RPC is queued, andopen_metadata()setsmd->archived_writesets= true. Whenin_worker0()wakesdo_work()for the rollover RPC,do_work()seesera->activated== true and runskick_off_digest()andprocess_old_eras()beforeprocess_rpc_calls(), which re-queuesera->workerviawake_worker(). Because the error paths inera_preresume()do not callstop_worker(era), the worker continues running and mutatingera->mdin the background while the device remains suspended, andera_dtr()can later destroy the failed table withoutera_postsuspend()having stopped the worker.Quoted 10 lines
return 0;}
drivers/md/dm-era-target.c +1
-0@@ -1658,10 +1699,16 @@ static void era_status(struct dm_target *ti, status_type_t type,ssize_t sz =0 ;struct metadata_stats stats;char buf[BDEVNAME_SIZE ]; +struct rpc rpc = { .fn1 = metadata_get_stats, .arg = &stats };switch (type) {drivers/md/dm-era-target.c static void era_status(struct dm_target *ti, status_type_t type, · line 1713
Quoted 10 lines
drivers/md/dm-era-target.c +6
-1case STATUSTYPE_INFO : - r =in_worker1 (era, metadata_get_stats, &stats); +/* +* Before the first resume our view of the metadata may be +* stale and must not be written. +*/ + rpc.no_commit = !READ_ONCE (era->activated); + r =perform_rpc (era, &rpc);if (r) goto err;[Severity: High]
Shouldera_message()also reject messages while !READ_ONCE(era->activated)?In
do_resume(),dm_swap_table()installs the new table as live and dropsmd->suspend_lockbeforedm_resume()callsera_preresume(). Ifera_activate()fails (or a message arrives afterdm_swap_table()beforeera_activate()completes), the table is live withera->activated== false,era->suspended== 0, and era->md->writesets[0..1].bits == NULL (since writeset bitmaps are only allocated bymetadata_resize()inera_activate()). A "checkpoint" or "take_metadata_snap" message then runsmetadata_new_era()->writeset_init()in the worker and dereferences a NULLws->bitspointer whenmd->nr_blocks> 0, while "drop_metadata_snap" commits the staleera->mdto disk viain_worker0().--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1