From 3f2a79888ada5c811151bc5c59a1c7f59c8f7b3b Mon Sep 17 00:00:00 2001 From: Minsik Jeon Date: Mon, 23 Nov 2026 16:24:17 +0900 Subject: [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() blk_mq_alloc_request_hctx() rejects any caller that does not pass BLK_MQ_REQ_NOWAIT, with a comment explaining that a sleeping tag allocation could hand back a request on a different hardware context: blk_mq_get_tag() re-picks data->hctx after io_schedule(). That reasoning only applies to the tag allocator. The same flags value is also handed to blk_queue_enter(), where waiting for the queue to unfreeze has no bearing on hctx selection at all, so the requirement as written is broader than the invariant it protects. Guarantee the invariant where it actually matters instead of asking every caller for it: force BLK_MQ_REQ_NOWAIT on the tag allocation itself, right before blk_mq_get_tag(), and drop the corresponding WARN_ON_ONCE(). blk_queue_enter() keeps using the caller's flags as-is, so entering a frozen queue may now block if the caller did not ask for BLK_MQ_REQ_NOWAIT. Because blk_queue_enter() can now sleep, two things that implicitly depended on it never blocking need to move: - nr_hw_queues and queue_hw_ctx are only changed with the queue frozen (__blk_mq_update_nr_hw_queues()), and the usage counter reference taken by blk_queue_enter() is what keeps that freeze from completing. Read hctx_idx against nr_hw_queues only after that reference is held, otherwise a caller that slept through an update could index a stale queue_hw_ctx. - alloc_time_ns is meant to measure this request's own depth and tag wait, not time spent waiting for the queue to unfreeze. Measure it after blk_queue_enter() instead of before. No functional change: the only caller, nvme's __nvme_submit_sync_cmd(), still passes BLK_MQ_REQ_NOWAIT for every qid-targeted command (fabrics connect, in-band auth). Co-authored-by: Jaewoong Jung Signed-off-by: Minsik Jeon --- block/blk-mq.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 54ccfc43c27c..2f7298580e35 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -737,26 +737,31 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q, unsigned int tag; int ret; - /* alloc_time includes depth and tag waits */ + if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED))) + return ERR_PTR(-EINVAL); + + ret = blk_queue_enter(q, flags); + if (ret) + return ERR_PTR(ret); + + /* + * alloc_time includes depth and tag waits, but not any time spent + * waiting for the queue to unfreeze above: that wait does not belong + * to this request's allocation latency. + */ if (blk_queue_rq_alloc_time(q)) alloc_time_ns = blk_time_get_ns(); /* - * If the tag allocator sleeps we could get an allocation for a - * different hardware context. No need to complicate the low level - * allocator for this for the rare use case of a command tied to - * a specific queue. + * nr_hw_queues and queue_hw_ctx only change with the queue frozen, and + * the reference just acquired prevents that freeze from completing. + * Callers are no longer required to pass BLK_MQ_REQ_NOWAIT, so + * blk_queue_enter() may have slept through such a change - read both + * only now that the reference guarantees they are stable. */ - if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) || - WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED))) - return ERR_PTR(-EINVAL); - + ret = -EIO; if (hctx_idx >= q->nr_hw_queues) - return ERR_PTR(-EIO); - - ret = blk_queue_enter(q, flags); - if (ret) - return ERR_PTR(ret); + goto out_queue_exit; /* * Check if the hardware context is actually mapped to anything. @@ -779,6 +784,17 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q, if (flags & BLK_MQ_REQ_RESERVED) data.rq_flags |= RQF_RESV; + /* + * The tag allocation is the only part that must not sleep: + * blk_mq_get_tag() re-picks data.hctx after io_schedule(), so a + * sleeping allocation could hand back a request on a hardware context + * other than the one asked for. Guarantee that here instead of + * requiring it of every caller. Entering the queue above is a + * separate matter - waiting for an unfreeze does not influence hctx + * selection - and is left to the caller's flags. + */ + data.flags |= BLK_MQ_REQ_NOWAIT; + ret = -EWOULDBLOCK; tag = blk_mq_get_tag(&data); if (tag == BLK_MQ_NO_TAG) -- 2.52.0