From 6693953129c96668a266d9c55476eaf75eb3bb7b Mon Sep 17 00:00:00 2001 From: Minsik Jeon Date: Mon, 23 Nov 2026 16:24:17 +0900 Subject: [PATCH v2] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() BLK_MQ_REQ_NOWAIT is mandatory in blk_mq_alloc_request_hctx() only to keep blk_mq_get_tag() from re-picking data->hctx after io_schedule(). But the same flags value is passed on to blk_queue_enter(), so a caller also takes -EAGAIN for a transient freeze that has no bearing on hctx selection. There is no way to ask for the hctx guarantee without also opting out of waiting. Force BLK_MQ_REQ_NOWAIT on the tag allocation alone, right before blk_mq_get_tag(), and let blk_queue_enter() honour the caller's flags. nr_hw_queues/queue_hw_ctx and alloc_time_ns move after blk_queue_enter() now that it can sleep through such an update. Co-authored-by: Jaewoong Jung Signed-off-by: Minsik Jeon --- Changes since v1: - rewrite the commit message to explain the motivation instead of restating the diff - say in the code comment why the no-sleep rule is forced at the tag allocation rather than required from callers block/blk-mq.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 54ccfc43c27c..cf5d98bd13e3 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -737,26 +737,27 @@ 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 the unfreeze wait */ 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 need not 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 +780,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. Force it here rather than require + * BLK_MQ_REQ_NOWAIT from every caller, which would also make + * blk_queue_enter() above fail with -EAGAIN for a freeze that has no + * bearing on which hctx the request lands on. + */ + data.flags |= BLK_MQ_REQ_NOWAIT; + ret = -EWOULDBLOCK; tag = blk_mq_get_tag(&data); if (tag == BLK_MQ_NO_TAG) -- 2.52.0