usbnet: fix EVENT_RX_PAUSED race in usbnet_skb_return()
Subsystems (5)
PATCH v5 1/1 usbnet: fix EVENT_RX_PAUSED race in usbnet_skb_return()
On SMP, usbnet_skb_return() can observe EVENT_RX_PAUSED as set on CPU0 while usbnet_resume_rx() clears the flag and drains rxq_pause on CPU1. The skb can then be queued after the drain has completed and remain stranded until the next MTU change or device stop.
Synchronize the EVENT_RX_PAUSED state transition and paused-queue dequeue operations with rxq_pause.lock. In usbnet_resume_rx() splice the paused queue into a local queue under the lock, then drain the local queue lock-free. In usbnet_skb_return() use a double-check pattern under rxq_pause.lock on the slow path, keeping the normal RX fast path lock-free. Use spin_lock_irqsave() to match the lockdep class shared with other sk_buff_head locks taken from hardirq context in defer_bh().
Discovered by Sashiko, not triggered, not tested on real hardware.
---
v5: Splice rxq_pause into a local queue under the lock and drain
lock-free, avoiding lock drop/reacquire per iteration, as suggested by Jakub Kicinski; fix reverse xmas tree ordering.
drivers/net/usb/usbnet.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/net/usb/usbnet.c +16 -4
@@ -329,9 +329,14 @@ void usbnet_skb_return(struct usbnet *dev, struct sk_buff *skb)unsigned long flags;int status; -if (test_bit (EVENT_RX_PAUSED , &dev->flags)) { -skb_queue_tail (&dev->rxq_pause, skb); -return ; +if (unlikely (test_bit (EVENT_RX_PAUSED , &dev->flags))) { +spin_lock_irqsave (&dev->rxq_pause.lock, flags); +if (test_bit (EVENT_RX_PAUSED , &dev->flags)) { +__skb_queue_tail (&dev->rxq_pause, skb); +spin_unlock_irqrestore (&dev->rxq_pause.lock, flags); +return ; + } +spin_unlock_irqrestore (&dev->rxq_pause.lock, flags); }/* only update if unset to allow minidriver rx_fixup override */
@@ -698,13 +703,20 @@ EXPORT_SYMBOL_GPL(usbnet_pause_rx);void usbnet_resume_rx (struct usbnet *dev) { +struct sk_buff_head q;struct sk_buff *skb; +unsigned long flags;int num =0 ; +__skb_queue_head_init (&q); +local_bh_disable (); +spin_lock_irqsave (&dev->rxq_pause.lock, flags);clear_bit (EVENT_RX_PAUSED , &dev->flags); +skb_queue_splice_init (&dev->rxq_pause, &q); +spin_unlock_irqrestore (&dev->rxq_pause.lock, flags); -while ((skb =skb_dequeue (&dev->rxq_pause)) !=NULL ) { +while ((skb =__skb_dequeue (&q))) {usbnet_skb_return (dev, skb); num++; }
--
2.56.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
On SMP,
usbnet_skb_return()can observeEVENT_RX_PAUSEDas set on CPU0 whileusbnet_resume_rx()clears the flag and drainsrxq_pauseon CPU1. The skb can then be queued after the drain has completed and remain stranded until the next MTU change or device stop.Synchronize the
EVENT_RX_PAUSEDstate transition and paused-queue dequeue operations with rxq_pause.lock. Inusbnet_resume_rx()splice the paused queue into a local queue under the lock, then drain the local queue lock-free. Inusbnet_skb_return()use a double-check pattern under rxq_pause.lock on the slow path, keeping the normal RX fast path lock-free. Usespin_lock_irqsave()to match the lockdep class shared with othersk_buff_headlocks taken from hardirq context indefer_bh().
Quoted 4 lines
Discovered by Sashiko, not triggered, not tested on real hardware.
Fixes 43daa96b166c ("usbnet: Stop RX Q on MTU change")Signed-off-by Ömer Mete Kaya <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 14 lines · On Sat, 24 Oct 2026 11:42:08 +0300 you wrote:
On SMP,
usbnet_skb_return()can observeEVENT_RX_PAUSEDas set on CPU0 whileusbnet_resume_rx()clears the flag and drainsrxq_pauseon CPU1. The skb can then be queued after the drain has completed and remain stranded until the next MTU change or device stop.Synchronize the
EVENT_RX_PAUSEDstate transition and paused-queue dequeue operations with rxq_pause.lock. Inusbnet_resume_rx()splice the paused queue into a local queue under the lock, then drain the local queue lock-free. Inusbnet_skb_return()use a double-check pattern under rxq_pause.lock on the slow path, keeping the normal RX fast path lock-free. Usespin_lock_irqsave()to match the lockdep class shared with othersk_buff_headlocks taken from hardirq context indefer_bh().
Quoted 1 line
[...]
Here is the summary with links:
- [net,v5] usbnet: fix EVENT_RX_PAUSED race in usbnet_skb_return()
https://git.kernel.org/netdev/net/c/67ae1b80cf73
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html