Skip to content

Commit 0a5aa8d

Browse files
kawasakiaxboe
authored andcommitted
block: fix blk_mq_attempt_bio_merge and rq_qos_throttle protection
Commit 9d497e2 ("block: don't protect submit_bio_checks by q_usage_counter") moved blk_mq_attempt_bio_merge and rq_qos_throttle calls out of q_usage_counter protection. However, these functions require q_usage_counter protection. The blk_mq_attempt_bio_merge call without the protection resulted in blktests block/005 failure with KASAN null- ptr-deref or use-after-free at bio merge. The rq_qos_throttle call without the protection caused kernel hang at qos throttle. To fix the failures, move the blk_mq_attempt_bio_merge and rq_qos_throttle calls back to q_usage_counter protection. Fixes: 9d497e2 ("block: don't protect submit_bio_checks by q_usage_counter") Signed-off-by: Shin'ichiro Kawasaki <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Ming Lei <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 3093929 commit 0a5aa8d

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

block/blk-mq.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,8 @@ static bool blk_mq_attempt_bio_merge(struct request_queue *q,
27182718

27192719
static struct request *blk_mq_get_new_requests(struct request_queue *q,
27202720
struct blk_plug *plug,
2721-
struct bio *bio)
2721+
struct bio *bio,
2722+
unsigned int nsegs)
27222723
{
27232724
struct blk_mq_alloc_data data = {
27242725
.q = q,
@@ -2730,6 +2731,11 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
27302731
if (unlikely(bio_queue_enter(bio)))
27312732
return NULL;
27322733

2734+
if (blk_mq_attempt_bio_merge(q, bio, nsegs))
2735+
goto queue_exit;
2736+
2737+
rq_qos_throttle(q, bio);
2738+
27332739
if (plug) {
27342740
data.nr_tags = plug->nr_ios;
27352741
plug->nr_ios = 1;
@@ -2742,12 +2748,13 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
27422748
rq_qos_cleanup(q, bio);
27432749
if (bio->bi_opf & REQ_NOWAIT)
27442750
bio_wouldblock_error(bio);
2751+
queue_exit:
27452752
blk_queue_exit(q);
27462753
return NULL;
27472754
}
27482755

27492756
static inline struct request *blk_mq_get_cached_request(struct request_queue *q,
2750-
struct blk_plug *plug, struct bio *bio)
2757+
struct blk_plug *plug, struct bio **bio, unsigned int nsegs)
27512758
{
27522759
struct request *rq;
27532760

@@ -2757,12 +2764,19 @@ static inline struct request *blk_mq_get_cached_request(struct request_queue *q,
27572764
if (!rq || rq->q != q)
27582765
return NULL;
27592766

2760-
if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
2767+
if (blk_mq_attempt_bio_merge(q, *bio, nsegs)) {
2768+
*bio = NULL;
2769+
return NULL;
2770+
}
2771+
2772+
rq_qos_throttle(q, *bio);
2773+
2774+
if (blk_mq_get_hctx_type((*bio)->bi_opf) != rq->mq_hctx->type)
27612775
return NULL;
2762-
if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
2776+
if (op_is_flush(rq->cmd_flags) != op_is_flush((*bio)->bi_opf))
27632777
return NULL;
27642778

2765-
rq->cmd_flags = bio->bi_opf;
2779+
rq->cmd_flags = (*bio)->bi_opf;
27662780
plug->cached_rq = rq_list_next(rq);
27672781
INIT_LIST_HEAD(&rq->queuelist);
27682782
return rq;
@@ -2800,14 +2814,11 @@ void blk_mq_submit_bio(struct bio *bio)
28002814
if (!bio_integrity_prep(bio))
28012815
return;
28022816

2803-
if (blk_mq_attempt_bio_merge(q, bio, nr_segs))
2804-
return;
2805-
2806-
rq_qos_throttle(q, bio);
2807-
2808-
rq = blk_mq_get_cached_request(q, plug, bio);
2817+
rq = blk_mq_get_cached_request(q, plug, &bio, nr_segs);
28092818
if (!rq) {
2810-
rq = blk_mq_get_new_requests(q, plug, bio);
2819+
if (!bio)
2820+
return;
2821+
rq = blk_mq_get_new_requests(q, plug, bio, nr_segs);
28112822
if (unlikely(!rq))
28122823
return;
28132824
}

0 commit comments

Comments
 (0)