[PATCH] blk-mq: add module parameter to not run block kworker on isolated CPUs

From: Ming Lei
Date: Tue Oct 10 2023 - 10:23:42 EST


Kernel parameter of `isolcpus=` is used for isolating CPUs for specific
task, and user often won't want block IO to disturb these CPUs, also long
IO latency may be caused if blk-mq kworker is scheduled on these isolated
CPUs.

Kernel workqueue only respects this limit for WQ_UNBOUND, for bound wq,
the responsibility should be on wq user.

Add one block layer parameter for not running block kworker on isolated
CPUs.

Cc: Juri Lelli <juri.lelli@xxxxxxxxxx>
Cc: Andrew Theurer <atheurer@xxxxxxxxxx>
Cc: Joe Mario <jmario@xxxxxxxxxx>
Cc: Sebastian Jug <sejug@xxxxxxxxxx>
Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx>
---
block/blk-mq.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index ec922c6bccbe..c53b5b522053 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -29,6 +29,7 @@
#include <linux/prefetch.h>
#include <linux/blk-crypto.h>
#include <linux/part_stat.h>
+#include <linux/sched/isolation.h>

#include <trace/events/block.h>

@@ -42,6 +43,13 @@
#include "blk-rq-qos.h"
#include "blk-ioprio.h"

+static bool respect_cpu_isolation;
+module_param(respect_cpu_isolation, bool, 0444);
+MODULE_PARM_DESC(respect_cpu_isolation,
+ "Don't schedule blk-mq worker on isolated CPUs passed in "
+ "isolcpus= or nohz_full=. User need to guarantee to not run "
+ "block IO on isolated CPUs (default: false)");
+
static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
static DEFINE_PER_CPU(call_single_data_t, blk_cpu_csd);

@@ -3926,6 +3934,13 @@ static void blk_mq_map_swqueue(struct request_queue *q)
*/
sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);

+ if (respect_cpu_isolation) {
+ cpumask_and(hctx->cpumask, hctx->cpumask,
+ housekeeping_cpumask(HK_TYPE_DOMAIN));
+ cpumask_and(hctx->cpumask, hctx->cpumask,
+ housekeeping_cpumask(HK_TYPE_WQ));
+ }
+
/*
* Initialize batch roundrobin counts
*/
--
2.41.0