From 23100176c03fe71538f23cd2be79da248b46c6fc Mon Sep 17 00:00:00 2001 From: Aaron Tomlin Date: Wed, 21 Oct 2026 23:47:37 -0400 Subject: [PATCH] blk-mq: dispatch kworker to housekeeping CPU when hctx cpumask is empty Commit a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs") excluded isolated CPUs from hctx->cpumask to prevent worker threads from executing block work on isolated CPUs. When all CPUs mapped to a hardware queue are isolated (i.e. blk_mq_hctx_empty_cpumask(hctx) evaluates to true), blk_mq_hctx_next_cpu() returns WORK_CPU_UNBOUND with the intention of offloading execution. However, kblockd_workqueue is allocated as a per-CPU workqueue (WQ_PERCPU). When WORK_CPU_UNBOUND is queued on a per-CPU workqueue, the workqueue core resolves the target CPU via raw_smp_processor_id(). Consequently, if a task executing on an isolated CPU submits I/O and triggers an asynchronous queue dispatch with zero delay (e.g. upon hardware queue congestion, tag/resource exhaustion returning BLK_STS_RESOURCE, or plugging), &hctx->run_work is queued directly onto the local isolated CPU's worker pool. This causes the local per-CPU high-priority kworker (e.g. "kworker/6:1H-kblockd") to be scheduled on the isolated CPU. In real-time environments, an active SCHED_FIFO workload will starve this SCHED_NORMAL worker, causing hardware dispatch to stall, in addition to introducing unwanted latency jitter on nohz_full cores, thereby defeating the original objective of commit a46c27026da1. Introduce blk_mq_hctx_unbound_cpu() to select an online housekeeping CPU from housekeeping_cpumask(HK_TYPE_DOMAIN) via cpumask_any_and_distribute() when executing on an isolated CPU, and otherwise fall back to WORK_CPU_UNBOUND. Use this helper across the unbound fallback paths in blk_mq_hctx_next_cpu(). Additionally, for single-queue devices, gate the check behind the static key housekeeping_overridden under CONFIG_CPU_ISOLATION to ensure that common configurations without CPU isolation incur zero overhead on the fast path. This ensures that: 1. When executing on an isolated CPU, deferred queue work is cleanly offloaded to an online housekeeping CPU across single-queue devices, hardware queues with empty cpumasks, and offline CPU fallback paths. 2. The per-CPU high-priority kworker on the isolated CPU is not scheduled, avoiding priority inversion and latency jitter. 3. When executing on a non-isolated CPU, WORK_CPU_UNBOUND is preserved, maintaining optimal local execution with zero CPU bouncing while avoiding timer pinning hazards. 4. On systems without CPU isolation, single-queue devices maintain the zero-overhead fast path via a static key. Fixes: a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs") Cc: Ming Lei Cc: Jens Axboe Cc: stable@vger.kernel.org Signed-off-by: Aaron Tomlin --- block/blk-mq.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 5ae22d556540..676fe285a0e5 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2252,6 +2252,23 @@ static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx) return hctx->next_cpu >= nr_cpu_ids; } +static inline int blk_mq_hctx_unbound_cpu(void) +{ + guard(rcu)(); + + if (cpu_is_isolated(raw_smp_processor_id())) { + int cpu; + + guard(preempt)(); + cpu = cpumask_any_and_distribute(housekeeping_cpumask(HK_TYPE_DOMAIN), + cpu_online_mask); + if (cpu < nr_cpu_ids) + return cpu; + } + + return WORK_CPU_UNBOUND; +} + /* * It'd be great if the workqueue API had a way to pass * in a mask and had some smarts for more clever placement. @@ -2263,9 +2280,17 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) bool tried = false; int next_cpu = hctx->next_cpu; - /* Switch to unbound if no allowable CPUs in this hctx */ - if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx)) + if (hctx->queue->nr_hw_queues == 1) { +#ifdef CONFIG_CPU_ISOLATION + if (static_branch_unlikely(&housekeeping_overridden)) + return blk_mq_hctx_unbound_cpu(); +#endif return WORK_CPU_UNBOUND; + } + + /* Switch to unbound if no allowable CPUs in this hctx */ + if (blk_mq_hctx_empty_cpumask(hctx)) + return blk_mq_hctx_unbound_cpu(); if (--hctx->next_cpu_batch <= 0) { select_cpu: @@ -2292,7 +2317,7 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) */ hctx->next_cpu = next_cpu; hctx->next_cpu_batch = 1; - return WORK_CPU_UNBOUND; + return blk_mq_hctx_unbound_cpu(); } hctx->next_cpu = next_cpu;