Skip to contentSkip to conversation

block: ensure we hold a queue reference when using queue limits

PATCH In review Jens Axboe 3 participants 4 messages Jan 12, 2024 – Jan 13, 2024 Block layer

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 block: ensure we hold a queue reference when using queue limits

q_usage_counter is the only thing preventing us from the limits changing under us in __bio_split_to_limits, but blk_mq_submit_bio doesn't hold it while calling into it.

Move the splitting inside the region where we know we've got a queue reference. Ideally this could still remain a shared section of code, but let's keep the fix simple and defer any refactoring here to later.

Fixes 9d497e2941c3 ("block: don't protect submit_bio_checks by q_usage_counter")

---

block/blk-mq.c +10 -6
index f57b86d6de6a..e02c4b1af8c5 100644
@@ -2964,12 +2964,6 @@ void blk_mq_submit_bio(struct bio *bio)
 	blk_status_t ret;
 
 	bio = blk_queue_bounce(bio, q);
-	if (bio_may_exceed_limits(bio, &q->limits)) {
-		bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
-		if (!bio)
-			return;
-	}
-
 	bio_set_ioprio(bio);
 
 	if (plug) {
@@ -2978,6 +2972,11 @@ void blk_mq_submit_bio(struct bio *bio)
 			rq = NULL;
 	}
 	if (rq) {
+		if (unlikely(bio_may_exceed_limits(bio, &q->limits))) {
+			bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
+			if (!bio)
+				return;
+		}
 		if (!bio_integrity_prep(bio))
 			return;
 		if (blk_mq_attempt_bio_merge(q, bio, nr_segs))
@@ -2988,6 +2987,11 @@ void blk_mq_submit_bio(struct bio *bio)
 	} else {
 		if (unlikely(bio_queue_enter(bio)))
 			return;
+		if (unlikely(bio_may_exceed_limits(bio, &q->limits))) {
+			bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
+			if (!bio)
+				goto fail;
+		}
 		if (!bio_integrity_prep(bio))
 			goto fail;
 	}
Signature · 1 line
Jens Axboe
Reviewed-by on v1 1/1
#2 lore raw
In reply to Jens Axboe
Quoted 11 lines · Jens Axboe

q_usage_counter is the only thing preventing us from the limits changing under us in __bio_split_to_limits, but blk_mq_submit_bio doesn't hold it while calling into it.

Move the splitting inside the region where we know we've got a queue reference. Ideally this could still remain a shared section of code, but let's keep the fix simple and defer any refactoring here to later.

Fixes 9d497e2941c3 ("block: don't protect submit_bio_checks by q_usage_counter")

The fixes tag is wrong, and it should be:

Fixes 900e08075202 ("block: move queue enter logic into blk_mq_submit_bio()")
Quoted 34 lines

---

block/blk-mq.c +5 -6
index f57b86d6de6a..e02c4b1af8c5 100644
@@ -2964,12 +2964,6 @@ void blk_mq_submit_bio(struct bio *bio)
        blk_status_t ret;
 
        bio = blk_queue_bounce(bio, q);
-       if (bio_may_exceed_limits(bio, &q->limits)) {
-               bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
-               if (!bio)
-                       return;
-       }
-
        bio_set_ioprio(bio);
 
        if (plug) {
@@ -2978,6 +2972,11 @@ void blk_mq_submit_bio(struct bio *bio)
                        rq = NULL;
        }
        if (rq) {
+               if (unlikely(bio_may_exceed_limits(bio, &q->limits))) {
+                       bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
+                       if (!bio)
+                               return;
+               }
                if (!bio_integrity_prep(bio))
                        return;
                if (blk_mq_attempt_bio_merge(q, bio, nr_segs))

block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 2994

Quoted 8 lines
block/blk-mq.c +5 -0
@@ -2988,6 +2987,11 @@ void blk_mq_submit_bio(struct bio *bio)
        } else {
                if (unlikely(bio_queue_enter(bio)))
                        return;
+               if (unlikely(bio_may_exceed_limits(bio, &q->limits))) {
+                       bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
+                       if (!bio)
+                               goto fail;
+               }

With "Fixes" tag fixed, the patch looks fine:

author maintainer
#3 lore raw
Quoted 10 lines · Ming Lei
Quoted 9 lines · On Sat, Jan 13, 2024 at 12:15?AM Jens Axboe <[email protected]> wrote:

q_usage_counter is the only thing preventing us from the limits changing under us in __bio_split_to_limits, but blk_mq_submit_bio doesn't hold it while calling into it.

Move the splitting inside the region where we know we've got a queue reference. Ideally this could still remain a shared section of code, but let's keep the fix simple and defer any refactoring here to later.

Quoted 6 lines
Quoted 2 lines
Fixes 9d497e2941c3 ("block: don't protect submit_bio_checks by q_usage_counter")

The fixes tag is wrong, and it should be:

Fixes 900e08075202 ("block: move queue enter logic into blk_mq_submit_bio()")

You're right, I fixed it up.

Signature · 1 line
Jens Axboe