Skip to contentSkip to conversation

bio_split() error handling rework

PATCH Superseded John Garry 4 participants 20 messages Oct 31, 2024 – Nov 7, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

John Garry

author
#0 lore raw

v3 cover bio_split() error handling rework

bio_split() error handling could be improved as follows:
- Instead of returning NULL for an error - which is vague - return a PTR_ERR, which may hint what went wrong.
- Remove BUG_ON() calls - which are generally not preferred - and instead WARN and pass an error code back to the caller. Many callers of bio_split() don't check the return code. As such, for an error we would be getting a crash still from an invalid pointer dereference.

Most bio_split() callers don't check the return value. However, it could be argued the bio_split() calls should not fail. So far I have just fixed up the md RAID code to handle these errors, as that is my interest now.

The motivator for this series was initial md RAID atomic write support in
https://lore.kernel.org/linux-block/[email protected]/T/#m5859ee900de8e6554d5bb027c0558f0147c32df8

There I wanted to ensure that we don't split an atomic write bio, and it made more sense to handle this in bio_split() (instead of the bio_split() caller).

Based on 133008e84b99 (block/for-6.13/block) blk-integrity: remove seed for user mapped buffers

Changes since v2:
- Drop "block: Use BLK_STS_OK in bio_init()" change (Christoph)
- Use proper rdev indexing in raid10_write_request() (Kuai)
- Decrement rdev nr_pending in raid1 read error path (Kuai)
- Add RB tags from Christoph, Johannes, and Kuai (thanks!)

Changes since RFC:
- proper handling to end the raid bio in all cases, and also pass back proper error code (Kuai)
- Add WARN_ON_ERROR in bio_split() (Johannes, Christoph)
- Add small patch to use BLK_STS_OK in bio_init()
- Change bio_submit_split() error path (Christoph)

John Garry (6):
block: Rework bio_split() return value
block: Error an attempt to split an atomic write in bio_split()
block: Handle bio_split() errors in bio_submit_split() md/raid0: Handle bio_split() errors
md/raid1: Handle bio_split() errors
md/raid10: Handle bio_split() errors

 block/bio.c                 | 14 +++++++----
 block/blk-crypto-fallback.c |  2 +-
 block/blk-merge.c           | 15 ++++++++----
 drivers/md/raid0.c          | 12 ++++++++++
 drivers/md/raid1.c          | 33 ++++++++++++++++++++++++--
 drivers/md/raid10.c         | 47 ++++++++++++++++++++++++++++++++++++-
 6 files changed, 110 insertions(+), 13 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v3 1/6 block: Rework bio_split() return value

Instead of returning an inconclusive value of NULL for an error in calling bio_split(), return a ERR_PTR() always.

Also remove the BUG_ON() calls, and WARN_ON_ONCE() instead. Indeed, since almost all callers don't check the return code from bio_split(), we'll crash anyway (for those failures).

Fix up the only user which checks bio_split() return code today (directly or indirectly), blk_crypto_fallback_split_bio_if_needed(). The md/bcache code does check the return code in cached_dev_cache_miss() -> bio_next_split() -> bio_split(), but only to see if there was a split, so there would be no change in behaviour here (when returning a ERR_PTR()).

---
 block/bio.c                 | 10 ++++++----
 block/blk-crypto-fallback.c |  2 +-
 2 files changed, 7 insertions(+), 5 deletions(-)
block/bio.c +6 -4
index 95e2ee14cea2..7a93724e4a49 100644
@@ -1740,16 +1740,18 @@ struct bio *bio_split(struct bio *bio, int sectors,
 {
 	struct bio *split;
 
-	BUG_ON(sectors <= 0);
-	BUG_ON(sectors >= bio_sectors(bio));
+	if (WARN_ON_ONCE(sectors <= 0))
+		return ERR_PTR(-EINVAL);
+	if (WARN_ON_ONCE(sectors >= bio_sectors(bio)))
+		return ERR_PTR(-EINVAL);
 
 	/* Zone append commands cannot be split */
 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
-		return NULL;
+		return ERR_PTR(-EINVAL);
 
 	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
 	if (!split)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	split->bi_iter.bi_size = sectors << 9;
 
block/blk-crypto-fallback.c +1 -1
index b1e7415f8439..29a205482617 100644
@@ -226,7 +226,7 @@ static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr)
 
 		split_bio = bio_split(bio, num_sectors, GFP_NOIO,
 				      &crypto_bio_split);
-		if (!split_bio) {
+		if (IS_ERR(split_bio)) {
 			bio->bi_status = BLK_STS_RESOURCE;
 			return false;
 		}
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v3 1/6] block: Rework bio_split() return value

Quoted 53 lines · John Garry

Instead of returning an inconclusive value of NULL for an error in calling bio_split(), return a ERR_PTR() always.

Also remove the BUG_ON() calls, and WARN_ON_ONCE() instead. Indeed, since almost all callers don't check the return code from bio_split(), we'll crash anyway (for those failures).

Fix up the only user which checks bio_split() return code today (directly or indirectly), blk_crypto_fallback_split_bio_if_needed(). The md/bcache code does check the return code in cached_dev_cache_miss() -> bio_next_split() -> bio_split(), but only to see if there was a split, so there would be no change in behaviour here (when returning a ERR_PTR()).

---
  block/bio.c                 | 10 ++++++----
  block/blk-crypto-fallback.c |  2 +-
  2 files changed, 7 insertions(+), 5 deletions(-)
block/bio.c +6 -4
index 95e2ee14cea2..7a93724e4a49 100644
@@ -1740,16 +1740,18 @@ struct bio *bio_split(struct bio *bio, int sectors,
  {
  	struct bio *split;
 
-	BUG_ON(sectors <= 0);
-	BUG_ON(sectors >= bio_sectors(bio));
+	if (WARN_ON_ONCE(sectors <= 0))
+		return ERR_PTR(-EINVAL);
+	if (WARN_ON_ONCE(sectors >= bio_sectors(bio)))
+		return ERR_PTR(-EINVAL);
 
  	/* Zone append commands cannot be split */
  	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
-		return NULL;
+		return ERR_PTR(-EINVAL);
 
  	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
  	if (!split)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
  	split->bi_iter.bi_size = sectors << 9;
 
block/blk-crypto-fallback.c +0 -0
index b1e7415f8439..29a205482617 100644

block/blk-crypto-fallback.c static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr) · line 232

Quoted 8 lines
block/blk-crypto-fallback.c +1 -1
@@ -226,7 +226,7 @@ static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr)
 
  		split_bio = bio_split(bio, num_sectors, GFP_NOIO,
  				      &crypto_bio_split);
-		if (!split_bio) {
+		if (IS_ERR(split_bio)) {
  			bio->bi_status = BLK_STS_RESOURCE;
  			return false;
  		}

Don't you need to modify block/bounce.c, too?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#3 lore raw

Re: [PATCH v3 1/6] block: Rework bio_split() return value

Quoted 52 lines · Hannes Reinecke
Quoted 51 lines · John Garry

Instead of returning an inconclusive value of NULL for an error in calling bio_split(), return a ERR_PTR() always.

Also remove the BUG_ON() calls, and WARN_ON_ONCE() instead. Indeed, since almost all callers don't check the return code from bio_split(), we'll crash anyway (for those failures).

Fix up the only user which checks bio_split() return code today (directly or indirectly), blk_crypto_fallback_split_bio_if_needed(). The md/bcache code does check the return code in cached_dev_cache_miss() -> bio_next_split() -> bio_split(), but only to see if there was a split, so there would be no change in behaviour here (when returning a ERR_PTR()).

---
  block/bio.c                 | 10 ++++++----
  block/blk-crypto-fallback.c |  2 +-

  2 files changed, 7 insertions(+), 5 deletions(-)
block/bio.c +6 -4
index 95e2ee14cea2..7a93724e4a49 100644
@@ -1740,16 +1740,18 @@ struct bio *bio_split(struct bio *bio, int 
 sectors,
   {
       struct bio *split;
-    BUG_ON(sectors <= 0);
-    BUG_ON(sectors >= bio_sectors(bio));
+    if (WARN_ON_ONCE(sectors <= 0))
+        return ERR_PTR(-EINVAL);
+    if (WARN_ON_ONCE(sectors >= bio_sectors(bio)))
+        return ERR_PTR(-EINVAL);
       /* Zone append commands cannot be split */
       if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
-        return NULL;
+        return ERR_PTR(-EINVAL);
       split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
       if (!split)
-        return NULL;
+        return ERR_PTR(-ENOMEM);
       split->bi_iter.bi_size = sectors << 9;
block/blk-crypto-fallback.c +0 -0
index b1e7415f8439..29a205482617 100644
@@ -226,7 +226,7 @@ static bool 
 blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr)
           split_bio = bio_split(bio, num_sectors, GFP_NOIO,
Quoted 8 lines

block/blk-crypto-fallback.c static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr) · line 232

Quoted 6 lines
block/blk-crypto-fallback.c +1 -1
                         &crypto_bio_split);
-        if (!split_bio) {
+        if (IS_ERR(split_bio)) {
               bio->bi_status = BLK_STS_RESOURCE;
               return false;
           }

Don't you need to modify block/bounce.c, too?

Today we have __blk_queue_bounce() -> bio_split(), but the return value from bio_split() is not checked for errors (NULL) there, so it is already in a poor state.

I will look to remedy that and other callsites which don't check bio_split() return value for errors in next phase.

Thanks,
John

author
#4 lore raw

PATCH v3 2/6 block: Error an attempt to split an atomic write in bio_split()

In reply to John Garry

This is disallowed.

---
 block/bio.c | 4 ++++
 1 file changed, 4 insertions(+)
block/bio.c +4 -0
index 7a93724e4a49..07b971853768 100644
@@ -1749,6 +1749,10 @@ struct bio *bio_split(struct bio *bio, int sectors,
 	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
 		return ERR_PTR(-EINVAL);
 
+	/* atomic writes cannot be split */
+	if (bio->bi_opf & REQ_ATOMIC)
+		return ERR_PTR(-EINVAL);
+
 	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
 	if (!split)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.31.1
Reviewed-by on v3 2/6
#5 lore raw

Re: [PATCH v3 2/6] block: Error an attempt to split an atomic write in bio_split()

Quoted 14 lines · John Garry

This is disallowed.

---
  block/bio.c | 4 ++++
  1 file changed, 4 insertions(+)
block/bio.c +0 -0
index 7a93724e4a49..07b971853768 100644

block/bio.c struct bio *bio_split(struct bio *bio, int sectors, · line 1758

Quoted 10 lines
block/bio.c +4 -0
@@ -1749,6 +1749,10 @@ struct bio *bio_split(struct bio *bio, int sectors,
  	if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND))
  		return ERR_PTR(-EINVAL);
 
+	/* atomic writes cannot be split */
+	if (bio->bi_opf & REQ_ATOMIC)
+		return ERR_PTR(-EINVAL);
+
  	split = bio_alloc_clone(bio->bi_bdev, bio, gfp, bs);
  	if (!split)
  		return ERR_PTR(-ENOMEM);

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#6 lore raw

PATCH v3 3/6 block: Handle bio_split() errors in bio_submit_split()

In reply to John Garry

bio_split() may error, so check this.

---
 block/blk-merge.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
block/blk-merge.c +10 -5
index a16abf7b65dc..4d7b6bc8814c 100644
@@ -107,11 +107,8 @@ static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
 
 static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
 {
-	if (unlikely(split_sectors < 0)) {
-		bio->bi_status = errno_to_blk_status(split_sectors);
-		bio_endio(bio);
-		return NULL;
-	}
+	if (unlikely(split_sectors < 0))
+		goto error;
 
 
 	if (unlikely((bio_op(bio) == REQ_OP_DISCARD)))
@@ -123,6 +120,10 @@ static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
 
 		split = bio_split(bio, split_sectors, GFP_NOIO,
 				&bio->bi_bdev->bd_disk->bio_split);
+		if (IS_ERR(split)) {
+			split_sectors = PTR_ERR(split);
+			goto error;
+		}
 		split->bi_opf |= REQ_NOMERGE;
 		blkcg_bio_issue_init(split);
 		bio_chain(split, bio);
@@ -133,6 +134,10 @@ static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
 	}
 
 	return bio;
+error:
+	bio->bi_status = errno_to_blk_status(split_sectors);
+	bio_endio(bio);
+	return NULL;
 }
 
 struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim,
Signature · 1 line
2.31.1
Reviewed-by on v3 3/6
#7 lore raw

Re: [PATCH v3 3/6] block: Handle bio_split() errors in bio_submit_split()

Quoted 39 lines · John Garry

bio_split() may error, so check this.

---
  block/blk-merge.c | 15 ++++++++++-----
  1 file changed, 10 insertions(+), 5 deletions(-)
block/blk-merge.c +6 -5
index a16abf7b65dc..4d7b6bc8814c 100644
@@ -107,11 +107,8 @@ static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
 
  static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
  {
-	if (unlikely(split_sectors < 0)) {
-		bio->bi_status = errno_to_blk_status(split_sectors);
-		bio_endio(bio);
-		return NULL;
-	}
+	if (unlikely(split_sectors < 0))
+		goto error;
 
 
  	if (unlikely((bio_op(bio) == REQ_OP_DISCARD)))
@@ -123,6 +120,10 @@ static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
 
  		split = bio_split(bio, split_sectors, GFP_NOIO,
  				&bio->bi_bdev->bd_disk->bio_split);
+		if (IS_ERR(split)) {
+			split_sectors = PTR_ERR(split);
+			goto error;
+		}
  		split->bi_opf |= REQ_NOMERGE;
  		blkcg_bio_issue_init(split);
  		bio_chain(split, bio);

block/blk-merge.c static struct bio *bio_submit_split(struct bio *bio, int split_sectors) · line 143

Quoted 10 lines
block/blk-merge.c +4 -0
@@ -133,6 +134,10 @@ static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
  	}
 
  	return bio;
+error:
+	bio->bi_status = errno_to_blk_status(split_sectors);
+	bio_endio(bio);
+	return NULL;
  }
 
  struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim,

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#8 lore raw

PATCH v3 4/6 md/raid0: Handle bio_split() errors

In reply to John Garry

Add proper bio_split() error handling. For any error, set bi_status, end the bio, and return.

---
 drivers/md/raid0.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/md/raid0.c +12 -0
index 32d587524778..baaf5f8b80ae 100644
@@ -466,6 +466,12 @@ static void raid0_handle_discard(struct mddev *mddev, struct bio *bio)
 		struct bio *split = bio_split(bio,
 			zone->zone_end - bio->bi_iter.bi_sector, GFP_NOIO,
 			&mddev->bio_set);
+
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return;
+		}
 		bio_chain(split, bio);
 		submit_bio_noacct(bio);
 		bio = split;
@@ -608,6 +614,12 @@ static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
 	if (sectors < bio_sectors(bio)) {
 		struct bio *split = bio_split(bio, sectors, GFP_NOIO,
 					      &mddev->bio_set);
+
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return true;
+		}
 		bio_chain(split, bio);
 		raid0_map_submit_bio(mddev, bio);
 		bio = split;
Signature · 1 line
2.31.1
Reviewed-by on v3 4/6
#9 lore raw

Re: [PATCH v3 4/6] md/raid0: Handle bio_split() errors

Quoted 9 lines · John Garry

Add proper bio_split() error handling. For any error, set bi_status, end the bio, and return.

---
  drivers/md/raid0.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#10 lore raw

PATCH v3 5/6 md/raid1: Handle bio_split() errors

In reply to John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return.

For the case of an in the write path, we need to undo the increment in the rdev pending count and NULLify the r1_bio->bios[] pointers.

For read path failure, we need to undo rdev pending count increment from the earlier read_balance() call.

---
 drivers/md/raid1.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)
drivers/md/raid1.c +31 -2
index 6c9d24203f39..7e023e9303c8 100644
@@ -1322,7 +1322,7 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
 	const enum req_op op = bio_op(bio);
 	const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
 	int max_sectors;
-	int rdisk;
+	int rdisk, error;
 	bool r1bio_existed = !!r1_bio;
 
 	/*
@@ -1383,6 +1383,11 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
 	if (max_sectors < bio_sectors(bio)) {
 		struct bio *split = bio_split(bio, max_sectors,
 					      gfp, &conf->bio_split);
+
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
 		bio_chain(split, bio);
 		submit_bio_noacct(bio);
 		bio = split;
@@ -1410,6 +1415,13 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
 	read_bio->bi_private = r1_bio;
 	mddev_trace_remap(mddev, read_bio, r1_bio->sector);
 	submit_bio_noacct(read_bio);
+	return;
+
+err_handle:
+	atomic_dec(&mirror->rdev->nr_pending);
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R1BIO_Uptodate, &r1_bio->state);
+	raid_end_bio_io(r1_bio);
 }
 
 static void raid1_write_request(struct mddev *mddev, struct bio *bio,
@@ -1417,7 +1429,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 {
 	struct r1conf *conf = mddev->private;
 	struct r1bio *r1_bio;
-	int i, disks;
+	int i, disks, k, error;
 	unsigned long flags;
 	struct md_rdev *blocked_rdev;
 	int first_clone;
@@ -1576,6 +1588,11 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 	if (max_sectors < bio_sectors(bio)) {
 		struct bio *split = bio_split(bio, max_sectors,
 					      GFP_NOIO, &conf->bio_split);
+
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
 		bio_chain(split, bio);
 		submit_bio_noacct(bio);
 		bio = split;
@@ -1660,6 +1677,18 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 
 	/* In case raid1d snuck in to freeze_array */
 	wake_up_barrier(conf);
+	return;
+err_handle:
+	for (k = 0; k < i; k++) {
+		if (r1_bio->bios[k]) {
+			rdev_dec_pending(conf->mirrors[k].rdev, mddev);
+			r1_bio->bios[k] = NULL;
+		}
+	}
+
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R1BIO_Uptodate, &r1_bio->state);
+	raid_end_bio_io(r1_bio);
 }
 
 static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
Signature · 1 line
2.31.1
Reviewed-by on v3 5/6
#11 lore raw

Re: [PATCH v3 5/6] md/raid1: Handle bio_split() errors

Quoted 6 lines · John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return.

For the case of an in the write path, we need to undo the increment in the rdev pending count and NULLify the r1_bio->bios[] pointers.

Quoted 8 lines

For read path failure, we need to undo rdev pending count increment from the earlier read_balance() call.

---
  drivers/md/raid1.c | 33 +++++++++++++++++++++++++++++++--
  1 file changed, 31 insertions(+), 2 deletions(-)

LGTM

Quoted 80 lines
drivers/md/raid1.c +31 -2
index 6c9d24203f39..7e023e9303c8 100644
@@ -1322,7 +1322,7 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
  	const enum req_op op = bio_op(bio);
  	const blk_opf_t do_sync = bio->bi_opf & REQ_SYNC;
  	int max_sectors;
-	int rdisk;
+	int rdisk, error;
  	bool r1bio_existed = !!r1_bio;
 
  	/*
@@ -1383,6 +1383,11 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
  	if (max_sectors < bio_sectors(bio)) {
  		struct bio *split = bio_split(bio, max_sectors,
  					      gfp, &conf->bio_split);
+
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
  		bio_chain(split, bio);
  		submit_bio_noacct(bio);
  		bio = split;
@@ -1410,6 +1415,13 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio,
  	read_bio->bi_private = r1_bio;
  	mddev_trace_remap(mddev, read_bio, r1_bio->sector);
  	submit_bio_noacct(read_bio);
+	return;
+
+err_handle:
+	atomic_dec(&mirror->rdev->nr_pending);
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R1BIO_Uptodate, &r1_bio->state);
+	raid_end_bio_io(r1_bio);
  }
 
  static void raid1_write_request(struct mddev *mddev, struct bio *bio,
@@ -1417,7 +1429,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
  {
  	struct r1conf *conf = mddev->private;
  	struct r1bio *r1_bio;
-	int i, disks;
+	int i, disks, k, error;
  	unsigned long flags;
  	struct md_rdev *blocked_rdev;
  	int first_clone;
@@ -1576,6 +1588,11 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
  	if (max_sectors < bio_sectors(bio)) {
  		struct bio *split = bio_split(bio, max_sectors,
  					      GFP_NOIO, &conf->bio_split);
+
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
  		bio_chain(split, bio);
  		submit_bio_noacct(bio);
  		bio = split;
@@ -1660,6 +1677,18 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
 
  	/* In case raid1d snuck in to freeze_array */
  	wake_up_barrier(conf);
+	return;
+err_handle:
+	for (k = 0; k < i; k++) {
+		if (r1_bio->bios[k]) {
+			rdev_dec_pending(conf->mirrors[k].rdev, mddev);
+			r1_bio->bios[k] = NULL;
+		}
+	}
+
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R1BIO_Uptodate, &r1_bio->state);
+	raid_end_bio_io(r1_bio);
  }
 
  static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
Reviewed-by on v3 5/6
#12 lore raw

Re: [PATCH v3 5/6] md/raid1: Handle bio_split() errors

In reply to John Garry
Quoted 6 lines · John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return.

For the case of an in the write path, we need to undo the increment in the rdev pending count and NULLify the r1_bio->bios[] pointers.

Quoted 8 lines

For read path failure, we need to undo rdev pending count increment from the earlier read_balance() call.

---
  drivers/md/raid1.c | 33 +++++++++++++++++++++++++++++++--
  1 file changed, 31 insertions(+), 2 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#13 lore raw

PATCH v3 6/6 md/raid10: Handle bio_split() errors

In reply to John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return. Except for discard, where we end the bio directly.

---
 drivers/md/raid10.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
drivers/md/raid10.c +46 -1
index f3bf1116794a..ccd95459b192 100644
@@ -1159,6 +1159,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	int slot = r10_bio->read_slot;
 	struct md_rdev *err_rdev = NULL;
 	gfp_t gfp = GFP_NOIO;
+	int error;
 
 	if (slot >= 0 && r10_bio->devs[slot].rdev) {
 		/*
@@ -1206,6 +1207,10 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	if (max_sectors < bio_sectors(bio)) {
 		struct bio *split = bio_split(bio, max_sectors,
 					      gfp, &conf->bio_split);
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
 		bio_chain(split, bio);
 		allow_barrier(conf);
 		submit_bio_noacct(bio);
@@ -1236,6 +1241,11 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	mddev_trace_remap(mddev, read_bio, r10_bio->sector);
 	submit_bio_noacct(read_bio);
 	return;
+err_handle:
+	atomic_dec(&rdev->nr_pending);
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R10BIO_Uptodate, &r10_bio->state);
+	raid_end_bio_io(r10_bio);
 }
 
 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
@@ -1347,9 +1357,10 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
 				 struct r10bio *r10_bio)
 {
 	struct r10conf *conf = mddev->private;
-	int i;
+	int i, k;
 	sector_t sectors;
 	int max_sectors;
+	int error;
 
 	if ((mddev_is_clustered(mddev) &&
 	     md_cluster_ops->area_resyncing(mddev, WRITE,
@@ -1482,6 +1493,10 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
 	if (r10_bio->sectors < bio_sectors(bio)) {
 		struct bio *split = bio_split(bio, r10_bio->sectors,
 					      GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
 		bio_chain(split, bio);
 		allow_barrier(conf);
 		submit_bio_noacct(bio);
@@ -1503,6 +1518,26 @@ 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;
+err_handle:
+	for (k = 0;  k < i; k++) {
+		int d = r10_bio->devs[k].devnum;
+		struct md_rdev *rdev = conf->mirrors[d].rdev;
+		struct md_rdev *rrdev = conf->mirrors[d].replacement;
+
+		if (r10_bio->devs[k].bio) {
+			rdev_dec_pending(rdev, mddev);
+			r10_bio->devs[k].bio = NULL;
+		}
+		if (r10_bio->devs[k].repl_bio) {
+			rdev_dec_pending(rrdev, mddev);
+			r10_bio->devs[k].repl_bio = NULL;
+		}
+	}
+
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R10BIO_Uptodate, &r10_bio->state);
+	raid_end_bio_io(r10_bio);
 }
 
 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
@@ -1644,6 +1679,11 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
 	if (remainder) {
 		split_size = stripe_size - remainder;
 		split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return 0;
+		}
 		bio_chain(split, bio);
 		allow_barrier(conf);
 		/* Resend the fist split part */
@@ -1654,6 +1694,11 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
 	if (remainder) {
 		split_size = bio_sectors(bio) - remainder;
 		split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return 0;
+		}
 		bio_chain(split, bio);
 		allow_barrier(conf);
 		/* Resend the second split part */
Signature · 1 line
2.31.1
Reviewed-by on v3 6/6
#14 lore raw

Re: [PATCH v3 6/6] md/raid10: Handle bio_split() errors

Quoted 9 lines · John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return. Except for discard, where we end the bio directly.

---
  drivers/md/raid10.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 46 insertions(+), 1 deletion(-)

LGTM

Quoted 110 lines
drivers/md/raid10.c +46 -1
index f3bf1116794a..ccd95459b192 100644
@@ -1159,6 +1159,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
  	int slot = r10_bio->read_slot;
  	struct md_rdev *err_rdev = NULL;
  	gfp_t gfp = GFP_NOIO;
+	int error;
 
  	if (slot >= 0 && r10_bio->devs[slot].rdev) {
  		/*
@@ -1206,6 +1207,10 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
  	if (max_sectors < bio_sectors(bio)) {
  		struct bio *split = bio_split(bio, max_sectors,
  					      gfp, &conf->bio_split);
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
  		bio_chain(split, bio);
  		allow_barrier(conf);
  		submit_bio_noacct(bio);
@@ -1236,6 +1241,11 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
  	mddev_trace_remap(mddev, read_bio, r10_bio->sector);
  	submit_bio_noacct(read_bio);
  	return;
+err_handle:
+	atomic_dec(&rdev->nr_pending);
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R10BIO_Uptodate, &r10_bio->state);
+	raid_end_bio_io(r10_bio);
  }
 
  static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
@@ -1347,9 +1357,10 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
  				 struct r10bio *r10_bio)
  {
  	struct r10conf *conf = mddev->private;
-	int i;
+	int i, k;
  	sector_t sectors;
  	int max_sectors;
+	int error;
 
  	if ((mddev_is_clustered(mddev) &&
  	     md_cluster_ops->area_resyncing(mddev, WRITE,
@@ -1482,6 +1493,10 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,
  	if (r10_bio->sectors < bio_sectors(bio)) {
  		struct bio *split = bio_split(bio, r10_bio->sectors,
  					      GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			error = PTR_ERR(split);
+			goto err_handle;
+		}
  		bio_chain(split, bio);
  		allow_barrier(conf);
  		submit_bio_noacct(bio);
@@ -1503,6 +1518,26 @@ 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;
+err_handle:
+	for (k = 0;  k < i; k++) {
+		int d = r10_bio->devs[k].devnum;
+		struct md_rdev *rdev = conf->mirrors[d].rdev;
+		struct md_rdev *rrdev = conf->mirrors[d].replacement;
+
+		if (r10_bio->devs[k].bio) {
+			rdev_dec_pending(rdev, mddev);
+			r10_bio->devs[k].bio = NULL;
+		}
+		if (r10_bio->devs[k].repl_bio) {
+			rdev_dec_pending(rrdev, mddev);
+			r10_bio->devs[k].repl_bio = NULL;
+		}
+	}
+
+	bio->bi_status = errno_to_blk_status(error);
+	set_bit(R10BIO_Uptodate, &r10_bio->state);
+	raid_end_bio_io(r10_bio);
  }
 
  static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
@@ -1644,6 +1679,11 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
  	if (remainder) {
  		split_size = stripe_size - remainder;
  		split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return 0;
+		}
  		bio_chain(split, bio);
  		allow_barrier(conf);
  		/* Resend the fist split part */
@@ -1654,6 +1694,11 @@ static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
  	if (remainder) {
  		split_size = bio_sectors(bio) - remainder;
  		split = bio_split(bio, split_size, GFP_NOIO, &conf->bio_split);
+		if (IS_ERR(split)) {
+			bio->bi_status = errno_to_blk_status(PTR_ERR(split));
+			bio_endio(bio);
+			return 0;
+		}
  		bio_chain(split, bio);
  		allow_barrier(conf);
  		/* Resend the second split part */
Reviewed-by on v3 6/6
#15 lore raw

Re: [PATCH v3 6/6] md/raid10: Handle bio_split() errors

In reply to John Garry
Quoted 9 lines · John Garry

Add proper bio_split() error handling. For any error, call raid_end_bio_io() and return. Except for discard, where we end the bio directly.

---
  drivers/md/raid10.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 46 insertions(+), 1 deletion(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#16 lore raw
In reply to John Garry

On 31/10/2024 09:59, John Garry wrote:

Hi Jens,

Could you kindly consider picking up this series via the block tree?

Please note that the raid 0/1/10 atomic write support in https://lore.kernel.org/linux-block/[email protected]/ depends on this series, so maybe you would also be willing to pick that one up (when it's fully reviewed). Or create a branch with all the block changes, like which was done for the atomic writes XFS support.

Thanks very much,
John

Quoted 52 lines

bio_split() error handling could be improved as follows:
- Instead of returning NULL for an error - which is vague - return a PTR_ERR, which may hint what went wrong.
- Remove BUG_ON() calls - which are generally not preferred - and instead WARN and pass an error code back to the caller. Many callers of bio_split() don't check the return code. As such, for an error we would be getting a crash still from an invalid pointer dereference.

Most bio_split() callers don't check the return value. However, it could be argued the bio_split() calls should not fail. So far I have just fixed up the md RAID code to handle these errors, as that is my interest now.

The motivator for this series was initial md RAID atomic write support in
https://lore.kernel.org/linux-block/[email protected]/T/#m5859ee900de8e6554d5bb027c0558f0147c32df8

There I wanted to ensure that we don't split an atomic write bio, and it made more sense to handle this in bio_split() (instead of the bio_split() caller).

Based on 133008e84b99 (block/for-6.13/block) blk-integrity: remove seed for user mapped buffers

Changes since v2:
- Drop "block: Use BLK_STS_OK in bio_init()" change (Christoph)
- Use proper rdev indexing in raid10_write_request() (Kuai)
- Decrement rdev nr_pending in raid1 read error path (Kuai)
- Add RB tags from Christoph, Johannes, and Kuai (thanks!)

Changes since RFC:
- proper handling to end the raid bio in all cases, and also pass back proper error code (Kuai)
- Add WARN_ON_ERROR in bio_split() (Johannes, Christoph)
- Add small patch to use BLK_STS_OK in bio_init()
- Change bio_submit_split() error path (Christoph)

John Garry (6):
block: Rework bio_split() return value
block: Error an attempt to split an atomic write in bio_split()
block: Handle bio_split() errors in bio_submit_split() md/raid0: Handle bio_split() errors
md/raid1: Handle bio_split() errors
md/raid10: Handle bio_split() errors

  block/bio.c                 | 14 +++++++----
  block/blk-crypto-fallback.c |  2 +-
  block/blk-merge.c           | 15 ++++++++----
  drivers/md/raid0.c          | 12 ++++++++++
  drivers/md/raid1.c          | 33 ++++++++++++++++++++++++--
  drivers/md/raid10.c         | 47 ++++++++++++++++++++++++++++++++++++-
  6 files changed, 110 insertions(+), 13 deletions(-)
author
#17 lore raw
In reply to John Garry

On 31/10/2024 09:59, John Garry wrote:

Hi Jens,

Could you kindly consider picking up this series via the block tree?

Please note that the raid 0/1/10 atomic write support in https://lore.kernel.org/linux-block/[email protected]/ depends on this series, so maybe you would also be willing to pick that one up (when it's fully reviewed). Or create a branch with all the block changes, like which was done for the atomic writes XFS support.

Thanks very much,
John

Quoted 52 lines

bio_split() error handling could be improved as follows:
- Instead of returning NULL for an error - which is vague - return a PTR_ERR, which may hint what went wrong.
- Remove BUG_ON() calls - which are generally not preferred - and instead WARN and pass an error code back to the caller. Many callers of bio_split() don't check the return code. As such, for an error we would be getting a crash still from an invalid pointer dereference.

Most bio_split() callers don't check the return value. However, it could be argued the bio_split() calls should not fail. So far I have just fixed up the md RAID code to handle these errors, as that is my interest now.

The motivator for this series was initial md RAID atomic write support in
https://lore.kernel.org/linux-block/[email protected]/T/#m5859ee900de8e6554d5bb027c0558f0147c32df8

There I wanted to ensure that we don't split an atomic write bio, and it made more sense to handle this in bio_split() (instead of the bio_split() caller).

Based on 133008e84b99 (block/for-6.13/block) blk-integrity: remove seed for user mapped buffers

Changes since v2:
- Drop "block: Use BLK_STS_OK in bio_init()" change (Christoph)
- Use proper rdev indexing in raid10_write_request() (Kuai)
- Decrement rdev nr_pending in raid1 read error path (Kuai)
- Add RB tags from Christoph, Johannes, and Kuai (thanks!)

Changes since RFC:
- proper handling to end the raid bio in all cases, and also pass back proper error code (Kuai)
- Add WARN_ON_ERROR in bio_split() (Johannes, Christoph)
- Add small patch to use BLK_STS_OK in bio_init()
- Change bio_submit_split() error path (Christoph)

John Garry (6):
block: Rework bio_split() return value
block: Error an attempt to split an atomic write in bio_split()
block: Handle bio_split() errors in bio_submit_split() md/raid0: Handle bio_split() errors
md/raid1: Handle bio_split() errors
md/raid10: Handle bio_split() errors

  block/bio.c                 | 14 +++++++----
  block/blk-crypto-fallback.c |  2 +-
  block/blk-merge.c           | 15 ++++++++----
  drivers/md/raid0.c          | 12 ++++++++++
  drivers/md/raid1.c          | 33 ++++++++++++++++++++++++--
  drivers/md/raid10.c         | 47 ++++++++++++++++++++++++++++++++++++-
  6 files changed, 110 insertions(+), 13 deletions(-)
maintainer
#18 lore raw
Quoted 7 lines · John Garry

On 31/10/2024 09:59, John Garry wrote:

Hi Jens,

Could you kindly consider picking up this series via the block tree?

Please note that the raid 0/1/10 atomic write support in https://lore.kernel.org/linux-block/[email protected]/ depends on this series, so maybe you would also be willing to pick that one up (when it's fully reviewed). Or create a branch with all the block changes, like which was done for the atomic writes XFS support.

The series doesn't apply to for-6.13/block - the 3rd patch for bio splitting conflicts with:

commit b35243a447b9fe6457fa8e1352152b818436ba5a

Author: Christoph Hellwig <[email protected]>
Date:   Mon Aug 26 19:37:54 2024 +0200

block: rework bio splitting

which was in long before for-6.13/block, which it's supposed to be based on?

Please double check and resend a v4.

Signature · 1 line
Jens Axboe
author
#19 lore raw
Quoted 7 lines · Jens Axboe

The series doesn't apply to for-6.13/block - the 3rd patch for bio splitting conflicts with:

commit b35243a447b9fe6457fa8e1352152b818436ba5a

Author: Christoph Hellwig<[email protected]>
Date:   Mon Aug 26 19:37:54 2024 +0200
Quoted 6 lines

block: rework bio splitting

which was in long before for-6.13/block, which it's supposed to be based on?

Please double check and resend a v4.

Hi Jens,

I was based on a recent commit, 133008e84b99 (block/for-6.13/block) blk-integrity: remove seed for user mapped buffers

Anyway, it is a week old, so I will rebase and repost.

Thanks,
John