blk-mq: dispatch kworker to housekeeping CPU when hctx cpumask is empty
Subsystems (2)
PATCH v5 1/1 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 (for instance, 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 schedules the local per-CPU high-priority kworker (such as kworker/6:1H-kblockd) on the isolated CPU. In real-time environments, an active SCHED_FIFO workload will starve this SCHED_NORMAL worker and stall hardware dispatch, whilst also introducing unwanted latency jitter on nohz_full cores, defeating that commit's original objective.
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 return WORK_CPU_UNBOUND. Gate the helper behind the static key housekeeping_overridden under CONFIG_CPU_ISOLATION so that common configurations without CPU isolation incur zero overhead across single-queue devices, hardware queues with empty cpumasks, and offline CPU fallback paths, whilst non-isolated CPUs preserve local execution via WORK_CPU_UNBOUND. Guard the slow-path check and cpumask selection with guard(preempt)() to pin raw_smp_processor_id(), satisfy RCU dereferencing of housekeeping_cpumask(HK_TYPE_DOMAIN), and protect the per-CPU distribution state.
---
Note: As Frederic Weisbecker pointed out on v4, workqueue-level support (WQ_PREFER_PERCPU) is currently in development to handle isolated CPUs automatically, at which point kblockd_workqueue will be a candidate for simplification; in the meantime, this provides a minimal fix suitable for backporting.
Changes since v4:
- Rebased against v7.4-rc1
- Hoisted #ifdef CONFIG_CPU_ISOLATION and static_branch_unlikely(&housekeeping_overridden) into blk_mq_hctx_unbound_cpu() so blk_mq_hctx_next_cpu() retains a single fast-path check and non-isolated configurations avoid any RCU or preemption guards across all unbound fallback paths
- Moved guard(preempt)() ahead of cpu_is_isolated(raw_smp_processor_id()) to prevent migration between reading the local CPU ID and selecting a housekeeping CPU under CONFIG_PREEMPT_RCU, whilst satisfying rcu_dereference_all_check() without a separate guard(rcu)()
- Consolidated the commit message summary into prose
- Link to v4: https://lore.kernel.org/lkml/[email protected]/
Changes since v3:
- Create companion blktests test case (block/050) to reproduce this issue and verify the fix (Daniel Wagner)
- Gate the single-queue (nr_hw_queues == 1) check behind the static key housekeeping_overridden under CONFIG_CPU_ISOLATION to ensure the common case without CPU isolation incurs zero overhead on the fast path (Jens Axboe)
- Wrap the evaluation of cpu_is_isolated() inside guard(rcu)() in blk_mq_hctx_unbound_cpu() to prevent an RCU lockdep warning and ensure safe dereferencing of the housekeeping cpumask while concurrent cpuset updates occur
- Disable preemption via guard(preempt)() around cpumask_any_and_distribute() to prevent a preemption warning under CONFIG_PREEMPT_RCU and avoid data races when accessing per-CPU distribution state
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes since v2:
- Query housekeeping_cpumask(HK_TYPE_DOMAIN) instead of HK_TYPE_TIMER to align with cpu_is_isolated(), workqueue affinity rules, and cpuset isolated partitions
- Query the housekeeping cpumask under guard(rcu)() to safely handle dynamic cpuset updates without lockdep warnings
- Validate that cpumask_any_and_distribute() returns a valid cpu ID before returning, falling back cleanly to WORK_CPU_UNBOUND
- Link to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Introduce the blk_mq_hctx_unbound_cpu() helper to steer deferred work to an online housekeeping CPU only when executing on an isolated core, preserving WORK_CPU_UNBOUND and local cache locality for housekeeping cores
- Apply blk_mq_hctx_unbound_cpu() across all unbound fallback paths in blk_mq_hctx_next_cpu(), comprehensively covering single-queue devices (nr_hw_queues == 1), empty hardware queue cpumasks, and the offline CPU fallback path.
- Elaborate in the commit log on the triggers for asynchronous queue dispatch (e.g. tag exhaustion, BLK_STS_RESOURCE backpressure, and plugging) and the SCHED_FIFO worker starvation hazard on isolated cores
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
--- block/blk-mq.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
block/blk-mq.c +21 -2
@@ -2252,6 +2252,25 @@ 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 ) +{ +#ifdef CONFIG_CPU_ISOLATION +if (static_branch_unlikely (&housekeeping_overridden)) { +int cpu; + +guard (preempt)(); +if (!cpu_is_isolated (raw_smp_processor_id ())) +return WORK_CPU_UNBOUND ; + + cpu =cpumask_any_and_distribute (housekeeping_cpumask (HK_TYPE_DOMAIN ), + cpu_online_mask); +if (cpu < nr_cpu_ids) +return cpu; + } +#endif +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.
@@ -2265,7 +2284,7 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)/* Switch to unbound if no allowable CPUs in this hctx */ if (hctx->queue->nr_hw_queues ==1 ||blk_mq_hctx_empty_cpumask (hctx)) -return WORK_CPU_UNBOUND ; +return blk_mq_hctx_unbound_cpu ();if (--hctx->next_cpu_batch <=0 ) { select_cpu:
@@ -2292,7 +2311,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;
Signature · 1 line
2.55.0
Hi Aaron,
Quoted 5 lines · Aaron Tomlin
[...] In real-time environments, an active
SCHED_FIFOworkload will starve thisSCHED_NORMALworker and stall hardware dispatch, whilst also introducing unwanted latency jitter onnohz_fullcores, defeating that commit's original objective.
I would phrase this differently. cpu_is_isolated() checks HK_TYPE_DOMAIN, so the jitter argument holds only when the nohz_full CPU is also domain isolated (isolcpus=domain or an isolated cpuset partition). With nohz_full alone, or isolcpus=nohz without domain, this patch does not apply and the behavior is unchanged.
block/blk-mq.c static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx) · line 2263
Quoted 9 lines
block/blk-mq.c +9
-0+static inline int blk_mq_hctx_unbound_cpu (void ) +{ +#ifdef CONFIG_CPU_ISOLATION +if (static_branch_unlikely (&housekeeping_overridden)) { +int cpu; + +guard (preempt)(); +if (!cpu_is_isolated (raw_smp_processor_id ())) +return WORK_CPU_UNBOUND ;
Since preemption is disabled by guard(preempt)(), you can use smp_processor_id() here instead of raw_smp_processor_id().
Thanks!
Signature · 3 lines
Marco Crivellari SUSE Labs
Quoted 1 line · Marco Crivellari
Hi Aaron,
Hi Marco,
Quoted 8 lines · Aaron Tomlin
Quoted 5 lines
[...] In real-time environments, an active
SCHED_FIFOworkload will starve thisSCHED_NORMALworker and stall hardware dispatch, whilst also introducing unwanted latency jitter onnohz_fullcores, defeating that commit's original objective.
Quoted 5 lines
I would phrase this differently.
cpu_is_isolated()checksHK_TYPE_DOMAIN, so the jitter argument holds only when thenohz_fullCPU is also domain isolated (isolcpus=domain or an isolated cpuset partition). Withnohz_fullalone, or isolcpus=nohz without domain, this patch does not apply and the behavior is unchanged.
Agreed, that distinction is much clearer. Since both commit a46c27026da1 and blk_mq_hctx_unbound_cpu() test cpu_is_isolated() (HK_TYPE_DOMAIN), CPUs configured solely with nohz_full= or isolcpus=nohz without domain isolation (or an isolated cpuset partition) remain unaffected. I will reword the commit log in the next iteration to clarify that this applies specifically to domain-isolated CPUs (including when combined with nohz_full).
Quoted 13 lines
block/blk-mq.c static bool blk_mq_hctx_empty_cpumask(struct blk_mq_hw_ctx *hctx) · line 2263
Quoted 10 lines
block/blk-mq.c +9
-0+static inline int blk_mq_hctx_unbound_cpu (void ) +{ +#ifdef CONFIG_CPU_ISOLATION +if (static_branch_unlikely (&housekeeping_overridden)) { +int cpu; + +guard (preempt)(); +if (!cpu_is_isolated (raw_smp_processor_id ())) +return WORK_CPU_UNBOUND ;Since preemption is disabled by guard(preempt)(), you can use
smp_processor_id()here instead ofraw_smp_processor_id().
Oops! raw_smp_processor_id() was a leftover from v4 before guard(preempt)() was moved ahead of cpu_is_isolated(). I will switch to smp_processor_id() in the next iteration.
Thank you for the review!
Kind regards,
Signature · 1 line
Aaron Tomlin