Skip to contentSkip to conversation

Move blk_mq_submit_bio() error handling

PATCH Superseded Bart Van Assche 3 participants 11 messages Dec 16, 2024 – Dec 18, 2024 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Move blk_mq_submit_bio() error handling

Hi Jens,

Some of the blk_mq_submit_bio() error handling occurs in that function itself while the remaining error handling code occurs in functions called by blk_mq_submit_bio(). This patch series makes blk_mq_submit_bio() easier to understand by moving all error handling code into blk_mq_submit_bio(). No functionality has been changed.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Bart Van Assche (2):
block: Optimize blk_mq_submit_bio() for the cache hit scenario
blk-mq: Move more error handling into blk_mq_submit_bio()

 block/blk-mq.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
#1 lore raw

PATCH 1/2 block: Optimize blk_mq_submit_bio() for the cache hit scenario

Help the CPU branch predictor in case of a cache hit by handling the cache hit scenario first.

Cc Damien Le Moal <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
block/blk-mq.c +3 -3
index 7ee21346a41e..8d2aab4d9ba9 100644
@@ -3102,12 +3102,12 @@ void blk_mq_submit_bio(struct bio *bio)
 		goto queue_exit;
 
 new_request:
-	if (!rq) {
+	if (rq) {
+		blk_mq_use_cached_rq(rq, plug, bio);
+	} else {
 		rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
 		if (unlikely(!rq))
 			goto queue_exit;
-	} else {
-		blk_mq_use_cached_rq(rq, plug, bio);
 	}
 
 	trace_block_getrq(bio);
#7 lore raw

PATCH 2/2 blk-mq: Move more error handling into blk_mq_submit_bio()

In reply to Bart Van Assche

The error handling code in blk_mq_get_new_requests() cannot be understood without knowing that this function is only called by blk_mq_submit_bio(). Hence move the code for handling blk_mq_get_new_requests() failures into blk_mq_submit_bio().

Cc Damien Le Moal <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
block/blk-mq.c +4 -3
index 8d2aab4d9ba9..80eb91296142 100644
@@ -2971,8 +2971,6 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	if (rq)
 		return rq;
 	rq_qos_cleanup(q, bio);
-	if (bio->bi_opf & REQ_NOWAIT)
-		bio_wouldblock_error(bio);
 	return NULL;
 }
 
@@ -3106,8 +3104,11 @@ void blk_mq_submit_bio(struct bio *bio)
 		blk_mq_use_cached_rq(rq, plug, bio);
 	} else {
 		rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
-		if (unlikely(!rq))
+		if (unlikely(!rq)) {
+			if (bio->bi_opf & REQ_NOWAIT)
+				bio_wouldblock_error(bio);
 			goto queue_exit;
+		}
 	}
 
 	trace_block_getrq(bio);
#2 lore raw

Re: [PATCH 1/2] block: Optimize blk_mq_submit_bio() for the cache hit scenario

In reply to Bart Van Assche
Quoted 2 lines · Bart Van Assche

Help the CPU branch predictor in case of a cache hit by handling the cache hit scenario first.

Numbers, please.

#8 lore raw

Re: [PATCH 2/2] blk-mq: Move more error handling into blk_mq_submit_bio()

In reply to Bart Van Assche

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2974

Quoted 11 lines · Bart Van Assche
block/blk-mq.c +0 -2
index 8d2aab4d9ba9..80eb91296142 100644
@@ -2971,8 +2971,6 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	if (rq)
 		return rq;
 	rq_qos_cleanup(q, bio);
-	if (bio->bi_opf & REQ_NOWAIT)
-		bio_wouldblock_error(bio);
 	return NULL;

Please turn this into:

	if (rq)
		rq_qos_cleanup(q, bio);
	return rq;

Otherwise this looks like a nice cleanup.

#3 lore raw

Re: [PATCH 1/2] block: Optimize blk_mq_submit_bio() for the cache hit scenario

In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Bart Van Assche

Help the CPU branch predictor in case of a cache hit by handling the cache hit scenario first.

Numbers, please.

For a single CPU core and with the brd driver and fio and the io_uring I/O engine, I see the following performance in a VM (three test runs for each test case):

Without this patch:      1619K, 1641K, 1638K IOPS or 1633 K +/- 10 K.
With this patch applied: 1650K, 1633K, 1635K IOPS or 1639 K +/-  8 K.

So there is a small performance improvement but the improvement is smaller than the measurement error. Is this sufficient data to proceed with this patch?

Thanks,

Bart.

#9 lore raw

Re: [PATCH 2/2] blk-mq: Move more error handling into blk_mq_submit_bio()

In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2974

Quoted 11 lines · Bart Van Assche
block/blk-mq.c +0 -2
index 8d2aab4d9ba9..80eb91296142 100644
@@ -2971,8 +2971,6 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
  	if (rq)
  		return rq;
  	rq_qos_cleanup(q, bio);
-	if (bio->bi_opf & REQ_NOWAIT)
-		bio_wouldblock_error(bio);
  	return NULL;

Please turn this into:

Quoted 5 lines
	if (rq)
		rq_qos_cleanup(q, bio);
	return rq;

Otherwise this looks like a nice cleanup.

It seems to me that there is a typo in the above code? Unless if you tell me otherwise, I will assume that this is what you meant:

block/blk-mq.c +7 -7
index 8d2aab4d9ba9..f4300e608ed8 100644
@@ -2968,12 +2968,9 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	}
 
 	rq = __blk_mq_alloc_requests(&data);
-	if (rq)
-		return rq;
-	rq_qos_cleanup(q, bio);
-	if (bio->bi_opf & REQ_NOWAIT)
-		bio_wouldblock_error(bio);
-	return NULL;
+	if (!rq)
+		rq_qos_cleanup(q, bio);
+	return rq;
 }
 
 /*
@@ -3106,8 +3103,11 @@ void blk_mq_submit_bio(struct bio *bio)
 		blk_mq_use_cached_rq(rq, plug, bio);
 	} else {
 		rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
-		if (unlikely(!rq))
+		if (unlikely(!rq)) {
+			if (bio->bi_opf & REQ_NOWAIT)
+				bio_wouldblock_error(bio);
 			goto queue_exit;
+		}
 	}
 
 	trace_block_getrq(bio);

Thanks,

Bart.

maintainer
#4 lore raw

Re: [PATCH 1/2] block: Optimize blk_mq_submit_bio() for the cache hit scenario

In reply to Bart Van Assche
Quoted 11 lines · Bart Van Assche
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Bart Van Assche

Help the CPU branch predictor in case of a cache hit by handling the cache hit scenario first.

Numbers, please.

For a single CPU core and with the brd driver and fio and the io_uring I/O engine, I see the following performance in a VM (three test runs for each test case):

Quoted 6 lines

Without this patch:      1619K, 1641K, 1638K IOPS or 1633 K +/- 10 K. With this patch applied: 1650K, 1633K, 1635K IOPS or 1639 K +/-  8 K.

So there is a small performance improvement but the improvement is smaller than the measurement error. Is this sufficient data to proceed with this patch?

I think it's fine, it's going to be very hard to reliably measure. I tend to prefer the expected case to be the one checked for, eg:

rq = get_cached();
if (rq)

	...

anyway, as then the expected outcome is what you're reading next anyway. Either that or

rq = get_cached();
if (unlikely(!rq))

	goto some_failure_path;

If you really wanted to measure it, you'd probably skip the IOPS part and just look at branch misprediction in perf.

Signature · 1 line
Jens Axboe
#5 lore raw

Re: [PATCH 1/2] block: Optimize blk_mq_submit_bio() for the cache hit scenario

In reply to Bart Van Assche
Quoted 10 lines · Bart Van Assche

For a single CPU core and with the brd driver and fio and the io_uring I/O engine, I see the following performance in a VM (three test runs for each test case):

Without this patch:      1619K, 1641K, 1638K IOPS or 1633 K +/- 10 K.
With this patch applied: 1650K, 1633K, 1635K IOPS or 1639 K +/-  8 K.

So there is a small performance improvement but the improvement is smaller than the measurement error. Is this sufficient data to proceed with this patch?

I think it's sufficient data that should not claim that it is an optimizations. The resulting code still looks nicer to me, but arguing with optimizations feels like BS.

#10 lore raw

Re: [PATCH 2/2] blk-mq: Move more error handling into blk_mq_submit_bio()

In reply to Bart Van Assche
Quoted 2 lines · Bart Van Assche

It seems to me that there is a typo in the above code? Unless if you tell me otherwise, I will assume that this is what you meant:

Yes.

#6 lore raw

Re: [PATCH 1/2] block: Optimize blk_mq_submit_bio() for the cache hit scenario

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Bart Van Assche

For a single CPU core and with the brd driver and fio and the io_uring I/O engine, I see the following performance in a VM (three test runs for each test case):

Without this patch:      1619K, 1641K, 1638K IOPS or 1633 K +/- 10 K.
With this patch applied: 1650K, 1633K, 1635K IOPS or 1639 K +/-  8 K.
Quoted 7 lines
Quoted 3 lines

So there is a small performance improvement but the improvement is smaller than the measurement error. Is this sufficient data to proceed with this patch?

I think it's sufficient data that should not claim that it is an optimizations. The resulting code still looks nicer to me, but arguing with optimizations feels like BS.

I will change the patch title.

Thanks,

Bart.