[PATCH 3/4] Workqueue: add WQ_PERCPU

From: Marco Crivellari
Date: Sat May 03 2025 - 04:29:15 EST


Currently alloc_workqueue() treats all queues as per-CPU by default,
while unbound workqueues must opt-in via WQ_UNBOUND.

This default is suboptimal: most workloads benefit from unbound queues,
allowing the scheduler to place worker threads where they’re needed and
reducing noise when CPUs are isolated.

This patch adds a new WQ_PERCPU flag to explicitly request the legacy
per-CPU behavior. Both flags coexist for one release cycle to allow
callers to transition their calls.

Once migration is complete, WQ_UNBOUND can be removed and unbound
will become the implicit default.

Suggested-by: Tejun Heo <tj@xxxxxxxxxx>
Signed-off-by: Marco Crivellari <marco.crivellari@xxxxxxxx>
---
include/linux/workqueue.h | 1 +
kernel/workqueue.c | 7 +++++++
2 files changed, 8 insertions(+)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 9dea10a09cc5..697aabbd6dcb 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -374,6 +374,7 @@ enum wq_flags {
WQ_HIGHPRI = 1 << 4, /* high priority */
WQ_CPU_INTENSIVE = 1 << 5, /* cpu intensive workqueue */
WQ_SYSFS = 1 << 6, /* visible in sysfs, see workqueue_sysfs_register() */
+ WQ_PERCPU = 1 << 7, /* bound to a specific cpu */

/*
* Per-cpu workqueues are generally preferred because they tend to
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 98cbfc685f39..0a088a2bd6a3 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -5670,6 +5670,13 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
return NULL;
}

+ /* one among WQ_UNBOUND and WQ_PERCPU should always be present */
+ if (WARN_ON_ONCE(flags & WQ_UNBOUND && flags & WQ_PERCPU))
+ return NULL;
+
+ if (WARN_ON_ONCE(!(flags & WQ_UNBOUND) && !(flags & WQ_PERCPU)))
+ return NULL;
+
/* see the comment above the definition of WQ_POWER_EFFICIENT */
if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
flags |= WQ_UNBOUND;
--
2.49.0