Re: [PATCH] blk-mq: fix null-ptr-deref in blk_mq_run_hw_queues
From: Bart Van Assche
Date: Wed Dec 02 2026 - 19:10:32 EST
On 12/2/26 2:48 PM, Jens Axboe wrote:
On 12/2/26 8:13 AM, Peiyang He wrote:
diff --git a/block/blk-mq.c b/block/blk-mq.c
index 54ccfc43c27c..bf129a6a77b5 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -2412,6 +2412,14 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
struct blk_mq_hw_ctx *hctx, *sq_hctx;
unsigned long i;
+ /*
+ * __blk_mq_update_nr_hw_queues() updates nr_hw_queues and queue_hw_ctx
+ * while the queue is frozen. So we can use q_usage_counter to avoid
+ * racing with it.
+ */
+ if (!percpu_ref_tryget(&q->q_usage_counter))
+ return;
+
sq_hctx = NULL;
if (blk_queue_sq_sched(q))
sq_hctx = blk_mq_get_sq_hctx(q);
@@ -2427,6 +2435,7 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async)
!list_empty_careful(&hctx->dispatch))
blk_mq_run_hw_queue(hctx, async);
}
+ blk_queue_exit(q);
}
EXPORT_SYMBOL(blk_mq_run_hw_queues);
That's a hard pass, adding overhead to a fast path for a corner case in one
driver. Yes we use this in tag iteration, but that's not at all a fast path.
Please rethink this approach.
Hi Jens,
Would wrapping the blk_mq_run_hw_queues() body with
__blk_mq_run_dispatch_ops() be acceptable? The implementation of that
macro is as follows:
/* run the code block in @dispatch_ops with rcu/srcu read lock held */
#define __blk_mq_run_dispatch_ops(q, check_sleep, dispatch_ops) \
do { \
if ((q)->tag_set->flags & BLK_MQ_F_BLOCKING) { \
struct blk_mq_tag_set *__tag_set = (q)->tag_set; \
int srcu_idx; \
\
might_sleep_if(check_sleep); \
srcu_idx = srcu_read_lock(__tag_set->srcu); \
(dispatch_ops); \
srcu_read_unlock(__tag_set->srcu, srcu_idx); \
} else { \
rcu_read_lock(); \
(dispatch_ops); \
rcu_read_unlock(); \
} \
} while (0)
Please note that this change by itself is not sufficient - code that
modifies the number of hardware queues will have to be modified too.
Thanks,
Bart.