* [PATCH RFC 1/3] workqueue: Add WQ_PREFER_PERCPU and system_prefer_percpu_wq
2026-11-26 15:24 [PATCH RFC 0/3] workqueue: Add WQ_PREFER_PERCPU Marco Crivellari
@ 2026-11-26 15:24 ` Marco Crivellari
2026-11-26 15:24 ` [PATCH RFC 2/3] workqueue: Offload WQ_PREFER_PERCPU work from isolated CPUs Marco Crivellari
2026-11-26 15:24 ` [PATCH RFC 3/3] workqueue: doc: Document WQ_PREFER_PERCPU Marco Crivellari
2 siblings, 0 replies; 4+ messages in thread
From: Marco Crivellari @ 2026-11-26 15:24 UTC (permalink / raw)
To: linux-kernel
Cc: Tejun Heo, Lai Jiangshan, Frederic Weisbecker,
Sebastian Andrzej Siewior, Marco Crivellari, Michal Hocko,
Breno Leitao
There may be situations where local execution is not strictly needed
for correctness, but it is preferred for performance gains.
The Workqueue API currently does not distinguish between these two
instances.
So add WQ_PREFER_PERCPU and system_prefer_percpu_wq, so that it would
be the first choice for workloads that don't strictly need local
execution for correctness but only to maintain locality.
Link: https://lore.kernel.org/all/20250221112003.1dSuoGyc@linutronix.de/
Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Marco Crivellari <marco.crivellari@suse.com>
---
include/linux/workqueue.h | 17 ++++++++++++++++-
kernel/workqueue.c | 7 ++++++-
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 1caacdb5bc8e..9c315f354b5a 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -403,6 +403,15 @@ enum wq_flags {
WQ_PERCPU = 1 << 8, /* bound to a specific cpu */
WQ_RT = 1 << 9, /* real-time priority, invalid with WQ_PERCPU */
+ /*
+ * Like WQ_PERCPU, for work that only prefers the queueing CPU for
+ * locality. While a CPU is isolated (isolcpus=domain or a cgroup v2
+ * isolated partition), work queued on it runs on an unbound pool on
+ * the housekeeping CPUs instead; once the CPU stops being isolated,
+ * its per-cpu pool is used again. Implies WQ_PERCPU.
+ */
+ WQ_PREFER_PERCPU = 1 << 10,
+
__WQ_DESTROYING = 1 << 15, /* internal: workqueue is destroying */
__WQ_DRAINING = 1 << 16, /* internal: workqueue is draining */
__WQ_ORDERED = 1 << 17, /* internal: workqueue is ordered */
@@ -459,6 +468,9 @@ enum wq_consts {
*
* system_bh[_highpri]_wq are convenience interface to softirq. BH work items
* are executed in the queueing CPU's BH context in the queueing order.
+ *
+ * system_prefer_percpu_wq is for work items which prefer to run on the
+ * queueing CPU for locality but don't require it; see WQ_PREFER_PERCPU.
*/
extern struct workqueue_struct *system_wq; /* use system_percpu_wq, this will be removed */
extern struct workqueue_struct *system_percpu_wq;
@@ -472,6 +484,7 @@ extern struct workqueue_struct *system_freezable_power_efficient_wq;
extern struct workqueue_struct *system_bh_wq;
extern struct workqueue_struct *system_bh_highpri_wq;
extern struct workqueue_struct *system_dfl_long_wq;
+extern struct workqueue_struct *system_prefer_percpu_wq;
void workqueue_softirq_action(bool highpri);
void workqueue_softirq_dead(unsigned int cpu);
@@ -821,7 +834,9 @@ extern void __warn_flushing_systemwide_wq(void)
(__builtin_constant_p(_wq == system_power_efficient_wq) && \
_wq == system_power_efficient_wq) || \
(__builtin_constant_p(_wq == system_freezable_power_efficient_wq) && \
- _wq == system_freezable_power_efficient_wq)) \
+ _wq == system_freezable_power_efficient_wq) || \
+ (__builtin_constant_p(_wq == system_prefer_percpu_wq) && \
+ _wq == system_prefer_percpu_wq)) \
__warn_flushing_systemwide_wq(); \
__flush_workqueue(_wq); \
})
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 558049dbb894..73c61a55d4ce 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -566,6 +566,8 @@ struct workqueue_struct *system_bh_highpri_wq;
EXPORT_SYMBOL_GPL(system_bh_highpri_wq);
struct workqueue_struct *system_dfl_long_wq __ro_after_init;
EXPORT_SYMBOL_GPL(system_dfl_long_wq);
+struct workqueue_struct *system_prefer_percpu_wq __ro_after_init;
+EXPORT_SYMBOL_GPL(system_prefer_percpu_wq);
static int worker_thread(void *__worker);
static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
@@ -8483,11 +8485,14 @@ void __init workqueue_init_early(void)
system_bh_highpri_wq = alloc_workqueue("events_bh_highpri",
WQ_BH | WQ_HIGHPRI | WQ_PERCPU, 0);
system_dfl_long_wq = alloc_workqueue("events_dfl_long", WQ_UNBOUND, WQ_MAX_ACTIVE);
+ system_prefer_percpu_wq = alloc_workqueue("events_prefer_percpu",
+ WQ_PERCPU | WQ_PREFER_PERCPU, 0);
BUG_ON(!system_wq || !system_percpu_wq|| !system_highpri_wq || !system_long_wq ||
!system_unbound_wq || !system_freezable_wq || !system_dfl_wq ||
!system_power_efficient_wq ||
!system_freezable_power_efficient_wq ||
- !system_bh_wq || !system_bh_highpri_wq || !system_dfl_long_wq);
+ !system_bh_wq || !system_bh_highpri_wq || !system_dfl_long_wq ||
+ !system_prefer_percpu_wq);
}
static void __init wq_cpu_intensive_thresh_init(void)
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH RFC 2/3] workqueue: Offload WQ_PREFER_PERCPU work from isolated CPUs
2026-11-26 15:24 [PATCH RFC 0/3] workqueue: Add WQ_PREFER_PERCPU Marco Crivellari
2026-11-26 15:24 ` [PATCH RFC 1/3] workqueue: Add WQ_PREFER_PERCPU and system_prefer_percpu_wq Marco Crivellari
@ 2026-11-26 15:24 ` Marco Crivellari
2026-11-26 15:24 ` [PATCH RFC 3/3] workqueue: doc: Document WQ_PREFER_PERCPU Marco Crivellari
2 siblings, 0 replies; 4+ messages in thread
From: Marco Crivellari @ 2026-11-26 15:24 UTC (permalink / raw)
To: linux-kernel
Cc: Tejun Heo, Lai Jiangshan, Frederic Weisbecker,
Sebastian Andrzej Siewior, Marco Crivellari, Michal Hocko,
Breno Leitao
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@suse.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread