The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded
@ 2026-11-27  0:32 Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails Miroslav Cimerman
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

raid1 and raid10 hold a write that failed on one device until the
superblock records that failure. This series fixes cases where a write
still completes with success, although its data is not safe:

1/8: the superblock write fails on the last working device, and reads
     can return old data after a restart.
2/8: raid10 waits for blocked devices by copy index, not by device.
3/8: as 1/8, for a write that waited for the superblock update.
4/8: with a bad block log, the device with the only good copy fails,
     and reads of the write return EIO.
5/8: a write whose copy fails on a device that is already faulty is
     not held at all.
6/8: raid1 can release a held write after it marks the device faulty
     and before it requests the superblock update.
7/8: a device that fails between the wait for blocked devices and the
     selection of the devices for a write is skipped, and the write is
     not held.
8/8: with write-behind, a write whose copy failed on a device completes
     once its write-mostly copy is done.

The MD_BROKEN checks skip dm-raid, which does not reject writes when
MD_BROKEN is set, so dm-raid can still acknowledge such writes. I have a
fix for dm-raid and a follow-up that drops these exceptions, both
tested, and will send them to dm-devel on top of this series. The other
changes apply to dm-raid too.

Patch 1 fixes a bug found with FuzzyRAID, a fault injection fuzzer for
RAID implementations. Patches 2 to 8 fix related cases found while
analyzing it. I used an LLM (Claude) for this series: it found the bugs
fixed by patches 2 to 8, drafted the code of patches 2, 4, 5, 6, 7 and 8
and the changelogs, and wrote and ran the tests. I reviewed everything.

Tested on md-7.5 with RAID1 (2 disks; 3 disks in controls) and RAID10 (2
and 4 disks, near and far), with and without a bad block log, RAID1 also
with a bitmap and with write-behind, and with failfast: 30 fault cases
in which an acknowledged write was lost or read back old data now fail
the write, and 20 controls give the same results as before (some now
wait for the superblock update). The cases for patches 6, 7 and 8
compare with the series without that patch, those for 6 and 7 with
delays added only for the test. All others compare with stock md-7.5. A
raid10 smoke test without faults (4-disk and 3-disk near, 2-disk far and
offset, with discards) passes too. Not tested: the race that patch 2
closes, MD_FAILLAST_DEV, dm-raid, replacements and reshape.

Miroslav Cimerman (8):
  md/raid1,raid10: fail held writes when the array fails
  md/raid10: wait for the devices of the write in wait_blocked_dev()
  md/raid1,raid10: fail writes that waited while the array failed
  md/raid1,raid10: fail held writes that no working device has
  md/raid1,raid10: hold writes that fail on a faulty device
  md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error()
  md/raid1,raid10: hold writes that skip a newly failed device
  md/raid1: hold write-behind writes that failed on a device

 drivers/md/raid1-10.c | 12 ++++++
 drivers/md/raid1.c    | 83 +++++++++++++++++++++++++++++++-------
 drivers/md/raid10.c   | 92 +++++++++++++++++++++++++++++++++----------
 3 files changed, 152 insertions(+), 35 deletions(-)


base-commit: c526b7cd32f75fea6021ffdab287b81cf38ddeb7
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 2/8] md/raid10: wait for the devices of the write in wait_blocked_dev() Miroslav Cimerman
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

raid1 and raid10 hold a write that failed on one device until the
superblock records that device as faulty.

If the superblock write then fails on the last working device, the
error handler sets MD_BROKEN, keeps that device In_sync and requests no
new superblock update. md_update_sb() clears MD_SB_CHANGE_PENDING, and
the held write completes with success. No superblock records the failed
device. After a restart, both devices assemble as in sync, and reads
can return the old data of the failed one.

Fail the held writes when MD_BROKEN is set. dm-raid does not reject
writes when MD_BROKEN is set, so leave it unchanged.

Found with FuzzyRAID, a fault injection fuzzer for RAID
implementations.

Fixes: 55ce74d4bfe1 ("md/raid1: ensure device failure recorded before write request returns.")
Fixes: 95af587e95aa ("md/raid10: ensure device failure recorded before write request returns.")
Cc: stable@vger.kernel.org # v5.19+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c  | 8 ++++++++
 drivers/md/raid10.c | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 25b80ad52f00..37d120a63778 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -2629,6 +2629,14 @@ static void raid1d(struct md_thread *thread)
 			list_del(&r1_bio->retry_list);
 			idx = sector_to_idx(r1_bio->sector);
 			atomic_dec(&conf->nr_queued[idx]);
+			/*
+			 * If the array failed while this write was held, the
+			 * failed copy might not be recorded. Fail the write,
+			 * except for dm-raid.
+			 */
+			if (!mddev_is_dm(mddev) &&
+			    test_bit(MD_BROKEN, &mddev->flags))
+				clear_bit(R1BIO_Uptodate, &r1_bio->state);
 			if (test_bit(R1BIO_WriteError, &r1_bio->state))
 				close_write(r1_bio);
 			raid_end_bio_io(r1_bio);
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 68f42cad32a5..a65bd65ae7bd 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -2942,6 +2942,14 @@ static void raid10d(struct md_thread *thread)
 						   retry_list);
 			list_del(&r10_bio->retry_list);
 
+			/*
+			 * If the array failed while this write was held, the
+			 * failed copy might not be recorded. Fail the write,
+			 * except for dm-raid.
+			 */
+			if (!mddev_is_dm(mddev) &&
+			    test_bit(MD_BROKEN, &mddev->flags))
+				clear_bit(R10BIO_Uptodate, &r10_bio->state);
 			if (test_bit(R10BIO_WriteError,
 				     &r10_bio->state))
 				close_write(r10_bio);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/8] md/raid10: wait for the devices of the write in wait_blocked_dev()
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 3/8] md/raid1,raid10: fail writes that waited while the array failed Miroslav Cimerman
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

wait_blocked_dev() checks conf->mirrors[i] for copy i of the write, but
copy i is on device r10_bio->devs[i].devnum. With more devices than
copies, a write to other devices does not wait for a blocked device or
for an unacknowledged bad block on them. With a far or offset layout,
the bad block check uses the address of a copy on another device. The
write can then complete before md records the failure or the bad block.

Check the device of each copy. raid10_handle_discard() does not set
devs[].devnum, so discards still check the first conf->copies devices,
as before.

Fixes: f2e7e269a752 ("md/raid10: pull the code that wait for blocked dev into one function")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid10.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index a65bd65ae7bd..a97831823c68 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -1273,9 +1273,11 @@ static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
 retry_wait:
 	blocked_rdev = NULL;
 	for (i = 0; i < conf->copies; i++) {
+		/* A discard (sectors == 0) does not use devs[].devnum. */
+		int d = r10_bio->sectors ? r10_bio->devs[i].devnum : i;
 		struct md_rdev *rdev, *rrdev;
 
-		rdev = conf->mirrors[i].rdev;
+		rdev = conf->mirrors[d].rdev;
 		if (rdev) {
 			sector_t dev_sector = r10_bio->devs[i].addr;
 
@@ -1300,7 +1302,7 @@ static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
 			}
 		}
 
-		rrdev = conf->mirrors[i].replacement;
+		rrdev = conf->mirrors[d].replacement;
 		if (rrdev && rdev_blocked(rrdev)) {
 			atomic_inc(&rrdev->nr_pending);
 			blocked_rdev = rrdev;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/8] md/raid1,raid10: fail writes that waited while the array failed
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 2/8] md/raid10: wait for the devices of the write in wait_blocked_dev() Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 4/8] md/raid1,raid10: fail held writes that no working device has Miroslav Cimerman
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

md_submit_bio() fails new writes once MD_BROKEN is set. A write that
arrives while a failed device is being recorded passes that check and
waits for the superblock update. If the update fails on the last
working device, MD_BROKEN is set during the wait. The write then
continues, reaches only the remaining device and completes with
success. After a restart, both devices assemble as in sync, and reads
can return the old data.

Check MD_BROKEN again after the wait, and fail the write. dm-raid does
not go through md_submit_bio(), so leave it unchanged. Large raid10
discards go through raid10_handle_discard() and are left as they are.

Fixes: 9631abdbf406 ("md: Set MD_BROKEN for RAID1 and RAID10")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c  | 12 ++++++++++++
 drivers/md/raid10.c | 11 +++++++++++
 2 files changed, 23 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 37d120a63778..88bbe81c7798 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -1489,6 +1489,18 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,
 
 	wait_blocked_rdev(mddev, bio);
 
+	/*
+	 * If the array failed while this write waited, the failed device
+	 * might not be recorded. Fail the write, as md_submit_bio() does,
+	 * except for dm-raid.
+	 */
+	if (unlikely(!mddev_is_dm(mddev) &&
+		     test_bit(MD_BROKEN, &mddev->flags))) {
+		bio_io_error(bio);
+		allow_barrier(conf, sector);
+		return false;
+	}
+
 	r1_bio = alloc_r1bio(mddev, bio);
 	r1_bio->sectors = max_sectors;
 
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index a97831823c68..a38b95585e35 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -1378,6 +1378,17 @@ static bool raid10_write_request(struct mddev *mddev, struct bio *bio,
 
 	wait_blocked_dev(mddev, r10_bio);
 
+	/*
+	 * If the array failed while this write waited, the failed device
+	 * might not be recorded. Fail the write, as md_submit_bio() does,
+	 * except for dm-raid.
+	 */
+	if (unlikely(!mddev_is_dm(mddev) &&
+		     test_bit(MD_BROKEN, &mddev->flags))) {
+		raid_end_bio_io(r10_bio);
+		return false;
+	}
+
 	max_sectors = r10_bio->sectors;
 
 	for (i = 0;  i < conf->copies; i++) {
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/8] md/raid1,raid10: fail held writes that no working device has
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
                   ` (2 preceding siblings ...)
  2026-11-27  0:32 ` [PATCH 3/8] md/raid1,raid10: fail writes that waited while the array failed Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device Miroslav Cimerman
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

With a bad block log, a failed copy is recorded as a bad block, and its
device stays in the array. If the superblock write then fails on the
device with the good copy, md marks that device faulty while the write
is held. The write still completes with success, although no working
device has its data, and reads of it return EIO.

Check at release that a working device has all of the write, and fail
the write otherwise.

Fixes: 55ce74d4bfe1 ("md/raid1: ensure device failure recorded before write request returns.")
Fixes: 95af587e95aa ("md/raid10: ensure device failure recorded before write request returns.")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c  | 28 ++++++++++++++++++++++++++++
 drivers/md/raid10.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 88bbe81c7798..fcdbccf6e368 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -2572,6 +2572,26 @@ static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
 	}
 }
 
+/*
+ * A device that is In_sync, not Faulty and has no bad block in the range
+ * of the write was not skipped and did not fail, so it has the whole write.
+ */
+static bool raid1_held_write_has_copy(struct r1conf *conf,
+				      struct r1bio *r1_bio)
+{
+	int i;
+
+	for (i = 0; i < conf->raid_disks * 2; i++) {
+		struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev);
+
+		if (rdev && test_bit(In_sync, &rdev->flags) &&
+		    !test_bit(Faulty, &rdev->flags) &&
+		    !rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors))
+			return true;
+	}
+	return false;
+}
+
 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
 {
 	struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
@@ -2649,6 +2669,14 @@ static void raid1d(struct md_thread *thread)
 			if (!mddev_is_dm(mddev) &&
 			    test_bit(MD_BROKEN, &mddev->flags))
 				clear_bit(R1BIO_Uptodate, &r1_bio->state);
+			/*
+			 * Fail the write if no working device has it now.
+			 * Skip write-behind writes: they completed already,
+			 * and nothing pins their rdevs.
+			 */
+			if (!test_bit(R1BIO_Returned, &r1_bio->state) &&
+			    !raid1_held_write_has_copy(conf, r1_bio))
+				clear_bit(R1BIO_Uptodate, &r1_bio->state);
 			if (test_bit(R1BIO_WriteError, &r1_bio->state))
 				close_write(r1_bio);
 			raid_end_bio_io(r1_bio);
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index a38b95585e35..838ae71205c1 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -2837,6 +2837,37 @@ static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
 	allow_barrier(conf);
 }
 
+static bool raid10_rdev_has_copy(struct md_rdev *rdev, sector_t addr,
+				 int sectors)
+{
+	return rdev && test_bit(In_sync, &rdev->flags) &&
+	       !test_bit(Faulty, &rdev->flags) &&
+	       !rdev_has_badblock(rdev, addr, sectors);
+}
+
+/*
+ * A device that is In_sync, not Faulty and has no bad block at the copy's
+ * address was not skipped and did not fail, so it has the copy. Check the
+ * replacement too: a finished replacement stays in its slot until moved.
+ */
+static bool raid10_held_write_has_copy(struct r10conf *conf,
+				       struct r10bio *r10_bio)
+{
+	int i;
+
+	for (i = 0; i < conf->copies; i++) {
+		int d = r10_bio->devs[i].devnum;
+		sector_t addr = r10_bio->devs[i].addr;
+		struct md_rdev *rdev = READ_ONCE(conf->mirrors[d].rdev);
+		struct md_rdev *rrdev = READ_ONCE(conf->mirrors[d].replacement);
+
+		if (raid10_rdev_has_copy(rdev, addr, r10_bio->sectors) ||
+		    raid10_rdev_has_copy(rrdev, addr, r10_bio->sectors))
+			return true;
+	}
+	return false;
+}
+
 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
 {
 	/* Some sort of write request has finished and it
@@ -2963,6 +2994,9 @@ static void raid10d(struct md_thread *thread)
 			if (!mddev_is_dm(mddev) &&
 			    test_bit(MD_BROKEN, &mddev->flags))
 				clear_bit(R10BIO_Uptodate, &r10_bio->state);
+			/* Fail the write if no working device has it now */
+			if (!raid10_held_write_has_copy(conf, r10_bio))
+				clear_bit(R10BIO_Uptodate, &r10_bio->state);
 			if (test_bit(R10BIO_WriteError,
 				     &r10_bio->state))
 				close_write(r10_bio);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
                   ` (3 preceding siblings ...)
  2026-11-27  0:32 ` [PATCH 4/8] md/raid1,raid10: fail held writes that no working device has Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 6/8] md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error() Miroslav Cimerman
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

If a device is already Faulty when a copy of a write fails on it, the
end_io does not hold the write. The write completes at once with
success from the other copy, before the superblock records the failure.
This happens to writes in flight when another write fails the device,
to writes that raid1_submit_write() fails on a Faulty device, and with
failfast, where md_error() runs in the end_io.

Hold these writes too, but skip narrow_write_error() for a Faulty
device. The "end bio when the device faulty" commits avoided these
retries.

Fixes: eeba6809d8d5 ("md/raid1: end bio when the device faulty")
Fixes: 7cee6d4e6035 ("md/raid10: end bio when the device faulty")
Fixes: 212e7eb7a340 ("md/raid1: add failfast handling for writes.")
Fixes: 1919cbb23bf1 ("md/raid10: add failfast handling for writes.")
Cc: stable@vger.kernel.org # v5.19+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c  | 16 ++++++----------
 drivers/md/raid10.c | 20 ++++++--------------
 2 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index fcdbccf6e368..54ba3013150e 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -493,16 +493,10 @@ static void raid1_end_write_request(struct bio *bio)
 		}
 
 		/*
-		 * When the device is faulty, it is not necessary to
-		 * handle write error.
+		 * Hold the write until the superblock records the failure,
+		 * also on a Faulty device.
 		 */
-		if (!test_bit(Faulty, &rdev->flags))
-			set_bit(R1BIO_WriteError, &r1_bio->state);
-		else {
-			/* Finished with this branch */
-			r1_bio->bios[mirror] = NULL;
-			to_put = bio;
-		}
+		set_bit(R1BIO_WriteError, &r1_bio->state);
 	} else {
 		/*
 		 * Set R1BIO_Uptodate in our master bio, so that we
@@ -2549,7 +2543,9 @@ static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
 			 * errors.
 			 */
 			fail = true;
-			narrow_write_error(r1_bio, m);
+			/* No retries on a Faulty device */
+			if (!test_bit(Faulty, &conf->mirrors[m].rdev->flags))
+				narrow_write_error(r1_bio, m);
 			rdev_dec_pending(conf->mirrors[m].rdev,
 					 conf->mddev);
 		}
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 838ae71205c1..519b6ac0accd 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -473,7 +473,6 @@ static void raid10_end_write_request(struct bio *bio)
 	struct r10conf *conf = r10_bio->mddev->private;
 	int slot, repl;
 	struct md_rdev *rdev = NULL;
-	struct bio *to_put = NULL;
 	bool ignore_error = !raid1_should_handle_error(bio) ||
 		(bio->bi_status && bio_op(bio) == REQ_OP_DISCARD);
 
@@ -505,17 +504,10 @@ static void raid10_end_write_request(struct bio *bio)
 			}
 
 			/*
-			 * When the device is faulty, it is not necessary to
-			 * handle write error.
+			 * Hold the write until the superblock records the
+			 * failure, also on a Faulty device.
 			 */
-			if (!test_bit(Faulty, &rdev->flags))
-				set_bit(R10BIO_WriteError, &r10_bio->state);
-			else {
-				/* Fail the request */
-				r10_bio->devs[slot].bio = NULL;
-				to_put = bio;
-				dec_rdev = 1;
-			}
+			set_bit(R10BIO_WriteError, &r10_bio->state);
 		}
 	} else {
 		/*
@@ -560,8 +552,6 @@ static void raid10_end_write_request(struct bio *bio)
 	one_write_done(r10_bio);
 	if (dec_rdev)
 		rdev_dec_pending(rdev, conf->mddev);
-	if (to_put)
-		bio_put(to_put);
 }
 
 /*
@@ -2926,7 +2916,9 @@ static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
 				rdev_dec_pending(rdev, conf->mddev);
 			} else if (bio != NULL && bio->bi_status) {
 				fail = true;
-				narrow_write_error(r10_bio, m);
+				/* No retries on a Faulty device */
+				if (!test_bit(Faulty, &rdev->flags))
+					narrow_write_error(r10_bio, m);
 				rdev_dec_pending(rdev, conf->mddev);
 			}
 			bio = r10_bio->devs[m].repl_bio;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 6/8] md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error()
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
                   ` (4 preceding siblings ...)
  2026-11-27  0:32 ` [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 8/8] md/raid1: hold write-behind writes that failed on a device Miroslav Cimerman
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

raid1_error() drops device_lock after it sets Faulty and before it sets
MD_SB_CHANGE_PENDING. raid1d can release a held write in between,
before the superblock records the failure.

Set MD_SB_CHANGE_PENDING under the lock, as raid10_error() does.

Fixes: 55ce74d4bfe1 ("md/raid1: ensure device failure recorded before write request returns.")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 54ba3013150e..f596b36ec6e1 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -1741,13 +1741,13 @@ static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
 	if (test_and_clear_bit(In_sync, &rdev->flags))
 		mddev->degraded++;
 	set_bit(Faulty, &rdev->flags);
+	set_mask_bits(&mddev->sb_flags, 0,
+		      BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
 	spin_unlock_irqrestore(&conf->device_lock, flags);
 	/*
 	 * if recovery is running, make sure it aborts.
 	 */
 	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
-	set_mask_bits(&mddev->sb_flags, 0,
-		      BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
 	pr_crit("md/raid1:%s: Disk failure on %pg, disabling device.\n"
 		"md/raid1:%s: Operation continuing on %d devices.\n",
 		mdname(mddev), rdev->bdev,
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
                   ` (5 preceding siblings ...)
  2026-11-27  0:32 ` [PATCH 6/8] md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error() Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  2026-11-27  0:32 ` [PATCH 8/8] md/raid1: hold write-behind writes that failed on a device Miroslav Cimerman
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

A device can fail between the wait for blocked devices and the device
selection of a write. The selection skips Faulty devices, so the write
completes before the superblock records the failure. Before the commits
below, the selection waited for a Blocked device.

Give the write to a Faulty device while its failure might not be
recorded: while it is Blocked, or after MD_BROKEN. raid1_submit_write()
fails the copy at once, and the write is held. Waiting for Blocked
again would miss MD_BROKEN: md_update_sb() clears Blocked even when the
superblock write failed.

This needs "md/raid1,raid10: hold writes that fail on a faulty device"
and "md/raid1,raid10: fail held writes when the array fails".

Fixes: 88ed59c4cc6c ("md/raid1: factor out helper to handle blocked rdev from raid1_write_request()")
Fixes: f2e7e269a752 ("md/raid10: pull the code that wait for blocked dev into one function")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1-10.c | 12 ++++++++++++
 drivers/md/raid1.c    |  9 ++++++++-
 drivers/md/raid10.c   | 13 +++++++++----
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/drivers/md/raid1-10.c b/drivers/md/raid1-10.c
index 341a9785e3f2..f8fa9cdc4080 100644
--- a/drivers/md/raid1-10.c
+++ b/drivers/md/raid1-10.c
@@ -125,6 +125,18 @@ static inline void raid1_submit_write(struct bio *bio)
 		submit_bio_noacct(bio);
 }
 
+/*
+ * Give a write to a Faulty device while its failure might not be recorded:
+ * Blocked is set until it is, and after MD_BROKEN it might never be.
+ * raid1_submit_write() fails the copy, and the write is held.
+ */
+static inline bool raid1_write_to_faulty(struct mddev *mddev,
+					 struct md_rdev *rdev)
+{
+	return test_bit(Blocked, &rdev->flags) ||
+	       (!mddev_is_dm(mddev) && test_bit(MD_BROKEN, &mddev->flags));
+}
+
 static inline bool raid1_add_bio_to_plug(struct mddev *mddev, struct bio *bio,
 				      blk_plug_cb_fn unplug, int copies)
 {
diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index f596b36ec6e1..508673e8c02e 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -1524,8 +1524,15 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,
 			write_behind = false;
 
 		r1_bio->bios[i] = NULL;
-		if (!rdev || test_bit(Faulty, &rdev->flags))
+		if (!rdev)
+			continue;
+		if (test_bit(Faulty, &rdev->flags)) {
+			if (raid1_write_to_faulty(mddev, rdev)) {
+				atomic_inc(&rdev->nr_pending);
+				r1_bio->bios[i] = bio;
+			}
 			continue;
+		}
 
 		if (test_bit(WriteErrorSeen, &rdev->flags)) {
 			sector_t first_bad;
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 519b6ac0accd..6ea84eb8bf87 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -1387,14 +1387,19 @@ static bool raid10_write_request(struct mddev *mddev, struct bio *bio,
 
 		rdev = conf->mirrors[d].rdev;
 		rrdev = conf->mirrors[d].replacement;
-		if (rdev && (test_bit(Faulty, &rdev->flags)))
+		r10_bio->devs[i].bio = NULL;
+		r10_bio->devs[i].repl_bio = NULL;
+
+		if (rdev && test_bit(Faulty, &rdev->flags)) {
+			if (raid1_write_to_faulty(mddev, rdev)) {
+				r10_bio->devs[i].bio = bio;
+				atomic_inc(&rdev->nr_pending);
+			}
 			rdev = NULL;
+		}
 		if (rrdev && (test_bit(Faulty, &rrdev->flags)))
 			rrdev = NULL;
 
-		r10_bio->devs[i].bio = NULL;
-		r10_bio->devs[i].repl_bio = NULL;
-
 		if (!rdev && !rrdev)
 			continue;
 		if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 8/8] md/raid1: hold write-behind writes that failed on a device
  2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
                   ` (6 preceding siblings ...)
  2026-11-27  0:32 ` [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device Miroslav Cimerman
@ 2026-11-27  0:32 ` Miroslav Cimerman
  7 siblings, 0 replies; 9+ messages in thread
From: Miroslav Cimerman @ 2026-11-27  0:32 UTC (permalink / raw)
  To: Song Liu, Yu Kuai
  Cc: Li Nan, Xiao Ni, NeilBrown, Mariusz Tkaczyk, Kenta Akagi,
	linux-raid, linux-kernel

With write-behind, raid1_end_write_request() completes the master bio
once only write-mostly copies are outstanding and one copy succeeded,
even if another copy failed. The write then completes before the
superblock records that failure.

Do not complete the master bio early when R1BIO_WriteError is set.

Fixes: 55ce74d4bfe1 ("md/raid1: ensure device failure recorded before write request returns.")
Cc: stable@vger.kernel.org
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
 drivers/md/raid1.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 508673e8c02e..8c4ca1b0f18c 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -541,10 +541,12 @@ static void raid1_end_write_request(struct bio *bio)
 		 * has safely reached all non-writemostly
 		 * disks. Setting the Returned bit ensures that this
 		 * gets done only once -- we don't ever want to return
-		 * -EIO here, instead we'll wait
+		 * -EIO here, instead we'll wait. A write with a failed
+		 * copy is held.
 		 */
 		if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
-		    test_bit(R1BIO_Uptodate, &r1_bio->state)) {
+		    test_bit(R1BIO_Uptodate, &r1_bio->state) &&
+		    !test_bit(R1BIO_WriteError, &r1_bio->state)) {
 			/* Maybe we can return now */
 			if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
 				struct bio *mbio = r1_bio->master_bio;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-27  0:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 2/8] md/raid10: wait for the devices of the write in wait_blocked_dev() Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 3/8] md/raid1,raid10: fail writes that waited while the array failed Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 4/8] md/raid1,raid10: fail held writes that no working device has Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 6/8] md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error() Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 8/8] md/raid1: hold write-behind writes that failed on a device Miroslav Cimerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox