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.

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 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.
>
> > diff --git a/drivers/md/dm-era-target.c b/drivers/md/dm-era-target.c
> > index ea499adca4ce2..801ca632581b4 100644
> > --- a/drivers/md/dm-era-target.c
> > +++ b/drivers/md/dm-era-target.c
> [ ... ]
> > @@ -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;
>
> [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.
>
> > +     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;
>
> [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?
>
> > +     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__);
> >               return 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.
>
> >
> >       return 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) {
> >       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;
>
> [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().
>
> --
> Sashiko AI review ยท 
> https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to