net: Move system_long_wq to system_dfl_long_wq
Subsystems (9)
v3 cover net: Move system_long_wq to system_dfl_long_wq
Hello,
Currently the code uses the per-cpu workqueue system_long_wq to schedule long running works.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Another good reason to have this unbound, is the "queue_delayed_work()" function, used to enqueue the work item. More details on this will follow in the next section.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
~~~ Details about queue_delayed_work ~~~
system_long_wq is a per-cpu workqueue and it is used as a parameter of queue_delayed_work(). This function schedule an item that it will later be enqueued (once the timer will fire). __queue_delayed_work() does the job receiving as "cpu" WORK_CPU_UNBOUND:
if (housekeeping_enabled(HK_TYPE_TIMER)) {
// [....]
} else {
if (likely(cpu == WORK_CPU_UNBOUND))
add_timer_global(timer);
else
add_timer_on(timer, cpu);
}The timer is global, so can fire everywhere, and the work item will be enqueued where the timer fired.
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change the workqueue with the new system_dfl_long_wq, so that the used workqueue is now unbound and can benefit from scheduler task placement.
Thanks!
---
Changes in v3:
- rebased on v7.2-rc4
- added "net: ti: icssg-prueth: Move long delayed work on system_dfl_long_wq"
- changed also the workqueue in ibmvnic_reset().
Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- rebased on v7.2-rc2
- dropped the RFC prefix, kept Ack and review tags
Link to v1: https://lore.kernel.org/all/[email protected]/
Marco Crivellari (6):
ibmvnic: Move long delayed work on system_dfl_long_wq
net: ti: icssg-stats: Move long delayed work on system_dfl_long_wq
net: ti: icssg-prueth: Move long delayed work on system_dfl_long_wq
net: thunderbolt: Move long delayed work on system_dfl_long_wq
net: usb: pegasus: Move long delayed work on system_dfl_long_wq
net: usb: r8152: Move long delayed work on system_dfl_long_wq
drivers/net/ethernet/ibm/ibmvnic.c | 6 +++--- drivers/net/ethernet/ti/icssg/icssg_prueth.c | 2 +- drivers/net/ethernet/ti/icssg/icssg_stats.c | 2 +- drivers/net/thunderbolt/main.c | 7 ++++--- drivers/net/usb/pegasus.c | 9 +++++---- drivers/net/usb/r8152.c | 7 ++++--- 6 files changed, 18 insertions(+), 15 deletions(-)
Signature · 1 line
2.54.0
PATCH v3 1/6 ibmvnic: Move long delayed work on system_dfl_long_wq
Currently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/ethernet/ibm/ibmvnic.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/ibm/ibmvnic.c +3 -3
@@ -3229,7 +3229,7 @@ static void __ibmvnic_reset(struct work_struct *work)if (adapter->state ==VNIC_PROBING && !wait_for_completion_timeout (&adapter->probe_done, timeout)) {dev_err (dev,"Reset thread timed out on probe" ); -queue_delayed_work (system_long_wq, +queue_delayed_work (system_dfl_long_wq, &adapter->ibmvnic_delayed_reset,IBMVNIC_RESET_DELAY );return ;
@@ -3267,7 +3267,7 @@ static void __ibmvnic_reset(struct work_struct *work)spin_lock (&adapter->rwi_lock);if (!list_empty (&adapter->rwi_list)) {if (test_and_set_bit_lock (0 , &adapter->resetting)) { -queue_delayed_work (system_long_wq, +queue_delayed_work (system_dfl_long_wq, &adapter->ibmvnic_delayed_reset,IBMVNIC_RESET_DELAY ); }else {
@@ -3454,7 +3454,7 @@ static int ibmvnic_reset(struct ibmvnic_adapter *adapter,list_add_tail (&rwi->list, &adapter->rwi_list);netdev_dbg (adapter->netdev,"Scheduling reset (reason %s)\n" ,reset_reason_to_string (reason)); -queue_work (system_long_wq, &adapter->ibmvnic_reset); +queue_work (system_dfl_long_wq, &adapter->ibmvnic_reset); ret =0 ; err:
Signature · 1 line
2.54.0
PATCH v3 2/6 net: ti: icssg-stats: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariCurrently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/ethernet/ti/icssg/icssg_stats.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_stats.c +1 -1
@@ -69,7 +69,7 @@ void icssg_stats_work_handler(struct work_struct *work) stats_work.work);emac_update_hardware_stats (emac); -queue_delayed_work (system_long_wq, &emac->stats_work, +queue_delayed_work (system_dfl_long_wq, &emac->stats_work,msecs_to_jiffies ((STATS_TIME_LIMIT_1G_MS *1000 ) / emac->speed)); }EXPORT_SYMBOL_GPL (icssg_stats_work_handler);
Signature · 1 line
2.54.0
PATCH v3 4/6 net: thunderbolt: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariCurrently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/thunderbolt/main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/thunderbolt/main.c +4 -3
@@ -316,7 +316,7 @@ static void start_login(struct tbnet *net) net->login_received = false;mutex_unlock (&net->connection_lock); -queue_delayed_work (system_long_wq, &net->login_work, +queue_delayed_work (system_dfl_long_wq, &net->login_work,msecs_to_jiffies (1000 )); }
@@ -460,7 +460,7 @@ static int tbnet_handle_packet(const void *buf, size_t size, void *data)if (net->login_retries >=TBNET_LOGIN_RETRIES || !net->login_sent) { net->login_retries =0 ; -queue_delayed_work (system_long_wq, +queue_delayed_work (system_dfl_long_wq, &net->login_work,0 ); }mutex_unlock (&net->connection_lock);
@@ -700,7 +700,8 @@ static void tbnet_login_work(struct work_struct *work)netdev_dbg (net->dev,"sending login request failed, ret=%d\n" , ret);if (net->login_retries++ <TBNET_LOGIN_RETRIES ) { -queue_delayed_work (system_long_wq, &net->login_work, +queue_delayed_work (system_dfl_long_wq, + &net->login_work, delay); }else {netdev_info (net->dev,"ThunderboltIP login timed out\n" );
Signature · 1 line
2.54.0
PATCH v3 3/6 net: ti: icssg-prueth: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariCurrently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/ethernet/ti/icssg/icssg_prueth.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/ti/icssg/icssg_prueth.c +1 -1
@@ -1100,7 +1100,7 @@ static int emac_ndo_open(struct net_device *ndev) prueth->emacs_initialized++; -queue_work (system_long_wq, &emac->stats_work.work); +queue_work (system_dfl_long_wq, &emac->stats_work.work);return 0 ;
Signature · 1 line
2.54.0
PATCH v3 5/6 net: usb: pegasus: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariCurrently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/usb/pegasus.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/usb/pegasus.c +5 -4
@@ -1126,8 +1126,9 @@ static void check_carrier(struct work_struct *work)pegasus_t *pegasus =container_of (work, pegasus_t, carrier_check.work);set_carrier (pegasus->net);if (!(pegasus->flags &PEGASUS_UNPLUG )) { -queue_delayed_work (system_long_wq, &pegasus->carrier_check, -CARRIER_CHECK_DELAY ); +queue_delayed_work (system_dfl_long_wq, + &pegasus->carrier_check, +CARRIER_CHECK_DELAY ); } }
@@ -1232,7 +1233,7 @@ static int pegasus_probe(struct usb_interface *intf, res =register_netdev (net);if (res) goto out3; -queue_delayed_work (system_long_wq, &pegasus->carrier_check, +queue_delayed_work (system_dfl_long_wq, &pegasus->carrier_check,CARRIER_CHECK_DELAY );dev_info (&intf->dev,"%s, %s, %pM\n" , net->name, usb_dev_id[dev_index].name, net->dev_addr);
@@ -1297,7 +1298,7 @@ static int pegasus_resume(struct usb_interface *intf) pegasus->intr_urb->actual_length =0 ;intr_callback (pegasus->intr_urb); } -queue_delayed_work (system_long_wq, &pegasus->carrier_check, +queue_delayed_work (system_dfl_long_wq, &pegasus->carrier_check,CARRIER_CHECK_DELAY );return 0 ; }
Signature · 1 line
2.54.0
PATCH v3 6/6 net: usb: r8152: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariCurrently the code enqueue work items using {queue|mod}_delayed_work(), using system_long_wq. This workqueue should be used when long works are expected and it is a per-cpu workqueue.
The function(s) end up calling __queue_delayed_work(), which set a global timer that could fire anywhere, enqueuing the work where the timer fired.
Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Long work shouldn't stick to a single CPU.
Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e ("workqueue: Add system_dfl_long_wq for long unbound works")
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change system_long_wq with system_dfl_long_wq so that the work may benefit from scheduler task placement.
--- drivers/net/usb/r8152.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/usb/r8152.c +4 -3
@@ -7072,7 +7072,8 @@ static void rtl_hw_phy_work_func_t(struct work_struct *work)/* Delay execution in case request_firmware() is not ready yet. */ -queue_delayed_work (system_long_wq, &tp->hw_phy_work,HZ *10 ); +queue_delayed_work (system_dfl_long_wq, &tp->hw_phy_work, +HZ *10 ); goto ignore_once; }
@@ -8840,7 +8841,7 @@ static int rtl8152_reset_resume(struct usb_interface *intf)clear_bit (SELECTIVE_SUSPEND , &tp->flags);rtl_reset_ocp_base (tp); tp->rtl_ops.init (tp); -queue_delayed_work (system_long_wq, &tp->hw_phy_work,0 ); +queue_delayed_work (system_dfl_long_wq, &tp->hw_phy_work,0 );set_ethernet_addr (tp, true);return rtl8152_resume (intf); }
@@ -10295,7 +10296,7 @@ static int rtl8152_probe_once(struct usb_interface *intf,/* Retry in case request_firmware() is not ready yet. */ tp->rtl_fw.retry = true;#endif -queue_delayed_work (system_long_wq, &tp->hw_phy_work,0 ); +queue_delayed_work (system_dfl_long_wq, &tp->hw_phy_work,0 );set_ethernet_addr (tp, false);usb_set_intfdata (intf, tp);
Signature · 1 line
2.54.0
Quoted 31 lines · Marco Crivellari
Hello,
Currently the code uses the per-cpu workqueue
system_long_wqto schedule long running works.Unbound works could benefit from scheduler task placement, to optimize performance and power consumption. Another good reason to have this unbound, is the "
queue_delayed_work()" function, used to enqueue the work item. More details on this will follow in the next section.Recently, a new unbound workqueue specific for long running work has been added:
c116737e972e("workqueue: Addsystem_dfl_long_wqfor long unbound works")~~~ Details about
queue_delayed_work~~~
system_long_wqis a per-cpu workqueue and it is used as a parameter ofqueue_delayed_work(). This function schedule an item that it will later be enqueued (once the timer will fire).__queue_delayed_work()does the job receiving as "cpu"WORK_CPU_UNBOUND:if (housekeeping_enabled(HK_TYPE_TIMER)) { // [....] } else { if (likely(cpu == WORK_CPU_UNBOUND)) add_timer_global(timer); else add_timer_on(timer, cpu); }
Quoted 8 lines
The timer is global, so can fire everywhere, and the work item will be enqueued where the timer fired.
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change the workqueue with the new
system_dfl_long_wq, so that the used workqueue is now unbound and can benefit from scheduler task placement.
Ok. So if I am understanding this correctly, the current code uses system_long_wq which is per-CPU, but is fired using an unbound timer. As a result, whichever CPU the timer triggers on will be the one which selects the work queue. From there, the work item will be enqueued to that work queue and remain on that work queue until resolving with no way for scheduler to adjust it?
With the new change, we schedule on the system_dfl_long_wq which *isn't* per CPU, so the scheduler is free to move the task around and reschedule. As a result we get better overall behavior with more input from the scheduler, instead of effective randomness from the timer which is then forced so that such long running task cannot migrate?
That sounds like a pretty good improvement for the cases where the queued work doesn't depend on any per-cpu behavior. Nice!
I am not sure I can speak to any of the individual drivers here since I wouldn't know whether moving that particular work item would be affected.. so feel free to take this review with a grain of salt :)
Hi,
Quoted 32 lines · Jacob Keller
[...]
Quoted 14 lines
system_long_wqis a per-cpu workqueue and it is used as a parameter ofqueue_delayed_work(). This function schedule an item that it will later be enqueued (once the timer will fire).__queue_delayed_work()does the job receiving as "cpu"WORK_CPU_UNBOUND:if (housekeeping_enabled(HK_TYPE_TIMER)) { // [....] } else { if (likely(cpu == WORK_CPU_UNBOUND)) add_timer_global(timer); else add_timer_on(timer, cpu); }Quoted 8 lines
The timer is global, so can fire everywhere, and the work item will be enqueued where the timer fired.
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change the workqueue with the new
system_dfl_long_wq, so that the used workqueue is now unbound and can benefit from scheduler task placement.Ok. So if I am understanding this correctly, the current code uses
system_long_wqwhich is per-CPU, but is fired using an unbound timer. As a result, whichever CPU the timer triggers on will be the one which selects the work queue. From there, the work item will be enqueued to that work queue and remain on that work queue until resolving with no way for scheduler to adjust it?
Quoted 8 lines
With the new change, we schedule on the
system_dfl_long_wqwhich *isn't* per CPU, so the scheduler is free to move the task around and reschedule. As a result we get better overall behavior with more input from the scheduler, instead of effective randomness from the timer which is then forced so that such long running task cannot migrate?That sounds like a pretty good improvement for the cases where the queued work doesn't depend on any per-cpu behavior. Nice!
Yes, that's pretty much it!
Quoted 5 lines
I am not sure I can speak to any of the individual drivers here since I wouldn't know whether moving that particular work item would be affected.. so feel free to take this review with a grain of salt :)
Reviewed-by Jacob Keller <[email protected]>
Sure, thank you!
Signature · 3 lines
Marco Crivellari SUSE Labs
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
In reply to Marco CrivellariOn 20.07.26 12:08, Marco Crivellari wrote:
Hi,
Quoted 5 lines
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change
system_long_wqwithsystem_dfl_long_wqso that the work may benefit from scheduler task placement.
these changes are problematic, although they look like a good cleanup in first place. But the test you are using to determine whether USB devices need their own work queue is incomplete because you are not considering the reason they allocate their own work queues.
These drivers have their own work queues because they are part of the block layer. USB devices can share a device with a block device (storage & UAS) and USB devices have common, per device operations, in particular reset and runtime power management and disconnect handling. Because these operations can be necessary to complete block IO neither they nor anything
they depend on can use IO to allocate memory. That is they need to perform any memory allocation with GFP_NOIO or GFP_ATOMIC.
That is also true for any operation on a work queue they need to wait for to make progress. That means you cannot limit your check to per-cpu variables. You also need to check for such dependencies. In particular any usage of flush_work() on such queues can deadlock, if you use common queues.
Please refrain from making such changes unless you have fully analyzed the dependencies.
Regards
Oliver
Sebastian Andrzej Siewior
Reviewed-byRe: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 2 lines · Oliver Neukum
On 20.07.26 12:08, Marco Crivellari wrote:
Hi,
Hi Oliver,
Quoted 9 lines
Quoted 4 lines
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change
system_long_wqwithsystem_dfl_long_wqso that the work may benefit from scheduler task placement.these changes are problematic, although they look like a good cleanup in first place. But the test you are using to determine whether USB devices need their own work queue is incomplete because you are not considering the reason they allocate their own work queues.
This driver does not using its own workqueue.
Quoted 7 lines
These drivers have their own work queues because they are part of the block layer. USB devices can share a device with a block device (storage & UAS) and USB devices have common, per device operations, in particular reset and runtime power management and disconnect handling. Because these operations can be necessary to complete block IO neither they nor anything
they depend on can use IO to allocate memory. That is they need to perform any memory allocation withGFP_NOIOorGFP_ATOMIC.
This is a networking driver. It has nothing to do with storage and UAS. It uses USB, yes.
Quoted 5 lines
That is also true for any operation on a work queue they need to wait for to make progress. That means you cannot limit your check to per-cpu variables. You also need to check for such dependencies. In particular any usage of
flush_work()on such queues can deadlock, if you use common queues.
It has nothing to do with per-CPU variables. In fact its queue_delayed_work(,, CARRIER_CHECK_DELAY) usage already ensures that it can be executed on a random and not on the submitting CPU.
Quoted 2 lines
Please refrain from making such changes unless you have fully analyzed the dependencies.
Did you refer to the wrong patch? As far as this patch goes I can not reason why you assume Marco did not fully analyze the dependencies. I don't see anything wrong with this patch.
Quoted 3 lines
Regards
Oliver
Sebastian
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 25 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Oliver Neukum
On 20.07.26 12:08, Marco Crivellari wrote:
Hi,Hi Oliver,
Quoted 9 lines
Quoted 4 lines
Since the workqueue work doesn't rely on per-cpu variables, there is no obvious reason that justify the use of a per-cpu workqueue. So change
system_long_wqwithsystem_dfl_long_wqso that the work may benefit from scheduler task placement.these changes are problematic, although they look like a good cleanup in first place. But the test you are using to determine whether USB devices need their own work queue is incomplete because you are not considering the reason they allocate their own work queues.
This driver does not using its own workqueue.
Quoted 7 lines
These drivers have their own work queues because they are part of the block layer. USB devices can share a device with a block device (storage & UAS) and USB devices have common, per device operations, in particular reset and runtime power management and disconnect handling. Because these operations can be necessary to complete block IO neither they nor anything
they depend on can use IO to allocate memory. That is they need to perform any memory allocation withGFP_NOIOorGFP_ATOMIC.
Quoted 2 lines
This is a networking driver. It has nothing to do with storage and UAS. It uses USB, yes.
Ah, but a composite USB device can have both a networking interface and a mass-storage interface.
Suppose you have such a device, and suppose the disk attached to its mass-storage interface contains a swap partition. Now suppose the device is being reset, and as part of the preparation for that reset the networking driver needs to flush its workqueue. This means waiting until the work routines that are already running have completed.
Since it's a general-purpose workqueue, you don't know what those work routines are going to do. One of them might try to allocate memory using GFP_KERNEL. Suppose that in order to satisfy the memory request, the kernel decides it needs to write some pages to the swap partition on the USB mass-storage interface. But the mass-storage driver is stuck; it can't do anything until the device reset finishes. Deadlock.
That's why USB drivers have to use their own workqueues.
Alan Stern
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 8 lines · Alan Stern
Quoted 8 lines
Quoted 7 lines
These drivers have their own work queues because they are part of the block layer. USB devices can share a device with a block device (storage & UAS) and USB devices have common, per device operations, in particular reset and runtime power management and disconnect handling. Because these operations can be necessary to complete block IO neither they nor anything
they depend on can use IO to allocate memory. That is they need to perform any memory allocation withGFP_NOIOorGFP_ATOMIC.
Quoted 5 lines
Quoted 2 lines
This is a networking driver. It has nothing to do with storage and UAS. It uses USB, yes.
Ah, but a composite USB device can have both a networking interface and a mass-storage interface.
okay.
Quoted 5 lines
Suppose you have such a device, and suppose the disk attached to its mass-storage interface contains a swap partition. Now suppose the device is being reset, and as part of the preparation for that reset the networking driver needs to flush its workqueue. This means waiting until the work routines that are already running have completed.
The individual functions are usually independent. But if the USB core would reset the whole device it would reset each function.
Quoted 6 lines
Since it's a general-purpose workqueue, you don't know what those work routines are going to do. One of them might try to allocate memory using
GFP_KERNEL. Suppose that in order to satisfy the memory request, the kernel decides it needs to write some pages to the swap partition on the USB mass-storage interface. But the mass-storage driver is stuck; it can't do anything until the device reset finishes. Deadlock.
So you are saying, the USB-storage device is in reset and we wait until the networking part finishes its workqueue flush. That flush is stuck behind behind a memory allocation which waits on the storage device. So any URB passed to usb_submit_urb() just waits for the reset to complete?
Quoted 1 line
That's why USB drivers have to use their own workqueues.
While this does make sense I don't see how this is related to this patch. The pegasus driver uses `system_long_wq'. This is a system wide workqueue_struct and is not limited to USB or this driver. This workqueue is per-CPU meaning if you enqueue the work item on CPU3 it will be executed on CPU3. However pegasus uses a delayed work item and the timer can fire on any CPU so even if it is enqueued on CPU3 it could be executed on CPU1.
Therefore the suggested change system_long_wq -> system_dfl_long_wq should not make a difference here: it is a different workqueue and it is unbound (instead of per-CPU) but given the usage it is unchanged but more obvious. Also its usage recommendations (use this for long running items) is the same.
The plan is remove system_long_wq from the tree.
Quoted 1 line
Alan Stern
Sebastian
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 40 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Alan Stern
Quoted 8 lines
Quoted 7 lines
These drivers have their own work queues because they are part of the block layer. USB devices can share a device with a block device (storage & UAS) and USB devices have common, per device operations, in particular reset and runtime power management and disconnect handling. Because these operations can be necessary to complete block IO neither they nor anything
they depend on can use IO to allocate memory. That is they need to perform any memory allocation withGFP_NOIOorGFP_ATOMIC.Quoted 5 lines
Quoted 2 lines
This is a networking driver. It has nothing to do with storage and UAS. It uses USB, yes.
Ah, but a composite USB device can have both a networking interface and a mass-storage interface.
okay.
Quoted 5 lines
Suppose you have such a device, and suppose the disk attached to its mass-storage interface contains a swap partition. Now suppose the device is being reset, and as part of the preparation for that reset the networking driver needs to flush its workqueue. This means waiting until the work routines that are already running have completed.
The individual functions are usually independent. But if the USB core would reset the whole device it would reset each function.
Quoted 6 lines
Since it's a general-purpose workqueue, you don't know what those work routines are going to do. One of them might try to allocate memory using
GFP_KERNEL. Suppose that in order to satisfy the memory request, the kernel decides it needs to write some pages to the swap partition on the USB mass-storage interface. But the mass-storage driver is stuck; it can't do anything until the device reset finishes. Deadlock.So you are saying, the USB-storage device is in reset and we wait until the networking part finishes its workqueue flush. That flush is stuck behind behind a memory allocation which waits on the storage device. So any URB passed to
usb_submit_urb()just waits for the reset to complete?Quoted 1 line
That's why USB drivers have to use their own workqueues.
Quoted 7 lines
While this does make sense I don't see how this is related to this patch. The pegasus driver uses `system_long_wq'. This is a system wide
workqueue_structand is not limited to USB or this driver. This workqueue is per-CPU meaning if you enqueue the work item on CPU3 it will be executed on CPU3. However pegasus uses a delayed work item and the timer can fire on any CPU so even if it is enqueued on CPU3 it could be executed on CPU1.
It doesn't matter what CPU the work item runs on. Here's the deadlock sequence, in brief:
USB device reset cannot proceed until network interface's ->pre_reset() method returns.
The ->pre_reset() method cannot return until its call to flush_workqueue() returns.
flush_workqueue() cannot return until the already executing work item finishes.
The work item cannot finish until its kmalloc() call returns.
kmalloc() won't return until the kernel can free up memory by writing some pages to the swap partition.
The write to the swap partition cannot take place until the usb_storage/uas driver carries it out.
usb_storage/uas cannot do anything until the USB device reset is finished.
Quoted 7 lines
Therefore the suggested change
system_long_wq->system_dfl_long_wqshould not make a difference here: it is a different workqueue and it is unbound (instead of per-CPU) but given the usage it is unchanged but more obvious. Also its usage recommendations (use this for long running items) is the same.The plan is remove
system_long_wqfrom the tree.
The point Oliver was making is that the driver shouldn't be using a general-purpose workqueue at all. Switching from one general-purpose workqueue to another ignores this point; it's not the right thing to do.
Alan Stern
Petko Manolov
maintainerRe: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 8 lines · Alan Stern
Quoted 6 lines · Sebastian Andrzej Siewior
Therefore the suggested change
system_long_wq->system_dfl_long_wqshould not make a difference here: it is a different workqueue and it is unbound (instead of per-CPU) but given the usage it is unchanged but more obvious. Also its usage recommendations (use this for long running items) is the same.
Quoted 5 lines
Quoted 1 line
The plan is remove
system_long_wqfrom the tree.The point Oliver was making is that the driver shouldn't be using a general-purpose workqueue at all. Switching from one general-purpose workqueue to another ignores this point; it's not the right thing to do.
I read this as "this driver should switch to its own work queue", correct?
Petko
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
In reply to Alan SternQuoted 11 lines · Alan Stern
Quoted 7 lines
While this does make sense I don't see how this is related to this patch. The pegasus driver uses `system_long_wq'. This is a system wide
workqueue_structand is not limited to USB or this driver. This workqueue is per-CPU meaning if you enqueue the work item on CPU3 it will be executed on CPU3. However pegasus uses a delayed work item and the timer can fire on any CPU so even if it is enqueued on CPU3 it could be executed on CPU1.It doesn't matter what CPU the work item runs on. Here's the deadlock sequence, in brief:
Quoted 8 lines
USB device reset cannot proceed until network interface's ->pre_reset() method returns.
The ->pre_reset() method cannot return until its call to
flush_workqueue()returns.
flush_workqueue()cannot return until the already executing work item finishes.
Not sure where you pointing at but you have an unordered workqueue (such as system_long_wq/ system_dfl_long_wq then you can have more than one work item executed in parallel. One item does not stall the other so you can flush your work item (waiting for it's completion) without having all other work item completed.
There is no need to flush the workqueue, that would force _all_ work item to complete. Flushing a workqueue would make sense if you have your own and you want to ensure that _all_ work items, that has been enqueued, did complete and you don't want to check them one by one.
To illustrate your point, the example would translate to something like the following:
| static struct work_struct test_worker_busy;
| static struct work_struct test_worker_reg;
|
| static void test_worker_complete_fn(struct work_struct *work)
| {
| int count = 0;
| while (1) {
| ssleep(1);
| count++;
| if (count > 10)
| break;
| }
| pr_err("%s()\n leaving", __func__);
| }
|
| static void test_worker_busy_fn(struct work_struct *work)
| {
| while (1) {
| ssleep(1);
| pr_err("%s()\n", __func__);
| }
| }
|
| static void the_workers(void)
| {
| INIT_WORK(&test_worker_busy, test_worker_busy_fn);
| INIT_WORK(&test_worker_reg, test_worker_complete_fn);
|
| queue_work(system_long_wq, &test_worker_busy);
| ssleep(1);
| queue_work(system_long_wq, &test_worker_reg);
| pr_err("%s() starting...\n", __func__);
| ssleep(1);
| pr_err("%s() cancel\n", __func__);
| flush_work(&test_worker_reg);
| pr_err("%s() moving on\n", __func__);
| }which leads to:
| [ 4.136593] the_workers() starting... | [ 4.137007] test_worker_busy_fn() | [ 5.161198] the_workers() cancel | [ 5.164832] test_worker_busy_fn() | [ 6.184710] test_worker_busy_fn() | [ 7.208525] test_worker_busy_fn() | [ 8.232754] test_worker_busy_fn() | [ 9.256522] test_worker_busy_fn() | [ 10.280716] test_worker_busy_fn() | [ 11.304523] test_worker_busy_fn() | [ 12.328671] test_worker_busy_fn() | [ 13.352517] test_worker_busy_fn() | [ 14.376842] test_worker_busy_fn() | [ 15.400539] test_worker_busy_fn() | [ 15.402676] test_worker_complete_fn() | [ 15.403290] the_workers() moving on | [ 16.424483] test_worker_busy_fn() | [ 17.448524] test_worker_busy_fn()
which means the test_worker_reg work item completed despite the fact that test_worker_busy remained busy.
Of course flushing the workqueue system_long_wq (via flush_workqueue()) would stall but also raise a warning…
Quoted 10 lines
The work item cannot finish until its
kmalloc()call returns.
kmalloc()won't return until the kernel can free up memory by writing some pages to the swap partition.The write to the swap partition cannot take place until the usb_storage/uas driver carries it out.
usb_storage/uas cannot do anything until the USB device reset is finished.
as shown, this is not a concern.
Quoted 11 lines
Quoted 7 lines
Therefore the suggested change
system_long_wq->system_dfl_long_wqshould not make a difference here: it is a different workqueue and it is unbound (instead of per-CPU) but given the usage it is unchanged but more obvious. Also its usage recommendations (use this for long running items) is the same.The plan is remove
system_long_wqfrom the tree.The point Oliver was making is that the driver shouldn't be using a general-purpose workqueue at all. Switching from one general-purpose workqueue to another ignores this point; it's not the right thing to do.
Still the wrong thing to do? The driver should do either flush_work() or cancel_work_sync() (not flush_workqueue()).
Quoted 1 line
Alan Stern
Sebastian
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 118 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Alan Stern
Quoted 7 lines
While this does make sense I don't see how this is related to this patch. The pegasus driver uses `system_long_wq'. This is a system wide
workqueue_structand is not limited to USB or this driver. This workqueue is per-CPU meaning if you enqueue the work item on CPU3 it will be executed on CPU3. However pegasus uses a delayed work item and the timer can fire on any CPU so even if it is enqueued on CPU3 it could be executed on CPU1.It doesn't matter what CPU the work item runs on. Here's the deadlock sequence, in brief:
Quoted 8 lines
USB device reset cannot proceed until network interface's ->pre_reset() method returns.
The ->pre_reset() method cannot return until its call to
flush_workqueue()returns.
flush_workqueue()cannot return until the already executing work item finishes.Not sure where you pointing at but you have an unordered workqueue (such as system_long_wq/
system_dfl_long_wqthen you can have more than one work item executed in parallel. One item does not stall the other so you can flush your work item (waiting for it's completion) without having all other work item completed.
There is no need to flush the workqueue, that would force _all_ work item to complete. Flushing a workqueue would make sense if you have your own and you want to ensure that _all_ work items, that has been enqueued, did complete and you don't want to check them one by one.To illustrate your point, the example would translate to something like the following:
| static struct work_struct test_worker_busy; | static struct work_struct test_worker_reg; | | static void test_worker_complete_fn(struct work_struct *work) | { | int count = 0; | while (1) { | ssleep(1); | count++; | if (count > 10) | break; | } | pr_err("%s()\n leaving", __func__); | } | | static void test_worker_busy_fn(struct work_struct *work) | { | while (1) { | ssleep(1); | pr_err("%s()\n", __func__); | } | } | | static void the_workers(void) | { | INIT_WORK(&test_worker_busy, test_worker_busy_fn); | INIT_WORK(&test_worker_reg, test_worker_complete_fn); | | queue_work(system_long_wq, &test_worker_busy); | ssleep(1); | queue_work(system_long_wq, &test_worker_reg); | pr_err("%s() starting...\n", __func__); | ssleep(1); | pr_err("%s() cancel\n", __func__); | flush_work(&test_worker_reg); | pr_err("%s() moving on\n", __func__); | }which leads to:
| [ 4.136593] the_workers() starting... | [ 4.137007] test_worker_busy_fn() | [ 5.161198] the_workers() cancel | [ 5.164832] test_worker_busy_fn() | [ 6.184710] test_worker_busy_fn() | [ 7.208525] test_worker_busy_fn() | [ 8.232754] test_worker_busy_fn() | [ 9.256522] test_worker_busy_fn() | [ 10.280716] test_worker_busy_fn() | [ 11.304523] test_worker_busy_fn() | [ 12.328671] test_worker_busy_fn() | [ 13.352517] test_worker_busy_fn() | [ 14.376842] test_worker_busy_fn() | [ 15.400539] test_worker_busy_fn() | [ 15.402676] test_worker_complete_fn() | [ 15.403290] the_workers() moving on | [ 16.424483] test_worker_busy_fn() | [ 17.448524] test_worker_busy_fn()which means the
test_worker_regwork item completed despite the fact thattest_worker_busyremained busy.Of course flushing the workqueue
system_long_wq(viaflush_workqueue()) would stall but also raise a warning…Quoted 10 lines
The work item cannot finish until its
kmalloc()call returns.
kmalloc()won't return until the kernel can free up memory by writing some pages to the swap partition.The write to the swap partition cannot take place until the usb_storage/uas driver carries it out.
usb_storage/uas cannot do anything until the USB device reset is finished.
as shown, this is not a concern.
Quoted 6 lines
Quoted 6 lines
Therefore the suggested change
system_long_wq->system_dfl_long_wqshould not make a difference here: it is a different workqueue and it is unbound (instead of per-CPU) but given the usage it is unchanged but more obvious. Also its usage recommendations (use this for long running items) is the same.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
The plan is remove
system_long_wqfrom the tree.The point Oliver was making is that the driver shouldn't be using a general-purpose workqueue at all. Switching from one general-purpose workqueue to another ignores this point; it's not the right thing to do.
Still the wrong thing to do? The driver should do either
flush_work()orcancel_work_sync()(notflush_workqueue()).
I think we're in agreement. If the driver relies on calling flush_workqueue(), it should not use a general-purpose workqueue. However, flush_work() or cancel_work_sync() is okay on an unordered general-purpose workqueue.
If Oliver still has any objections, he can raise them.
Alan Stern
Petko Manolov
maintainerRe: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
Quoted 9 lines · Alan Stern
Quoted 3 lines · Sebastian Andrzej Siewior
Still the wrong thing to do? The driver should do either
flush_work()orcancel_work_sync()(notflush_workqueue()).I think we're in agreement. If the driver relies on calling
flush_workqueue(), it should not use a general-purpose workqueue. However,flush_work()orcancel_work_sync()is okay on an unordered general-purpose workqueue.
The driver is actually using queue_delayed_work()/cancel_delayed_work_sync() so the above is of no concern.
Due to HW limitations the carrier state should be checked on a regular interval and reported to the upper network layers. This worker relies only on generic USB transfers, to and from the usb-to-eth adapter, and as such does not depend on other I/O.
Failure to acquire correct carrier check does not seem to affect the device's functionality. Overall i don't think the proposed workqueue change is disruptive.
Petko
Re: [PATCH v3 net-next 5/6] net: usb: pegasus: Move long delayed work on system_dfl_long_wq
In reply to Alan SternQuoted 7 lines · Alan Stern
Quoted 2 lines
Still the wrong thing to do? The driver should do either
flush_work()orcancel_work_sync()(notflush_workqueue()).I think we're in agreement. If the driver relies on calling
flush_workqueue(), it should not use a general-purpose workqueue. However,flush_work()orcancel_work_sync()is okay on an unordered general-purpose workqueue.
Perfect, thank you.
Quoted 1 line
If Oliver still has any objections, he can raise them.
Okay. So far this is good to go ;)
Quoted 1 line
Alan Stern
Sebastian