Skip to contentSkip to conversation

dm thin: complete writes only after the metadata commit

PATCH New Peng Yu 2 participants 5 messages Nov 25

Subsystems (2)
lore
Share & tools
lore

Peng Yu

author
#0 lore raw

cover dm thin: complete writes only after the metadata commit

This series adds a new option to the dm thin pool: wait_for_commit. When wait_for_commit is enabled, a write or a discard will return after the metadata is committed. By default, wait_for_commit is disabled, and the thin pool behaves exactly as before.

Why we need wait_for_commit:

Assume there is a server A, and the underlying device of its thin pool is an NVMe-oF device. Server A creates a thin device and exports the thin device to a remote host via NVMe-oF too. When server A fails, a control plane will rebuild the pool and thin device on a standby server B, and server B will export the thin device to the same remote host. When the remote host issues a FLUSH, server A fails, so the metadata are not committed to the pool. The thin pool fails over to server B, and then the NVMe-oF multipath resends the FLUSH to B. B receives the command and finds nothing to flush. In this case, some uncommitted data are lost, but the remote host can't get any error.

As the dm thin pool doc says: "the thin-provisioning target behaves like a physical disk that has a volatile write cache". If the underlying disk of a thin pool is a remote disk and we want to fail over the thin pool from server A to server B, the "volatile write cache" is only on server A, and server B doesn't know about it, so we will get a consistency issue. The wait_for_commit option could fix this issue with a performance penalty.

Below is a performance test on my laptop. We got the results in a VM on loop devices.

1 MiB sequential first writes (no zero fill)

|                      | 1 job, depth 1           | 8 jobs, depth 8          |
|                      | MiB/s | mean ms | p99 ms | MiB/s | mean ms | p99 ms |
| no wait_for_commit   | 1307  | 0.65    | 1.59   | 1596  | 39.7    | 276    |
| with wait_for_commit | 131   | 7.57    | 17.7   | 776   | 78.2    | 148    |

4 KiB random writes, mostly to new blocks (allocation and zero fill)

|                      | 1 job, depth 1, 2000 writes | 8 jobs, depth 2, 10000 writes |
|                      | s       | IOPS   | p99 ms   | s       | IOPS    | p99 ms    |
| no wait_for_commit   | 1.16    | 1723   | 0.97     | 4.96    | 2066    | 15.3      |
| with wait_for_commit | 10.5    | 195    | 11.6     | 15.1    | 673     | 49.5      |

4 KiB random overwrites of mapped blocks, 4 jobs, depth 16

|                      | IOPS   | mean ms | p99 ms |
| no wait_for_commit   | 202095 | 0.31    | 1.12   |
| with wait_for_commit | 203293 | 0.31    | 1.13   |

Peng Yu (2):
dm thin metadata: report uncommitted state and refuse commits after a
failure
dm thin: add wait_for_commit pool feature

 .../device-mapper/thin-provisioning.rst       |  25 +++-
 drivers/md/dm-thin-metadata.c                 |  61 +++++++--
 drivers/md/dm-thin-metadata.h                 |  14 +++
 drivers/md/dm-thin.c                          | 116 +++++++++++++++---
 4 files changed, 190 insertions(+), 26 deletions(-)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 dm thin metadata: report uncommitted state and refuse commits after a failure

Add two flags for a thin pool:
* commit_failed: Set to true if a commit failed. It is a fact that may be caused by IO errors.
* refuse_commit_after_failure: Set to true if we enable the wait_for_commit option. It is selected by a user.

If both of them are true, the user should repair the pool.

Add a new field:
* changed: A new field of struct dm_thin_lookup_result, set by each lookup that finds a mapping. False means the thin device's metadata in memory was the same as on disk at the time of the lookup; true means they may not be the same.

Add some helper functions:
* dm_thin_uncommitted: Return true if the thin device might have metadata changes that are not on disk yet. When we get a commit failure, td->changed might be cleared, but the metadata changes are not on disk due to the commit failure. So when commit_failed is true, the function will always return true.
* dm_pool_metadata_set_refuse_commit_after_failure: Set the refuse_commit_after_failure flag.

Assisted-by Claude:claude-opus-5-5 [Claude Code]
Assisted-by Claude:claude-fable-5-1 [Claude Code]
---
 drivers/md/dm-thin-metadata.c | 61 +++++++++++++++++++++++++++++++----
 drivers/md/dm-thin-metadata.h | 14 ++++++++
 2 files changed, 68 insertions(+), 7 deletions(-)
drivers/md/dm-thin-metadata.c +54 -7
index e60e1326376a..7b56c970a524 100644
@@ -221,6 +221,9 @@ struct dm_pool_metadata {
 	 */
 	bool in_service:1;
 
+	bool commit_failed;
+	bool refuse_commit_after_failure;
+
 	/*
 	 * Reading the space map roots can fail, so we read it into these
 	 * buffers before the superblock is locked and updated.
@@ -972,6 +975,8 @@ struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
 	INIT_LIST_HEAD(&pmd->thin_devices);
 	pmd->fail_io = false;
 	pmd->in_service = false;
+	pmd->commit_failed = false;
+	pmd->refuse_commit_after_failure = false;
 	pmd->bdev = bdev;
 	pmd->data_block_size = data_block_size;
 	pmd->pre_commit_fn = NULL;
@@ -1020,10 +1025,15 @@ int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
 
 	pmd_write_lock_in_core(pmd);
 	if (!pmd->fail_io && !dm_bm_is_read_only(pmd->bm)) {
-		r = __commit_transaction(pmd);
-		if (r < 0)
-			DMWARN("%s: __commit_transaction() failed, error = %d",
-			       __func__, r);
+		if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+			DMERR_LIMIT("%s: refusing to commit after a failed commit",
+				    __func__);
+		} else {
+			r = __commit_transaction(pmd);
+			if (r < 0)
+				DMWARN("%s: __commit_transaction() failed, error = %d",
+				       __func__, r);
+		}
 	}
 	pmd_write_unlock(pmd);
 	__destroy_persistent_data_objects(pmd, true);
@@ -1351,6 +1361,7 @@ static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
 	 */
 	r = __commit_transaction(pmd);
 	if (r < 0) {
+		pmd->commit_failed = true;
 		DMWARN("%s: __commit_transaction() failed, error = %d",
 		       __func__, r);
 		return r;
@@ -1397,8 +1408,15 @@ int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
 	int r = -EINVAL;
 
 	pmd_write_lock(pmd);
-	if (!pmd->fail_io)
-		r = __reserve_metadata_snap(pmd);
+	if (!pmd->fail_io) {
+		if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+			DMERR_LIMIT("%s: refusing to commit after a failed commit",
+				    __func__);
+			r = -EIO;
+		} else {
+			r = __reserve_metadata_snap(pmd);
+		}
+	}
 	pmd_write_unlock(pmd);
 
 	return r;
@@ -1516,6 +1534,7 @@ static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
 	unpack_block_time(block_time, &exception_block, &exception_time);
 	result->block = exception_block;
 	result->shared = __snapshotted_since(td, exception_time);
+	result->changed = td->changed || td->pmd->commit_failed;
 }
 
 static int __find_block(struct dm_thin_device *td, dm_block_t block,
@@ -1796,6 +1815,17 @@ bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
 	return r;
 }
 
+bool dm_thin_uncommitted(struct dm_thin_device *td)
+{
+	bool r;
+
+	down_read(&td->pmd->root_lock);
+	r = td->changed || td->pmd->commit_failed;
+	up_read(&td->pmd->root_lock);
+
+	return r;
+}
+
 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
 {
 	bool r = false;
@@ -1836,6 +1866,14 @@ int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
 	return r;
 }
 
+void dm_pool_metadata_set_refuse_commit_after_failure(struct dm_pool_metadata *pmd,
+						      bool refuse_commit_after_failure)
+{
+	pmd_write_lock_in_core(pmd);
+	pmd->refuse_commit_after_failure = refuse_commit_after_failure;
+	pmd_write_unlock(pmd);
+}
+
 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 {
 	int r = -EINVAL;
@@ -1848,9 +1886,18 @@ int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 	if (pmd->fail_io)
 		goto out;
 
+	if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+		DMERR_LIMIT("%s: refusing to commit after a failed commit",
+			    __func__);
+		r = -EIO;
+		goto out;
+	}
+
 	r = __commit_transaction(pmd);
-	if (r < 0)
+	if (r < 0) {
+		pmd->commit_failed = true;
 		goto out;
+	}
 
 	/*
 	 * Open the next transaction.
drivers/md/dm-thin-metadata.h +14 -0
index 2f64f48b5f19..d7b149471e0b 100644
@@ -81,9 +81,16 @@ int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
 /*
  * Commits _all_ metadata changes: device creation, deletion, mapping
  * updates.
+ *
+ * Returns -EIO without committing once a commit has failed, if the pool
+ * asked to refuse commits after a failure (see
+ * dm_pool_metadata_set_refuse_commit_after_failure()).
  */
 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
 
+void dm_pool_metadata_set_refuse_commit_after_failure(struct dm_pool_metadata *pmd,
+						      bool refuse_commit_after_failure);
+
 /*
  * Discards all uncommitted changes.  Rereads the superblock, rolling back
  * to the last good transaction.  Thin devices remain open.
@@ -112,6 +119,10 @@ int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  * operations only.  A small performance hit is incurred by providing this
  * copy of the metadata to userland due to extra copy-on-write operations
  * on the metadata nodes.  Release this as soon as you finish with it.
+ *
+ * The reservation commits first, so it returns -EIO once a commit has
+ * failed, if the pool asked to refuse commits after a failure (see
+ * dm_pool_metadata_set_refuse_commit_after_failure()).
  */
 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
@@ -136,6 +147,7 @@ dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
 struct dm_thin_lookup_result {
 	dm_block_t block;
 	bool shared:1;
+	bool changed:1;
 };
 
 /*
@@ -175,6 +187,8 @@ int dm_thin_remove_range(struct dm_thin_device *td,
  */
 bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
 
+bool dm_thin_uncommitted(struct dm_thin_device *td);
+
 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
 
 bool dm_thin_aborted_changes(struct dm_thin_device *td);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dm thin metadata: inconsistent td->changed state after __commit_transaction() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dm thin metadata: inconsistent td->changed state after __commit_transaction()
--

commit 0a571384422ac18472855b719999a78add089dae
Author: Peng Yu <[email protected]>

dm thin metadata: report uncommitted state and refuse commits after a failure

This commit tracks metadata commit failures via a pool-wide commit_failed flag, exposes whether a thin device or lookup result has uncommitted metadata changes, and optionally refuses further commits after a failure.

Quoted 4 lines
drivers/md/dm-thin-metadata.c +0 -0
index e60e1326376a0..7b56c970a524d 100644

[ ... ]

Quoted 40 lines
drivers/md/dm-thin-metadata.c +22 -0
@@ -1796,6 +1815,17 @@ bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
 	return r;
 }
 
+bool dm_thin_uncommitted(struct dm_thin_device *td)
+{
+	bool r;
+
+	down_read(&td->pmd->root_lock);
+	r = td->changed || td->pmd->commit_failed;
+	up_read(&td->pmd->root_lock);
+
+	return r;
+}
+
 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
 {
 	bool r = false;
@@ -1836,6 +1866,14 @@ int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
 	return r;
 }
 
+void dm_pool_metadata_set_refuse_commit_after_failure(struct dm_pool_metadata *pmd,
+						      bool refuse_commit_after_failure)
+{
+	pmd_write_lock_in_core(pmd);
+	pmd->refuse_commit_after_failure = refuse_commit_after_failure;
+	pmd_write_unlock(pmd);
+}
+
 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 {
 	int r = -EINVAL;
@@ -1848,9 +1886,18 @@ int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 	if (pmd->fail_io)
 		goto out;
 
+	if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+		DMERR_LIMIT("%s: refusing to commit after a failed commit",
+			    __func__);

drivers/md/dm-thin-metadata.c int dm_pool_commit_metadata(struct dm_pool_metadata *pmd) · line 1900

Quoted 10 lines
drivers/md/dm-thin-metadata.c +7 -1
+		r = -EIO;
+		goto out;
+	}
+
 	r = __commit_transaction(pmd);
-	if (r < 0)
+	if (r < 0) {
+		pmd->commit_failed = true;
 		goto out;
+	}

[Severity: Medium]
Does setting pmd->commit_failed after __commit_transaction() fails leave td->changed and td->aborted_with_changes inconsistent with the actual on-disk state?

Inside __commit_transaction(), __write_changed_details() clears td->changed = false (and frees closed thin devices) before dm_sm_commit(), dm_tm_pre_commit(), save_sm_roots(), superblock_lock(), and dm_tm_commit() run:

drivers/md/dm-thin-metadata.c:__write_changed_details() {
	...
		if (td->open_count)
			td->changed = false;
		else {
			list_del(&td->list);
			kfree(td);
		}
	...
}

When a commit fails in any of those later steps, td->changed is already false, while dm_thin_changed_this_transaction(), dm_pool_changed_this_transaction(), and __set_abort_with_changes_flags() still read td->changed directly:

drivers/md/dm-thin-metadata.c:__set_abort_with_changes_flags() {
	...
	list_for_each_entry(td, &pmd->thin_devices, list)
		td->aborted_with_changes = td->changed;
}

When commit() in drivers/md/dm-thin.c fails and calls metadata_operation_failed() -> dm_pool_abort_metadata(), td->aborted_with_changes is set to false. Subsequent REQ_PREFLUSH and REQ_FUA bios then see both dm_thin_changed_this_transaction() and dm_thin_aborted_changes() as false in bio_triggers_commit(), and since wait_for_commit_write() in commit "dm thin: add wait_for_commit pool feature" skips op_is_flush(bio->bi_opf), those flush and FUA bios silently succeed instead of failing with an I/O error.

In addition, when pmd->refuse_commit_after_failure is false, pmd->commit_failed is never cleared if a later commit or abort succeeds, leaving result->changed and dm_thin_uncommitted() permanently true across the pool. Could __write_changed_details() defer clearing td->changed (and freeing closed td entries) until dm_tm_commit() succeeds?

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

PATCH 2/2 dm thin: add wait_for_commit pool feature

In reply to Peng Yu

Add the wait_for_commit option to the thin pool. When it is set, a write or a discard will be completed after the metadata changes are committed. We perform the actions below to make sure of it:
* In the worker, a plain write is treated like a FUA write for the commit.
* On the direct path of thin_bio_map(), a write to a mapped block whose device has uncommitted changes is handed to the worker.
* A write that covers a whole new block still sends its data at once. Its completion waits for the commit, similar to a FUA write.
* A discard will wait for the removal of its mapping. A discard may remove multiple mappings, and the discard will wait until all these mapping removals are committed.

With wait_for_commit set, every plain write fails after a failed commit.

We change the version of the thin-pool and thin targets to 1.25.0.

Assisted-by Claude:claude-opus-5-5 [Claude Code]
Assisted-by Claude:claude-fable-5-1 [Claude Code]
---
 .../device-mapper/thin-provisioning.rst       |  25 +++-
 drivers/md/dm-thin.c                          | 116 +++++++++++++++---
 2 files changed, 122 insertions(+), 19 deletions(-)
Documentation/admin-guide/device-mapper/thin-provisioning.rst +24 -1
index b2fa49a5608a..8c5fdb1475af 100644
@@ -138,6 +138,19 @@ means the thin-provisioning target behaves like a physical disk that has
 a volatile write cache.  If power is lost you may lose some recent
 writes.  The metadata should always be consistent in spite of any crash.
 
+With the wait_for_commit feature a write or a discard whose thin device
+has uncommitted metadata changes is completed only after the next
+commit, so the mapping of every completed write is on disk.  Commits
+stay batched: the pool commits once for all the IO that is waiting, and
+IO to blocks whose mapping is already committed is not affected.  The
+feature is for a pool whose devices another node takes over after a
+crash: that node starts from the last commit, and a flush it receives
+cannot cover what the crashed node completed.  It costs a commit per
+batch of allocating writes, and every device of the pool waits for the
+metadata lock while a commit runs.  After a failed commit a pool with
+this feature refuses every later commit and fails its writes until it is
+repaired, because a later commit could not be trusted.
+
 If data space is exhausted the pool will either error or queue IO
 according to the configuration (see: error_if_no_space).  If metadata
 space is exhausted or a metadata operation fails: the pool will error IO
@@ -291,6 +304,13 @@ i) Constructor
       error_if_no_space:
 	Error IOs, instead of queueing, if no space.
 
+      wait_for_commit:
+	Complete a write or a discard only after the metadata changes
+	it depends on are committed.  Writes to blocks whose mapping is
+	already committed are not affected.  Data durability still
+	needs FLUSH or FUA, as for any device with a write cache.  See
+	"Updating on-disk metadata" above.
+
     Data block size must be between 64KiB (128 sectors) and 1GiB
     (2097152 sectors) inclusive.
 
@@ -302,7 +322,7 @@ ii) Status
       <transaction id> <used metadata blocks>/<total metadata blocks>
       <used data blocks>/<total data blocks> <held metadata root>
       ro|rw|out_of_data_space [no_]discard_passdown [error|queue]_if_no_space
-      needs_check|- metadata_low_watermark
+      needs_check|- metadata_low_watermark [wait_for_commit]
 
     transaction id:
 	A 64-bit number used by userspace to help synchronise with metadata
@@ -354,6 +374,9 @@ ii) Status
 	value internally but userspace needs to know this value to
 	determine if an event was caused by crossing this threshold.
 
+    wait_for_commit:
+	Present when the wait_for_commit feature is on.  Absent otherwise.
+
 iii) Messages
 
     create_thin <dev id>
drivers/md/dm-thin.c +98 -18

293 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/2] dm thin: add wait_for_commit pool feature

Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] dm thin: bio_triggers_commit() misses failed commits on flush and FUA bios - [High] dm thin…

Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] dm thin: bio_triggers_commit() misses failed commits on flush and FUA bios
- [High] dm thin: process_prepared_discard_passdown_pt2() skips commit after passdown
- [High] dm thin: __issue() defers plain writes after releasing virtual prison cell
- [High] dm thin: intermediate commit in alloc_data_block() reorders or fails writes
- [Medium] dm thin: deferred discard mappings in complete_discard_mapping() stall worker
- [Medium] dm thin: process_discard_cell_passdown() completes unmapped discards early
- [Medium] dm thin: process_prepared_discard_no_passdown() unlocks cell before commit
--

commit 98dea49cd9146a4b359aeeb9b766e485fc31d970
Author: Peng Yu <[email protected]>

dm thin: add wait_for_commit pool feature

This commit adds the wait_for_commit pool feature to dm-thin so that writes and discards on a thin device with uncommitted metadata changes wait for the metadata transaction to be committed before completing.

Quoted 4 lines
drivers/md/dm-thin.c +0 -0
index 358ed77ffb2be..93ce83674c63f 100644

[ ... ]

Quoted 10 lines
drivers/md/dm-thin.c +2 -3
@@ -732,10 +734,18 @@ static void remap_to_origin(struct thin_c *tc, struct bio *bio)
 	bio_set_dev(bio, tc->origin_dev->bdev);
 }
 
-static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
+static bool wait_for_commit_write(struct thin_c *tc, struct bio *bio)
 {
-	return op_is_flush(bio->bi_opf) &&
-		dm_thin_changed_this_transaction(tc->td);
+	return tc->pool->pf.wait_for_commit && bio_op(bio) == REQ_OP_WRITE &&

drivers/md/dm-thin.c static void remap_to_origin(struct thin_c *tc, struct bio *bio) · line 749

Quoted 10 lines
drivers/md/dm-thin.c +9 -0
+		!op_is_flush(bio->bi_opf);
+}
+
+static int bio_triggers_commit(struct thin_c *tc, struct bio *bio, bool hold)
+{
+	if (op_is_flush(bio->bi_opf))
+		return dm_thin_changed_this_transaction(tc->td);
+
+	return hold && dm_thin_uncommitted(tc->td);
 }

[Severity: High]
Can REQ_FUA and REQ_PREFLUSH bios bypass failed-commit detection here when wait_for_commit is enabled?

In __commit_transaction(), __write_changed_details() clears td->changed on all open thin devices before dm_sm_commit() and dm_tm_commit() run. If the commit fails after __write_changed_details(), td->changed is false while pmd->commit_failed is true (and abort_transaction() copies the cleared td->changed into td->aborted_with_changes).

Because bio_triggers_commit() checks dm_thin_changed_this_transaction() instead of dm_thin_uncommitted() when op_is_flush(bio->bi_opf) is true, it returns false after such a commit failure. An in-flight REQ_FUA whole-block overwrite whose new mapping was rolled back will then complete with success in complete_overwrite_bio(), and subsequent REQ_FUA or REQ_PREFLUSH writes in __issue() will be submitted and succeed even though plain writes fail.

Quoted 17 lines
drivers/md/dm-thin.c +3 -2
 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
@@ -749,11 +759,12 @@ static void inc_all_io_entry(struct pool *pool, struct bio *bio)
 	h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
 }
 
-static void issue(struct thin_c *tc, struct bio *bio)
+static void __issue(struct thin_c *tc, struct bio *bio, bool may_hold)
 {
 	struct pool *pool = tc->pool;
+	bool hold = may_hold && wait_for_commit_write(tc, bio);
 
-	if (!bio_triggers_commit(tc, bio)) {
+	if (!bio_triggers_commit(tc, bio, hold)) {
 		dm_submit_bio_remap(bio, NULL);
 		return;
 	}

drivers/md/dm-thin.c static void issue(struct thin_c *tc, struct bio *bio) · line 794

Quoted 7 lines
drivers/md/dm-thin.c +4 -0
@@ -777,6 +788,11 @@ static void issue(struct thin_c *tc, struct bio *bio)
 	spin_unlock_irq(&pool->lock);
 }
 
+static void issue(struct thin_c *tc, struct bio *bio)
+{
+	__issue(tc, bio, true);
+}

[Severity: High]
Does deferring plain writes to pool->deferred_flush_bios in __issue() allow reads and later writes to the same block to overtake them once the virtual prison cell is released?

In process_prepared_mapping() (when m->bio is NULL) and process_cell(), remap_and_issue() queues the holder write bio onto pool->deferred_flush_bios without submitting it to the data device, and inc_remap_and_issue_cell() immediately releases the virtual cell:

kworker (do_worker) CPU1
process_prepared_mapping()
remap_and_issue(holder_write)

    __issue()
      bio_list_add(&pool->deferred_flush_bios,
                   holder_write)

inc_remap_and_issue_cell(m->cell)
cell_release()
remap_and_issue(detained_read)
__issue()
dm_submit_bio_remap(detained_read)
<-- submitted before holder_write
thin_bio_map(new_read)
bio_detain(virt_cell)
...
return DM_MAPIO_REMAPPED
<-- submitted before holder_write
process_deferred_bios()
commit(pool)

    __write_changed_details()
      td->changed = false
                                  thin_bio_map(newer_write)
                                    dm_thin_find_block()
                                      result.changed == false
                                    return DM_MAPIO_REMAPPED
                                      <-- overtakes holder_write

dm_submit_bio_remap(holder_write)

Detained and concurrent reads observe stale pre-write data on the data device, and a concurrent write arriving in thin_bio_map() right after __write_changed_details() clears td->changed can be submitted before process_deferred_bios() drains deferred_flush_bios, causing the older write to overwrite the newer one.

[ ... ]

Quoted 15 lines
drivers/md/dm-thin.c +11 -0
@@ -1056,6 +1074,28 @@ static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
 	free_discard_mapping(m);
 }
 
+static void complete_discard_mapping(struct dm_thin_new_mapping *m)
+{
+	struct thin_c *tc = m->tc;
+	struct pool *pool = tc->pool;
+
+	if (!pool->pf.wait_for_commit || !dm_thin_uncommitted(tc->td)) {
+		bio_endio(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+		return;
+	}
+

drivers/md/dm-thin.c static void process_prepared_discard_success(struct dm_thin_new_mapping *m) · line 1097

Quoted 10 lines
drivers/md/dm-thin.c +10 -0
+	if (dm_thin_aborted_changes(tc->td)) {
+		bio_io_error(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+		return;
+	}
+
+	spin_lock_irq(&pool->lock);
+	list_add_tail(&m->list, &pool->deferred_discard_completions);
+	spin_unlock_irq(&pool->lock);
+}

[Severity: Medium]
When discard_passdown is enabled, can a discard bio complete without reaching complete_discard_mapping() even though tc->td has uncommitted metadata changes?

drivers/md/dm-thin.c:process_discard_cell_passdown() {
	...
	h->cell = virt_cell;
	break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);

	bio_endio(bio);
}

If the target range has no mapped blocks in the in-memory btree (for example, because an earlier discard in the same uncommitted transaction already removed the mapping), break_up_discard_bio() creates no dm_thin_new_mapping objects and process_discard_cell_passdown() completes the discard bio immediately via bio_endio(bio) before the transaction is committed.

Quoted 8 lines
drivers/md/dm-thin.c +0 -1
 static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
 {
 	int r;
@@ -1065,11 +1105,13 @@ static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
 	if (r) {
 		metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
 		bio_io_error(m->bio);
-	} else

drivers/md/dm-thin.c static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m) · line 1115

Quoted 10 lines
drivers/md/dm-thin.c +5 -2
-		bio_endio(m->bio);
+		cell_defer_no_holder(tc, m->cell);
+		mempool_free(m, &tc->pool->mapping_pool);
+		return;
+	}
 
 	cell_defer_no_holder(tc, m->cell);
-	mempool_free(m, &tc->pool->mapping_pool);
+	complete_discard_mapping(m);
 }

[Severity: Medium]
In process_prepared_discard_no_passdown(), does calling cell_defer_no_holder(tc, m->cell) before the discard is committed release detained bios too early?

When do_worker() runs process_prepared() for prepared_discards, cell_defer_no_holder() releases the virtual cell and moves any detained bios onto tc->deferred_bio_list while m is queued on pool->deferred_discard_completions. process_deferred_bios() then runs process_thin_deferred_bios(tc) before calling commit(pool), so a detained read to the discarded block sees -ENODATA in the uncommitted btree and completes with zeroes before the discard has been committed (even if commit(pool) subsequently fails).

Quoted 5 lines
drivers/md/dm-thin.c +0 -1
@@ -1206,11 +1248,13 @@ static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
 	if (r) {
 		metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
 		bio_io_error(m->bio);
-	} else

drivers/md/dm-thin.c static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m) · line 1258

Quoted 10 lines
drivers/md/dm-thin.c +5 -2
-		bio_endio(m->bio);
+		cell_defer_no_holder(tc, m->cell);
+		mempool_free(m, &pool->mapping_pool);
+		return;
+	}
 
 	cell_defer_no_holder(tc, m->cell);
-	mempool_free(m, &pool->mapping_pool);
+	complete_discard_mapping(m);
 }

[Severity: High]
Can complete_discard_mapping() see dm_thin_uncommitted(tc->td) == false in process_prepared_discard_passdown_pt2() and complete the discard without committing the data space-map decrement?

In process_prepared_discard_passdown_pt1(), dm_thin_remove_range() sets tc->td->changed = true and dm_pool_inc_data_range() pins the data blocks while the passdown discard bio is in flight. If a commit runs while the passdown bio is in flight (for instance, triggered by another write or discard in the batch), __write_changed_details() clears tc->td->changed to false. When the passdown bio finishes and process_prepared_discard_passdown_pt2() calls dm_pool_dec_data_range(), that call mutates pool->pmd->data_sm without setting tc->td->changed, so complete_discard_mapping() sees dm_thin_uncommitted(tc->td) == false and completes m->bio immediately while leaving the freed data blocks uncommitted on disk.

[ ... ]

Quoted 43 lines
drivers/md/dm-thin.c +11 -0
@@ -2359,6 +2404,8 @@ static void process_deferred_bios(struct pool *pool)
 {
 	struct bio *bio;
 	struct bio_list bios, bio_completions;
+	struct list_head discard_completions;
+	struct dm_thin_new_mapping *m, *tmp;
 	struct thin_c *tc;
 
 	tc = get_first_thin(pool);
@@ -2374,6 +2421,7 @@ static void process_deferred_bios(struct pool *pool)
 	 */
 	bio_list_init(&bios);
 	bio_list_init(&bio_completions);
+	INIT_LIST_HEAD(&discard_completions);
 
 	spin_lock_irq(&pool->lock);
 	bio_list_merge(&bios, &pool->deferred_flush_bios);
@@ -2381,9 +2429,12 @@ static void process_deferred_bios(struct pool *pool)
 
 	bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
 	bio_list_init(&pool->deferred_flush_completions);
+
+	list_splice_init(&pool->deferred_discard_completions, &discard_completions);
 	spin_unlock_irq(&pool->lock);
 
 	if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
+	    list_empty(&discard_completions) &&
 	    !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
 		return;
 
@@ -2392,6 +2443,11 @@ static void process_deferred_bios(struct pool *pool)
 
 		while ((bio = bio_list_pop(&bios)))
 			bio_io_error(bio);
+
+		list_for_each_entry_safe(m, tmp, &discard_completions, list) {
+			bio_io_error(m->bio);
+			mempool_free(m, &pool->mapping_pool);
+		}
 		return;
 	}
 	pool->last_commit_jiffies = jiffies;

drivers/md/dm-thin.c static void process_deferred_bios(struct pool *pool) · line 2461

Quoted 7 lines
drivers/md/dm-thin.c +4 -0
@@ -2399,6 +2455,11 @@ static void process_deferred_bios(struct pool *pool)
 	while ((bio = bio_list_pop(&bio_completions)))
 		bio_endio(bio);
 
+	list_for_each_entry_safe(m, tmp, &discard_completions, list) {
+		bio_endio(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+	}

[Severity: High]
What happens to bios already queued in deferred_flush_bios, deferred_flush_completions, and deferred_discard_completions when alloc_data_block() triggers an intermediate commit(pool) inside the process_thin_deferred_cells() / process_thin_deferred_bios() loop at the top of process_deferred_bios()?

When alloc_data_block() runs out of free data or metadata blocks in the current transaction, it calls commit(pool) directly without draining the deferred lists:

drivers/md/dm-thin.c:alloc_data_block() {
	...
	if (!free_blocks) {
		/* Let's commit before we use up the metadata reserve. */
		r = commit(pool);
		if (r)
			return r;
	}
	...
}

That intermediate commit clears td->changed on all thin devices while earlier plain writes from process_prepared_mapping() or earlier iterations of the loop are still sitting unsubmitted in pool->deferred_flush_bios, so any subsequent write to the same mapped block processed later in process_thin_deferred_bios() sees td->changed == false and is submitted to the data device ahead of the older write. In addition, if that intermediate commit succeeds and check_for_metadata_space() transitions the pool to PM_OUT_OF_METADATA_SPACE, the second commit(pool) in process_deferred_bios() returns -EINVAL and fails all waiting bios in bios, bio_completions, and discard_completions with BLK_STS_IOERR even though their metadata changes were already committed.

[Severity: Medium]
Can holding struct dm_thin_new_mapping objects in pool->deferred_discard_completions until the end of process_deferred_bios() exhaust pool->mapping_pool and stall deferred work?

Previously, completed discard mappings were freed back to pool->mapping_pool before process_deferred_bios() ran, so ensure_next_mapping(pool) only failed when all mappings were in asynchronous preparation stages whose completion callbacks call wake_worker(pool). Now mappings waiting in pool->deferred_discard_completions remain allocated across process_thin_deferred_cells() and process_thin_deferred_bios() (and in no_discard_passdown mode when pool->all_io_ds is empty, process_discard_cell_no_passdown() synchronously adds another mapping to deferred_discard_completions on each iteration). If pool->mapping_pool is exhausted, ensure_next_mapping(pool) aborts the loop and leaves the remaining cells and bios on tc->deferred_cells and tc->deferred_bio_list, and freeing discard_completions after commit(pool) does not call wake_worker(pool).

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