* [PATCH] md/raid10: don't take a recovering member as the resync source
@ 2026-12-05 19:07 Miroslav Cimerman
2026-12-05 19:29 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Miroslav Cimerman @ 2026-12-05 19:07 UTC (permalink / raw)
To: Song Liu, Yu Kuai; +Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] md/raid10: don't take a recovering member as the resync source
2026-12-05 19:07 [PATCH] md/raid10: don't take a recovering member as the resync source Miroslav Cimerman
@ 2026-12-05 19:29 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 19:29 UTC (permalink / raw)
To: Miroslav Cimerman; +Cc: linux-raid, yukuai
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] md/raid10: sync_request_write() lacks Faulty check after
sync_copy_valid()
- [Low] md/raid10: data race on rdev->recovery_offset in
sync_copy_valid()
--
commit 722f3c309b30c64a76fdb814b1a1c2ccb688d55e
Author: Miroslav Cimerman <mc@raidlab.org>
md/raid10: don't take a recovering member as the resync source
This commit adds sync_copy_valid() to prevent RAID10 resync from using
data above recovery_offset on a non-In_sync member as the resync source
and to overwrite such copies with the valid source.
> diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
> index 68f42cad32a59..2139c7030bfef 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;
> +}
[Severity: Low]
Can this plain read of rdev->recovery_offset race with md_reload_sb() on
clustered RAID10 arrays?
raid10d() calls sync_request_write() without holding mddev->reconfig_mutex.
On a clustered RAID10 near array, recv_daemon() can concurrently handle a
METADATA_UPDATED message and reload the superblock for non-Faulty members
with MD_FEATURE_RECOVERY_OFFSET set:
CPU0 (raid10d) CPU1 (recv_daemon)
sync_request_write() process_metadata_update()
sync_copy_valid() mddev_trylock(mddev)
md_reload_sb()
read_rdev()
read rdev->recovery_offset rdev->recovery_offset = ...
<-- data race
While the resyncing node holds resync_lockres, read_rdev() normally writes
the same value, causing a data race under CONFIG_KCSAN_STRICT. If a peer
broadcasts a stale recovery_offset, a 32-bit kernel can also observe a torn
64-bit load.
[ ... ]
> @@ -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) {
[Severity: Medium]
Can this submit write bios to a disk that has just been marked Faulty?
Assembled In_sync members keep rdev->recovery_offset at 0. If a disk fails
while resync r10_bios are in flight or queued on conf->retry_list (for
example via raid10_error() from an I/O error or mdadm --fail), raid10_error()
clears In_sync and sets Faulty while leaving recovery_offset at 0:
CPU0 (raid10d) CPU1
raid10_error()
clear_bit(In_sync, &rdev->flags)
set_bit(Faulty, &rdev->flags)
sync_request_write()
!sync_copy_valid() -> true
submit_bio_noacct(tbio) <-- writes to Faulty disk
For every remaining r10_bio whose read on that disk succeeded,
sync_copy_valid() now returns false even when the data matches, so this
branch skips memcmp() and falls through to submit REQ_OP_WRITE bios to the
Faulty disk. That mutates the failed device, keeps rdev->nr_pending and
mddev->recovery_active elevated while draining, and can spuriously record
write errors in end_sync_write().
Checking test_bit(Faulty, &rdev->flags) before writing (as raid1's
sync_request_write() does) would avoid issuing writes to a failed member.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205190740.1519480-1-mc@raidlab.org?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 19:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 19:07 [PATCH] md/raid10: don't take a recovering member as the resync source Miroslav Cimerman
2026-12-05 19:29 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox