md/raid1,raid10: fix write-path reference leaks and clean up error handling
v2 cover md/raid1,raid10: fix write-path reference leaks and clean up error handling
Hi,
This series fixes several write-path failure handling issues in raid1 and raid10 and then follows up with a cleanup of raid1_write_request().
The first two patches fix writes_pending leaks caused by failure paths that complete bios without reaching the normal write completion path. The raid1 fix also addresses a barrier reference leak when wait_blocked_rdev() fails after wait_barrier() succeeds.
The third patch fixes additional writes_pending and barrier reference leaks in raid10 discard handling.
The final patch simplifies raid1_write_request() error handling.
Patches:
md/raid1: fix writes_pending and barrier reference leaks on write failures md/raid10: fix writes_pending leak on write request failures md/raid10: fix writes_pending and barrier reference leaks on discard failures md/raid1: simplify raid1_write_request() error handling
Changes in v2:
- fix writes_pending leaks in addition to the barrier reference leaks
- add raid10 fixes for analogous write and discard failure paths
- add a follow-up cleanup patch to simplify raid1_write_request()
- simplifies raid1_write_request() error handling.
- Link v1: https://lore.kernel.org/linux-raid/[email protected]/
Thanks,
Abd-Alrhman
Abd-Alrhman Masalkhi (4):
md/raid1: fix writes_pending and barrier reference leaks on write
failures
md/raid10: fix writes_pending leak on write request failures md/raid10: fix writes_pending and barrier reference leaks on discard
failures
md/raid1: simplify raid1_write_request() error handling
drivers/md/raid1.c | 74 ++++++++++++++++++++++++--------------------- drivers/md/raid10.c | 28 ++++++++++++----- 2 files changed, 60 insertions(+), 42 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/4 md/raid1: fix writes_pending and barrier reference leaks on write failures
raid1_make_request() acquires a writes_pending reference with md_write_start() before calling raid1_write_request(). Several failure paths in raid1_write_request() complete the bio and return without reaching the normal write completion path, causing the corresponding md_write_end() to be skipped.
Make raid1_write_request() return a status indicating whether the write request was successfully queued. This allows raid1_make_request() to call md_write_end() when raid1_write_request() fails.
Additionally, if wait_blocked_rdev() fails after wait_barrier() succeeds, the associated barrier reference is not released.
Call allow_barrier() before returning from that path to keep the barrier accounting balanced.
---
Changes in v2:
- fix writes_pending leaks in addition to the barrier reference leak.
- make raid1_write_request() return whether the write was successfully
queued so raid1_make_request() can release writes_pending references
on failure paths.
- Link v1: https://lore.kernel.org/linux-raid/[email protected]/
--- drivers/md/raid1.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/md/raid1.c +12 -7
@@ -1501,7 +1501,7 @@ static void raid1_start_write_behind(struct mddev *mddev, struct r1bio *r1_bio, } -static void raid1_write_request (struct mddev *mddev,struct bio *bio, +static bool raid1_write_request (struct mddev *mddev,struct bio *bio,int max_write_sectors) {struct r1conf *conf = mddev->private;
@@ -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,
@@ -1519,7 +1520,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,if (bio->bi_opf &REQ_NOWAIT ) {bio_wouldblock_error (bio); -return ; +return false; }wait_event_idle (conf->wait_barrier, !mddev->cluster_ops->area_resyncing(mddev, WRITE,
@@ -1535,12 +1536,13 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,if (!wait_barrier (conf, bio->bi_iter.bi_sector, bio->bi_opf &REQ_NOWAIT )) {bio_wouldblock_error (bio); -return ; +return false; }if (!wait_blocked_rdev (mddev, bio)) {bio_wouldblock_error (bio); -return ; +allow_barrier (conf, sector); +return false; } r1_bio =alloc_r1bio (mddev, bio);
@@ -1699,7 +1701,8 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,/* In case raid1d snuck in to freeze_array */ wake_up_barrier (conf); -return ; +return true; + err_handle:for (k =0 ; k < i; k++) {if (r1_bio->bios[k]) {
@@ -1709,6 +1712,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio, }raid_end_bio_io (r1_bio); +return false; }static bool raid1_make_request (struct mddev *mddev,struct bio *bio)
@@ -1732,8 +1736,9 @@ static bool raid1_make_request(struct mddev *mddev, struct bio *bio)if (bio_data_dir (bio) ==READ )raid1_read_request (mddev, bio, sectors,NULL );else { -md_write_start (mddev,bio); -raid1_write_request (mddev, bio, sectors); +md_write_start (mddev, bio); +if (!raid1_write_request (mddev, bio, sectors)) +md_write_end (mddev); }return true; }
Signature · 1 line
2.43.0
PATCH v2 2/4 md/raid10: fix writes_pending leak on write request failures
In reply to Abd-Alrhman Masalkhiraid10_make_request() acquires a writes_pending reference with md_write_start() before dispatching write requests. Several failure paths in raid10_write_request() complete the bio and return without reaching the normal write completion path, causing the corresponding md_write_end() to be skipped.
Make raid10_write_request() return a status indicating whether the write request was successfully queued. This allows raid10_make_request() to release the writes_pending reference with md_write_end() when a write request fails.
---
Changes in v2:
- new patch.
--- drivers/md/raid10.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/md/raid10.c +15 -8
@@ -1349,7 +1349,7 @@ static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio) } } -static void raid10_write_request (struct mddev *mddev,struct bio *bio, +static bool raid10_write_request (struct mddev *mddev,struct bio *bio,struct r10bio *r10_bio) {struct r10conf *conf = mddev->private;
@@ -1365,7 +1365,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,/* Bail out if REQ_NOWAIT is set for the bio */ if (bio->bi_opf &REQ_NOWAIT ) {bio_wouldblock_error (bio); -return ; +return false; }for (;;) { prepare_to_wait(&conf->wait_barrier,
@@ -1381,7 +1381,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio, sectors = r10_bio->sectors;if (!regular_request_wait (mddev, conf, bio, sectors)) {free_r10bio (r10_bio); -return ; +return false; } if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
@@ -1398,7 +1398,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,if (bio->bi_opf &REQ_NOWAIT ) {allow_barrier (conf);bio_wouldblock_error (bio); -return ; +return false; }mddev_add_trace_msg (conf->mddev,"raid10 wait reshape metadata" );
@@ -1514,7 +1514,8 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,raid10_write_one_disk (mddev, r10_bio, bio, true, i); }one_write_done (r10_bio); -return ; +return true; + err_handle:for (k =0 ; k < i; k++) {int d = r10_bio->devs[k].devnum;
@@ -1532,10 +1533,12 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio, }raid_end_bio_io (r10_bio); +return false; } -static void __make_request (struct mddev *mddev,struct bio *bio,int sectors) +static bool __make_request (struct mddev *mddev,struct bio *bio,int sectors) { +bool ret;struct r10conf *conf = mddev->private;struct r10bio *r10_bio;
@@ -1551,10 +1554,13 @@ static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)memset (r10_bio->devs,0 ,sizeof (r10_bio->devs[0 ]) * conf->geo.raid_disks); + ret = true;if (bio_data_dir (bio) ==READ )raid10_read_request (mddev, bio, r10_bio);else -raid10_write_request (mddev, bio, r10_bio); + ret =raid10_write_request (mddev, bio, r10_bio); + +return ret; }static void raid_end_discard_bio (struct r10bio *r10bio)
@@ -1900,7 +1906,8 @@ static bool raid10_make_request(struct mddev *mddev, struct bio *bio) sectors = chunk_sects - (bio->bi_iter.bi_sector & (chunk_sects -1 )); -__make_request (mddev, bio, sectors); +if (!__make_request (mddev, bio, sectors)) +md_write_end (mddev);/* In case raid10d snuck in to freeze_array */ wake_up_barrier (conf);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 2/4] md/raid10: fix writes_pending leak on write request failures
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The r10_bio structure allocated from the mempool is leaked when raid10_write_request() bails out early due to REQ_NOWAIT. --
commit 3940ccdb4c7c7e19c20f9b5e2e1d12545c2558b5
Author: Abd-Alrhman Masalkhi <[email protected]>
md/raid10: fix writes_pending leak on write request failures
raid10_make_request() acquires a writes_pending reference with md_write_start() before dispatching write requests. Several failure paths in raid10_write_request() complete the bio and return without reaching the normal write completion path, causing the corresponding md_write_end() to be skipped.
Quoted 4 lines
drivers/md/raid10.c +0
-0
[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1368
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1365,7 +1365,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,/* Bail out if REQ_NOWAIT is set for the bio */ if (bio->bi_opf &REQ_NOWAIT ) {bio_wouldblock_error (bio); -return ; +return false;
[Severity: High]
This isn't a bug introduced by this patch, but does this early return leak the r10_bio structure?
__make_request() allocates r10_bio using:
r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
It then passes it to raid10_write_request(). When the bio has REQ_NOWAIT set during a clustered area resync, we complete the bio with an error and return false without freeing the r10_bio. Could repeatedly triggering this exhaust the r10bio_pool, causing deadlocks on subsequent operations?
Note that the regular_request_wait() error path right below correctly calls free_r10bio(r10_bio) before returning.
Quoted 2 lines
drivers/md/raid10.c +0
-0}for (;;) {
[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1401
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1398,7 +1398,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,if (bio->bi_opf &REQ_NOWAIT ) {allow_barrier (conf);bio_wouldblock_error (bio); -return ; +return false;
[Severity: High]
This is a pre-existing issue, but does this path also leak the r10_bio mempool allocation when waiting for a reshape metadata update?
Similar to the clustered area resync case above, the r10_bio allocated in __make_request() does not appear to be freed before returning here.
Quoted 2 lines
drivers/md/raid10.c +0
-0} mddev_add_trace_msg(conf->mddev,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
yu kuai
maintainerRe: [PATCH v2 2/4] md/raid10: fix writes_pending leak on write request failures
Hi,
Quoted 5 lines · 在 2026/6/14 2:40, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Ther10_biostructure allocated from the mempool is leaked whenraid10_write_request()bails out early due toREQ_NOWAIT. --
This looks like a real issue, do you plan to fix this as well?
Quoted 56 lines
commit
3940ccdb4c7c7e19c20f9b5e2e1d12545c2558b5
Author: Abd-Alrhman Masalkhi <[email protected]>md/raid10: fix
writes_pendingleak on write request failures
raid10_make_request()acquires awrites_pendingreference withmd_write_start()before dispatching write requests. Several failure paths inraid10_write_request()complete the bio and return without reaching the normal write completion path, causing the correspondingmd_write_end()to be skipped.Quoted 4 lines
drivers/md/raid10.c +0
-0[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1368
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1365,7 +1365,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,/* Bail out if REQ_NOWAIT is set for the bio */ if (bio->bi_opf &REQ_NOWAIT ) {bio_wouldblock_error (bio); -return ; +return false;[Severity: High]
This isn't a bug introduced by this patch, but does this early return leak ther10_biostructure?
__make_request()allocatesr10_biousing:r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);It then passes it to
raid10_write_request(). When the bio hasREQ_NOWAITset during a clustered area resync, we complete the bio with an error and return false without freeing ther10_bio. Could repeatedly triggering this exhaust ther10bio_pool, causing deadlocks on subsequent operations?Note that the
regular_request_wait()error path right below correctly callsfree_r10bio(r10_bio)before returning.Quoted 2 lines
drivers/md/raid10.c +0
-0}for (;;) {[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1401
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1398,7 +1398,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,if (bio->bi_opf &REQ_NOWAIT ) {allow_barrier (conf);bio_wouldblock_error (bio); -return ; +return false;[Severity: High]
This is a pre-existing issue, but does this path also leak ther10_biomempool allocation when waiting for a reshape metadata update?Similar to the clustered area resync case above, the
r10_bioallocated in__make_request()does not appear to be freed before returning here.Quoted 2 lines
drivers/md/raid10.c +0
-0} mddev_add_trace_msg(conf->mddev,
Signature · 2 lines
Thanks, Kuai
Re: [PATCH v2 2/4] md/raid10: fix writes_pending leak on write request failures
Hi Kaui,
Quoted 9 lines · yu kuai
Hi,
Quoted 5 lines · 在 2026/6/14 2:40, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Ther10_biostructure allocated from the mempool is leaked whenraid10_write_request()bails out early due toREQ_NOWAIT. --This looks like a real issue, do you plan to fix this as well?
Yes, I plan to fix that issue as well. I'll include this fix along with the other patches.
Quoted 60 lines
Quoted 56 lines
commit
3940ccdb4c7c7e19c20f9b5e2e1d12545c2558b5
Author: Abd-Alrhman Masalkhi <[email protected]>md/raid10: fix
writes_pendingleak on write request failures
raid10_make_request()acquires awrites_pendingreference withmd_write_start()before dispatching write requests. Several failure paths inraid10_write_request()complete the bio and return without reaching the normal write completion path, causing the correspondingmd_write_end()to be skipped.Quoted 4 lines
drivers/md/raid10.c +0
-0[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1368
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1365,7 +1365,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,/* Bail out if REQ_NOWAIT is set for the bio */ if (bio->bi_opf &REQ_NOWAIT ) {bio_wouldblock_error (bio); -return ; +return false;[Severity: High]
This isn't a bug introduced by this patch, but does this early return leak ther10_biostructure?
__make_request()allocatesr10_biousing:r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);It then passes it to
raid10_write_request(). When the bio hasREQ_NOWAITset during a clustered area resync, we complete the bio with an error and return false without freeing ther10_bio. Could repeatedly triggering this exhaust ther10bio_pool, causing deadlocks on subsequent operations?Note that the
regular_request_wait()error path right below correctly callsfree_r10bio(r10_bio)before returning.Quoted 2 lines
drivers/md/raid10.c +0
-0}for (;;) {[ ... ]
drivers/md/raid10.c static void raid10_write_request(struct mddev *mddev, struct bio *bio, · line 1401
Quoted 6 lines
drivers/md/raid10.c +1
-1@@ -1398,7 +1398,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,if (bio->bi_opf &REQ_NOWAIT ) {allow_barrier (conf);bio_wouldblock_error (bio); -return ; +return false;[Severity: High]
This is a pre-existing issue, but does this path also leak ther10_biomempool allocation when waiting for a reshape metadata update?Similar to the clustered area resync case above, the
r10_bioallocated in__make_request()does not appear to be freed before returning here.Quoted 2 lines
drivers/md/raid10.c +0
-0} mddev_add_trace_msg(conf->mddev,Signature · 2 lines
Thanks, Kuai
Signature · 2 lines
Best Regards, Abd-Alrhman
PATCH v2 3/4 md/raid10: fix writes_pending and barrier reference leaks on discard failures
In reply to Abd-Alrhman Masalkhiraid10_make_request() acquires a writes_pending reference with md_write_start() before calling raid10_handle_discard(). Several failure paths in raid10_handle_discard() complete the bio and return without releasing the corresponding reference, causing md_write_end() to be skipped.
Call md_write_end() before returning from these failure paths to keep writes_pending accounting balanced.
Additionally, discard split allocation failures can occur after wait_barrier() succeeds. Those paths return without calling allow_barrier(), leaking the associated barrier reference.
Release the barrier before returning from those paths.
---
Changes in v2:
- new patch.
--- drivers/md/raid10.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/md/raid10.c +5 -0
@@ -1639,6 +1639,7 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)if (!wait_barrier (conf, bio->bi_opf &REQ_NOWAIT )) {bio_wouldblock_error (bio); +md_write_end (mddev);return 0 ; }
@@ -1681,6 +1682,8 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)if (IS_ERR (split)) { bio->bi_status =errno_to_blk_status (PTR_ERR (split));bio_endio (bio); +md_write_end (mddev); +allow_barrier (conf);return 0 ; }
@@ -1698,6 +1701,8 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)if (IS_ERR (split)) { bio->bi_status =errno_to_blk_status (PTR_ERR (split));bio_endio (bio); +md_write_end (mddev); +allow_barrier (conf);return 0 ; }
Signature · 1 line
2.43.0
PATCH v2 4/4 md/raid1: simplify raid1_write_request() error handling
In reply to Abd-Alrhman Masalkhiraid1_write_request() increments rdev->nr_pending before checking the badblocks and then immediately decrements it again when a device is skipped. Move the increment until after the checks succeed so the reference accounting is easier to follow.
Consolidate the failure paths so that each error label releases exactly the resources acquired up to that point. err_dec_pending drops pending references and frees the r1bio, while err_allow_barrier handles the barrier release before returning.
When a REQ_ATOMIC write cannot be satisfied due to a badblock range, complete the bio with BLK_STS_NOTSUPP rather than reporting an I/O error, since the operation is unsupported rather than having failed during I/O.
Rename max_write_sectors to max_sectors and remove the redundant local copy.
---
Changes in v2:
- new patch, depends on patch 1.
--- drivers/md/raid1.c | 59 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 29 deletions(-)
drivers/md/raid1.c +30 -29
@@ -1502,29 +1502,29 @@ static void raid1_start_write_behind(struct mddev *mddev, struct r1bio *r1_bio, }static bool raid1_write_request (struct mddev *mddev,struct bio *bio, -int max_write_sectors) +int max_sectors) {struct r1conf *conf = mddev->private;struct r1bio *r1_bio;int i, disks, k;unsigned long flags;int first_clone; -int max_sectors;bool write_behind = false; -bool is_discard = (bio_op (bio) ==REQ_OP_DISCARD ); +bool nowait = bio->bi_opf &REQ_NOWAIT ; +bool is_discard =op_is_discard (bio->bi_opf);sector_t sector = bio->bi_iter.bi_sector;if (mddev_is_clustered (mddev) && - mddev->cluster_ops->area_resyncing (mddev,WRITE , - bio->bi_iter.bi_sector,bio_end_sector (bio))) { + mddev->cluster_ops->area_resyncing (mddev,WRITE , sector, +bio_end_sector (bio))) { -if (bio->bi_opf &REQ_NOWAIT ) { +if (nowait) {bio_wouldblock_error (bio);return false; }wait_event_idle (conf->wait_barrier, !mddev->cluster_ops->area_resyncing (mddev,WRITE , - bio->bi_iter.bi_sector, + sector,bio_end_sector (bio))); }
@@ -1533,20 +1533,18 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,* thread has put up a bar for new requests. * Continue immediately if no resync is active currently. */ -if (!wait_barrier (conf, bio->bi_iter.bi_sector, - bio->bi_opf &REQ_NOWAIT )) { +if (!wait_barrier (conf, sector, nowait)) {bio_wouldblock_error (bio);return false; }if (!wait_blocked_rdev (mddev, bio)) {bio_wouldblock_error (bio); -allow_barrier (conf, sector); -return false; + goto err_allow_barrier; } r1_bio =alloc_r1bio (mddev, bio); - r1_bio->sectors = max_write_sectors; + r1_bio->sectors = max_sectors;/* first select target devices under rcu_lock and * inc refcount on their rdev. Record them by setting
@@ -1560,7 +1558,6 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,*/ disks = conf->raid_disks *2 ; - max_sectors = r1_bio->sectors;for (i =0 ; i < disks; i++) {struct md_rdev *rdev = conf->mirrors[i].rdev;
@@ -1576,23 +1573,21 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,if (!rdev ||test_bit (Faulty, &rdev->flags))continue ; -atomic_inc (&rdev->nr_pending);if (test_bit (WriteErrorSeen, &rdev->flags)) {sector_t first_bad;sector_t bad_sectors;int is_bad; - is_bad =is_badblock (rdev, r1_bio->sector, max_sectors, + is_bad =is_badblock (rdev, sector, max_sectors, &first_bad, &bad_sectors); -if (is_bad && first_bad <= r1_bio->sector) { +if (is_bad && first_bad <= sector) {/* Cannot write here at all */ - bad_sectors -= (r1_bio->sector - first_bad); + bad_sectors -= (sector - first_bad);if (bad_sectors < max_sectors)/* mustn't write more than bad_sectors * to other devices yet */ max_sectors = bad_sectors; -rdev_dec_pending (rdev, mddev);continue ; }if (is_bad) {
@@ -1606,15 +1601,18 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,* the benefit. */ if (bio->bi_opf &REQ_ATOMIC ) { -rdev_dec_pending (rdev, mddev); - goto err_handle; + bio->bi_status =BLK_STS_NOTSUPP ; +bio_endio (bio); + goto err_dec_pending; } - good_sectors = first_bad - r1_bio->sector; + good_sectors = first_bad - sector;if (good_sectors < max_sectors) max_sectors = good_sectors; } } + +atomic_inc (&rdev->nr_pending); r1_bio->bios[i] = bio; }
@@ -1630,10 +1628,8 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,if (max_sectors <bio_sectors (bio)) { bio =bio_submit_split_bioset (bio, max_sectors, &conf->bio_split); -if (!bio) { -set_bit (R1BIO_Returned, &r1_bio->state); - goto err_handle; - } +if (!bio) + goto err_dec_pending; r1_bio->master_bio = bio; r1_bio->sectors = max_sectors;
@@ -1677,7 +1673,7 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio, mbio->bi_opf &= ~REQ_NOWAIT ; r1_bio->bios[i] = mbio; - mbio->bi_iter.bi_sector = (r1_bio->sector + rdev->data_offset); + mbio->bi_iter.bi_sector = sector + rdev->data_offset; mbio->bi_end_io = raid1_end_write_request; if (test_bit(FailFast, &rdev->flags) && !test_bit(WriteMostly, &rdev->flags) &&
@@ -1686,7 +1682,7 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio, mbio->bi_private = r1_bio;atomic_inc (&r1_bio->remaining); -mddev_trace_remap (mddev, mbio, r1_bio->sector); +mddev_trace_remap (mddev, mbio, sector);/* flush_pending_writes() needs access to the rdev so...*/ mbio->bi_bdev = (void *)rdev;if (!raid1_add_bio_to_plug (mddev, mbio, raid1_unplug, disks)) {
@@ -1701,9 +1697,10 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,/* In case raid1d snuck in to freeze_array */ wake_up_barrier (conf); +return true; -err_handle: +err_dec_pending:for (k =0 ; k < i; k++) {if (r1_bio->bios[k]) {rdev_dec_pending (conf->mirrors[k].rdev, mddev);
@@ -1711,7 +1708,11 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio, } } -raid_end_bio_io (r1_bio); +free_r1bio (r1_bio); + +err_allow_barrier: +allow_barrier (conf, sector); +return false; }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 4/4] md/raid1: simplify raid1_write_request() error handling
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Critical] Moving atomic_inc(&rdev->nr_pending) down opens a race window with hot_remove_disk, leading to a NULL pointer dereference during error handling or a Use-After-Free in clustered configurations.
Pre-existing issues:
- [High] A REQ_ATOMIC write can be silently split if a bad block overlaps the start of the write, violating atomicity guarantees. --
commit c334f57d91677f32131f21e50bb32b83136e5a60
Author: Abd-Alrhman Masalkhi <[email protected]>
md/raid1: simplify raid1_write_request() error handling
This commit delays the rdev->nr_pending increment until after badblocks are checked to avoid incrementing and immediately decrementing on skipped devices. It also consolidates error handling paths and modifies REQ_ATOMIC failures to complete with BLK_STS_NOTSUPP instead of I/O errors.
Quoted 4 lines
drivers/md/raid1.c +0
-0
[ ... ]
Quoted 15 lines
drivers/md/raid1.c +2
-3@@ -1576,23 +1573,21 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,if (!rdev ||test_bit (Faulty, &rdev->flags))continue ; -atomic_inc (&rdev->nr_pending);if (test_bit (WriteErrorSeen, &rdev->flags)) {sector_t first_bad;sector_t bad_sectors;int is_bad; - is_bad =is_badblock (rdev, r1_bio->sector, max_sectors, + is_bad =is_badblock (rdev, sector, max_sectors, &first_bad, &bad_sectors); -if (is_bad && first_bad <= r1_bio->sector) { +if (is_bad && first_bad <= sector) {
drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1591
Quoted 10 lines
drivers/md/raid1.c +1
-2/* Cannot write here at all */ - bad_sectors -= (r1_bio->sector - first_bad); + bad_sectors -= (sector - first_bad);if (bad_sectors < max_sectors)/* mustn't write more than bad_sectors * to other devices yet */ max_sectors = bad_sectors; -rdev_dec_pending (rdev, mddev);continue ;
[Severity: High]
This is a pre-existing issue, but does this logic skip the rejection of REQ_ATOMIC bios if a bad block overlaps the start of the write?
When is_badblock() finds a bad block at or before the write sector, the code shrinks max_sectors to force a split at the boundary and then calls continue. Because of the continue, the REQ_ATOMIC bio skips the rejection logic below.
Could this silently split an atomic write into smaller pieces and submit them to the healthy mirrors via bio_submit_split_bioset(), violating block-level atomicity guarantees?
Quoted 2 lines
drivers/md/raid1.c +0
-0}if (is_bad) {
[ ... ]
Quoted 6 lines
drivers/md/raid1.c +3
-2if (bio->bi_opf &REQ_ATOMIC ) { -rdev_dec_pending (rdev, mddev); - goto err_handle; + bio->bi_status =BLK_STS_NOTSUPP ; +bio_endio (bio); + goto err_dec_pending;
drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1615
Quoted 10 lines
drivers/md/raid1.c +3
-1} - good_sectors = first_bad - r1_bio->sector; + good_sectors = first_bad - sector;if (good_sectors < max_sectors) max_sectors = good_sectors; } } + +atomic_inc (&rdev->nr_pending);
[Severity: Critical]
Does moving atomic_inc(&rdev->nr_pending) down open a race window with hot_remove_disk()?
Because nr_pending acts as an existence lock that prevents the device from being removed from the array configuration by raid1_remove_conf(), incrementing it after the is_badblock() and test_bit(WriteErrorSeen, ...) checks means md_check_recovery() could evaluate rdev_removeable() while nr_pending is 0 and call hot_remove_disk().
If raid1_remove_conf() unlinks the device by setting conf->mirrors[i].rdev = NULL, when this thread resumes and the I/O eventually fails, would the error handling path (handle_write_finished() calling narrow_write_error()) read conf->mirrors[i].rdev and dereference a NULL pointer?
Additionally, in clustered configurations, if the unlinked rdev is immediately freed by md_kick_rdev_from_array(), could this lead to a use-after-free directly in raid1_write_request() when evaluating test_bit(WriteErrorSeen, &rdev->flags) or is_badblock()?
Quoted 2 lines
drivers/md/raid1.c +0
-0r1_bio->bios[i] = bio; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 4/4] md/raid1: simplify raid1_write_request() error handling
Hi Kuai,
Quoted 5 lines · On Sat, Jun 13, 2026 at 18:47 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Critical] Movingatomic_inc(&rdev->nr_pending)down opens a race window withhot_remove_disk, leading to a NULL pointer dereference during error handling or a Use-After-Free in clustered configurations.
I don't think this can happen. the array must be suspended before any disk can be removed.
Quoted 2 lines
Pre-existing issues:
- [High] AREQ_ATOMICwrite can be silently split if a bad block overlaps the start of the write, violating atomicity guarantees.
I don't think this is possible. bio_submit_split_bioset() will fail rather than split the bio.
Quoted 56 lines
--
commit
c334f57d91677f32131f21e50bb32b83136e5a60
Author: Abd-Alrhman Masalkhi <[email protected]>md/raid1: simplify
raid1_write_request()error handlingThis commit delays the
rdev->nr_pendingincrement until after badblocks are checked to avoid incrementing and immediately decrementing on skipped devices. It also consolidates error handling paths and modifiesREQ_ATOMICfailures to complete withBLK_STS_NOTSUPPinstead of I/O errors.Quoted 4 lines
drivers/md/raid1.c +0
-0[ ... ]
Quoted 15 lines
drivers/md/raid1.c +2
-3@@ -1576,23 +1573,21 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,if (!rdev ||test_bit (Faulty, &rdev->flags))continue ; -atomic_inc (&rdev->nr_pending);if (test_bit (WriteErrorSeen, &rdev->flags)) {sector_t first_bad;sector_t bad_sectors;int is_bad; - is_bad =is_badblock (rdev, r1_bio->sector, max_sectors, + is_bad =is_badblock (rdev, sector, max_sectors, &first_bad, &bad_sectors); -if (is_bad && first_bad <= r1_bio->sector) { +if (is_bad && first_bad <= sector) {drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1591
Quoted 10 lines
drivers/md/raid1.c +1
-2/* Cannot write here at all */ - bad_sectors -= (r1_bio->sector - first_bad); + bad_sectors -= (sector - first_bad);if (bad_sectors < max_sectors)/* mustn't write more than bad_sectors * to other devices yet */ max_sectors = bad_sectors; -rdev_dec_pending (rdev, mddev);continue ;[Severity: High]
This is a pre-existing issue, but does this logic skip the rejection ofREQ_ATOMICbios if a bad block overlaps the start of the write?When
is_badblock()finds a bad block at or before the write sector, the code shrinksmax_sectorsto force a split at the boundary and then calls continue. Because of the continue, theREQ_ATOMICbio skips the rejection logic below.Could this silently split an atomic write into smaller pieces and submit them to the healthy mirrors via
bio_submit_split_bioset(), violating block-level atomicity guarantees?
Quoted 8 lines
Quoted 2 lines
drivers/md/raid1.c +0
-0}if (is_bad) {[ ... ]
drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1605
Quoted 5 lines
drivers/md/raid1.c +2
-2if (bio->bi_opf &REQ_ATOMIC ) { -rdev_dec_pending (rdev, mddev); - goto err_handle; + bio->bi_status =BLK_STS_NOTSUPP ; +bio_endio (bio);
I misunderstood the comment. it should call bio_io_error(bio) instead. I'll fix this in v3.
Quoted 38 lines
drivers/md/raid1.c static bool raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1615
Quoted 11 lines
drivers/md/raid1.c +4
-1+ goto err_dec_pending; } - good_sectors = first_bad - r1_bio->sector; + good_sectors = first_bad - sector;if (good_sectors < max_sectors) max_sectors = good_sectors; } } + +atomic_inc (&rdev->nr_pending);[Severity: Critical]
Does movingatomic_inc(&rdev->nr_pending)down open a race window withhot_remove_disk()?Because
nr_pendingacts as an existence lock that prevents the device from being removed from the array configuration byraid1_remove_conf(), incrementing it after theis_badblock()andtest_bit(WriteErrorSeen, ...)checks meansmd_check_recovery()could evaluaterdev_removeable()whilenr_pendingis 0 and callhot_remove_disk().If
raid1_remove_conf()unlinks the device by setting conf->mirrors[i].rdev = NULL, when this thread resumes and the I/O eventually fails, would the error handling path (handle_write_finished()callingnarrow_write_error()) read conf->mirrors[i].rdev and dereference a NULL pointer?Additionally, in clustered configurations, if the unlinked rdev is immediately freed by
md_kick_rdev_from_array(), could this lead to a use-after-free directly inraid1_write_request()when evaluatingtest_bit(WriteErrorSeen, &rdev->flags)oris_badblock()?Quoted 2 lines
drivers/md/raid1.c +0
-0r1_bio->bios[i] = bio; }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Signature · 2 lines
Best Regards, Abd-Alrhman
Quoted 39 lines · Abd-Alrhman Masalkhi
Hi,
This series fixes several write-path failure handling issues in raid1 and raid10 and then follows up with a cleanup of
raid1_write_request().The first two patches fix
writes_pendingleaks caused by failure paths that complete bios without reaching the normal write completion path. The raid1 fix also addresses a barrier reference leak whenwait_blocked_rdev()fails afterwait_barrier()succeeds.The third patch fixes additional
writes_pendingand barrier reference leaks in raid10 discard handling.The final patch simplifies
raid1_write_request()error handling.Patches:
md/raid1: fixwrites_pendingand barrier reference leaks on write failures md/raid10: fixwrites_pendingleak on write request failures md/raid10: fixwrites_pendingand barrier reference leaks on discard failures md/raid1: simplifyraid1_write_request()error handlingChanges in v2:
- fixwrites_pendingleaks in addition to the barrier reference leaks
- add raid10 fixes for analogous write and discard failure paths
- add a follow-up cleanup patch to simplifyraid1_write_request()
- simplifiesraid1_write_request()error handling.
- Link v1: https://lore.kernel.org/linux-raid/[email protected]/Thanks,
Abd-AlrhmanAbd-Alrhman Masalkhi (4):
md/raid1: fixwrites_pendingand barrier reference leaks on write
failures
md/raid10: fixwrites_pendingleak on write request failures md/raid10: fixwrites_pendingand barrier reference leaks on discard
failures
md/raid1: simplifyraid1_write_request()error handling
Quoted 3 lines
drivers/md/raid1.c | 74 ++++++++++++++++++++++++--------------------- drivers/md/raid10.c | 28 ++++++++++++----- 2 files changed, 60 insertions(+), 42 deletions(-)
Applied to md-7.2
Signature · 2 lines
Thanks, Kuai