Skip to contentSkip to conversation

blk-mq: call blk_mq_hctx_stopped in __blk_mq_run_hw_queue

PATCH In review Christoph Hellwig 4 participants 7 messages Apr 12, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 blk-mq: call blk_mq_hctx_stopped in __blk_mq_run_hw_queue

Instead of calling blk_mq_hctx_stopped in both callers, move it right next to the dispatching.

---
 block/blk-mq.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
block/blk-mq.c +6 -12
index 03c6fa4afcdb91..cdf1d5ca04bba2 100644
@@ -2150,6 +2150,8 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
 	 */
 	WARN_ON_ONCE(in_interrupt());
 
+	if (blk_mq_hctx_stopped(hctx))
+		return;
 	blk_mq_run_dispatch_ops(hctx->queue,
 			blk_mq_sched_dispatch_requests(hctx));
 }
@@ -2221,9 +2223,6 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
 static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
 					unsigned long msecs)
 {
-	if (unlikely(blk_mq_hctx_stopped(hctx)))
-		return;
-
 	if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
 		if (cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
 			__blk_mq_run_hw_queue(hctx);
@@ -2231,6 +2230,8 @@ static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
 		}
 	}
 
+	if (unlikely(blk_mq_hctx_stopped(hctx)))
+		return;
 	kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
 				    msecs_to_jiffies(msecs));
 }
@@ -2439,15 +2440,8 @@ EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
 
 static void blk_mq_run_work_fn(struct work_struct *work)
 {
-	struct blk_mq_hw_ctx *hctx;
-
-	hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
-
-	/*
-	 * If we are stopped, don't run the queue.
-	 */
-	if (blk_mq_hctx_stopped(hctx))
-		return;
+	struct blk_mq_hw_ctx *hctx =
+		container_of(work, struct blk_mq_hw_ctx, run_work.work);
 
 	__blk_mq_run_hw_queue(hctx);
 }
Signature · 1 line
2.39.2
#1 lore raw

PATCH 2/2 blk-mq: move the !async handling out of __blk_mq_delay_run_hw_queue

Only blk_mq_run_hw_queue can call __blk_mq_delay_run_hw_queue with async=false, so move the handling there.

With this __blk_mq_delay_run_hw_queue can be merged into blk_mq_delay_run_hw_queue.

---
 block/blk-mq.c | 38 +++++++++++---------------------------
 1 file changed, 11 insertions(+), 27 deletions(-)
block/blk-mq.c +11 -27
index cdf1d5ca04bba2..ff03f7d3c5c708 100644
@@ -2212,41 +2212,19 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
 }
 
 /**
- * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
+ * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
  * @hctx: Pointer to the hardware queue to run.
- * @async: If we want to run the queue asynchronously.
  * @msecs: Milliseconds of delay to wait before running the queue.
  *
- * If !@async, try to run the queue now. Else, run the queue asynchronously and
- * with a delay of @msecs.
+ * Run a hardware queue asynchronously with a delay of @msecs.
  */
-static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
-					unsigned long msecs)
+void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
 {
-	if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
-		if (cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
-			__blk_mq_run_hw_queue(hctx);
-			return;
-		}
-	}
-
 	if (unlikely(blk_mq_hctx_stopped(hctx)))
 		return;
 	kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
 				    msecs_to_jiffies(msecs));
 }
-
-/**
- * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
- * @hctx: Pointer to the hardware queue to run.
- * @msecs: Milliseconds of delay to wait before running the queue.
- *
- * Run a hardware queue asynchronously with a delay of @msecs.
- */
-void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
-{
-	__blk_mq_delay_run_hw_queue(hctx, true, msecs);
-}
 EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
 
 /**
@@ -2274,8 +2252,14 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
 		need_run = !blk_queue_quiesced(hctx->queue) &&
 		blk_mq_hctx_has_pending(hctx));
 
-	if (need_run)
-		__blk_mq_delay_run_hw_queue(hctx, async, 0);
+	if (!need_run)
+		return;
+
+	if (async || (hctx->flags & BLK_MQ_F_BLOCKING) ||
+	    !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask))
+		blk_mq_delay_run_hw_queue(hctx, 0);
+	else
+		__blk_mq_run_hw_queue(hctx);
 }
 EXPORT_SYMBOL(blk_mq_run_hw_queue);
 
Signature · 1 line
2.39.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] blk-mq: move the !async handling out of __blk_mq_delay_run_hw_queue

Quoted 7 lines · Christoph Hellwig

Only blk_mq_run_hw_queue can call __blk_mq_delay_run_hw_queue with async=false, so move the handling there.

With this __blk_mq_delay_run_hw_queue can be merged into blk_mq_delay_run_hw_queue.

Looks OK to me.

#4 lore raw
In reply to Christoph Hellwig
Quoted 13 lines · Christoph Hellwig

Instead of calling blk_mq_hctx_stopped in both callers, move it right next to the dispatching.

---
 block/blk-mq.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
block/blk-mq.c +0 -0
index 03c6fa4afcdb91..cdf1d5ca04bba2 100644

block/blk-mq.c static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) · line 2156

Quoted 7 lines
block/blk-mq.c +2 -0
@@ -2150,6 +2150,8 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
 	 */
 	WARN_ON_ONCE(in_interrupt());
 
+	if (blk_mq_hctx_stopped(hctx))
+		return;
 	blk_mq_run_dispatch_ops(hctx->queue,
 			blk_mq_sched_dispatch_requests(hctx));

The above new check isn't needed actually, since blk_mq_sched_dispatch_requests() does check it with rcu read lock held, which should be more reliable.

Thanks,
Ming

#5 lore raw
Quoted 7 lines · Ming Lei

block/blk-mq.c static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) · line 2156

Quoted 4 lines
block/blk-mq.c +2 -0
+	if (blk_mq_hctx_stopped(hctx))
+		return;
 	blk_mq_run_dispatch_ops(hctx->queue,
 			blk_mq_sched_dispatch_requests(hctx));

The above new check isn't needed actually, since blk_mq_sched_dispatch_requests() does check it with rcu read lock held, which should be more reliable.

The check is not new, it is just moved. But yes, we could probably
just remove it instead.