Skip to contentSkip to conversation

raid1: fix nr_pending leak in REQ_ATOMIC bad-block error path

PATCH Applied Abd-Alrhman Masalkhi 3 participants 6 messages May 30 – Jun 1 MD RAID

lore
Share & tools
lore
#0 lore raw

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.

Fixes f2a38abf5f1c ("md/raid1: Atomic write support")
---
 drivers/md/raid1.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/raid1.c +3 -1
index 181400e147c0..0084bbc24076 100644
@@ -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
maintainer Applied
#1 lore raw
Quoted 11 lines · Abd-Alrhman Masalkhi

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.

Quoted 7 lines

Fix this by calling rdev_dec_pending() before jumping to err_handle.

Fixes f2a38abf5f1c ("md/raid1: Atomic write support")
---
  drivers/md/raid1.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Applied to md-7.2

Signature · 2 lines
Thansk,
Kuai
Reviewed-by on v1 1/1
#2 lore raw
In reply to Abd-Alrhman Masalkhi
Quoted 11 lines · Abd-Alrhman Masalkhi

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.

Quoted 4 lines

Fix this by calling rdev_dec_pending() before jumping to err_handle.

Fixes f2a38abf5f1c ("md/raid1: Atomic write support")

FWIW,

Quoted 9 lines
---
  drivers/md/raid1.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/raid1.c +0 -0
index 181400e147c0..0084bbc24076 100644

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 -0
  					goto err_handle;
+				}
 
  				good_sectors = first_bad - r1_bio->sector;
  				if (good_sectors < max_sectors)
#3 lore raw

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 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.

Quoted 4 lines

Fix this by calling rdev_dec_pending() before jumping to err_handle.

Fixes f2a38abf5f1c ("md/raid1: Atomic write support")

FWIW,

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
index 181400e147c0..0084bbc24076 100644
@@ -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

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 -0
  					goto err_handle;
+				}
 
  				good_sectors = first_bad - r1_bio->sector;
  				if (good_sectors < max_sectors)
Signature · 2 lines
Best Regards,
Abd-Alrhman
#4 lore raw
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.c

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 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.

#5 lore raw

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.c

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 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