Skip to contentSkip to conversation

blk-mq: two patches related with CPU hotplug

PATCH Applied Ming Lei 2 participants 4 messages Jan 17, 2018 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v2 cover blk-mq: two patches related with CPU hotplug

Hi Jens,

The 1st one fixes one regression caused by 20e4d81393 ("blk-mq: simplify queue mapping & schedule with each possisble CPU").

The 2nd one add comments for current two races, and convert the WARN_ON into printk(KERN_WARN) with dump_stack() since this warning is harmless.

V2:
- fix comment and commit log in patch 1
- use dump_stack() with printk to replace WARN_ON() in patch 2

Thanks,

Ming Lei (2):
blk-mq: make sure hctx->next_cpu is set correctly
blk-mq: avoid one WARN_ON in __blk_mq_run_hw_queue to printk

 block/blk-mq.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 48 insertions(+), 4 deletions(-)
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH v2 1/2 blk-mq: make sure hctx->next_cpu is set correctly

When hctx->next_cpu is set from possible online CPUs, there is one race in which hctx->next_cpu may be set as >= nr_cpu_ids, and finally break workqueue.

The race can be triggered in the following two sitations:

1) when one CPU is becoming DEAD, blk_mq_hctx_notify_dead() is called to dispatch requests from the DEAD cpu context, but at that time, this DEAD CPU has been cleared from 'cpu_online_mask', so all CPUs in hctx->cpumask may become offline, and cause hctx->next_cpu set a bad value.

2) blk_mq_delay_run_hw_queue() is called from CPU B, and found the queue should be run on the other CPU A, then CPU A may become offline at the same time and all CPUs in hctx->cpumask become offline.

This patch deals with this issue by re-selecting next CPU, and make sure it is set correctly.

Cc Christian Borntraeger <[email protected]>
Cc Stefan Haberland <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Thomas Gleixner <[email protected]>
Fixes 20e4d81393 ("blk-mq: simplify queue mapping & schedule with each possisble CPU")
---
 block/blk-mq.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
block/blk-mq.c +28 -2
index c376d1b6309a..69e73b4e32f3 100644
@@ -1416,21 +1416,47 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
  */
 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
 {
+	bool tried = false;
+
 	if (hctx->queue->nr_hw_queues == 1)
 		return WORK_CPU_UNBOUND;
 
 	if (--hctx->next_cpu_batch <= 0) {
 		int next_cpu;
-
+select_cpu:
 		next_cpu = cpumask_next_and(hctx->next_cpu, hctx->cpumask,
 				cpu_online_mask);
 		if (next_cpu >= nr_cpu_ids)
 			next_cpu = cpumask_first_and(hctx->cpumask,cpu_online_mask);
 
-		hctx->next_cpu = next_cpu;
+		/*
+		 * No online CPU is found, so have to make sure hctx->next_cpu
+		 * is set correctly for not breaking workqueue.
+		 */
+		if (next_cpu >= nr_cpu_ids)
+			hctx->next_cpu = cpumask_first(hctx->cpumask);
+		else
+			hctx->next_cpu = next_cpu;
 		hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
 	}
 
+	/*
+	 * Do unbound schedule if we can't find a online CPU for this hctx,
+	 * and it should only happen in the path of handling CPU DEAD.
+	 */
+	if (!cpu_online(hctx->next_cpu)) {
+		if (!tried) {
+			tried = true;
+			goto select_cpu;
+		}
+
+		/*
+		 * Make sure to re-select CPU next time once after CPUs
+		 * in hctx->cpumask become online again.
+		 */
+		hctx->next_cpu_batch = 1;
+		return WORK_CPU_UNBOUND;
+	}
 	return hctx->next_cpu;
 }
 
Signature · 1 line
2.9.5
author
#2 lore raw

PATCH v2 2/2 blk-mq: avoid one WARN_ON in __blk_mq_run_hw_queue to printk

In reply to Ming Lei

We know this WARN_ON is harmless and in reality it may be trigged, so convert it to printk() and dump_stack() for avoiding to confuse people.

Also add comment about two releated races here.

Cc Christian Borntraeger <[email protected]>
Cc Stefan Haberland <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "jianchao.wang" <[email protected]>
---
 block/blk-mq.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
block/blk-mq.c +20 -2
index 69e73b4e32f3..480fc7623a5e 100644
@@ -1391,9 +1391,27 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
 	/*
 	 * We should be running this queue from one of the CPUs that
 	 * are mapped to it.
+	 *
+	 * There are at least two related races now between setting
+	 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
+	 * __blk_mq_run_hw_queue():
+	 *
+	 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
+	 *   but later it becomes online, then this warning is harmless
+	 *   at all
+	 *
+	 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
+	 *   but later it becomes offline, then the warning can't be
+	 *   triggered, and we depend on blk-mq timeout handler to
+	 *   handle dispatched requests to this hctx
 	 */
-	WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
-		cpu_online(hctx->next_cpu));
+	if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
+		cpu_online(hctx->next_cpu)) {
+		printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
+			raw_smp_processor_id(),
+			cpumask_empty(hctx->cpumask) ? "inactive": "active");
+		dump_stack();
+	}
 
 	/*
 	 * We can't run the queue inline with ints disabled. Ensure that
Signature · 1 line
2.9.5
maintainer Applied
#3 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

Hi Jens,

The 1st one fixes one regression caused by 20e4d81393 ("blk-mq: simplify queue mapping & schedule with each possisble CPU").

The 2nd one add comments for current two races, and convert the WARN_ON into printk(KERN_WARN) with dump_stack() since this warning is harmless.

V2:
- fix comment and commit log in patch 1
- use dump_stack() with printk to replace WARN_ON() in patch 2

Applied, thanks.

Signature · 1 line
Jens Axboe