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

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>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
Changes since v2:
- resend only; the v2 mail had the patch headers pasted into the message
  body. No changes to the code or the commit message.
 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 v3] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
  2026-11-25  7:39 ` [PATCH v3] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() 전민식
@ 2026-11-25  8:11   ` Hannes Reinecke
  0 siblings, 0 replies; 2+ messages in thread
From: Hannes Reinecke @ 2026-11-25  8:11 UTC (permalink / raw)
  To: hmi.jeon, hch@infradead.org, axboe@kernel.dk,
	linux-block@vger.kernel.org
  Cc: 정재웅

On 11/25/26 8:39 AM, 전민식 wrote:
> 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>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> ---
> Changes since v2:
> - resend only; the v2 mail had the patch headers pasted into the message
>    body. No changes to the code or the commit message.
>   block/blk-mq.c | 40 ++++++++++++++++++++++++++--------------
>   1 file changed, 26 insertions(+), 14 deletions(-)
> 
Reviewed-by: Hannes Reinecke <hare@kernel.org>

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare@suse.de                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich

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

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

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

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