Linux block layer
 help / color / mirror / Atom feed
* [PATCH v2] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
       [not found] <CGME20261125072500epcms2p4dbf32261de0ff182820c3a5ae8daec38@epcms2p4>
@ 2026-11-25  7:25 ` 전민식
  2026-11-25  7:32   ` hch
  0 siblings, 1 reply; 2+ messages in thread
From: 전민식 @ 2026-11-25  7:25 UTC (permalink / raw)
  To: hch@infradead.org, axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, 전민식

From 6693953129c96668a266d9c55476eaf75eb3bb7b Mon Sep 17 00:00:00 2001
From: Minsik Jeon <hmi.jeon@samsung.com>
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 <jaew.jung@samsung.com>
Signed-off-by: Minsik Jeon <hmi.jeon@samsung.com>
---
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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
  2026-11-25  7:25 ` [PATCH v2] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() 전민식
@ 2026-11-25  7:32   ` hch
  0 siblings, 0 replies; 2+ messages in thread
From: hch @ 2026-11-25  7:32 UTC (permalink / raw)
  To: 전민식
  Cc: hch@infradead.org, axboe@kernel.dk, linux-block@vger.kernel.org

On Wed, Nov 25, 2026 at 04:25:00PM +0900, 전민식 wrote:
> >From 6693953129c96668a266d9c55476eaf75eb3bb7b Mon Sep 17 00:00:00 2001
> From: Minsik Jeon <hmi.jeon@samsung.com>
> 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()

Looks like the headers got duplicated into the message body.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  7:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20261125072500epcms2p4dbf32261de0ff182820c3a5ae8daec38@epcms2p4>
2026-11-25  7:25 ` [PATCH v2] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() 전민식
2026-11-25  7:32   ` hch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox