Skip to contentSkip to conversation

dm era: add lazy mode

PATCH New Ronnie Sahlberg 2 participants 2 messages Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm era: add lazy mode

The first write to a block in each era is deferred to the worker, which records the block in the on-disk writeset and commits the metadata before the write is issued. This makes tracking crash safe, but every batch of first writes waits for a metadata commit, including two cache flushes of the metadata device.

Add an optional "lazy" mode which trades that cost for a full resync after a crash, the same trade MD RAID makes without a write-intent bitmap:

era <metadata dev> <origin dev> <block size> [<#feature args> [lazy]

      [clean_delay <ms>]]

In lazy mode era_map() marks new blocks in core only and lets the write through. The bits are recorded in a second "pending" bitmap and written to the on-disk writeset with the next metadata commit.

While in-core bits may not be on disk, the superblock carries a new dirty flag (bit 1; bit 0 is "clean shutdown" in thin-provisioning-tools) and is written as metadata version 2, so kernels without lazy mode refuse to open it instead of trusting incomplete writesets. Clean metadata is still written as version 1.

The metadata is marked dirty by the worker before the first write to a new block after it was clean; until that commit has succeeded such bios are deferred as before. It is marked clean on suspend, and after clean_delay milliseconds (default 1000, 0 disables) without writes to new blocks: era_map() is stopped from marking new blocks, an RCU grace period waits for calls already marking, and the pending bits are committed together with the clean flag. So a crash while idle loses nothing, and each idle to busy transition costs one commit.

If the metadata is found dirty on activation, with or without lazy mode, the new era starts with every block marked, on disk and in core, in the same commit that clears the flag. era_invalidate then reports every block for any era since the crash.

Because era_map() no longer goes through the worker, an era rollover in lazy mode switches era_map() to the new writeset, and waits for an RCU grace period, before writing out and archiving the old one. If archiving fails the metadata is kept marked dirty, so the next activation invalidates instead of losing writes.

Without the lazy feature argument the target behaves as before.

---
This depends on a matching thin-provisioning-tools change, so that era_check, era_dump, era_restore and era_repair handle the new dirty flag and version 2 metadata, and era_invalidate reports every block for crashed metadata:
https://github.com/jthornber/thin-provisioning-tools/pull/332

The two only make sense together: without the tools change, era_check rejects dirty metadata, and era_restore/era_repair would silently mark crashed metadata clean.

This patch was developed with an AI coding assistant (Claude), which wrote the implementation and documentation from my design notes; I reviewed and directed the changes.

Tested on 7.4-rc4 in a VM with lockdep and PROVE_RCU enabled:
- table parsing,
- dirty/clean transitions, checked on the on-disk superblock,
- era_invalidate accuracy across checkpoints,
- simulated crashes (metadata device swapped to dm-error) while dirty and while clean,
- a non-lazy table on dirty metadata,
- fio random writes covering every block once while checkpoint and take_metadata_snap run concurrently.
No performance numbers: the test VM's disk is not suitable.

 .../admin-guide/device-mapper/era.rst         |  35 +-
 drivers/md/dm-era-target.c                    | 409 ++++++++++++++++--
 2 files changed, 416 insertions(+), 28 deletions(-)
Documentation/admin-guide/device-mapper/era.rst +34 -1
index 90dd5c670b9f..8de71cff25b9 100644
@@ -18,15 +18,48 @@ coherency after rolling back a vendor snapshot.
 Constructor
 ===========
 
-era <metadata dev> <origin dev> <block size>
+era <metadata dev> <origin dev> <block size> [<#feature args> [<arg>]*]
 
  ================ ======================================================
  metadata dev     fast device holding the persistent metadata
  origin dev	  device holding data blocks that may change
  block size       block size of origin data device, granularity that is
 		  tracked by the target
+ #feature args    number of feature arguments that follow
  ================ ======================================================
 
+Optional feature arguments are:
+
+ ================ ======================================================
+ lazy             Track writes in memory only, and write the tracking
+		  information to the metadata device later.  See
+		  "Lazy mode" below.
+ clean_delay <ms> Lazy mode only.  Mark the metadata clean after this
+		  many milliseconds without a write to a block that is
+		  not already marked in the current era.  0 means the
+		  metadata is only marked clean on suspend.  Default 1000.
+ ================ ======================================================
+
+Lazy mode
+=========
+
+By default the first write to each block in an era waits for the block to
+be recorded on the metadata device, which costs a metadata commit.
+
+In lazy mode writes are never delayed by metadata updates.  Instead, the
+metadata is marked dirty before the first write after it was clean, and
+marked clean again once all tracking information has been written: on
+suspend, and after clean_delay milliseconds without writes to new blocks.
+
+If the target is activated and the metadata is still marked dirty, the
+tracking information from before the unclean shutdown may be incomplete.
+The new era then starts with every block marked as written, so the next
+era_invalidate reports the whole device.
+
+This check is done whether or not the new table uses lazy mode.  While
+the metadata is dirty it carries metadata format version 2, which kernels
+without lazy mode support refuse to open.  Clean metadata uses version 1.
+
 Messages
 ========
 
drivers/md/dm-era-target.c +382 -27

668 lines not on this page · raw message

Signature · 1 line
2.56.0
#1 lore raw
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…

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 <[email protected]>

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.

Quoted 4 lines
drivers/md/dm-era-target.c +0 -0
index ea499adca4ce2..fcf466f2e760f 100644

[ ... ]

Quoted 23 lines
drivers/md/dm-era-target.c +8 -2
@@ -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;

drivers/md/dm-era-target.c static int open_metadata(struct era_metadata *md) · line 669

Quoted 10 lines
drivers/md/dm-era-target.c +6 -0
 
+	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.

[ ... ]

Quoted 1 line
drivers/md/dm-era-target.c +0 -0
@@ -892,22 +949,60 @@ static int metadata_resize(struct era_metadata *md, void *arg)

[ ... ]

Quoted 24 lines
drivers/md/dm-era-target.c +5 -4
+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);

drivers/md/dm-era-target.c static int metadata_era_archive(struct era_metadata *md) · line 1019

Quoted 9 lines
drivers/md/dm-era-target.c +1 -1
@@ -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.

[ ... ]

Quoted 1 line
drivers/md/dm-era-target.c +0 -0
@@ -934,13 +1040,51 @@ static int metadata_new_era(struct era_metadata *md)

[ ... ]

Quoted 21 lines
drivers/md/dm-era-target.c +21 -0
+/*
+ * 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);

drivers/md/dm-era-target.c static int metadata_new_era(struct era_metadata *md)

Quoted 10 lines
drivers/md/dm-era-target.c +10 -0
+		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.

drivers/md/dm-era-target.c static int metadata_new_era(struct era_metadata *md) · line 1054

Quoted 5 lines
drivers/md/dm-era-target.c +5 -0
+
+	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.

[ ... ]

Quoted 16 lines
drivers/md/dm-era-target.c +12 -0
@@ -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))

drivers/md/dm-era-target.c static bool metadata_current_marked(struct era_metadata *md, dm_block_t block) · line 1153

Quoted 10 lines
drivers/md/dm-era-target.c +10 -0
+			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?

[ ... ]

Quoted 9 lines
drivers/md/dm-era-target.c +4 -0
@@ -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);

drivers/md/dm-era-target.c static int era_preresume(struct dm_target *ti) · line 1998

Quoted 10 lines
drivers/md/dm-era-target.c +3 -0
 	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1