* [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
[not found] <CGME20261124002518epcms2p847d3246dc989469f326f7a2b47f2f950@epcms2p8>
@ 2026-11-24 0:25 ` 전민식
2026-11-25 4:55 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: 전민식 @ 2026-11-24 0:25 UTC (permalink / raw)
To: axboe@kernel.dk; +Cc: linux-block@vger.kernel.org, 전민식
From 3f2a79888ada5c811151bc5c59a1c7f59c8f7b3b 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] 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 <jaew.jung@samsung.com>
Signed-off-by: Minsik Jeon <hmi.jeon@samsung.com>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
2026-11-24 0:25 ` [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() 전민식
@ 2026-11-25 4:55 ` Christoph Hellwig
2026-11-25 6:53 ` RE(2): " 전민식
0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2026-11-25 4:55 UTC (permalink / raw)
To: 전민식; +Cc: axboe@kernel.dk, linux-block@vger.kernel.org
This commit messages reiterates in annoying detail what the change does,
which is mostly obvious from the code. It entirely fails to explain
why you want to make this change.
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE(2): [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
2026-11-25 4:55 ` Christoph Hellwig
@ 2026-11-25 6:53 ` 전민식
2026-11-25 6:58 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: 전민식 @ 2026-11-25 6:53 UTC (permalink / raw)
To: Christoph Hellwig
Cc: axboe@kernel.dk, linux-block@vger.kernel.org,
전민식
On Wed, 25 Nov 2026 at 13:55 +0900, Christoph Hellwig wrote:
> This commit messages reiterates in annoying detail what the change does,
> which is mostly obvious from the code. It entirely fails to explain
> why you want to make this change.
Fair. The motivation I left out: 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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: RE(2): [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx()
2026-11-25 6:53 ` RE(2): " 전민식
@ 2026-11-25 6:58 ` Christoph Hellwig
0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-11-25 6:58 UTC (permalink / raw)
To: 전민식
Cc: Christoph Hellwig, axboe@kernel.dk, linux-block@vger.kernel.org
On Wed, Nov 25, 2026 at 03:53:32PM +0900, 전민식 wrote:
> On Wed, 25 Nov 2026 at 13:55 +0900, Christoph Hellwig wrote:
> > This commit messages reiterates in annoying detail what the change does,
> > which is mostly obvious from the code. It entirely fails to explain
> > why you want to make this change.
>
> Fair. The motivation I left out: 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.
Please put this into the commit log, and given that it is not obvious
maybe also into a code comment. Please also describe if this improves
a workload you care about or if you just found it by code inspection.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-25 6:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <CGME20261124002518epcms2p847d3246dc989469f326f7a2b47f2f950@epcms2p8>
2026-11-24 0:25 ` [PATCH] blk-mq: scope the no-sleep requirement in blk_mq_alloc_request_hctx() 전민식
2026-11-25 4:55 ` Christoph Hellwig
2026-11-25 6:53 ` RE(2): " 전민식
2026-11-25 6:58 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox