workqueue: Add warnings and check WQ flags usage
v3 cover workqueue: Add warnings and check WQ flags usage
Hi,
Currently system_wq and system_unbound_wq can still be used and no message is printed. To avoid further use of them, add a warning. The detection of deprecated workqueue is done using a new internal flag, called __WQ_DEPRECATED.
Similar scenario for WQ_PERCPU and WQ_UNBOUND. Currently there are still users that use alloc_workqueue(,0,). To avoid such situations, a couple of checks have been added:
- if neither of the flag is present, set WQ_PERCPU
- if both are present, remove WQ_PERCPU
Along with these, a warning will be printed.
Thanks!
Marco Crivellari (2):
workqueue: Add warnings and fallback if system_{unbound}_wq is used
workqueue: Add warnings and ensure one among WQ_PERCPU or WQ_UNBOUND
is present
include/linux/workqueue.h | 1 + kernel/workqueue.c | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-)
---
Changes in v3:
- system_wq / system_unbond_wq not routed to the newer. The warnings have been simpliified keeping only one of them inside __queue_work() printing the work function.
- the deprecated status is detected using a new internal flag, called __WQ_DEPRECATED.
Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- rebased on v7.1-rc5
- fixed typo in the text
- cleared WQ_PERCPU when wq_pwer_efficient flag is set, to avoid triggering a warning
- if statement to route old workqueue to newer also added in queue_rcu_work()
Link to v1: https://lore.kernel.org/all/[email protected]/
Signature · 1 line
2.54.0
PATCH v3 1/2 workqueue: Add warnings and fallback if system_{unbound}_wq is used
Currently many users transitioned already to the new introduced workqueue (system_percpu_wq, system_dfl_wq), but there are new users who still use the older system_wq and system_unbound_wq.
This change try to push this transition forward, by warning whether the old workqueus are used.
--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)
include/linux/workqueue.h +1 -0
@@ -409,6 +409,7 @@ enum wq_flags { __WQ_DRAINING =1 <<16 ,/* internal: workqueue is draining */ __WQ_ORDERED =1 <<17 ,/* internal: workqueue is ordered */ __WQ_LEGACY =1 <<18 ,/* internal: create*_workqueue() */ + __WQ_DEPRECATED =1 <<19 ,/* internal: workqueue is deprecated */ /* BH wq only allows the following flags */ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI | WQ_PERCPU,
kernel/workqueue.c +10 -2
@@ -2280,6 +2280,14 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,unsigned int work_flags;unsigned int req_cpu = cpu; +/* +* NOTE: Check whether the used workqueue is deprecated and warn +*/ +if (unlikely (wq->flags & __WQ_DEPRECATED)) +pr_warn_once ("workqueue: work func %ps enqueued on deprecated workqueue. " +"Use system_{percpu|dfl}_wq instead.\n" , + work->func); +/* * While a work item is PENDING && off queue, a task trying to * steal the PENDING will busy-loop waiting for it to either get
@@ -8037,12 +8045,12 @@ void __init workqueue_init_early(void) ordered_wq_attrs[i] = attrs; } - system_wq =alloc_workqueue ("events" ,WQ_PERCPU ,0 ); + system_wq =alloc_workqueue ("events" ,WQ_PERCPU | __WQ_DEPRECATED,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_unbound_wq =alloc_workqueue ("events_unbound" ,WQ_UNBOUND | __WQ_DEPRECATED,WQ_MAX_ACTIVE ); system_dfl_wq =alloc_workqueue ("events_unbound" ,WQ_UNBOUND ,WQ_MAX_ACTIVE ); system_freezable_wq =alloc_workqueue ("events_freezable" ,WQ_FREEZABLE |WQ_PERCPU ,0 );
Signature · 1 line
2.54.0
Re: [PATCH v3 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Quoted 32 lines · Marco Crivellari
Currently many users transitioned already to the new introduced workqueue (
system_percpu_wq,system_dfl_wq), but there are new users who still use the oldersystem_wqandsystem_unbound_wq.This change try to push this transition forward, by warning whether the old workqueus are used.
Suggested-by Tejun Heo <[email protected]>Signed-off-by Marco Crivellari <[email protected]>--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)include/linux/workqueue.h +1
-0@@ -409,6 +409,7 @@ enum wq_flags { __WQ_DRAINING =1 <<16 ,/* internal: workqueue is draining */ __WQ_ORDERED =1 <<17 ,/* internal: workqueue is ordered */ __WQ_LEGACY =1 <<18 ,/* internal: create*_workqueue() */ + __WQ_DEPRECATED =1 <<19 ,/* internal: workqueue is deprecated */ /* BH wq only allows the following flags */ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI | WQ_PERCPU,kernel/workqueue.c +0
-0
kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2289
Quoted 10 lines
kernel/workqueue.c +7
-0@@ -2280,6 +2280,14 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,unsigned int work_flags;unsigned int req_cpu = cpu; +/* +* NOTE: Check whether the used workqueue is deprecated and warn +*/ +if (unlikely (wq->flags & __WQ_DEPRECATED)) +pr_warn_once ("workqueue: work func %ps enqueued on deprecated workqueue. " +"Use system_{percpu|dfl}_wq instead.\n" , + work->func);
Silly question, can we make this a build warning?
Re: [PATCH v3 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Quoted 37 lines · Breno Leitao
Quoted 35 lines · Marco Crivellari
Currently many users transitioned already to the new introduced workqueue (
system_percpu_wq,system_dfl_wq), but there are new users who still use the oldersystem_wqandsystem_unbound_wq.This change try to push this transition forward, by warning whether the old workqueus are used.
Suggested-by Tejun Heo <[email protected]>Signed-off-by Marco Crivellari <[email protected]>--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)include/linux/workqueue.h +1
-0@@ -409,6 +409,7 @@ enum wq_flags { __WQ_DRAINING =1 <<16 ,/* internal: workqueue is draining */ __WQ_ORDERED =1 <<17 ,/* internal: workqueue is ordered */ __WQ_LEGACY =1 <<18 ,/* internal: create*_workqueue() */ + __WQ_DEPRECATED =1 <<19 ,/* internal: workqueue is deprecated */ /* BH wq only allows the following flags */ __WQ_BH_ALLOWS = WQ_BH | WQ_HIGHPRI | WQ_PERCPU,kernel/workqueue.c +0
-0@@ -2280,6 +2280,14 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,unsigned int work_flags;unsigned int req_cpu = cpu;
Quoted 9 lines
kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2289
Quoted 7 lines
kernel/workqueue.c +7
-0+/* +* NOTE: Check whether the used workqueue is deprecated and warn +*/ +if (unlikely (wq->flags & __WQ_DEPRECATED)) +pr_warn_once ("workqueue: work func %ps enqueued on deprecated workqueue. " +"Use system_{percpu|dfl}_wq instead.\n" , + work->func);Silly question, can we make this a build warning?
Mmh not sure. I tried something similar before the v1 to warn about the workqueue, but I realized it doesn't work in every case, so at least both should be kept if we go that way.
There is already something similar for users who try to flush the system workqueue.
Thanks!
--
Marco Crivellari
SUSE Labs
PATCH v3 2/2 workqueue: Add warnings and ensure one among WQ_PERCPU or WQ_UNBOUND is present
In reply to Marco CrivellariCurrently there are no checks in order to enforce the use of WQ_PERCPU and avoid this flag is used if WQ_UNBOUND is already present.
So act as following:
- if neither of them is present, set WQ_PERCPU
- if both are present, remove WQ_PERCPU
Along with this change, print a warning, so that the code still uses both or neither of them, can be changed.
--- kernel/workqueue.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
kernel/workqueue.c +18 -1
@@ -5826,7 +5826,7 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,/* see the comment above the definition of WQ_POWER_EFFICIENT */ if ((flags &WQ_POWER_EFFICIENT ) && wq_power_efficient) - flags |=WQ_UNBOUND ; + flags = (flags & ~WQ_PERCPU ) |WQ_UNBOUND ;/* allocate wq and format name */ if (flags &WQ_UNBOUND )
@@ -5850,6 +5850,23 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,pr_warn_once ("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n" , wq->name); +/* +* One among WQ_PERCPU and WQ_UNBOUND must be set, but not both. +* - If neither is set, default to WQ_PERCPU +* - If both are set, default to WQ_UNBOUND +* +* This code can be removed after workqueue are unbound by default +*/ +if (!(flags & (WQ_UNBOUND |WQ_PERCPU ))) { +pr_warn_ratelimited ("workqueue: %s is using neither WQ_PERCPU or WQ_UNBOUND. " +"Setting WQ_PERCPU.\n" , wq->name); + flags |=WQ_PERCPU ; + }else if ((flags &WQ_PERCPU ) && (flags &WQ_UNBOUND )) { +pr_warn_ratelimited ("workqueue: %s uses both WQ_PERCPU and WQ_UNBOUND. " +"Dropped WQ_PERCPU, keeping WQ_UNBOUND.\n" , wq->name); + flags &= (~WQ_PERCPU ); + } +if (flags &WQ_BH ) {/* * BH workqueues always share a single execution context per CPU
Signature · 1 line
2.54.0
Tejun Heo
maintainerRe: [PATCH v3 2/2] workqueue: Add warnings and ensure one among WQ_PERCPU or WQ_UNBOUND is present
On Thu, May 28, 2026 at 06:01:59PM +0200, Marco Crivellari wrote:
...
Quoted 10 lines
kernel/workqueue.c +6
-0@@ -5850,6 +5850,23 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,pr_warn_once ("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n" , wq->name); +/* +* One among WQ_PERCPU and WQ_UNBOUND must be set, but not both. +* - If neither is set, default to WQ_PERCPU +* - If both are set, default to WQ_UNBOUND +* +* This code can be removed after workqueue are unbound by default
kernel/workqueue.c static struct workqueue_struct *__alloc_workqueue(const char *fmt, · line 5868
Quoted 10 lines
kernel/workqueue.c +10
-0+*/ +if (!(flags & (WQ_UNBOUND |WQ_PERCPU ))) { +pr_warn_ratelimited ("workqueue: %s is using neither WQ_PERCPU or WQ_UNBOUND. " +"Setting WQ_PERCPU.\n" , wq->name); + flags |=WQ_PERCPU ; + }else if ((flags &WQ_PERCPU ) && (flags &WQ_UNBOUND )) { +pr_warn_ratelimited ("workqueue: %s uses both WQ_PERCPU and WQ_UNBOUND. " +"Dropped WQ_PERCPU, keeping WQ_UNBOUND.\n" , wq->name); + flags &= (~WQ_PERCPU ); + }
Let's just WARN_ONCE() on these. These are outright bugs, right?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v3 2/2] workqueue: Add warnings and ensure one among WQ_PERCPU or WQ_UNBOUND is present
Hi,
Quoted 17 lines · Tejun Heo
On Thu, May 28, 2026 at 06:01:59PM +0200, Marco Crivellari wrote:
...Quoted 14 lines
kernel/workqueue.c +10
-0@@ -5850,6 +5850,23 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,pr_warn_once ("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n" , wq->name); +/* +* One among WQ_PERCPU and WQ_UNBOUND must be set, but not both. +* - If neither is set, default to WQ_PERCPU +* - If both are set, default to WQ_UNBOUND +* +* This code can be removed after workqueue are unbound by default +*/ +if (!(flags & (WQ_UNBOUND |WQ_PERCPU ))) { +pr_warn_ratelimited ("workqueue: %s is using neither WQ_PERCPU or WQ_UNBOUND. " +"Setting WQ_PERCPU.\n" , wq->name);
Quoted 8 lines
kernel/workqueue.c static struct workqueue_struct *__alloc_workqueue(const char *fmt, · line 5868
Quoted 6 lines
kernel/workqueue.c +6
-0+ flags |=WQ_PERCPU ; + }else if ((flags &WQ_PERCPU ) && (flags &WQ_UNBOUND )) { +pr_warn_ratelimited ("workqueue: %s uses both WQ_PERCPU and WQ_UNBOUND. " +"Dropped WQ_PERCPU, keeping WQ_UNBOUND.\n" , wq->name); + flags &= (~WQ_PERCPU ); + }Let's just
WARN_ONCE()on these. These are outright bugs, right?
Yes, they are. I will change this to use WARN_ONCE() for the next version.
Thanks!
Signature · 3 lines
Marco Crivellari SUSE Labs