Skip to contentSkip to conversation

md/raid1,raid10: fix writes acknowledged before a failure is recorded

PATCH New Miroslav Cimerman 2 participants 11 messages Nov 27 MD RAID

lore
Share & tools
lore
#0 lore raw

cover md/raid1,raid10: fix writes acknowledged before a failure is recorded

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

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/8 md/raid1,raid10: fail held writes when the array fails

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 [email protected] # v5.19+
---
 drivers/md/raid1.c  | 8 ++++++++
 drivers/md/raid10.c | 8 ++++++++
 2 files changed, 16 insertions(+)
drivers/md/raid1.c +8 -0
index 25b80ad52f00..37d120a63778 100644
@@ -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);
drivers/md/raid10.c +8 -0
index 68f42cad32a5..a65bd65ae7bd 100644
@@ -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);
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/8 md/raid10: wait for the devices of the write in wait_blocked_dev()

In reply to Miroslav Cimerman

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")
---
 drivers/md/raid10.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/raid10.c +4 -2
index a65bd65ae7bd..a97831823c68 100644
@@ -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;
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/8 md/raid1,raid10: fail writes that waited while the array failed

In reply to Miroslav Cimerman

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")
---
 drivers/md/raid1.c  | 12 ++++++++++++
 drivers/md/raid10.c | 11 +++++++++++
 2 files changed, 23 insertions(+)
drivers/md/raid1.c +12 -0
index 37d120a63778..88bbe81c7798 100644
@@ -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;
 
drivers/md/raid10.c +11 -0
index a97831823c68..a38b95585e35 100644
@@ -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++) {
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/8 md/raid1,raid10: fail held writes that no working device has

In reply to Miroslav Cimerman

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.")
---
 drivers/md/raid1.c  | 28 ++++++++++++++++++++++++++++
 drivers/md/raid10.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
drivers/md/raid1.c +28 -0
index 88bbe81c7798..fcdbccf6e368 100644
@@ -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);
drivers/md/raid10.c +34 -0
index a38b95585e35..838ae71205c1 100644
@@ -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);
Signature · 1 line
2.53.0
#5 lore raw

PATCH 5/8 md/raid1,raid10: hold writes that fail on a faulty device

In reply to Miroslav Cimerman

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 [email protected] # v5.19+
---
 drivers/md/raid1.c  | 16 ++++++----------
 drivers/md/raid10.c | 20 ++++++--------------
 2 files changed, 12 insertions(+), 24 deletions(-)
drivers/md/raid1.c +6 -10
index fcdbccf6e368..54ba3013150e 100644
@@ -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);
 		}
drivers/md/raid10.c +6 -14
index 838ae71205c1..519b6ac0accd 100644
@@ -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;
Signature · 1 line
2.53.0
#7 lore raw

PATCH 6/8 md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error()

In reply to Miroslav Cimerman

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.")
---
 drivers/md/raid1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/raid1.c +2 -2
index 54ba3013150e..f596b36ec6e1 100644
@@ -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,
Signature · 1 line
2.53.0
#8 lore raw

PATCH 7/8 md/raid1,raid10: hold writes that skip a newly failed device

In reply to Miroslav Cimerman

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")
---
 drivers/md/raid1-10.c | 12 ++++++++++++
 drivers/md/raid1.c    |  9 ++++++++-
 drivers/md/raid10.c   | 13 +++++++++----
 3 files changed, 29 insertions(+), 5 deletions(-)
drivers/md/raid1-10.c +12 -0
index 341a9785e3f2..f8fa9cdc4080 100644
@@ -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)
 {
drivers/md/raid1.c +8 -1
index f596b36ec6e1..508673e8c02e 100644
@@ -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;
drivers/md/raid10.c +9 -4
index 519b6ac0accd..6ea84eb8bf87 100644
@@ -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)) {
Signature · 1 line
2.53.0
#10 lore raw

PATCH 8/8 md/raid1: hold write-behind writes that failed on a device

In reply to Miroslav Cimerman

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.")
---
 drivers/md/raid1.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/raid1.c +4 -2
index 508673e8c02e..8c4ca1b0f18c 100644
@@ -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;
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device

In reply to Miroslav Cimerman
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] md/raid10: unheld write and nr_pending leak when repl_bio fails --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] md/raid10: unheld write and nr_pending leak when repl_bio fails
--

commit 13f36c8d107f279dd87814c77406033640b24af3
Author: Miroslav Cimerman <[email protected]>

md/raid1,raid10: hold writes that fail on a faulty device

This commit holds writes whose mirror copy fails on an already Faulty device in raid1 and raid10 until the superblock records the failure, while skipping narrow_write_error() retries on Faulty devices.

Quoted 43 lines
drivers/md/raid10.c +3 -13
index 838ae71205c1a..519b6ac0accd0 100644
@@ -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);
 }
 
 /*

drivers/md/raid10.c static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) · line 2924

Quoted 10 lines
drivers/md/raid10.c +3 -1
@@ -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;

[Severity: Medium]
Can a failing r10_bio->devs[slot].repl_bio complete without being held or leak rdev->nr_pending?

In raid10_end_write_request(), if conf->mirrors[dev].replacement is still present (repl == 1), a write failure only calls md_error() and never sets R10BIO_WriteError, so the write completes immediately instead of waiting for the superblock update.

If instead raid10_remove_disk() concurrently promotes p->replacement to p->rdev and clears p->replacement while repl_bio is in flight, raid10_end_write_request() falls back to repl = 0, sets dec_rdev = 0 and R10BIO_WriteError (now even when the promoted device is Faulty):

drivers/md/raid10.c:raid10_end_write_request() {
	if (repl)
		rdev = conf->mirrors[dev].replacement;
	if (!rdev) {
		smp_rmb();
		repl = 0;
		rdev = conf->mirrors[dev].rdev;
	}
	if (bio->bi_status && !ignore_error) {
		if (repl)
			md_error(rdev->mddev, rdev);
		else {
			rdev_record_write_error(rdev);
			dec_rdev = 0;
			...
			set_bit(R10BIO_WriteError, &r10_bio->state);
		}
	}
}

Because the failed bio sits in r10_bio->devs[m].repl_bio rather than r10_bio->devs[m].bio, handle_write_completed() never sees bio->bi_status on it:

CPU0 CPU1
raid10_remove_disk()
WRITE_ONCE(p->rdev, p->replacement);

  WRITE_ONCE(p->replacement, NULL);
                                  raid10_end_write_request(repl_bio)
                                    rdev = conf->mirrors[dev].replacement;
                                    /* NULL -> repl = 0, rdev = p->rdev */
                                    dec_rdev = 0;
                                    set_bit(R10BIO_WriteError, ...);
                                  raid10d()
                                    handle_write_completed()
                                      bio = r10_bio->devs[m].bio;
                                      /* bio->bi_status == 0 */
                                      bio = r10_bio->devs[m].repl_bio;
                                      /* only IO_MADE_GOOD is checked */
                                      <-- rdev->nr_pending leaked,
                                          write not held

That permanently leaks rdev->nr_pending (preventing later disk removal) and skips holding the write.

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

Re: [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device

In reply to Miroslav Cimerman
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] md/raid1,raid10: ignored errors on faulty device skip hold and underflow nr_pending - [Medium]…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] md/raid1,raid10: ignored errors on faulty device skip hold and underflow nr_pending
- [Medium] md/raid10: newly failed replacement devices skipped in raid10_write_request()
- [Medium] md/raid1: write-behind bypasses hold for faulty rdev in raid1_write_request()
- [Medium] md/raid1,raid10: race with slot_store() when selecting faulty rdev
--

commit 1e300613f1afedbc7152ab6fe06cab49a896efac
Author: Miroslav Cimerman <[email protected]>

md/raid1,raid10: hold writes that skip a newly failed device

This commit assigns writes to a newly Faulty device while it is still Blocked or after MD_BROKEN is set, so that raid1_submit_write() fails the copy and holds the write until the superblock records the failure.

[ ... ]

Quoted 11 lines
drivers/md/raid1.c +2 -1
index f596b36ec6e10..508673e8c02e8 100644
@@ -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;

drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1538

Quoted 10 lines
drivers/md/raid1.c +6 -0
+		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;

[Severity: Medium]
Can a write-behind bio still complete early before the superblock update when a WriteMostly device becomes Faulty after wait_blocked_rdev()?

raid1_write_request() sets write_behind = true for a WriteMostly rdev before checking Faulty, and later increments r1_bio->behind_remaining when cloning for that Faulty rdev. Because write-behind requires an active bitmap, raid1_add_bio_to_plug() queues the clones instead of calling raid1_submit_write() immediately, so R1BIO_WriteError is not set yet. If the working non-WriteMostly copy completes before raid1_submit_write() fails the queued Faulty WriteMostly clone, raid1_end_write_request() sees behind_remaining >= remaining - 1 with R1BIO_Uptodate set and R1BIO_WriteError still clear, and completes master_bio early.

[Severity: Medium]
Can selecting an already-Faulty rdev here (and in raid10_write_request()) race with slot_store() and cause a NULL pointer dereference?

Unlike state_store(), slot_store() does not suspend the array via rdev_attr_store(). Because an already-Faulty rdev has In_sync cleared, slot_store() can remove it as soon as Blocked is clear and nr_pending is 0:

CPU0 CPU1
raid1_write_request()
test_bit(Faulty, &rdev->flags)
raid1_write_to_faulty(mddev, rdev)

                                      slot_store()
                                        clear_bit(Blocked, &rdev->flags)
                                        remove_and_add_spares()
                                          ...
                                          raid1_remove_disk()
                                            /* nr_pending == 0 */
                                            p->rdev = NULL

atomic_inc(&rdev->nr_pending)
r1_bio->bios[i] = bio
...
rdev = conf->mirrors[i].rdev
bio_alloc_clone(rdev->bdev, ...) <-- NULL pointer dereference

Quoted 9 lines
drivers/md/raid10.c +0 -1
index 519b6ac0accd0..6ea84eb8bf874 100644
@@ -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)))

drivers/md/raid10.c static bool raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1399

Quoted 10 lines
drivers/md/raid10.c +9 -0
+		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;
+		}

[Severity: High]
What happens when bio is a REQ_OP_DISCARD or REQ_RAHEAD request and raid1_write_to_faulty() gives it to a Faulty rdev (both here and in raid1_write_request())?

raid1_submit_write() fails the clone with bio_io_error(), but raid1_end_write_request() and raid10_end_write_request() compute ignore_error = true for REQ_OP_DISCARD and REQ_RAHEAD bios:

drivers/md/raid10.c:raid10_end_write_request() {
	bool ignore_error = !raid1_should_handle_error(bio) ||
		(bio->bi_status && bio_op(bio) == REQ_OP_DISCARD);
	...
	if (bio->bi_status && !ignore_error) {
		...
	} else {
		...
	}

	one_write_done(r10_bio);
	if (dec_rdev)
		rdev_dec_pending(rdev, conf->mddev);
}

Because ignore_error is true, R1BIO_WriteError / R10BIO_WriteError is never set, so the request is not held for the superblock update (or failed on MD_BROKEN). In raid10_end_write_request(), taking the else branch also leaves dec_rdev = 1 and decrements rdev->nr_pending while leaving r10_bio->devs[slot].bio non-NULL with bi_status != 0. If another copy in the same r10_bio sets R10BIO_MadeGood and schedules handle_write_completed(), handle_write_completed() sees bio != NULL && bio->bi_status on the Faulty slot and calls rdev_dec_pending() a second time, underflowing rdev->nr_pending (or dereferencing NULL if rdev was already removed).

drivers/md/raid10.c static bool raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1401

Quoted 2 lines
drivers/md/raid10.c +0 -0
 		if (rrdev && (test_bit(Faulty, &rrdev->flags)))
 			rrdev = NULL;

[Severity: Medium]
Should a newly Faulty replacement device (rrdev) also hold the write while it is Blocked or after MD_BROKEN is set?

wait_blocked_dev() waits for blocked replacement devices just as it did before commit f2e7e269a752 ("md/raid10: pull the code that wait for blocked dev into one function"), and after raid10_spare_active() marks a finished replacement In_sync and marks the old rdev Faulty (without Blocked), the active mirror stays in conf->mirrors[d].replacement until raid10_remove_disk() moves it. If rrdev fails between wait_blocked_dev() and this loop (or after MD_BROKEN is set), setting rrdev = NULL skips it without holding the write, allowing the write to complete before the superblock records rrdev's failure. (Note that raid10_end_write_request() and handle_write_completed() also currently do not set R10BIO_WriteError or hold writes when a repl_bio fails.)

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