All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peng Yu <yupeng0921@gmail.com>
To: Alasdair Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Benjamin Marzinski <bmarzins@redhat.com>
Cc: Joe Thornber <ejt@redhat.com>,
	dm-devel@lists.linux.dev, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, Peng Yu <yupeng0921@gmail.com>,
	Sashiko <sashiko-bot@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH v2 1/2] dm thin metadata: keep the changed flags until the commit succeeds
Date: Mon, 30 Nov 2026 23:25:31 -0800	[thread overview]
Message-ID: <20261201072532.68398-2-yupeng0921@gmail.com> (raw)
In-Reply-To: <20261201072532.68398-1-yupeng0921@gmail.com>

The code clears td->changed before a commit succeeds. So we may get the
issue below:
When a commit fails, dm-thin aborts the transaction, and
__set_abort_with_changes_flags() copies td->changed to
td->aborted_with_changes. td->changed is already false, so the abort
records that no thin device lost changes. A later FLUSH or FUA write
then succeeds: bio_triggers_commit() sees no changes, and issue() and
complete_overwrite_bio() do not get to the dm_thin_aborted_changes()
check. The changes of the aborted transaction are lost and nothing
reports an error.

Clear td->changed, and free the closed devices, only after
dm_tm_commit() has succeeded.

Reproducer. The metadata device fails writes but not reads, so the pool
becomes read-only and does not go to the fail mode:

  dmsetup create meta --table "0 16384 linear $META 0"
  dd if=/dev/zero of=/dev/mapper/meta bs=4k count=1 oflag=direct
  dmsetup create pool --table \
    "0 2097152 thin-pool /dev/mapper/meta $DATA 128 0"
  dmsetup message pool 0 "create_thin 0"
  dmsetup create thin --table "0 2097152 thin /dev/mapper/pool 0"
  # map block 0 and commit
  dd if=/dev/zero of=/dev/mapper/thin bs=4k count=1 oflag=direct \
    conv=fsync
  # from now on writes to the metadata device fail
  dmsetup suspend meta
  dmsetup load meta --table "0 16384 flakey $META 0 0 1 1 error_writes"
  dmsetup resume meta
  # provision block 8; the commit one second later fails
  dd if=/dev/urandom of=/dev/mapper/thin bs=4k seek=128 count=1 \
    oflag=direct
  sleep 3
  dmsetup status pool   # ro, needs_check
  # FUA write to block 0
  dd if=/dev/zero of=/dev/mapper/thin bs=4k count=1 oflag=direct,dsync

Without this patch the last dd succeeds although block 8 was lost. With
this patch it fails with an I/O error.

Fixes: da105ed5fd7e ("dm thin metadata: introduce dm_pool_abort_metadata")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-232598@kernel.org/
Cc: stable@vger.kernel.org
Signed-off-by: Peng Yu <yupeng0921@gmail.com>
Assisted-by: Claude:claude-fable-5-2 [Claude Code]
---
New in v2. Sashiko's review of v1 patch 1 asked if the clearing of
td->changed could wait until dm_tm_commit() has succeeded. v1 had a
commit_failed flag instead, which covered only the plain writes of the
new feature. This patch fixes the cause for the FLUSH and FUA writes of
every pool, and patch 2 no longer needs the flag.

Tested with the reproducer above. A second thin device of the same pool
that had no uncommitted changes still completes FLUSH and FUA writes
after the failed commit.

Sashiko's review of v1 patch 1: https://lore.kernel.org/all/sashiko-outbox-232598@kernel.org/
v1: https://lore.kernel.org/all/20261125043325.138351-1-yupeng0921@gmail.com/

 drivers/md/dm-thin-metadata.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
index 8fca13dcb..ecea21dd1 100644
--- a/drivers/md/dm-thin-metadata.c
+++ b/drivers/md/dm-thin-metadata.c
@@ -852,11 +852,11 @@ static int __begin_transaction(struct dm_pool_metadata *pmd)
 static int __write_changed_details(struct dm_pool_metadata *pmd)
 {
 	int r;
-	struct dm_thin_device *td, *tmp;
+	struct dm_thin_device *td;
 	struct disk_device_details details;
 	uint64_t key;
 
-	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
+	list_for_each_entry(td, &pmd->thin_devices, list) {
 		if (!td->changed)
 			continue;
 
@@ -872,6 +872,23 @@ static int __write_changed_details(struct dm_pool_metadata *pmd)
 				    &key, &details, &pmd->details_root);
 		if (r)
 			return r;
+	}
+
+	return 0;
+}
+
+/*
+ * The commit has succeeded, so the details of the changed devices are on
+ * disk.  The changed flags must stay set until now: if the commit fails,
+ * dm_pool_abort_metadata() uses them to tell which devices lost changes.
+ */
+static void __clear_changed_details(struct dm_pool_metadata *pmd)
+{
+	struct dm_thin_device *td, *tmp;
+
+	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
+		if (!td->changed)
+			continue;
 
 		if (td->open_count)
 			td->changed = false;
@@ -880,8 +897,6 @@ static int __write_changed_details(struct dm_pool_metadata *pmd)
 			kfree(td);
 		}
 	}
-
-	return 0;
 }
 
 static int __commit_transaction(struct dm_pool_metadata *pmd)
@@ -937,7 +952,13 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 
 	copy_sm_roots(pmd, disk_super);
 
-	return dm_tm_commit(pmd->tm, sblock);
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r < 0)
+		return r;
+
+	__clear_changed_details(pmd);
+
+	return 0;
 }
 
 static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
-- 
2.43.0


  reply	other threads:[~2026-12-01  7:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  7:25 [PATCH v2 0/2] dm thin: complete writes only after the metadata commit Peng Yu
2026-12-01  7:25 ` Peng Yu [this message]
2026-12-01  7:25 ` [PATCH v2 2/2] dm thin: add wait_for_commit pool feature Peng Yu
2026-12-01  7:52   ` sashiko-bot
2026-12-02  2:40     ` peng yu

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=20261201072532.68398-2-yupeng0921@gmail.com \
    --to=yupeng0921@gmail.com \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=ejt@redhat.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=sashiko-bot@kernel.org \
    --cc=snitzer@kernel.org \
    --cc=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.