Skip to contentSkip to conversation

net: usb: rtl8150: break cycle between tasklet and URB

PATCH New Oliver Neukum 3 participants 3 messages Nov 18 – Nov 21

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: usb: rtl8150: break cycle between tasklet and URB

The tasklet can resubmit the URB, which in turn can resubmit the tasklet. The cycle needs to be broken. The simplest way to do so is to prevent the resubmission of the URBs by poisoning them.

This has been detected by automatic code analysis.

Fixes eff674a514bd3 ("USB: rtl8150_disconnect() needs tasklet_kill()")
---
 drivers/net/usb/rtl8150.c | 10 +++++++++-

V2: Sashiko pointed out errors which conceptually arose from handling close() and disconnect() in the same way. Treating disconnect() separatly makes them go away

 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/usb/rtl8150.c +9 -1
index d51e43170e03..6d20f52d52ef 100644
@@ -353,6 +353,13 @@ static void free_all_urbs(rtl8150_t * dev)
 	usb_free_urb(dev->intr_urb);
 }
 
+static void terminate_all_urbs(rtl8150_t *dev)
+{
+	usb_poison_urb(dev->rx_urb);
+	usb_poison_urb(dev->tx_urb);
+	usb_poison_urb(dev->intr_urb);
+}
+
 static void unlink_all_urbs(rtl8150_t * dev)
 {
 	usb_kill_urb(dev->rx_urb);
@@ -970,7 +977,8 @@ static void rtl8150_disconnect(struct usb_interface *intf)
 		set_bit(RTL8150_UNPLUG, &dev->flags);
 		tasklet_kill(&dev->tl);
 		unregister_netdev(dev->netdev);
-		unlink_all_urbs(dev);
+		terminate_all_urbs(dev);
+		tasklet_kill(&dev->tl);
 		free_all_urbs(dev);
 		free_skb_pool(dev);
 		dev_kfree_skb(dev->rx_skb);
Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] net: usb: rtl8150: tasklet livelock on poisoned URB in rx_fixup() - [Medium] net: usb: rtl8150: URBs…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] net: usb: rtl8150: tasklet livelock on poisoned URB in rx_fixup()
- [Medium] net: usb: rtl8150: URBs poisoned too late in rtl8150_disconnect()
--

commit 1541c4307c346b500c4b1389da4c563d28fd64b6
Author: Oliver Neukum <[email protected]>

net: usb: rtl8150: break cycle between tasklet and URB

This commit replaces unlink_all_urbs() with terminate_all_urbs() using usb_poison_urb() in rtl8150_disconnect(), followed by a second tasklet_kill(), to break the resubmission cycle between URBs and the tasklet.

Quoted 19 lines
drivers/net/usb/rtl8150.c +7 -0
index d51e43170e033..6d20f52d52efd 100644
@@ -353,6 +353,13 @@ static void free_all_urbs(rtl8150_t * dev)
 	usb_free_urb(dev->intr_urb);
 }
 
+static void terminate_all_urbs(rtl8150_t *dev)
+{
+	usb_poison_urb(dev->rx_urb);
+	usb_poison_urb(dev->tx_urb);
+	usb_poison_urb(dev->intr_urb);
+}
+
 static void unlink_all_urbs(rtl8150_t * dev)
 {
 	usb_kill_urb(dev->rx_urb);

drivers/net/usb/rtl8150.c static void rtl8150_disconnect(struct usb_interface *intf) · line 980

Quoted 5 lines
drivers/net/usb/rtl8150.c +1 -1
@@ -970,7 +977,8 @@ static void rtl8150_disconnect(struct usb_interface *intf)
 		set_bit(RTL8150_UNPLUG, &dev->flags);
 		tasklet_kill(&dev->tl);
 		unregister_netdev(dev->netdev);
-		unlink_all_urbs(dev);
+		terminate_all_urbs(dev);

[Severity: Medium]
Can rx_fixup() still run during unregister_netdev() before terminate_all_urbs() poisons the URBs?

CPU0 CPU1

                                        read_bulk_callback()
                                          test_bit(RTL8150_UNPLUG, ...)

rtl8150_disconnect()
set_bit(RTL8150_UNPLUG, ...)
tasklet_kill(&dev->tl)
unregister_netdev(dev->netdev)

    ...
      rtl8150_close()
        unlink_all_urbs(dev)
          usb_kill_urb(dev->rx_urb)
                                          usb_submit_urb() -> -EPERM
                                          tasklet_schedule(&dev->tl)
          /* urb->reject returns to 0 */
                                        rx_fixup()
                                          usb_submit_urb(dev->rx_urb, ...)

terminate_all_urbs(dev)

Because rtl8150_close() only uses usb_kill_urb(), dev->rx_urb is unpoisoned again as soon as usb_kill_urb() returns. If an in-flight read_bulk_callback() passed the RTL8150_UNPLUG check just before rtl8150_disconnect() set the bit, its usb_submit_urb() fails with -EPERM while usb_kill_urb() is waiting and schedules dev->tl. rx_fixup() can then run before terminate_all_urbs() and either resubmit dev->rx_urb after rtl8150_close() or, if usb_submit_urb() returns -ENODEV, call netif_device_detach() on an unregistered net_device.

Would calling terminate_all_urbs() before the first tasklet_kill() and unregister_netdev() avoid this window?

drivers/net/usb/rtl8150.c static void rtl8150_disconnect(struct usb_interface *intf) · line 981

Quoted 1 line
drivers/net/usb/rtl8150.c +1 -0
+		tasklet_kill(&dev->tl);

[Severity: High]
Will tasklet_kill(&dev->tl) livelock if dev->tl is scheduled after dev->rx_urb is poisoned?

drivers/net/usb/rtl8150.c:rx_fixup() {
	...
try_again:
	status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
	if (status == -ENODEV) {
		netif_device_detach(dev->netdev);
	} else if (status) {
		set_bit(RX_URB_FAIL, &dev->flags);
		goto tlsched;
	} else {
		clear_bit(RX_URB_FAIL, &dev->flags);
	}

	return;
tlsched:
	tasklet_schedule(&dev->tl);
}

Submitting a poisoned URB returns -EPERM (or -ENOENT once the endpoint is disabled), not -ENODEV. Because rx_fixup() does not check RTL8150_UNPLUG and treats any error other than -ENODEV as retryable, it sets RX_URB_FAIL and reschedules dev->tl on every run. On subsequent runs RX_URB_FAIL makes rx_fixup() jump straight to try_again, get -EPERM from the poisoned dev->rx_urb, and call tasklet_schedule(&dev->tl) again, spinning in softirq and hanging tasklet_kill() in rtl8150_disconnect() during driver unbind or disconnect.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Oliver Neukum
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 1 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] Poisoning the URBs does not break the cycle.
- [Medium] The tasklet/URB resubmission cycle from the commit message is still open in rtl8150_close() and rtl8150_suspend().

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but read_bulk_callback() hands dev->rx_skb to netif_rx() (line 430).
- [Medium] This problem wasn't introduced by this patch, but rx_fixup() has a second self-rescheduling path that never touches a URB.

--

net: usb: rtl8150: break cycle between tasklet and URB

In rtl8150_disconnect(), this patch swaps usb_kill_urb() for usb_poison_urb() on all three URBs. The goal is to stop the rx_fixup() tasklet from resubmitting rx_urb. It also adds a second tasklet_kill() after poisoning, so the tasklet is stopped before the URBs and skbs are freed.

Quoted 8 lines
drivers/net/usb/rtl8150.c +0 -0
index d51e43170e033..6d20f52d52efd 100644
@@ -353,6 +353,13 @@ static void free_all_urbs(rtl8150_t * dev)
 	usb_free_urb(dev->intr_urb);
 }
 

drivers/net/usb/rtl8150.c static void free_all_urbs(rtl8150_t * dev) · line 365

Quoted 10 lines
drivers/net/usb/rtl8150.c +7 -0
+static void terminate_all_urbs(rtl8150_t *dev)
+{
+	usb_poison_urb(dev->rx_urb);
+	usb_poison_urb(dev->tx_urb);
+	usb_poison_urb(dev->intr_urb);
+}
+
 static void unlink_all_urbs(rtl8150_t * dev)
 {
 	usb_kill_urb(dev->rx_urb);

[Severity: Medium]
The commit message says the tasklet/URB cycle needs to be broken. Is it still open in rtl8150_close() and rtl8150_suspend()? Both stop rx_urb with usb_kill_urb() only, and neither one stops the tasklet:

rtl8150_close() {
    ...
	if (!test_bit(RTL8150_UNPLUG, &dev->flags))
		disable_net_traffic(dev);
	unlink_all_urbs(dev);
    ...
}

usb_kill_urb() raises urb->reject only while the call is running. If rx_fixup() is pending or looping when close runs, its submit fails with -EPERM during the kill and it reschedules itself. The next run, after usb_kill_urb() has returned, then submits rx_urb successfully:

rx_fixup() {
    ...
try_again:
	status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
    ...
}

The tasklet can easily be pending at close time. read_bulk_callback() schedules it whenever the 4-entry rx_skb_pool runs dry, and it keeps looping while GFP_ATOMIC allocation fails.

disable_net_traffic() has already turned the receiver off, so the bulk-IN URB just stays in flight. On the next ndo_open, rtl8150_open() refills and resubmits the URB that is still active:

	usb_fill_bulk_urb(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
		      dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
	if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL))) {

That would hit the "URB %p submitted while active" WARN_ONCE in usb_submit_urb() and return -EBUSY. Wouldn't every later open then fail the same way until the device is replugged or rebound?

The suspend path looks similar. A tasklet submit can follow the usb_kill_urb() in rtl8150_suspend(), and a later resubmit of rx_urb after resume would then fail with -EBUSY.

[ ... ]

drivers/net/usb/rtl8150.c static void rtl8150_disconnect(struct usb_interface *intf) · line 981

Quoted 7 lines
drivers/net/usb/rtl8150.c +2 -1
@@ -970,7 +977,8 @@ static void rtl8150_disconnect(struct usb_interface *intf)
 		set_bit(RTL8150_UNPLUG, &dev->flags);
 		tasklet_kill(&dev->tl);
 		unregister_netdev(dev->netdev);
-		unlink_all_urbs(dev);
+		terminate_all_urbs(dev);
+		tasklet_kill(&dev->tl);

[Severity: High]
Does poisoning actually break the cycle here? It makes every resubmit fail. However, rx_fixup() reschedules itself after any submit error other than -ENODEV, and it never checks RTL8150_UNPLUG:

rx_fixup() {
    ...
	if (test_bit(RX_URB_FAIL, &dev->flags))
		if (dev->rx_skb)
			goto try_again;
    ...
try_again:
	status = usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
	if (status == -ENODEV) {
		netif_device_detach(dev->netdev);
	} else if (status) {
		set_bit(RX_URB_FAIL, &dev->flags);
		goto tlsched;
    ...
}

On a soft unbind (rmmod, sysfs unbind, or a forced unbind from usb_reset_device()), the device is still attached, so usb_submit_urb() never returns -ENODEV. Instead:

- rtl8150_driver has no soft_unbind, so usb_unbind_interface() disables the endpoints before calling ->disconnect. Submits then fail with -ENOENT from usb_hcd_link_urb_to_ep().
- After terminate_all_urbs(), urb->reject stays raised for good, so submits fail with -EPERM.

Either way, once the tasklet runs it reschedules itself forever.

tasklet_action_common() clears TASKLET_STATE_SCHED just before it calls the callback, and the callback sets it again. tasklet_kill() waits with wait_on_bit_lock(SCHED) in TASK_UNINTERRUPTIBLE, so it can only win inside that short window.

On a non-RT kernel, a waiter on the same CPU cannot preempt the softirq, so it only ever sees SCHED set. On UP, wouldn't this be a deterministic livelock? rtl8150_disconnect() would sit in D state holding the device lock while ksoftirqd spins. On SMP the waiter can keep losing the race.

The loop can also start before this point. On a soft unbind with the interface up, usb_disable_interface() flushes rx_urb with -ESHUTDOWN while RTL8150_UNPLUG is still clear. read_bulk_callback() takes the default case to goon, the resubmit fails with -ENOENT, and it calls tasklet_schedule(). So the existing first tasklet_kill() already faces this loop, and on a soft unbind the new second one always faces it.

Could rx_fixup() be stopped from rescheduling instead? For example, it could check RTL8150_UNPLUG on entry or treat -EPERM/-ENOENT as fatal. Another option is to disable the tasklet before killing it.

[Severity: Medium]
This isn't a bug introduced by this patch, but rx_fixup() has a second self-rescheduling path that never touches a URB:

rx_fixup() {
    ...
	spin_lock_irq(&dev->rx_pool_lock);
	skb = pull_skb(dev);
	spin_unlock_irq(&dev->rx_pool_lock);
	if (skb == NULL)
		goto tlsched;
    ...
}

If dev_alloc_skb(RTL8150_MTU + 2) in fill_skb_pool() fails while the pool is empty, pull_skb() returns NULL and the tasklet reschedules itself. Poisoning in terminate_all_urbs() has no effect on this path, and RTL8150_UNPLUG is not checked.

While GFP_ATOMIC allocations keep failing at disconnect time, can both tasklet_kill() calls in rtl8150_disconnect() stall the same way as described above? The stall ends once an allocation succeeds, so it only lasts as long as the memory pressure. A check for RTL8150_UNPLUG at the top of rx_fixup() would fix this along with the issue above.

drivers/net/usb/rtl8150.c static void rtl8150_disconnect(struct usb_interface *intf) · line 984

Quoted 3 lines
drivers/net/usb/rtl8150.c +0 -0
 		free_all_urbs(dev);
 		free_skb_pool(dev);
 		dev_kfree_skb(dev->rx_skb);

[Severity: High]
This is a pre-existing issue, but can dev->rx_skb be stale here? read_bulk_callback() hands dev->rx_skb to the stack. When the pool is empty, it leaves the old pointer in place:

read_bulk_callback() {
    ...
	netif_rx(dev->rx_skb);
    ...
	spin_lock_irqsave(&dev->rx_pool_lock, flags);
	skb = pull_skb(dev);
	spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
	if (!skb)
		goto resched;

	dev->rx_skb = skb;
    ...
}

The pool has only 4 entries and only the tasklet refills it, so this happens routinely. Nothing ever sets dev->rx_skb to NULL. The pointer is only replaced by a later successful pull_skb() in rx_fixup().

If that replacement never happens, this dev_kfree_skb() frees an skb that the network stack already owns. It can fail to happen in two ways:

- dev_alloc_skb() in rx_fixup() fails on the run during which tasklet_kill() wins.
- A completion passes the RTL8150_UNPLUG check just before set_bit(). Its tasklet_schedule() then runs while the first tasklet_kill() holds TASKLET_STATE_SCHED through wait_on_bit_lock(). tasklet_schedule() does nothing when SCHED is already set, so the tasklet never runs.

The same stale pointer also seems to affect rtl8150_open():

	if (dev->rx_skb == NULL)
		dev->rx_skb = pull_skb(dev);

Suppose a close/open happens while the tasklet is still looping on allocation failure. Wouldn't rx_urb then be submitted with the freed skb's data as its DMA buffer?

Because of this, the "if (!dev->rx_skb) goto resched;" check in read_bulk_callback() also looks like dead code.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118133550.955312-1-oneukum%40suse.com