Skip to contentSkip to conversation

md/raid1: release barrier when REQ_NOWAIT write would block

PATCH In review Abd-Alrhman Masalkhi 2 participants 3 messages Jun 11 – Jun 12 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md/raid1: release barrier when REQ_NOWAIT write would block

raid1_write_request() calls wait_barrier(), which raises conf->nr_pending, before calling wait_blocked_rdev(). When wait_blocked_rdev() fails for a REQ_NOWAIT write, the bio is completed and the function returns without calling allow_barrier(), leaking the nr_pending reference taken by wait_barrier().

freeze_array() waits for nr_pending to drop to zero, so a leaked reference prevents the array from ever being frozen again. Any later operation that freezes the array then hangs, including resync, reshape, device removal and array teardown.

Release the barrier on the REQ_NOWAIT failure path.

Fixes 88ed59c4cc6c ("md/raid1: factor out helper to handle blocked rdev from raid1_write_request()")
---
 drivers/md/raid1.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/md/raid1.c +2 -0
index 6263a1d45f86..e9086ba1f796 100644
@@ -1522,6 +1522,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 	int max_sectors;
 	bool write_behind = false;
 	bool is_discard = (bio_op(bio) == REQ_OP_DISCARD);
+	sector_t sector = bio->bi_iter.bi_sector;
 
 	if (mddev_is_clustered(mddev) &&
 	    mddev->cluster_ops->area_resyncing(mddev, WRITE,
@@ -1550,6 +1551,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 
 	if (!wait_blocked_rdev(mddev, bio)) {
 		bio_wouldblock_error(bio);
+		allow_barrier(conf, sector);
 		return;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing RCU read lock around conf->mirrors[i].rdev access in wait_blocked_rdev().
- [High] Early failure paths in raid1_write_request() leak the mddev->writes_pending reference.
- [High] Allocating bios and r1bios can sleep even when the REQ_NOWAIT flag is set. --

commit 3db9da0ec804800aa3afb93361ccd18de39ce5f0
Author: Abd-Alrhman Masalkhi <[email protected]>

md/raid1: release barrier when REQ_NOWAIT write would block

raid1_write_request() leaks the barrier reference taken by wait_barrier() if wait_blocked_rdev() fails for a REQ_NOWAIT write. This patch releases the barrier on this failure path to prevent array freeze hangs.

Quoted 13 lines
drivers/md/raid1.c +1 -0
index 5b9368bd9e700..f3cc30932ba2b 100644
@@ -1512,6 +1512,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 	int max_sectors;
 	bool write_behind = false;
 	bool is_discard = (bio_op(bio) == REQ_OP_DISCARD);
+	sector_t sector = bio->bi_iter.bi_sector;
 
 	if (mddev_is_clustered(mddev) &&
 	    mddev->cluster_ops->area_resyncing(mddev, WRITE,

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

Quoted 4 lines
drivers/md/raid1.c +1 -0
@@ -1540,6 +1541,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 
 	if (!wait_blocked_rdev(mddev, bio)) {
 		bio_wouldblock_error(bio);
+		allow_barrier(conf, sector);

[Severity: High]
This is a pre-existing issue, but does this early return path also leak the mddev->writes_pending reference?

In raid1_make_request(), md_write_start() unconditionally takes a per-cpu reference on mddev->writes_pending before calling raid1_write_request(). Normally, this is released via md_write_end() inside close_write().

By directly returning here after bio_wouldblock_error(bio), we never call md_write_end(). Does this permanently leak the writes_pending counter and prevent the array from transitioning to a clean state?

It looks like this same leak exists on the clustered area_resyncing and wait_barrier REQ_NOWAIT failure paths earlier in this function, as well as the err_handle path if bio_submit_split_bioset() fails.

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

Quoted 2 lines
drivers/md/raid1.c +0 -0
 		return;
 	}

[Severity: Critical]
This is a pre-existing issue, but is it safe to call wait_blocked_rdev() here without holding the RCU read lock?

Looking at wait_blocked_rdev():

	for (i = 0; i < disks; i++) {
		struct md_rdev *rdev = conf->mirrors[i].rdev;

		if (!rdev)
			continue;

		/* don't write here until the bad block is acknowledged */
		if (test_bit(WriteErrorSeen, &rdev->flags) &&

It accesses the rdev pointer and its fields without rcu_read_lock(). Could a concurrent device removal (via md_ioctl -> raid1_remove_conf) replace the pointer with NULL and free the rdev structure while we are dereferencing it here, resulting in a use-after-free?

[Severity: High]
This is also a pre-existing issue, but if we proceed past this point for a REQ_NOWAIT bio, we call alloc_r1bio() and eventually bio_alloc_clone(), both of which use GFP_NOIO.

Since memory pool allocations with GFP_NOIO can block when the mempool is exhausted under memory pressure, could this cause REQ_NOWAIT writes to sleep, defeating the purpose of the REQ_NOWAIT flag?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 4 lines · On Thu, Jun 11, 2026 at 13:41 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing RCU read lock around conf->mirrors[i].rdev access in wait_blocked_rdev().

I'm not quite sure whether this is a real bug.

Quoted 1 line

- [High] Early failure paths in raid1_write_request() leak the mddev->writes_pending reference.

I'll submit a v2 containing this fix, along with the fix for the mddev->writes_pending

Quoted 1 line

- [High] Allocating bios and r1bios can sleep even when the REQ_NOWAIT flag is set.

@kuai, Should I handle this issue as well, or should I just ignore it for now?

Quoted 81 lines

--

commit 3db9da0ec804800aa3afb93361ccd18de39ce5f0
Author: Abd-Alrhman Masalkhi <[email protected]>

md/raid1: release barrier when REQ_NOWAIT write would block

raid1_write_request() leaks the barrier reference taken by wait_barrier() if wait_blocked_rdev() fails for a REQ_NOWAIT write. This patch releases the barrier on this failure path to prevent array freeze hangs.

Quoted 13 lines
drivers/md/raid1.c +1 -0
index 5b9368bd9e700..f3cc30932ba2b 100644
@@ -1512,6 +1512,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 	int max_sectors;
 	bool write_behind = false;
 	bool is_discard = (bio_op(bio) == REQ_OP_DISCARD);
+	sector_t sector = bio->bi_iter.bi_sector;
 
 	if (mddev_is_clustered(mddev) &&
 	    mddev->cluster_ops->area_resyncing(mddev, WRITE,

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

Quoted 4 lines
drivers/md/raid1.c +1 -0
@@ -1540,6 +1541,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 
 	if (!wait_blocked_rdev(mddev, bio)) {
 		bio_wouldblock_error(bio);
+		allow_barrier(conf, sector);

[Severity: High]
This is a pre-existing issue, but does this early return path also leak the mddev->writes_pending reference?

In raid1_make_request(), md_write_start() unconditionally takes a per-cpu reference on mddev->writes_pending before calling raid1_write_request(). Normally, this is released via md_write_end() inside close_write().

By directly returning here after bio_wouldblock_error(bio), we never call md_write_end(). Does this permanently leak the writes_pending counter and prevent the array from transitioning to a clean state?

It looks like this same leak exists on the clustered area_resyncing and wait_barrier REQ_NOWAIT failure paths earlier in this function, as well as the err_handle path if bio_submit_split_bioset() fails.

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

Quoted 2 lines
drivers/md/raid1.c +0 -0
 		return;
 	}

[Severity: Critical]
This is a pre-existing issue, but is it safe to call wait_blocked_rdev() here without holding the RCU read lock?

Looking at wait_blocked_rdev():

	for (i = 0; i < disks; i++) {
		struct md_rdev *rdev = conf->mirrors[i].rdev;

		if (!rdev)
			continue;

		/* don't write here until the bad block is acknowledged */
		if (test_bit(WriteErrorSeen, &rdev->flags) &&

It accesses the rdev pointer and its fields without rcu_read_lock(). Could a concurrent device removal (via md_ioctl -> raid1_remove_conf) replace the pointer with NULL and free the rdev structure while we are dereferencing it here, resulting in a use-after-free?

[Severity: High]
This is also a pre-existing issue, but if we proceed past this point for a REQ_NOWAIT bio, we call alloc_r1bio() and eventually bio_alloc_clone(), both of which use GFP_NOIO.

Since memory pool allocations with GFP_NOIO can block when the mempool is exhausted under memory pressure, could this cause REQ_NOWAIT writes to sleep, defeating the purpose of the REQ_NOWAIT flag?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Signature · 2 lines
Best Regards,
Abd-Alrhman