[PATCH 3/3] workqueue: WQ_PERCPU added to alloc_workqueue users

Marco Crivellari posted 3 patches 4 days, 12 hours ago
[PATCH 3/3] workqueue: WQ_PERCPU added to alloc_workqueue users
Posted by Marco Crivellari 4 days, 12 hours ago
Currently if a user enqueue a work item using schedule_delayed_work() the
used wq is "system_wq" (per-cpu wq) while queue_delayed_work() use
WORK_CPU_UNBOUND (used when a cpu is not specified). The same applies to
schedule_work() that is using system_wq and queue_work(), that makes use
again of WORK_CPU_UNBOUND.
This lack of consistentcy cannot be addressed without refactoring the API.

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 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 use of
the 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.

With the introduction of the WQ_PERCPU flag (equivalent to !WQ_UNBOUND),
any alloc_workqueue() caller that doesn’t explicitly specify WQ_UNBOUND
must now use WQ_PERCPU.

All existing users have been updated accordingly.

Suggested-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Marco Crivellari <marco.crivellari@suse.com>
---
 include/linux/workqueue.h |  4 ++--
 kernel/workqueue.c        | 19 ++++++++++---------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 542529d3b41b..4db59f886656 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -409,7 +409,7 @@ enum wq_flags {
 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
 
 	/* BH wq only allows the following flags */
-	__WQ_BH_ALLOWS		= WQ_BH | WQ_HIGHPRI,
+	__WQ_BH_ALLOWS		= WQ_BH | WQ_HIGHPRI | WQ_PERCPU,
 };
 
 enum wq_consts {
@@ -568,7 +568,7 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
 	alloc_workqueue(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, ##args)
 
 #define create_workqueue(name)						\
-	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM, 1, (name))
+	alloc_workqueue("%s", __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_PERCPU, 1, (name))
 #define create_freezable_workqueue(name)				\
 	alloc_workqueue("%s", __WQ_LEGACY | WQ_FREEZABLE | WQ_UNBOUND |	\
 			WQ_MEM_RECLAIM, 1, (name))
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 89839eebb359..d33ca6acc5a5 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -7819,22 +7819,23 @@ void __init workqueue_init_early(void)
 		ordered_wq_attrs[i] = attrs;
 	}
 
-	system_wq = alloc_workqueue("events", 0, 0);
-	system_percpu_wq = alloc_workqueue("events", 0, 0);
-	system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
-	system_long_wq = alloc_workqueue("events_long", 0, 0);
+	system_wq = alloc_workqueue("events", WQ_PERCPU, 0);
+	system_percpu_wq = alloc_workqueue("events", WQ_PERCPU, 0);
+	system_highpri_wq = alloc_workqueue("events_highpri",
+					    WQ_HIGHPRI | WQ_PERCPU, 0);
+	system_long_wq = alloc_workqueue("events_long", WQ_PERCPU, 0);
 	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, WQ_MAX_ACTIVE);
 	system_dfl_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, WQ_MAX_ACTIVE);
 	system_freezable_wq = alloc_workqueue("events_freezable",
-					      WQ_FREEZABLE, 0);
+					      WQ_FREEZABLE | WQ_PERCPU, 0);
 	system_power_efficient_wq = alloc_workqueue("events_power_efficient",
 					      WQ_POWER_EFFICIENT, 0);
 	system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient",
-					      WQ_FREEZABLE | WQ_POWER_EFFICIENT,
-					      0);
-	system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0);
+					      WQ_FREEZABLE | WQ_POWER_EFFICIENT, 0);
+	system_bh_wq = alloc_workqueue("events_bh", WQ_BH | WQ_PERCPU, 0);
 	system_bh_highpri_wq = alloc_workqueue("events_bh_highpri",
-					       WQ_BH | WQ_HIGHPRI, 0);
+					WQ_BH | WQ_HIGHPRI | WQ_PERCPU, 0);
+
 	BUG_ON(!system_wq || !system_percpu_wq || !system_highpri_wq || !system_long_wq ||
 	       !system_unbound_wq || !system_dfl_wq || !system_freezable_wq ||
 	       !system_power_efficient_wq ||
-- 
2.51.0

Re: [PATCH 3/3] workqueue: WQ_PERCPU added to alloc_workqueue users
Posted by Tejun Heo 4 days, 4 hours ago
Hello,

The patch doesn't apply to wq/for-6.18 and...

> diff --git a/kernel/workqueue.c b/kernel/workqueue.c
> index 89839eebb359..d33ca6acc5a5 100644
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -7819,22 +7819,23 @@ void __init workqueue_init_early(void)
>  		ordered_wq_attrs[i] = attrs;
>  	}
>  
> -	system_wq = alloc_workqueue("events", 0, 0);
> -	system_percpu_wq = alloc_workqueue("events", 0, 0);
> -	system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
> -	system_long_wq = alloc_workqueue("events_long", 0, 0);
> +	system_wq = alloc_workqueue("events", WQ_PERCPU, 0);
> +	system_percpu_wq = alloc_workqueue("events", WQ_PERCPU, 0);
> +	system_highpri_wq = alloc_workqueue("events_highpri",
> +					    WQ_HIGHPRI | WQ_PERCPU, 0);
> +	system_long_wq = alloc_workqueue("events_long", WQ_PERCPU, 0);
>  	system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, WQ_MAX_ACTIVE);
>  	system_dfl_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, WQ_MAX_ACTIVE);
>  	system_freezable_wq = alloc_workqueue("events_freezable",
> -					      WQ_FREEZABLE, 0);
> +					      WQ_FREEZABLE | WQ_PERCPU, 0);
>  	system_power_efficient_wq = alloc_workqueue("events_power_efficient",
>  					      WQ_POWER_EFFICIENT, 0);
>  	system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient",
> -					      WQ_FREEZABLE | WQ_POWER_EFFICIENT,
> -					      0);
> -	system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0);
> +					      WQ_FREEZABLE | WQ_POWER_EFFICIENT, 0);

Isn't this missing WQ_PERCPU for system_freezable_power_efficient_wq?

Thanks.

-- 
tejun
Re: [PATCH 3/3] workqueue: WQ_PERCPU added to alloc_workqueue users
Posted by Marco Crivellari 1 day, 8 hours ago
On Fri, Sep 5, 2025 at 7:25 PM Tejun Heo <tj@kernel.org> wrote:
> Isn't this missing WQ_PERCPU for system_freezable_power_efficient_wq?

I guess the flag is also missing for system_power_efficient_wq,
because by default
it should be per-cpu, unless there is  wq_power_efficient=true.

-- 

Marco Crivellari

L3 Support Engineer, Technology & Product

marco.crivellari@suse.com
Re: [PATCH 3/3] workqueue: WQ_PERCPU added to alloc_workqueue users
Posted by Marco Crivellari 1 day, 9 hours ago
On Fri, Sep 5, 2025 at 7:25 PM Tejun Heo <tj@kernel.org> wrote:
>
> Hello,
>
> The patch doesn't apply to wq/for-6.18 and...

Hello Tejun,

I will rebase the work.

> >       system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient",
> > -                                           WQ_FREEZABLE | WQ_POWER_EFFICIENT,
> > -                                           0);
>
> Isn't this missing WQ_PERCPU for system_freezable_power_efficient_wq?

Yes, you're right. Sorry for that.

I will send the v2.

Thanks a lot!

--

Marco Crivellari

L3 Support Engineer, Technology & Product

marco.crivellari@suse.com