[PATCH] blk-mq: Mark concurrent accesses to dispatch_busy
From: Zhu Jun
Date: Wed Nov 11 2026 - 21:37:03 EST
Dispatching on different CPUs updates the same hardware context's
dispatch_busy estimate without a common lock. Scheduling and debugfs also
read it concurrently, as reported by KCSAN.
Use READ_ONCE() and WRITE_ONCE() for the shared estimate. The EWMA remains
an approximate dispatch hint; concurrent updates do not need to preserve
every observation.
OpenAI Codex assisted with analysis and patch preparation.
Fixes: 6e768717304b ("blk-mq: dequeue request one by one from sw queue if hctx is busy")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
block/blk-mq-debugfs.c | 2 +-
block/blk-mq-sched.c | 4 ++--
block/blk-mq.c | 8 ++++----
3 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/block/blk-mq-debugfs.c b/block/blk-mq-debugfs.c
index 6754d8f9449c..d8b4e3b606e6 100644
--- a/block/blk-mq-debugfs.c
+++ b/block/blk-mq-debugfs.c
@@ -488,7 +488,7 @@ static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
{
struct blk_mq_hw_ctx *hctx = data;
- seq_printf(m, "%u\n", hctx->dispatch_busy);
+ seq_printf(m, "%u\n", READ_ONCE(hctx->dispatch_busy));
return 0;
}
diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
index d7b45778732b..66e03b676838 100644
--- a/block/blk-mq-sched.c
+++ b/block/blk-mq-sched.c
@@ -92,7 +92,7 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
LIST_HEAD(rq_list);
int count = 0;
- if (hctx->dispatch_busy)
+ if (READ_ONCE(hctx->dispatch_busy))
max_dispatch = 1;
else
max_dispatch = hctx->queue->nr_requests;
@@ -300,7 +300,7 @@ static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
return 0;
need_dispatch = true;
} else {
- need_dispatch = hctx->dispatch_busy;
+ need_dispatch = READ_ONCE(hctx->dispatch_busy);
}
if (hctx->queue->elevator)
diff --git a/block/blk-mq.c b/block/blk-mq.c
index 5ae22d556540..322bad6896d4 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -2007,7 +2007,7 @@ static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
{
unsigned int ewma;
- ewma = hctx->dispatch_busy;
+ ewma = READ_ONCE(hctx->dispatch_busy);
if (!ewma && !busy)
return;
@@ -2017,7 +2017,7 @@ static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
- hctx->dispatch_busy = ewma;
+ WRITE_ONCE(hctx->dispatch_busy, ewma);
}
#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
@@ -2587,7 +2587,7 @@ static void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx,
* Try to issue requests directly if the hw queue isn't busy to save an
* extra enqueue & dequeue to the sw queue.
*/
- if (!hctx->dispatch_busy && !run_queue_async) {
+ if (!READ_ONCE(hctx->dispatch_busy) && !run_queue_async) {
blk_mq_run_dispatch_ops(hctx->queue,
blk_mq_try_issue_list_directly(hctx, list));
if (list_empty(list))
@@ -3235,7 +3235,7 @@ void blk_mq_submit_bio(struct bio *bio)
hctx = rq->mq_hctx;
if ((rq->rq_flags & RQF_USE_SCHED) ||
- (hctx->dispatch_busy && (q->nr_hw_queues == 1 || !is_sync))) {
+ (READ_ONCE(hctx->dispatch_busy) && (q->nr_hw_queues == 1 || !is_sync))) {
blk_mq_insert_request(rq, 0);
blk_mq_run_hw_queue(hctx, true);
} else {
--
2.51.0