raid1: fix nr_pending leak in REQ_ATOMIC bad-block error path
PATCH 1/1 raid1: fix nr_pending leak in REQ_ATOMIC bad-block error path
In raid1_write_request(), each per-mirror loop iteration begins by incrementing rdev->nr_pending. If a REQ_ATOMIC write encounters a badblock within the requested range, the code jumps to err_handle without dropping the reference taken for the current mirror.
err_handle's cleanup loop will only decrements for k < i and r1_bio->bios[k] is non-NULL. The current slot is therefore skipped, leaving its nr_pending reference leaked permanently. The reference prevents the rdev from ever being removed, since raid1_remove_conf() refuses to remove an rdev with nr_pending > 0.
Fix this by calling rdev_dec_pending() before jumping to err_handle.
--- drivers/md/raid1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/raid1.c +3 -1
@@ -1580,8 +1580,10 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,* complexity of supporting that is not worth * the benefit. */ -if (bio->bi_opf &REQ_ATOMIC ) +if (bio->bi_opf &REQ_ATOMIC ) { +rdev_dec_pending (rdev, mddev); goto err_handle; + } good_sectors = first_bad - r1_bio->sector;if (good_sectors < max_sectors)
Signature · 1 line
2.43.0
Quoted 11 lines · Abd-Alrhman Masalkhi
In
raid1_write_request(), each per-mirror loop iteration begins by incrementingrdev->nr_pending. If aREQ_ATOMICwrite encounters a badblock within the requested range, the code jumps toerr_handlewithout dropping the reference taken for the current mirror.err_handle's cleanup loop will only decrements for k < i and r1_bio->bios[k] is non-NULL. The current slot is therefore skipped, leaving its
nr_pendingreference leaked permanently. The reference prevents the rdev from ever being removed, sinceraid1_remove_conf()refuses to remove an rdev withnr_pending> 0.
Quoted 7 lines
Fix this by calling
rdev_dec_pending()before jumping toerr_handle.Fixes f2a38abf5f1c ("md/raid1: Atomic write support")Signed-off-by Abd-Alrhman Masalkhi<[email protected]>--- drivers/md/raid1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Applied to md-7.2
Signature · 2 lines
Thansk, Kuai
Quoted 11 lines · Abd-Alrhman Masalkhi
In
raid1_write_request(), each per-mirror loop iteration begins by incrementingrdev->nr_pending. If aREQ_ATOMICwrite encounters a badblock within the requested range, the code jumps toerr_handlewithout dropping the reference taken for the current mirror.err_handle's cleanup loop will only decrements for k < i and r1_bio->bios[k] is non-NULL. The current slot is therefore skipped, leaving its
nr_pendingreference leaked permanently. The reference prevents the rdev from ever being removed, sinceraid1_remove_conf()refuses to remove an rdev withnr_pending> 0.
Quoted 4 lines
Fix this by calling
rdev_dec_pending()before jumping toerr_handle.Fixes f2a38abf5f1c ("md/raid1: Atomic write support")Signed-off-by Abd-Alrhman Masalkhi <[email protected]>
FWIW,
Quoted 9 lines
--- drivers/md/raid1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/md/raid1.c +0
-0
drivers/md/raid1.c static void raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1584
Quoted 6 lines
drivers/md/raid1.c +2
-1@@ -1580,8 +1580,10 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,* complexity of supporting that is not worth * the benefit. */ -if (bio->bi_opf &REQ_ATOMIC ) +if (bio->bi_opf &REQ_ATOMIC ) { +rdev_dec_pending (rdev, mddev);
It's not so nice that we have 2x locations that does the rdev_dec_pending work
Quoted 5 lines
drivers/md/raid1.c +1
-0goto err_handle; + } good_sectors = first_bad - r1_bio->sector;if (good_sectors < max_sectors)
hi,
Thank you for the feedback.
Quoted 25 lines · John Garry
Quoted 11 lines · Abd-Alrhman Masalkhi
In
raid1_write_request(), each per-mirror loop iteration begins by incrementingrdev->nr_pending. If aREQ_ATOMICwrite encounters a badblock within the requested range, the code jumps toerr_handlewithout dropping the reference taken for the current mirror.err_handle's cleanup loop will only decrements for k < i and r1_bio->bios[k] is non-NULL. The current slot is therefore skipped, leaving its
nr_pendingreference leaked permanently. The reference prevents the rdev from ever being removed, sinceraid1_remove_conf()refuses to remove an rdev withnr_pending> 0.Quoted 4 lines
Fix this by calling
rdev_dec_pending()before jumping toerr_handle.Fixes f2a38abf5f1c ("md/raid1: Atomic write support")Signed-off-by Abd-Alrhman Masalkhi <[email protected]>FWIW,
Reviewed-by John Garry <[email protected]>Quoted 4 lines
--- drivers/md/raid1.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines
drivers/md/raid1.c static void raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1584
Quoted 11 lines
drivers/md/raid1.c +2
-1@@ -1580,8 +1580,10 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,* complexity of supporting that is not worth * the benefit. */ -if (bio->bi_opf &REQ_ATOMIC ) +if (bio->bi_opf &REQ_ATOMIC ) { +rdev_dec_pending (rdev, mddev);It's not so nice that we have 2x locations that does the
rdev_dec_pendingwork
Are you suggesting deferring atomic_inc(&rdev->nr_pending) until after the if (test_bit(WriteErrorSeen, &rdev->flags)) {..} block? The patch is already in md-7.2; should I send a separate cleanup patch?
Quoted 6 lines
Quoted 5 lines
drivers/md/raid1.c +1
-0goto err_handle; + } good_sectors = first_bad - r1_bio->sector;if (good_sectors < max_sectors)
Signature · 2 lines
Best Regards, Abd-Alrhman
Quoted 11 lines · Abd-Alrhman Masalkhi
Quoted 11 lines
drivers/md/raid1.c static void raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1584
Quoted 8 lines
+++
b/drivers/md/raid1.cdrivers/md/raid1.c +2
-1@@ -1580,8 +1580,10 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,* complexity of supporting that is not worth * the benefit. */ -if (bio->bi_opf &REQ_ATOMIC ) +if (bio->bi_opf &REQ_ATOMIC ) { +rdev_dec_pending (rdev, mddev);It's not so nice that we have 2x locations that does the
rdev_dec_pendingwork
Quoted 3 lines
Are you suggesting deferring
atomic_inc(&rdev->nr_pending)until after the if (test_bit(WriteErrorSeen, &rdev->flags)) {..} block? The patch is already in md-7.2; should I send a separate cleanup patch?
I'm not suggesting any further change. I am just mentioning that it is unfortunate that we have 2x locations which does the decrement, which makes error handling harder to follow.
hi,
Quoted 12 lines · John Garry
Quoted 11 lines · Abd-Alrhman Masalkhi
Quoted 11 lines
drivers/md/raid1.c static void raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1584
Quoted 8 lines
+++
b/drivers/md/raid1.cdrivers/md/raid1.c +2
-1@@ -1580,8 +1580,10 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,* complexity of supporting that is not worth * the benefit. */ -if (bio->bi_opf &REQ_ATOMIC ) +if (bio->bi_opf &REQ_ATOMIC ) { +rdev_dec_pending (rdev, mddev);It's not so nice that we have 2x locations that does the
rdev_dec_pendingwork
Quoted 7 lines
Quoted 3 lines
Are you suggesting deferring
atomic_inc(&rdev->nr_pending)until after the if (test_bit(WriteErrorSeen, &rdev->flags)) {..} block? The patch is already in md-7.2; should I send a separate cleanup patch?I'm not suggesting any further change. I am just mentioning that it is unfortunate that we have 2x locations which does the decrement, which makes error handling harder to follow.
You are absolutely right. Having two decrement paths makes the error handling harder to follow. Thanks for pointing that out.
Signature · 2 lines
Best Regards, Abd-Alrhman