From: Miroslav Cimerman <mc@raidlab.org>
To: Song Liu <song@kernel.org>, Yu Kuai <yukuai@fygo.io>
Cc: Li Nan <magiclinan@didiglobal.com>, Xiao Ni <xiao@kernel.org>,
linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] md/raid10: don't take a recovering member as the resync source
Date: Sat, 5 Dec 2026 19:07:40 +0000 [thread overview]
Message-ID: <20261205190740.1519480-1-mc@raidlab.org> (raw)
A power failure during a recovery can leave the array dirty. At the
next assembly the recovering member keeps its slot, without In_sync
and with the recovery offset from its superblock. This holds for a new
spare and for a member re-added with a write-intent bitmap. md resyncs
a dirty array before it recovers, so raid10 resyncs with the member in
its slot.
The resync reads every copy of a row whose member is present and not
Faulty. sync_request_write() takes the first copy that read without
error as the source, and overwrites every copy that differs from it.
When the recovering member holds the first copy of a row past its
recovery offset, its old content overwrites the other copy. For a
spare:
assembly after the power failure
array dirty, so md starts with a resync
spare in its slot, not In_sync, recovery_offset from the superblock
resync, every row past the recovery offset
raid10_sync_request(): reads the spare and the surviving copy
sync_request_write(): fbio = read from the spare,
overwrites the surviving copy with its data
recovery
skipped, the resync cleared conf->fullsync
the spare becomes In_sync
Every such row then reads back the spare's old content from both
copies, and md reports the array in sync. For a re-added member, the
resync covers only the regions its bitmap marks dirty. The surviving
copy gets the member's old content there.
read_balance() never reads a member that is not In_sync above its
recovery_offset, and raid1_sync_request() only writes to such a member.
Apply the same rule in sync_request_write(): such a copy is never the
source, and it is always overwritten with the source. The resync then
rewrites the recovering member as well.
Found with FuzzyRAID, a fault injection tester for RAID.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org # 6.12+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
6.12+ because 4b10a3bc67c1 ("md: ensure resync is prioritized over
recovery") made the bug reachable. Before it, md recovered the member
first, and the recovery reads only In_sync copies. 4b10a3bc67c1 is in
6.15, 6.12.y and 6.14.y. In the tests below, its parent loses nothing,
and 4b10a3bc67c1 loses the same as md-7.5.
Reproduced on md-7.5 with null_blk members that have a volatile cache:
RAID10 near=2 over 4 members with 126 MiB of data, and a power-off of
the members after a 4 KiB write at 31% of a recovery. With a spare
added after a failed member, 86 of the 126 MiB read back wrong after
the assembly, every MiB from MiB 40 on (mismatch_cnt 87808 sectors).
MiB 40 of the array is the spare's recovery offset of 20 MiB. The lost
rows were written and flushed before the member failed, and the one
write before the power-off read back correctly. The same happened with
a write-intent bitmap, with the far=2 layout, and with a member
re-added with a bitmap. Without the write, the array stays clean and
nothing is lost. With this patch, all data read back intact in every
case.
If the only valid copy of a row fails to read, the row still reads
back the member's old content without an error. I have a follow-up
patch ready that marks such a row bad on the member.
Prepared with help from an LLM.
drivers/md/raid10.c | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 68f42cad32a5..2139c7030bfe 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -2292,6 +2292,19 @@ static void end_sync_write(struct bio *bio)
end_sync_request(r10_bio);
}
+/*
+ * A member that is not In_sync holds valid data only below its
+ * recovery_offset, see read_balance().
+ */
+static bool sync_copy_valid(struct r10conf *conf, struct r10bio *r10_bio,
+ int i)
+{
+ struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
+ sector_t end = r10_bio->devs[i].addr + r10_bio->sectors;
+
+ return test_bit(In_sync, &rdev->flags) || end <= rdev->recovery_offset;
+}
+
/*
* Note: sync and recover and handled very differently for raid10
* This code is for resync.
@@ -2320,7 +2333,8 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
/* find the first device with a block */
for (i=0; i<conf->copies; i++)
- if (!r10_bio->devs[i].bio->bi_status)
+ if (!r10_bio->devs[i].bio->bi_status &&
+ sync_copy_valid(conf, r10_bio, i))
break;
if (i == conf->copies)
@@ -2349,7 +2363,15 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
tpages = get_resync_pages(tbio)->pages;
d = r10_bio->devs[i].devnum;
rdev = conf->mirrors[d].rdev;
- if (!r10_bio->devs[i].bio->bi_status) {
+ if (!r10_bio->devs[i].bio->bi_status &&
+ !sync_copy_valid(conf, r10_bio, i)) {
+ /*
+ * Not recovered yet: don't compare, write the source
+ * to it, unless this is a check.
+ */
+ if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
+ continue;
+ } else if (!r10_bio->devs[i].bio->bi_status) {
/* We know that the bi_io_vec layout is the same for
* both 'first' and 'i', so we just compare them.
* All vec entries are PAGE_SIZE;
base-commit: c526b7cd32f75fea6021ffdab287b81cf38ddeb7
--
2.53.0
next reply other threads:[~2026-12-05 19:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 19:07 Miroslav Cimerman [this message]
2026-12-05 19:29 ` [PATCH] md/raid10: don't take a recovering member as the resync source sashiko-bot
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=20261205190740.1519480-1-mc@raidlab.org \
--to=mc@raidlab.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=magiclinan@didiglobal.com \
--cc=song@kernel.org \
--cc=xiao@kernel.org \
--cc=yukuai@fygo.io \
/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