workqueue: Add warnings and check WQ flags usage
v2 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 and route the workqueue on system_percpu_wq or system_dfl_wq.
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.
~~~~
Few things to note, and that's the main reason because it is an RFC:
1. I wanted to use a compile time error, but considering the new users still relying on the old workqueue or using alloc_workqueue() without one of the flags, it means break the code. So I decided to avoid.
2. there are no compile-time warnings: should we add something?
3. __alloc_workqueue() prints the WQ_* messages with pr_warn_ratelimited(); I used this because otherwise with the _once version we're going to see only the 1st.
4. another option would have been make the workqueue unbound by default, but of course, it's still not possible due to point 1.
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
kernel/workqueue.c | 91 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-)
---
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
RFC PATCH v2 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 and redirecting them old used workqueue with the appropriate new one.
--- kernel/workqueue.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
kernel/workqueue.c +72 -0
@@ -2437,6 +2437,24 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,bool ret = false;unsigned long irq_flags; +/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) { +pr_warn_once ("workqueue: system_wq will be removed shortly. " +"Use system_percpu_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_percpu_wq; + }else if (unlikely (wq == system_unbound_wq)) { +pr_warn_once ("workqueue: system_unbound_wq will be removed shortly. " +"Use system_dfl_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_dfl_wq; + } +local_irq_save (irq_flags); if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) &&
@@ -2604,6 +2622,24 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,bool ret = false;unsigned long irq_flags; +/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) { +pr_warn_once ("workqueue: system_wq will be removed shortly. " +"Use system_percpu_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_percpu_wq; + }else if (unlikely (wq == system_unbound_wq)) { +pr_warn_once ("workqueue: system_unbound_wq will be removed shortly. " +"Use system_dfl_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_dfl_wq; + } +/* read the comment in __queue_work() */ local_irq_save (irq_flags);
@@ -2642,6 +2678,24 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,unsigned long irq_flags;bool ret; +/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) { +pr_warn_once ("workqueue: system_wq will be removed shortly. " +"Use system_percpu_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_percpu_wq; + }else if (unlikely (wq == system_unbound_wq)) { +pr_warn_once ("workqueue: system_unbound_wq will be removed shortly. " +"Use system_dfl_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_dfl_wq; + } + ret =work_grab_pending (&dwork->work,WORK_CANCEL_DELAYED , &irq_flags);if (!clear_pending_if_disabled (&dwork->work))
@@ -2676,6 +2730,24 @@ bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) {struct work_struct *work = &rwork->work; +/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) { +pr_warn_once ("workqueue: system_wq will be removed shortly. " +"Use system_percpu_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_percpu_wq; + }else if (unlikely (wq == system_unbound_wq)) { +pr_warn_once ("workqueue: system_unbound_wq will be removed shortly. " +"Use system_dfl_wq instead. Caller: %ps\n" , +__builtin_return_address (0 )); + wq = system_dfl_wq; + } +/* * rcu_work can't be canceled or disabled. Warn if the user reached * inside @rwork and disabled the inner work.
Signature · 1 line
2.54.0
Tejun Heo
maintainerRe: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Hello,
Quoted 20 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 and redirecting them old used workqueue with the appropriate new one.
Suggested-by Tejun Heo <[email protected]>Signed-off-by Marco Crivellari <[email protected]>--- kernel/workqueue.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)kernel/workqueue.c +0
-0
kernel/workqueue.c bool queue_work_on(int cpu, struct workqueue_struct *wq, · line 2446
Quoted 10 lines
kernel/workqueue.c +7
-0@@ -2437,6 +2437,24 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,bool ret = false;unsigned long irq_flags; +/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) {
Can you add an internal flag to mark the workqueues and use a helper so that the warning messages aren't replicated in multiple places? Also, do we need to have the warning on delayed_queue? Just put it in a single spot where they'll all converge.
Thanks.
Signature · 1 line
tejun
Re: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Hello,
Quoted 9 lines · Tejun Heo
[...]
kernel/workqueue.c bool queue_work_on(int cpu, struct workqueue_struct *wq, · line 2446
Quoted 7 lines
kernel/workqueue.c +7
-0+/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) {
Quoted 4 lines
Can you add an internal flag to mark the workqueues and use a helper so that the warning messages aren't replicated in multiple places? Also, do we need to have the warning on
delayed_queue? Just put it in a single spot where they'll all converge.
Yes, that sounds better indeed. So I can place the warnings directly inside __queue_work(), calling an inline function?
Regarding the internal flags I guess you mean something like __WQ_DEPRECATED and use this flag instead of the pointer to perform the check.
Thanks!
Signature · 3 lines
Marco Crivellari SUSE Labs
Tejun Heo
maintainerRe: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Hello,
Quoted 12 lines · Marco Crivellari
Hello,
Quoted 9 lines · Tejun Heo
[...]
Quoted 7 lines
kernel/workqueue.c +7
-0+/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) {
Quoted 7 lines
Quoted 4 lines
Can you add an internal flag to mark the workqueues and use a helper so that the warning messages aren't replicated in multiple places? Also, do we need to have the warning on
delayed_queue? Just put it in a single spot where they'll all converge.Yes, that sounds better indeed. So I can place the warnings directly inside
__queue_work(), calling an inline function?
If it's just a single palce, open-coding it should be fine too.
Thanks.
Signature · 1 line
tejun
Re: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
In reply to Tejun HeoQuoted 27 lines · Tejun Heo
Hello,
Quoted 23 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 and redirecting them old used workqueue with the appropriate new one.
Suggested-by Tejun Heo <[email protected]>Signed-off-by Marco Crivellari <[email protected]>--- kernel/workqueue.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)kernel/workqueue.c +0
-0@@ -2437,6 +2437,24 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,bool ret = false;unsigned long irq_flags;
Quoted 12 lines
kernel/workqueue.c bool queue_work_on(int cpu, struct workqueue_struct *wq, · line 2446
Quoted 7 lines
kernel/workqueue.c +7
-0+/* +* NOTE: These checks are here to assure that no users will still +* rely on system_wq and system_unbound_wq. +* They can be removed along with those workqueue when the +* time comes. +*/ +if (unlikely (wq == system_wq)) {Can you add an internal flag to mark the workqueues and use a helper so that the warning messages aren't replicated in multiple places? Also, do we need to have the warning on
delayed_queue? Just put it in a single spot where they'll all converge.
Hello Tejun,
That's maybe a silly observation on my side.
If I remove the checks from where I placed them and move them deeper (__queue_work() for example), I won't know how to print the "caller," making it harder for people to understand which code is using the deprecated workqueue.
Printing the stack with dump_stack() is not doable.
Do you have suggestions?
Thanks!
Signature · 3 lines
Marco Crivellari SUSE Labs
Tejun Heo
maintainerRe: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Quoted 10 lines · Marco Crivellari
That's maybe a silly observation on my side.
If I remove the checks from where I placed them and move them deeper (
__queue_work()for example), I won't know how to print the "caller," making it harder for people to understand which code is using the deprecated workqueue.Printing the stack with
dump_stack()is not doable.Do you have suggestions?
For workqueues, identifying the work function and workqueue name should be good enough. It's not like these are difficult to find.
Thanks.
Signature · 1 line
tejun
Re: [RFC PATCH v2 1/2] workqueue: Add warnings and fallback if system_{unbound}_wq is used
Quoted 9 lines · Tejun Heo
Quoted 7 lines · Marco Crivellari
That's maybe a silly observation on my side.
If I remove the checks from where I placed them and move them deeper (
__queue_work()for example), I won't know how to print the "caller," making it harder for people to understand which code is using the deprecated workqueue.
Quoted 6 lines
Quoted 3 lines
Printing the stack with
dump_stack()is not doable.Do you have suggestions?
For workqueues, identifying the work function and workqueue name should be good enough. It's not like these are difficult to find.
Cool, let's do like this.
Thanks!
Signature · 3 lines
Marco Crivellari SUSE Labs
RFC PATCH v2 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
@@ -5890,7 +5890,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 )
@@ -5914,6 +5914,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