Skip to contentSkip to conversation

blk-mq: dispatch kworker to housekeeping CPU when hctx cpumask is empty

PATCH Superseded Aaron Tomlin 3 participants 11 messages Oct 12 – Oct 15

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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, &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, introducing unwanted latency jitter and execution delays, thereby defeating the original objective of commit a46c27026da1.

Rectify this by selecting an online housekeeping CPU via housekeeping_any_cpu(HK_TYPE_TIMER) when blk_mq_hctx_empty_cpumask(hctx) is true. This ensures that:
1. The delayed work is cleanly offloaded to a housekeeping CPU.

2. The per-CPU high-priority kworker on the isolated CPU is not scheduled.

3. Hardware queues with valid mapped CPUs preserve their per-CPU locality and batch round-robin behaviour.

Fixes a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs")
Cc Ming Lei <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +5 -2
index 025a799f35b9..9480c3aac898 100644
@@ -2252,8 +2252,11 @@ 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))
+	/* Switch to housekeeping CPU if no allowable CPUs in this hctx */
+	if (blk_mq_hctx_empty_cpumask(hctx))
+		return housekeeping_any_cpu(HK_TYPE_TIMER);
+
+	if (hctx->queue->nr_hw_queues == 1)
 		return WORK_CPU_UNBOUND;
 
 	if (--hctx->next_cpu_batch <= 0) {
Signature · 1 line
2.55.0
#1 lore raw
Quoted 11 lines · Aaron Tomlin

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().

Then it looks like you need to fix WORK_CPU_UNBOUND, which has more wide uses, if you think it is one problem.

Quoted 3 lines

Consequently, if a task executing on an isolated CPU submits I/O and triggers an asynchronous queue dispatch with zero delay, &hctx->run_work is queued directly onto the local isolated CPU's worker pool. This

If your theory is correct, why do botter to change to housekeeping_any_cpu(HK_TYPE_TIMER)
if the user is submitting IO from an isolated CPU?

Quoted 31 lines

causes the local per-CPU high-priority kworker
(e.g. "kworker/6:1H-kblockd") to be scheduled on the isolated CPU, introducing unwanted latency jitter and execution delays, thereby defeating the original objective of commit a46c27026da1.

Rectify this by selecting an online housekeeping CPU via housekeeping_any_cpu(HK_TYPE_TIMER) when blk_mq_hctx_empty_cpumask(hctx) is true. This ensures that:
1. The delayed work is cleanly offloaded to a housekeeping CPU.

2. The per-CPU high-priority kworker on the isolated CPU is not scheduled.

3. Hardware queues with valid mapped CPUs preserve their per-CPU locality and batch round-robin behaviour.

Fixes a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs")
Cc Ming Lei <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/blk-mq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-mq.c +0 -0
index 025a799f35b9..9480c3aac898 100644
@@ -2252,8 +2252,11 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
        bool tried = false;

block/blk-mq.c static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) · line 2260

Quoted 10 lines
block/blk-mq.c +5 -2
        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))
+       /* Switch to housekeeping CPU if no allowable CPUs in this hctx */
+       if (blk_mq_hctx_empty_cpumask(hctx))
+               return housekeeping_any_cpu(HK_TYPE_TIMER);
+
+       if (hctx->queue->nr_hw_queues == 1)
                return WORK_CPU_UNBOUND;

Why is SQ so special here to keep WORK_CPU_UNBOUND?

Thanks,
Ming Lei

#2 lore raw
Quoted 9 lines · Ming Lei
Quoted 8 lines · Aaron Tomlin

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.

Quoted 6 lines
Quoted 3 lines

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().

Then it looks like you need to fix WORK_CPU_UNBOUND, which has more wide uses, if you think it is one problem.

Hi Ming,

Since kblockd_workqueue is WQ_PERCPU, the responsibility rests with blk-mq to provide a non-isolated CPU when dispatching &hctx->run_work, no?

Quoted 7 lines
Quoted 3 lines

Consequently, if a task executing on an isolated CPU submits I/O and triggers an asynchronous queue dispatch with zero delay, &hctx->run_work is queued directly onto the local isolated CPU's worker pool. This

If your theory is correct, why do botter to change to housekeeping_any_cpu(HK_TYPE_TIMER)
if the user is submitting IO from an isolated CPU?

I suspect, steering the asynchronous dispatch to a housekeeping CPU is both architecturally necessary and fully aligned with the rest of the kernel, no? When a queue is plugged, throttled, or returns BLK_STS_RESOURCE, blk-mq defers execution to a background workqueue. That work runs asynchronously at some indeterminate time in the future, long after the thread has returned to its time-critical user loop. Having a kworker suddenly preempt the thread later is likely not desired.

Quoted 8 lines
Quoted 8 lines
block/blk-mq.c +0 -0
index 025a799f35b9..9480c3aac898 100644
@@ -2252,8 +2252,11 @@ static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
        bool tried = false;
        int next_cpu = hctx->next_cpu;
 
Quoted 10 lines

block/blk-mq.c static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) · line 2260

Quoted 8 lines
block/blk-mq.c +5 -2
-       /* Switch to unbound if no allowable CPUs in this hctx */
-       if (hctx->queue->nr_hw_queues == 1 || blk_mq_hctx_empty_cpumask(hctx))
+       /* Switch to housekeeping CPU if no allowable CPUs in this hctx */
+       if (blk_mq_hctx_empty_cpumask(hctx))
+               return housekeeping_any_cpu(HK_TYPE_TIMER);
+
+       if (hctx->queue->nr_hw_queues == 1)
                return WORK_CPU_UNBOUND;

Why is SQ so special here to keep WORK_CPU_UNBOUND?

Indeed, it is not special here, and neither is the offline CPU fallback path (!cpu_online(next_cpu)). Both currently return WORK_CPU_UNBOUND and suffer from the exact same issue if triggered from an isolated CPU.

To address this consistently without penalising non-isolated CPUs with unnecessary cross-CPU bounces, we can define a small helper. Furthermore, the following will address the valid issue raised by Sashiko [1] comprehensively, using the new helper:
1. When executing on an isolated CPU cpu_is_isolated(raw_smp_processor_id()) evaluates to true. It redirects to an online housekeeping CPU (housekeeping_any_cpu(HK_TYPE_TIMER)), preventing kworkers from running on isolated cores across all three cases:
- Single-queue devices (nr_hw_queues == 1)
- Hardware queues with empty cpumasks (blk_mq_hctx_empty_cpumask(hctx))
- Offline CPU fallback paths (!cpu_online(next_cpu))

2. When executing on a housekeeping CPU cpu_is_isolated() is false. It returns WORK_CPU_UNBOUND, which kblockd_workqueue executes on the local CPU (raw_smp_processor_id()), preserving optimal cache locality with zero CPU bouncing.

3. When CPU isolation is not configured cpu_is_isolated() uses a static branch (static_branch_unlikely(&housekeeping_overridden)), compiling to a NOP that returns WORK_CPU_UNBOUND with zero overhead.

[1]: https://sashiko.dev/#/patchset/20261012012905.267920-1-atomlin%40atomlin.com

block/blk-mq.c +9 -2
index e28c467a18bb..9fb1a93b2bb1 100644
@@ -2244,6 +2244,13 @@ 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)
+{
+    if (cpu_is_isolated(raw_smp_processor_id()))
+        return housekeeping_any_cpu(HK_TYPE_TIMER);
+    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.
@@ -2256,7 +2263,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:
@@ -2281,7 +2288,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;

Kind regards,

Signature · 1 line
Aaron Tomlin
#3 lore raw
Quoted 14 lines · Aaron Tomlin
Quoted 13 lines · Ming Lei
Quoted 11 lines · Aaron Tomlin

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().

Quoted 7 lines
Quoted 2 lines

Then it looks like you need to fix WORK_CPU_UNBOUND, which has more wide uses, if you think it is one problem.

Hi Ming,

Since kblockd_workqueue is WQ_PERCPU, the responsibility rests with blk-mq to provide a non-isolated CPU when dispatching &hctx->run_work, no?

Does every WORK_CPU_UNBOUND user have to handle it?

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Consequently, if a task executing on an isolated CPU submits I/O and triggers an asynchronous queue dispatch with zero delay, &hctx->run_work is queued directly onto the local isolated CPU's worker pool. This

If your theory is correct, why do botter to change to housekeeping_any_cpu(HK_TYPE_TIMER)
if the user is submitting IO from an isolated CPU?

Quoted 7 lines

I suspect, steering the asynchronous dispatch to a housekeeping CPU is both architecturally necessary and fully aligned with the rest of the kernel, no? When a queue is plugged, throttled, or returns BLK_STS_RESOURCE, blk-mq defers execution to a background workqueue. That work runs asynchronously at some indeterminate time in the future, long after the thread has returned to its time-critical user loop. Having a kworker suddenly preempt the thread later is likely not desired.

We are talking about if a task executing on an isolated CPU submits I/O .

If the task has been submitting IO on isolated CPU, why can't we keep scheduling WQ on isolated CPU for running blk-mq queue? I am not sure your change makes any difference from user viewpoint.

Thanks,
Ming

#4 lore raw
Quoted 4 lines · Ming Lei
Quoted 2 lines

Since kblockd_workqueue is WQ_PERCPU, the responsibility rests with blk-mq to provide a non-isolated CPU when dispatching &hctx->run_work, no?

Does every WORK_CPU_UNBOUND user have to handle it?

[Adding Tejun Heo]

No, because almost all other subsystems passing WORK_CPU_UNBOUND do so on unbound workqueues (e.g. system_unbound_wq).

In kernel/workqueue.c, when WORK_CPU_UNBOUND is queued on a WQ_UNBOUND workqueue, workqueue core already handles CPU isolation automatically via wq_select_unbound_cpu(), which respects wq_unbound_cpumask and steers the work item to a housekeeping CPU.

kblockd_workqueue is unique in that it is allocated as WQ_PERCPU to preserve per-CPU locality for multi-queue devices. For WQ_PERCPU workqueues, workqueue core cannot silently migrate WORK_CPU_UNBOUND to a remote CPU because callers of bound per-CPU workqueues expect local per-CPU execution. Thus, blk-mq is in the unusual position of passing WORK_CPU_UNBOUND to a bound per-CPU workqueue with the expectation that it avoids isolated cores.

Quoted 8 lines
Quoted 7 lines

I suspect, steering the asynchronous dispatch to a housekeeping CPU is both architecturally necessary and fully aligned with the rest of the kernel, no? When a queue is plugged, throttled, or returns BLK_STS_RESOURCE, blk-mq defers execution to a background workqueue. That work runs asynchronously at some indeterminate time in the future, long after the thread has returned to its time-critical user loop. Having a kworker suddenly preempt the thread later is likely not desired.

Quoted 5 lines

We are talking about if a task executing on an isolated CPU submits I/O .

If the task has been submitting IO on isolated CPU, why can't we keep scheduling WQ on isolated CPU for running blk-mq queue? I am not sure your change makes any difference from user viewpoint.

When a task submits I/O, synchronous submission occurs directly in syscall context. Note, in the following example, IOCB_DIRECT is applied since the file was opened with file->f_flags & O_DIRECT:

__x64_sys_write

    ksys_write
      vfs_write
        else if (file->f_op->write_iter)
          new_sync_write(file, buf, count, pos)
            // filp->f_op->write_iter(&kiocb, &iter)
            ext4_file_write_iter
              if (iocb->ki_flags & IOCB_DIRECT)
               ext4_dio_write_iter(iocb, from) 
                 iomap_dio_rw(iocb, from, &ext4_iomap_ops,
                              &ext4_dio_write_ops, dio_flags, NULL, 0)
                   __iomap_dio_rw
                     ...
                     iomap_dio_submit_bio
                       blk_crypto_submit_bio(bio)
                         if (!bio_has_crypt_ctx(bio) ...)
                           submit_bio(bio)
                            ...
                             __submit_bio
                               blk_mq_submit_bio(bio)
                                 // blk_mq_run_dispatch_ops(q, blk_mq_try_issue_directly(hctx, rq))
                                 __blk_mq_run_dispatch_ops(q, true blk_mq_try_issue_directly(hctx, rq))
                                   blk_mq_try_issue_directly(hctx, rq)
                                     __blk_mq_issue_directly(hctx, rq, true)
                                       // q->mq_ops->queue_rq(hctx, &bd)

However, if dispatch must be deferred (e.g. BLK_STS_RESOURCE), blk-mq queues &hctx->run_work. Now, if &hctx->run_work is queued on the isolated CPU, it must be executed by the local kworker. Because that kworker runs under SCHED_NORMAL, the user's SCHED_FIFO task completely starves the kworker. As a result, blk_mq_run_work_fn() never executes, hardware dispatch stalls, and the I/O pipeline hangs indefinitely. Albeit, I believe an enabled Fair server, under the EEVDF scheduler, will prevent this stall.

Anyhow, offloading deferred dispatch to an online housekeeping CPU allows the kworker to run immediately and complete dispatch without priority inversion. As noted in commit a46c27026da1:
"it isn't expected to let block IO disturb these CPUs. blk-mq kworker
shouldn't be scheduled on isolated CPUs."

Kind regards,

Signature · 1 line
Aaron Tomlin
#5 lore raw
Quoted 16 lines · Aaron Tomlin
Quoted 4 lines · Ming Lei
Quoted 2 lines

Since kblockd_workqueue is WQ_PERCPU, the responsibility rests with blk-mq to provide a non-isolated CPU when dispatching &hctx->run_work, no?

Does every WORK_CPU_UNBOUND user have to handle it?

[Adding Tejun Heo]

No, because almost all other subsystems passing WORK_CPU_UNBOUND do so on unbound workqueues (e.g. system_unbound_wq).

In kernel/workqueue.c, when WORK_CPU_UNBOUND is queued on a WQ_UNBOUND workqueue, workqueue core already handles CPU isolation automatically via wq_select_unbound_cpu(), which respects wq_unbound_cpumask and steers the work item to a housekeeping CPU.

Quoted 7 lines

kblockd_workqueue is unique in that it is allocated as WQ_PERCPU to preserve per-CPU locality for multi-queue devices. For WQ_PERCPU workqueues, workqueue core cannot silently migrate WORK_CPU_UNBOUND to a remote CPU because callers of bound per-CPU workqueues expect local per-CPU execution. Thus, blk-mq is in the unusual position of passing WORK_CPU_UNBOUND to a bound per-CPU workqueue with the expectation that it avoids isolated cores.

No.

For example, schedule_work() is queue_work_on(WORK_CPU_UNBOUND, system_percpu_wq), a WQ_PERCPU workqueue, so every schedule_work() from an isolated CPU runs on that CPU. blk-mq isn't special here; this is the general tradeoff of submitting IO from an isolated CPU.

Quoted 55 lines
Quoted 9 lines
Quoted 8 lines

I suspect, steering the asynchronous dispatch to a housekeeping CPU is both architecturally necessary and fully aligned with the rest of the kernel, no? When a queue is plugged, throttled, or returns BLK_STS_RESOURCE, blk-mq defers execution to a background workqueue. That work runs asynchronously at some indeterminate time in the future, long after the thread has returned to its time-critical user loop. Having a kworker suddenly preempt the thread later is likely not desired.

Quoted 5 lines

We are talking about if a task executing on an isolated CPU submits I/O .

If the task has been submitting IO on isolated CPU, why can't we keep scheduling WQ on isolated CPU for running blk-mq queue? I am not sure your change makes any difference from user viewpoint.

When a task submits I/O, synchronous submission occurs directly in syscall context. Note, in the following example, IOCB_DIRECT is applied since the file was opened with file->f_flags & O_DIRECT:

__x64_sys_write

    ksys_write
      vfs_write
        else if (file->f_op->write_iter)
          new_sync_write(file, buf, count, pos)
            // filp->f_op->write_iter(&kiocb, &iter)
            ext4_file_write_iter
              if (iocb->ki_flags & IOCB_DIRECT)
               ext4_dio_write_iter(iocb, from) 
                 iomap_dio_rw(iocb, from, &ext4_iomap_ops,
                              &ext4_dio_write_ops, dio_flags, NULL, 0)
                   __iomap_dio_rw
                     ...
                     iomap_dio_submit_bio
                       blk_crypto_submit_bio(bio)
                         if (!bio_has_crypt_ctx(bio) ...)
                           submit_bio(bio)
                            ...
                             __submit_bio
                               blk_mq_submit_bio(bio)
                                 // blk_mq_run_dispatch_ops(q, blk_mq_try_issue_directly(hctx, rq))
                                 __blk_mq_run_dispatch_ops(q, true blk_mq_try_issue_directly(hctx, rq))
                                   blk_mq_try_issue_directly(hctx, rq)
                                     __blk_mq_issue_directly(hctx, rq, true)
                                       // q->mq_ops->queue_rq(hctx, &bd)

However, if dispatch must be deferred (e.g. BLK_STS_RESOURCE), blk-mq queues &hctx->run_work. Now, if &hctx->run_work is queued on the isolated CPU, it must be executed by the local kworker. Because that kworker runs under SCHED_NORMAL, the user's SCHED_FIFO task completely starves the kworker. As a result, blk_mq_run_work_fn() never executes, hardware dispatch stalls, and the I/O pipeline hangs indefinitely. Albeit, I believe an enabled Fair server, under the EEVDF scheduler, will prevent this stall.

Quoted 5 lines

Anyhow, offloading deferred dispatch to an online housekeeping CPU allows the kworker to run immediately and complete dispatch without priority inversion. As noted in commit a46c27026da1:
"it isn't expected to let block IO disturb these CPUs. blk-mq kworker
shouldn't be scheduled on isolated CPUs."

You already disturb isolated CPUs by submitting IO via syscall in this case, so why does it matter here to dispatch on same isolated CPU one more time?

Thanks,
Ming

#6 lore raw
Quoted 6 lines · Ming Lei

No.

For example, schedule_work() is queue_work_on(WORK_CPU_UNBOUND, system_percpu_wq), a WQ_PERCPU workqueue, so every schedule_work() from an isolated CPU runs on that CPU. blk-mq isn't special here; this is the general tradeoff of submitting IO from an isolated CPU.

Fair point regarding schedule_work(). I concede that mechanically, kblockd_workqueue is not unique in accepting WORK_CPU_UNBOUND on a per-CPU workqueue. schedule_work() indeed directs WORK_CPU_UNBOUND to system_percpu_wq, which resolves to raw_smp_processor_id() within __queue_work().

However, the crucial distinction lies in the semantic contract and intent. Callers of schedule_work() seek local (or best-effort local) execution; there is neither an assumption nor a guarantee that schedule_work() avoids isolated CPUs.

By contrast, blk-mq has an explicit architectural contract to avoid running block kworkers on isolated CPUs. In commit a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs"), the intent was to deliberately excluded isolated CPUs from hctx->cpumask precisely because:

"blk-mq kworker shouldn't be scheduled on isolated CPUs."

Furthermore, blk_mq_run_hw_queue() explicitly honours this policy:

    if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
        blk_mq_delay_run_hw_queue(hctx, 0);
        return;
    }

When executing on an isolated core (!cpumask_test_cpu(...)), blk-mq deliberately refrains from inline dispatch specifically to offload the work asynchronously via blk_mq_delay_run_hw_queue().

When all CPUs mapped to that hardware queue are isolated (blk_mq_hctx_empty_cpumask(hctx)), commit a46c27026da1 added:

    /* 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;

The express intention of that change was to switch away from the isolated CPUs. However, because kblockd_workqueue is WQ_PERCPU, returning WORK_CPU_UNBOUND inadvertently resolves to raw_smp_processor_id(), queuing the work right back onto the isolated CPU that blk-mq just went out of its way to avoid.

That is why blk-mq is in an unusual position; it intended to avoid isolated cores, but relied on WORK_CPU_UNBOUND to achieve that on a bound per-CPU workqueue where that mechanism cannot take effect.

Quoted 3 lines

You already disturb isolated CPUs by submitting IO via syscall in this case, so why does it matter here to dispatch on same isolated CPU one more time?

There is a fundamental difference between synchronous syscall execution and asynchronous background kworker execution:

1. When an isolated user task issues a synchronous I/O request (e.g. via write(2)), execution occurs synchronously within the task's own context and CPU accounting. The application remains in control of when this overhead is incurred.

2. When dispatch must be deferred (e.g. BLK_STS_RESOURCE), blk-mq queues &hctx->run_work. A background kworker runs under SCHED_NORMAL. On isolated cores, workloads typically execute under real-time scheduling classes (SCHED_FIFO, SCHED_RR, SCHED_DEADLINE) or tight polling loops. A SCHED_FIFO task executing on that core will completely starve the local kworker. Consequently, blk_mq_run_work_fn() never runs, hardware dispatch stalls, pending completions cannot occur, and the I/O pipeline hangs indefinitely.

3. Even where starvation is avoided (e.g. via the EEVDF scheduler's Fair server or cooperative yielding), queuing bound work on a nohz_full isolated core forces the scheduler tick to re-activate, injecting unwanted jitter and defeating deterministic isolation guarantees.

Steering deferred queue dispatch to an online housekeeping CPU via housekeeping_any_cpu(HK_TYPE_TIMER) sidesteps these pitfalls entirely. The kworker executes promptly on a housekeeping core without priority inversion, hardware dispatch proceeds unhindered, and the isolated CPU's low-latency domain is preserved.

Kind regards,

Signature · 1 line
Aaron Tomlin
#7 lore raw
Quoted 14 lines · Aaron Tomlin
Quoted 6 lines · Ming Lei

No.

For example, schedule_work() is queue_work_on(WORK_CPU_UNBOUND, system_percpu_wq), a WQ_PERCPU workqueue, so every schedule_work() from an isolated CPU runs on that CPU. blk-mq isn't special here; this is the general tradeoff of submitting IO from an isolated CPU.

Fair point regarding schedule_work(). I concede that mechanically, kblockd_workqueue is not unique in accepting WORK_CPU_UNBOUND on a per-CPU workqueue. schedule_work() indeed directs WORK_CPU_UNBOUND to system_percpu_wq, which resolves to raw_smp_processor_id() within __queue_work().

Quoted 4 lines

However, the crucial distinction lies in the semantic contract and intent. Callers of schedule_work() seek local (or best-effort local) execution; there is neither an assumption nor a guarantee that schedule_work() avoids isolated CPUs.

Then you admit the same generic problem, nothing different with blk-mq.

Quoted 7 lines

By contrast, blk-mq has an explicit architectural contract to avoid running block kworkers on isolated CPUs. In commit a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs"), the intent was to deliberately excluded isolated CPUs from hctx->cpumask precisely because:

"blk-mq kworker shouldn't be scheduled on isolated CPUs."

I am pretty sure that the pre-condition is that IO isn't submitted from isolated CPU. You can send a patch to make the point explicitly.

Quoted 56 lines

Furthermore, blk_mq_run_hw_queue() explicitly honours this policy:

    if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
        blk_mq_delay_run_hw_queue(hctx, 0);
        return;
    }

When executing on an isolated core (!cpumask_test_cpu(...)), blk-mq deliberately refrains from inline dispatch specifically to offload the work asynchronously via blk_mq_delay_run_hw_queue().

When all CPUs mapped to that hardware queue are isolated (blk_mq_hctx_empty_cpumask(hctx)), commit a46c27026da1 added:

    /* 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;

The express intention of that change was to switch away from the isolated CPUs. However, because kblockd_workqueue is WQ_PERCPU, returning WORK_CPU_UNBOUND inadvertently resolves to raw_smp_processor_id(), queuing the work right back onto the isolated CPU that blk-mq just went out of its way to avoid.

That is why blk-mq is in an unusual position; it intended to avoid isolated cores, but relied on WORK_CPU_UNBOUND to achieve that on a bound per-CPU workqueue where that mechanism cannot take effect.

Quoted 3 lines

You already disturb isolated CPUs by submitting IO via syscall in this case, so why does it matter here to dispatch on same isolated CPU one more time?

There is a fundamental difference between synchronous syscall execution and asynchronous background kworker execution:

1. When an isolated user task issues a synchronous I/O request (e.g. via write(2)), execution occurs synchronously within the task's own context and CPU accounting. The application remains in control of when this overhead is incurred.

2. When dispatch must be deferred (e.g. BLK_STS_RESOURCE), blk-mq queues &hctx->run_work. A background kworker runs under SCHED_NORMAL. On isolated cores, workloads typically execute under real-time scheduling classes (SCHED_FIFO, SCHED_RR, SCHED_DEADLINE) or tight polling loops. A SCHED_FIFO task executing on that core will completely starve the local kworker. Consequently, blk_mq_run_work_fn() never runs, hardware dispatch stalls, pending completions cannot occur, and the I/O pipeline hangs indefinitely.

3. Even where starvation is avoided (e.g. via the EEVDF scheduler's Fair server or cooperative yielding), queuing bound work on a nohz_full isolated core forces the scheduler tick to re-activate, injecting unwanted jitter and defeating deterministic isolation guarantees.

Quoted 5 lines

Steering deferred queue dispatch to an online housekeeping CPU via housekeeping_any_cpu(HK_TYPE_TIMER) sidesteps these pitfalls entirely. The kworker executes promptly on a housekeeping core without priority inversion, hardware dispatch proceeds unhindered, and the isolated CPU's low-latency domain is preserved.

Any user visible effect? Both are a piece of code running on isolated CPU, the choice is in application. If they want to run IO on isolated CPU, we do it in same way. Otherwise, we won't schedule work on isolated CPU for respecting the kernel interface.

Thanks,
Ming

#8 lore raw
Quoted 4 lines · Ming Lei

Any user visible effect? Both are a piece of code running on isolated CPU, the choice is in application. If they want to run IO on isolated CPU, we do it in same way. Otherwise, we won't schedule work on isolated CPU for respecting the kernel interface.

That assertion is directly contradicted by how blk-mq is implemented today. blk-mq already actively shields isolated CPUs from running kworkers even when I/O is initiated on an isolated CPU.

Consider a multi-queue device where an hctx maps CPU 0 (housekeeping) and CPU 4 (isolated):

    1.  In blk_mq_map_swqueue(), commit a46c27026da1 explicitly stripped
        isolated CPUs from hctx->cpumask. Thus, hctx->cpumask contains
        solely CPU 0.

    2.  When a thread executing on isolated CPU 4 submits I/O and calls
        blk_mq_run_hw_queue(), blk-mq checks:

           if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) {
               blk_mq_delay_run_hw_queue(hctx, 0);
               return;
           }

        Because CPU 4 is not in hctx->cpumask, blk-mq refuses inline
        dispatch on CPU 4 and offloads via blk_mq_delay_run_hw_queue().

    3.  blk_mq_delay_run_hw_queue() calls blk_mq_hctx_next_cpu(), which
        iterates over hctx->cpumask and selects CPU 0 (the housekeeping
        CPU).

    4.  kblockd_mod_delayed_work_on() queues the delayed work onto CPU 0.

Therefore, for any hardware queue with at least one housekeeping CPU, blk-mq already offloads the kworker to a housekeeping CPU, completely sparing the isolated CPU that initiated the I/O.

The only scenario where this offload breaks down today is when all CPUs mapped to that hardware queue are isolated (blk_mq_hctx_empty_cpumask(hctx) is true). In that case, commit a46c27026da1 added:

        /* 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;

The comment and intention were unambiguous. However, because kblockd_workqueue is allocated as WQ_PERCPU, WORK_CPU_UNBOUND inadvertently resolves to raw_smp_processor_id(), dropping the kworker straight back onto isolated CPU 4.

Hence, blk-mq's established policy is already to steer kworkers away from isolated CPUs regardless of where I/O was submitted. This patch merely ensures that the empty-mask fallback honours that existing contract rather than accidentally violating it. I will send the next iteration for review.

Kind regards,

Signature · 1 line
Aaron Tomlin
#9 lore raw
In reply to Aaron Tomlin

Hello, Aaron.

Quoted 2 lines · Aaron Tomlin

kblockd_workqueue is unique in that it is allocated as WQ_PERCPU to preserve per-CPU locality for multi-queue devices.

I haven't followed the earlier discussion, but if CPU locality is important without requiring hard per-CPU affinity, an unbound workqueue with non-strict "cpu" (or "smt" for core) affinity scope can provide both:
locality by default with flexibility to migrate when needed.

Thanks.

Signature · 1 line
tejun
#10 lore raw
Quoted 10 lines · Tejun Heo

Hello, Aaron.

Quoted 2 lines · Aaron Tomlin

kblockd_workqueue is unique in that it is allocated as WQ_PERCPU to preserve per-CPU locality for multi-queue devices.

I haven't followed the earlier discussion, but if CPU locality is important without requiring hard per-CPU affinity, an unbound workqueue with non-strict "cpu" (or "smt" for core) affinity scope can provide both:
locality by default with flexibility to migrate when needed.

Hi Tejun,

Thank you for your response.

That is an elegant solution. Moving kblockd_workqueue to an unbound workqueue with a non-strict "cpu" affinity scope (WQ_AFFN_CPU) would preserve the per-CPU/per-queue locality essential for multi-queue devices by default, while delegating CPU isolation natively to workqueue core via wq_unbound_cpumask. It would ultimately eliminate the need for blk-mq to maintain ad-hoc cpumask filtering or manual housekeeping redirection.

The rationale behind the current patch is strictly as a targeted, low-risk bugfix. Commit a46c27026da1 ("blk-mq: don't schedule block kworker on isolated CPUs") attempted to offload queue dispatch when all mapped CPUs are isolated by falling back to WORK_CPU_UNBOUND:

        /* 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;

However, because kblockd_workqueue is presently WQ_PERCPU, WORK_CPU_UNBOUND resolves straight to raw_smp_processor_id(). When deferred dispatch is triggered from an isolated core, the kworker is scheduled locally, where it faces severe priority inversion and starvation against SCHED_FIFO workloads.

Given that commit a46c27026da1 was backported to stable kernels, this minimal change serves as an immediate fix suitable for stable trees without altering the concurrency and execution semantics of kblockd across the rest of the block layer.

That said, I would be very keen to explore transitioning kblockd_workqueue to an unbound workqueue with non-strict CPU affinity as a follow-up series.

Kind regards,

Signature · 1 line
Aaron Tomlin