Linux Device Mapper development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ronnie Sahlberg" <ronniesahlberg@gmail.com>, dm-devel@lists.linux.dev
Cc: agk@redhat.com, snitzer@kernel.org, mpatocka@redhat.com,
	bmarzins@redhat.com, ejt@redhat.com
Subject: Re: [PATCH] dm era: add lazy mode
Date: Tue, 24 Nov 2026 05:36:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-230557@kernel.org> (raw)
In-Reply-To: <20261124051653.346631-1-ronniesahlberg@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] dm era: premature dirty flag clear on resize commit in
  era_preresume()
- [High] dm era: lost write tracking on live rollover after archive
  failure
- [Medium] dm era: tracking_lost cleared after rollover commit in
  era_preresume()
- [Medium] dm era: on-disk bitset leak on archive failure in
  metadata_era_rollover_lazy()
- [Medium] dm era: stalled writeset digestion after process_rpc_calls()
  in do_work()
- [Low] dm era: unconditional atomic_set() in metadata_lazy_mark()
  dirties shared line
--

commit 2dc3d476acb2e69fd1c049996bc8d1fd635e17e8
Author: Ronnie Sahlberg <ronniesahlberg@gmail.com>

dm era: add lazy mode

This commit adds an optional lazy mode to dm-era that tracks newly written
blocks in memory and defers on-disk writeset updates to periodic or
suspend-time metadata commits, using a version 2 dirty superblock flag to
force full invalidation after an unclean shutdown.

> diff --git a/drivers/md/dm-era-target.c b/drivers/md/dm-era-target.c
> index ea499adca4ce2..fcf466f2e760f 100644
> --- a/drivers/md/dm-era-target.c
> +++ b/drivers/md/dm-era-target.c
[ ... ]
> @@ -505,11 +548,17 @@ static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
>  static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
>  {
>  	disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
> -	disk->flags = cpu_to_le32(0ul);
> +
> +	if (md->lazy_dirty || md->tracking_lost) {
> +		disk->flags = cpu_to_le32(SUPERBLOCK_FLAG_LAZY_DIRTY);
> +		disk->version = cpu_to_le32(ERA_VERSION_LAZY_DIRTY);
> +	} else {
> +		disk->flags = cpu_to_le32(0ul);
> +		disk->version = cpu_to_le32(MIN_ERA_VERSION);
> +	}
>  
>  	/* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
>  	memset(disk->uuid, 0, sizeof(disk->uuid));
> -	disk->version = cpu_to_le32(MAX_ERA_VERSION);
>  
>  	copy_sm_root(md, disk);
>  
> @@ -609,6 +658,12 @@ static int open_metadata(struct era_metadata *md)
>  	md->metadata_snap = le64_to_cpu(disk->metadata_snap);
>  	md->archived_writesets = true;
>  
> +	if (le32_to_cpu(disk->version) >= ERA_VERSION_LAZY_DIRTY ||
> +	    (le32_to_cpu(disk->flags) & SUPERBLOCK_FLAG_LAZY_DIRTY)) {
> +		DMWARN("metadata was not shut down cleanly, all blocks will be marked as written");
> +		md->needs_invalidate = true;
> +	}
> +
>  	dm_bm_unlock(sblock);
>  
>  	return 0;

[Severity: High]
Does prepare_superblock() also need to keep SUPERBLOCK_FLAG_LAZY_DIRTY set
while md->needs_invalidate is true?

When open_metadata() finds dirty metadata on disk, it sets
md->needs_invalidate = true while md->lazy_dirty and md->tracking_lost are
still false.  On initial activation era->nr_blocks is 0, so era_preresume()
always enters the resize branch and commits the superblock before rolling
over to the new all-ones era:

drivers/md/dm-era-target.c:era_preresume() {
	if (era->nr_blocks != new_size) {
		r = metadata_resize(era->md, &new_size);
		...
		r = metadata_commit(era->md);
		...
	}

	start_worker(era);
	...
	r = in_worker0(era, metadata_era_rollover);
}

That resize commit writes a clean version 1 superblock before
metadata_era_rollover() creates the invalidated writeset (and
process_rpc_calls() also commits even if metadata_era_rollover() fails).
If a crash or rollover failure happens after the resize commit, the next
activation will see clean metadata and trust the incomplete pre-crash
writesets.

[ ... ]

> @@ -892,22 +949,60 @@ static int metadata_resize(struct era_metadata *md, void *arg)
[ ... ]
> +static int __metadata_era_archive(struct era_metadata *md,
> +				  struct writeset *ws, uint32_t era)
>  {
>  	int r;
>  	uint64_t keys[1];
>  	struct writeset_disk value;
>  
> -	r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
> -			    &md->current_writeset->md.root);
> +	r = dm_bitset_flush(&md->bitset_info, ws->md.root, &ws->md.root);
>  	if (r) {
>  		DMERR("%s: dm_bitset_flush failed", __func__);
>  		return r;
>  	}
>  
> -	ws_pack(&md->current_writeset->md, &value);
> +	ws_pack(&ws->md, &value);
>  
> -	keys[0] = md->current_era;
> +	keys[0] = era;
>  	__dm_bless_for_disk(&value);
>  	r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
>  			    keys, &value, &md->writeset_tree_root);
> @@ -917,12 +1012,23 @@ static int metadata_era_archive(struct era_metadata *md)
>  		return r;
>  	}
>  
> -	md->current_writeset->md.root = INVALID_WRITESET_ROOT;
> +	ws->md.root = INVALID_WRITESET_ROOT;
>  	md->archived_writesets = true;
>  
>  	return 0;
>  }

[Severity: Medium]
Can archived writesets remain undigested in lazy mode after an RPC
rollover?

do_work() runs kick_off_digest() and process_old_eras() before
process_rpc_calls():

drivers/md/dm-era-target.c:do_work() {
	kick_off_digest(era);
	process_old_eras(era);
	process_deferred_bios(era);
	process_rpc_calls(era);
}

When metadata_era_rollover(), metadata_checkpoint(), or
metadata_take_snap() runs via process_rpc_calls(), __metadata_era_archive()
sets md->archived_writesets = true after kick_off_digest() has already run,
without re-arming era->worker.  In non-lazy mode the first write in the new
era called defer_bio() and woke the worker, but in lazy mode when
md->lazy_gate_open is already true (or when md->needs_invalidate filled the
new writeset with all ones), era_map() never defers bios and do_clean()
runs on era->clean_work, so digestion is never started to fold the archived
writesets into the era array and free their on-disk bitset blocks.

[ ... ]

> @@ -934,13 +1040,51 @@ static int metadata_new_era(struct era_metadata *md)
[ ... ]
> +/*
> + * era_map() sets bits without the worker in lazy mode, so switch it to the
> + * new writeset before archiving the old one.
> + */
> +static int metadata_era_rollover_lazy(struct era_metadata *md)
> +{
> +	int r;
> +	struct writeset *old_writeset = md->current_writeset;
> +	struct writeset *new_writeset = next_writeset(md);
> +
> +	r = writeset_init(&md->bitset_info, new_writeset, md->nr_blocks,
> +			  md->needs_invalidate);
> +	if (r) {
> +		DMERR("%s: writeset_init failed", __func__);
> +		return r;
> +	}
> +
> +	swap_writeset(md, new_writeset);
> +
> +	if (old_writeset->md.root != INVALID_WRITESET_ROOT) {
> +		r = writeset_sync_pending(md, old_writeset);
> +		if (!r)
> +			r = __metadata_era_archive(md, old_writeset,
> +						   md->current_era);
> +		if (r) {
> +			DMERR("%s: couldn't archive era %u", __func__,
> +			      md->current_era);
> +			md->tracking_lost = true;
> +			return r;
> +		}
> +	}

[Severity: Medium]
If archiving old_writeset fails here after swap_writeset() has already made
new_writeset current, does this leak the on-disk bitset blocks rooted at
old_writeset->md.root?

old_writeset->md.root is never inserted into md->writeset_tree_root or
freed with dm_bitset_del(), and process_rpc_calls() then commits
new_writeset as disk->current_writeset.  The next rollover overwrites
old_writeset->md.root in writeset_init(), permanently orphaning those
blocks in md->sm.

> +
> +	md->current_era++;
> +
> +	return 0;
> +}

[Severity: High]
Can a live rollover after an archive failure here wipe unarchived write
tracking without invalidating all blocks?

When archiving old_writeset fails during a checkpoint or
take_metadata_snap message, swap_writeset() has already switched
md->current_writeset to an all-zero new_writeset and set
md->tracking_lost = true.  Because md->tracking_lost is only converted into
md->needs_invalidate = true in era_preresume(), a second checkpoint or
take_metadata_snap message before a suspend/resume cycle will pick
old_writeset as next_writeset(), reinitialize it with
md->needs_invalidate == false, and archive the uninvalidated writeset into
the btree and metadata snapshot, silently losing the blocks written before
the failed rollover.

[ ... ]

> @@ -982,12 +1129,41 @@ static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
>  	return r;
>  }
>  
> +/*
> + * Returns false if the bio must wait for the metadata to be marked dirty.
> + */
> +static bool metadata_lazy_mark(struct era_metadata *md, dm_block_t block)
> +{
> +	bool r = true;
> +	struct writeset *ws;
> +
> +	rcu_read_lock();
> +	ws = rcu_dereference(md->current_writeset);
> +	if (!test_bit(block, ws->bits)) {
> +		if (!READ_ONCE(md->lazy_gate_open))
> +			r = false;
> +		else if (!test_and_set_bit(block, ws->bits)) {
> +			set_bit(block, ws->pending);
> +			atomic_set(&md->lazy_activity, 1);
> +		}
> +	}
> +	rcu_read_unlock();
> +
> +	return r;
> +}

[Severity: Low]
md->lazy_activity shares a cacheline in struct era_metadata with md->lazy
(read on every write bio in era_map()) and md->lazy_gate_open.  Could this
check !atomic_read(&md->lazy_activity) before calling atomic_set() so that
marking new blocks across different bitmap regions does not repeatedly
dirty that shared cacheline while md->lazy_activity is already set?

[ ... ]

> @@ -1635,12 +1981,19 @@ static int era_preresume(struct dm_target *ti)
>  
>  	start_worker(era);
>  
> +	/* Writes may have been lost, so mark every block in the new era. */
> +	if (era->md->tracking_lost)
> +		era->md->needs_invalidate = true;
> +
>  	r = in_worker0(era, metadata_era_rollover);
>  	if (r) {
>  		DMERR("%s: metadata_era_rollover failed", __func__);
>  		return r;
>  	}
>  
> +	era->md->needs_invalidate = false;
> +	era->md->tracking_lost = false;
> +
>  	return 0;
>  }

[Severity: Medium]
Is era->md->tracking_lost cleared too late here for the rollover commit to
clear the on-disk dirty flag?

in_worker0(era, metadata_era_rollover) runs process_rpc_calls(), which
calls metadata_commit(era->md) before completing the RPC and returning to
era_preresume().  During that commit era->md->tracking_lost is still true,
so prepare_superblock() writes SUPERBLOCK_FLAG_LAZY_DIRTY and
ERA_VERSION_LAZY_DIRTY to disk even though the new era was just initialized
with every block marked.  Because all bits in ws->bits are now set, later
writes in this era will not defer to the worker or schedule do_clean(),
leaving the on-disk superblock marked dirty while clean and idle.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124051653.346631-1-ronniesahlberg@gmail.com?part=1

      reply	other threads:[~2026-11-24  5:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  5:16 [PATCH] dm era: add lazy mode Ronnie Sahlberg
2026-11-24  5:36 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-230557@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=ejt@redhat.com \
    --cc=mpatocka@redhat.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=snitzer@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox