[PATCH RFC 2/3] workqueue: Offload WQ_PREFER_PERCPU work from isolated CPUs

From: Marco Crivellari

Date: Thu Nov 26 2026 - 10:25:28 EST


CPUs can be isolated at boot with isolcpus=domain, or at runtime through
cgroup v2 cpuset isolated partitions. Unbound workqueues already follow
this, as their cpumask is restricted to the housekeeping CPUs. Per-cpu
workqueues don't, so work queued on an isolated CPU keeps running there.

Many WQ_PERCPU users don't need per-cpu execution for correctness, they
only want cache locality. For them, running on an isolated CPU is just
noise for the isolated workload.

WQ_PREFER_PERCPU covers this case. It implies WQ_PERCPU, so by default
the workqueue is a regular concurrency-managed per-cpu workqueue. When a
CPU is isolated, its pwq is backed by an unbound pool running on the
housekeeping CPUs instead. When the CPU stops being isolated, the per-cpu
pool is used again.

The switch happens in workqueue_unbound_housekeeping_update(). For each
WQ_PREFER_PERCPU workqueue, the pwq of a CPU that becomes isolated, or
stops being isolated, is replaced. CPUs that stay isolated get a new pwq
only if their unbound pool must change, e.g. because it can run on a CPU
that is now isolated. The same happens when the workqueue cpumask is
changed through sysfs. As the workqueue is not WQ_UNBOUND, the fallback
pwqs keep using the per-cpu nr_active accounting.

Signed-off-by: Marco Crivellari <marco.crivellari@xxxxxxxx>
---
kernel/workqueue.c | 143 +++++++++++++++++++++++++++++++++++++++------
1 file changed, 126 insertions(+), 17 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 73c61a55d4ce..0665ffaf87c5 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1858,10 +1858,12 @@ static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill)
lockdep_assert_held(&pool->lock);

/*
- * A concurrency-managed per-cpu pool accounts nr_active per pwq, so
- * pwq->nr_active against wq->percpu_max_active is sufficient.
+ * A concurrency-managed per-cpu workqueue accounts nr_active per pwq,
+ * so pwq->nr_active against wq->percpu_max_active is sufficient. This
+ * includes WQ_PREFER_PERCPU pwqs backed by an unbound pool on isolated
+ * CPUs: the workqueue isn't WQ_UNBOUND and has no node_nr_active.
*/
- if (is_percpu_pool(pool)) {
+ if (wq->flags & WQ_PERCPU) {
obtained = pwq->nr_active < READ_ONCE(wq->percpu_max_active);
goto out;
}
@@ -2099,10 +2101,11 @@ static void pwq_dec_nr_active(struct pool_workqueue *pwq)
pwq->nr_active--;

/*
- * A concurrency-managed per-cpu pool only needs to kick the first
- * inactive work item on @pwq itself.
+ * A concurrency-managed WQ_PERCPU pwq only needs to kick the first
+ * inactive work item on @pwq itself; see the matching comment in
+ * pwq_tryinc_nr_active().
*/
- if (is_percpu_pool(pool)) {
+ if (pwq->wq->flags & WQ_PERCPU) {
pwq_activate_first_inactive(pwq, false);
return;
}
@@ -2497,14 +2500,15 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
}

/*
- * pwq is determined and locked. For unbound pools, we could have raced
+ * pwq is determined and locked. For unbound and WQ_PREFER_PERCPU
+ * workqueues, whose pwqs get replaced at runtime, we could have raced
* with pwq release and it could already be dead. If its refcnt is zero,
- * repeat pwq selection. Note that unbound pwqs never die without
- * another pwq replacing it in cpu_pwq or while work items are executing
- * on it, so the retrying is guaranteed to make forward-progress.
+ * repeat pwq selection. Note that such pwqs never die without another
+ * pwq replacing it in cpu_pwq or while work items are executing on it,
+ * so the retrying is guaranteed to make forward-progress.
*/
if (unlikely(!pwq->refcnt)) {
- if (wq->flags & WQ_UNBOUND) {
+ if (wq->flags & (WQ_UNBOUND | WQ_PREFER_PERCPU)) {
raw_spin_unlock(&pool->lock);
cpu_relax();
goto retry;
@@ -5602,10 +5606,28 @@ static struct worker_pool *get_percpu_pool(struct workqueue_struct *wq, int cpu)
return &per_cpu_ptr(pools, cpu)[highpri];
}

+/*
+ * Whether @wq's pwq on @cpu must be backed by the per-cpu pool of @cpu: true
+ * for WQ_PERCPU workqueues, except for WQ_PREFER_PERCPU ones while @cpu is
+ * isolated, which are backed by an unbound pool on the housekeeping CPUs.
+ */
+static bool wq_cpu_wants_percpu_pool(struct workqueue_struct *wq, int cpu)
+{
+ if (!(wq->flags & WQ_PERCPU))
+ return false;
+ if (!(wq->flags & WQ_PREFER_PERCPU))
+ return true;
+
+ /* the HK_TYPE_DOMAIN cpumask is freed under RCU when isolation changes */
+ guard(rcu)();
+ return !cpu_is_isolated(cpu);
+}
+
/*
* Obtain the pool backing @wq on @cpu and create a pwq associating the two.
- * A WQ_PERCPU workqueue is backed by the static per-cpu pool of @cpu,
- * everything else by a pool matching @attrs. @cpu < 0 is always unbound.
+ * A WQ_PERCPU workqueue is backed by the static per-cpu pool of @cpu, unless
+ * it is WQ_PREFER_PERCPU and @cpu is isolated; everything else by a pool
+ * matching @attrs. @cpu < 0 is always unbound.
*/
static struct pool_workqueue *alloc_pwq(struct workqueue_struct *wq,
const struct workqueue_attrs *attrs,
@@ -5618,7 +5640,7 @@ static struct pool_workqueue *alloc_pwq(struct workqueue_struct *wq,

WARN_ON_ONCE((wq->flags & WQ_PERCPU) && cpu < 0);

- if (cpu >= 0 && (wq->flags & WQ_PERCPU)) {
+ if (cpu >= 0 && wq_cpu_wants_percpu_pool(wq, cpu)) {
pool = get_percpu_pool(wq, cpu);
} else {
pool = get_unbound_pool(attrs);
@@ -6136,6 +6158,15 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
flags |= WQ_PERCPU;
}

+ /*
+ * WQ_PREFER_PERCPU behaves like WQ_PERCPU unless the target CPU is
+ * isolated; see its definition. Force WQ_PERCPU here, before the
+ * WQ_UNBOUND defaulting below, so that a bare WQ_PREFER_PERCPU caller
+ * ends up percpu by default.
+ */
+ if (flags & WQ_PREFER_PERCPU)
+ flags |= WQ_PERCPU;
+
/*
* Default to WQ_UNBOUND if neither WQ_PERCPU nor WQ_UNBOUND is set,
* and drop WQ_PERCPU if both are. This must happen before the WQ_RT
@@ -6160,6 +6191,10 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
flags = (flags & ~WQ_PERCPU) | WQ_UNBOUND;

+ /* WQ_PREFER_PERCPU is meaningless on an unbound workqueue */
+ if (flags & WQ_UNBOUND)
+ flags &= ~WQ_PREFER_PERCPU;
+
/* allocate wq and format name */
if (flags & WQ_UNBOUND)
wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1);
@@ -7487,6 +7522,63 @@ static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)
return ret;
}

+/*
+ * Back @cpu's pwq of the WQ_PREFER_PERCPU @wq with the pool matching @cpu's
+ * isolation state: the per-cpu pool, or an unbound pool if @cpu is isolated.
+ * Nothing is done if the installed pwq already uses the right pool.
+ */
+static void prefer_percpu_wq_update_pwq(struct workqueue_struct *wq, int cpu)
+{
+ struct workqueue_attrs *attrs = unbound_wq_update_pwq_attrs_buf;
+ struct worker_pool *pool = installed_pwq(wq, cpu)->pool;
+ struct pool_workqueue *old_pwq, *pwq;
+
+ lockdep_assert_held(&wq_pool_mutex);
+
+ if (wq_cpu_wants_percpu_pool(wq, cpu)) {
+ if (is_percpu_pool(pool))
+ return;
+ } else {
+ copy_workqueue_attrs(attrs, wq->attrs);
+ wqattrs_actualize_cpumask(attrs, wq_unbound_cpumask);
+ wq_calc_pod_cpumask(attrs, cpu);
+ if (!is_percpu_pool(pool) && wqattrs_equal(attrs, pool->attrs))
+ return;
+ }
+
+ pwq = alloc_pwq(wq, attrs, cpu);
+ if (!pwq) {
+ pr_warn("workqueue: allocation failed while updating \"%s\" on CPU %d\n",
+ wq->name, cpu);
+ return;
+ }
+
+ mutex_lock(&wq->mutex);
+ old_pwq = install_pwq(wq, cpu, pwq);
+ mutex_unlock(&wq->mutex);
+ put_pwq_unlocked(old_pwq);
+}
+
+/*
+ * Update the pwqs of every WQ_PREFER_PERCPU workqueue on @cpus. These
+ * workqueues are WQ_PERCPU, so workqueue_apply_unbound_cpumask() never
+ * iterates them.
+ */
+static void wq_update_prefer_percpu_pwqs(const struct cpumask *cpus)
+{
+ struct workqueue_struct *wq;
+ int cpu;
+
+ lockdep_assert_held(&wq_pool_mutex);
+
+ list_for_each_entry(wq, &workqueues, list) {
+ if (!(wq->flags & WQ_PREFER_PERCPU) || (wq->flags & __WQ_DESTROYING))
+ continue;
+ for_each_cpu(cpu, cpus)
+ prefer_percpu_wq_update_pwq(wq, cpu);
+ }
+}
+
/**
* workqueue_unbound_housekeeping_update - Propagate housekeeping cpumask update
* @hk: the new housekeeping cpumask
@@ -7495,6 +7587,10 @@ static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)
* that the effective unbound affinity is the intersection of the new housekeeping
* with the requested affinity set via nohz_full=/isolcpus= or sysfs.
*
+ * The pwqs of WQ_PREFER_PERCPU workqueues on newly isolated CPUs are moved to
+ * unbound pools, and back to their per-cpu pools once the CPUs stop being
+ * isolated.
+ *
* Return: 0 on success and -errno on failure.
*/
int workqueue_unbound_housekeeping_update(const struct cpumask *hk)
@@ -7519,8 +7615,18 @@ int workqueue_unbound_housekeeping_update(const struct cpumask *hk)
ret = workqueue_apply_unbound_cpumask(cpumask);

/* Save the current isolated cpumask & export it via sysfs */
- if (!ret)
- cpumask_andnot(wq_isolated_cpumask, cpu_possible_mask, hk);
+ if (!ret) {
+ cpumask_andnot(cpumask, cpu_possible_mask, hk);
+ if (!cpumask_equal(cpumask, wq_isolated_cpumask)) {
+ /*
+ * CPUs that stay isolated are included too: their
+ * unbound pool may run on a CPU that is now isolated.
+ */
+ cpumask_or(cpumask, cpumask, wq_isolated_cpumask);
+ cpumask_andnot(wq_isolated_cpumask, cpu_possible_mask, hk);
+ wq_update_prefer_percpu_pwqs(cpumask);
+ }
+ }

mutex_unlock(&wq_pool_mutex);
free_cpumask_var(cpumask);
@@ -7891,8 +7997,11 @@ static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
if (!cpumask_empty(cpumask)) {
ret = 0;
mutex_lock(&wq_pool_mutex);
- if (!cpumask_equal(cpumask, wq_unbound_cpumask))
+ if (!cpumask_equal(cpumask, wq_unbound_cpumask)) {
ret = workqueue_apply_unbound_cpumask(cpumask);
+ if (!ret)
+ wq_update_prefer_percpu_pwqs(wq_isolated_cpumask);
+ }
if (!ret)
cpumask_copy(wq_requested_unbound_cpumask, cpumask);
mutex_unlock(&wq_pool_mutex);
--
2.56.0