6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Keith Busch [ Upstream commit dc278e9bf2b9513a763353e6b9cc21e0f532954e ] When submitting a bio to blk-mq, if the task should sleep after peeking a cached request, but before it pops it, the plug flushes and calls blk_mq_free_plug_rqs, freeing the cached_rqs. This creates a use-after-free bug. Fix this by popping the cached request before any possible blocking calls if it is suitable for use. Popping this request first holds a queue reference, so avoid any serialization races with queue freezes and can safely proceed with dispatching that request to the driver. This potentially increases a timing window from when a driver wants to freeze its queue to when requests stop being dispatched. That scenario is off the fast path though, and drivers need to appropriately handle requests during a freeze request anyway. The downside is the popped element needs to be individually freed when we performed a bio plug merge. The cached request would have had to be freed later anyway, but this patch does it inline with building the plug list instead of after flushing it. Fixes: b0077e269f6c1 ("blk-mq: make sure active queue usage is held for bio_integrity_prep()") Fixes: 7b4f36cd22a65 ("block: ensure we hold a queue reference when using queue limits") Signed-off-by: Keith Busch Link: https://patch.msgid.link/20260521190253.242065-1-kbusch@meta.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-mq.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index bf69079f6bd51..bc619519dd665 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2953,16 +2953,7 @@ static bool blk_mq_can_use_cached_rq(struct request *rq, struct blk_plug *plug, if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf)) return false; - /* - * If any qos ->throttle() end up blocking, we will have flushed the - * plug and hence killed the cached_rq list as well. Pop this entry - * before we throttle. - */ plug->cached_rq = rq_list_next(rq); - rq_qos_throttle(rq->q, bio); - - rq->cmd_flags = bio->bi_opf; - INIT_LIST_HEAD(&rq->queuelist); return true; } @@ -2994,20 +2985,23 @@ void blk_mq_submit_bio(struct bio *bio) rq = rq_list_peek(&plug->cached_rq); if (rq && rq->q != q) rq = NULL; + if (rq && !blk_mq_can_use_cached_rq(rq, plug, 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; + goto free_rq; } if (!bio_integrity_prep(bio)) - return; + goto free_rq; if (blk_mq_attempt_bio_merge(q, bio, nr_segs)) - return; - if (blk_mq_can_use_cached_rq(rq, plug, bio)) - goto done; - percpu_ref_get(&q->q_usage_counter); + goto free_rq; + rq_qos_throttle(q, bio); + rq->cmd_flags = bio->bi_opf; + INIT_LIST_HEAD(&rq->queuelist); + goto done; } else { if (unlikely(bio_queue_enter(bio))) return; @@ -3056,6 +3050,10 @@ void blk_mq_submit_bio(struct bio *bio) else blk_mq_run_dispatch_ops(rq->q, blk_mq_try_issue_directly(rq->mq_hctx, rq)); + return; + +free_rq: + blk_mq_free_request(rq); } #ifdef CONFIG_BLK_MQ_STACKING -- 2.53.0