[PATCH] blk-mq: update hctx->cpumask at cpu-hotplug

From: Wenbin Zeng
Date: Mon Jun 24 2019 - 11:24:27 EST


Currently hctx->cpumask is not updated when hot-plugging new cpus,
as there are many chances kblockd_mod_delayed_work_on() getting
called with WORK_CPU_UNBOUND, workqueue blk_mq_run_work_fn may run
on the newly-plugged cpus, consequently __blk_mq_run_hw_queue()
reporting excessive "run queue from wrong CPU" messages because
cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) returns false.

This patch added a cpu-hotplug handler into blk-mq, updating
hctx->cpumask at cpu-hotplug.

Signed-off-by: Wenbin Zeng <wenbinzeng@xxxxxxxxxxx>
---
block/blk-mq.c | 29 +++++++++++++++++++++++++++++
include/linux/blk-mq.h | 1 +
2 files changed, 30 insertions(+)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index ce0f5f4..2e465fc 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -39,6 +39,8 @@
#include "blk-mq-sched.h"
#include "blk-rq-qos.h"

+static enum cpuhp_state cpuhp_blk_mq_online;
+
static void blk_mq_poll_stats_start(struct request_queue *q);
static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);

@@ -2215,6 +2217,21 @@ int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
return -ENOMEM;
}

+static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
+{
+ struct blk_mq_hw_ctx *hctx;
+
+ hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_online);
+
+ if (!cpumask_test_cpu(cpu, hctx->cpumask)) {
+ mutex_lock(&hctx->queue->sysfs_lock);
+ cpumask_set_cpu(cpu, hctx->cpumask);
+ mutex_unlock(&hctx->queue->sysfs_lock);
+ }
+
+ return 0;
+}
+
/*
* 'cpu' is going away. splice any existing rq_list entries from this
* software queue to the hw queue dispatch list, and ensure that it
@@ -2251,6 +2268,9 @@ static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)

static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
{
+ if (cpuhp_blk_mq_online > 0)
+ cpuhp_state_remove_instance_nocalls(cpuhp_blk_mq_online,
+ &hctx->cpuhp_online);
cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
&hctx->cpuhp_dead);
}
@@ -2310,6 +2330,9 @@ static int blk_mq_init_hctx(struct request_queue *q,
{
hctx->queue_num = hctx_idx;

+ if (cpuhp_blk_mq_online > 0)
+ cpuhp_state_add_instance_nocalls(cpuhp_blk_mq_online,
+ &hctx->cpuhp_online);
cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);

hctx->tags = set->tags[hctx_idx];
@@ -3544,6 +3567,12 @@ unsigned int blk_mq_rq_cpu(struct request *rq)

static int __init blk_mq_init(void)
{
+ cpuhp_blk_mq_online = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+ "block/mq:online",
+ blk_mq_hctx_notify_online, NULL);
+ if (cpuhp_blk_mq_online <= 0)
+ pr_warn("blk_mq_init: failed to setup cpu online callback\n");
+
cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
blk_mq_hctx_notify_dead);
return 0;
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index 15d1aa5..5241659 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -58,6 +58,7 @@ struct blk_mq_hw_ctx {

atomic_t nr_active;

+ struct hlist_node cpuhp_online;
struct hlist_node cpuhp_dead;
struct kobject kobj;

--
1.8.3.1