Skip to contentSkip to conversation

net: usbnet: Fix the wrong netif_carrier_on() call placement

PATCH Superseded Ammar Faizi 5 participants 13 messages Aug 1, 2025 – Aug 12, 2025

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: usbnet: Fix the wrong netif_carrier_on() call placement

The commit in the Fixes tag breaks my laptop (found by git bisect). My home RJ45 LAN cable cannot connect after that commit.

The call to netif_carrier_on() should be done when netif_carrier_ok() is false. Not when it's true. Because calling netif_carrier_on() when __LINK_STATE_NOCARRIER is not set actually does nothing.

Cc Armando Budianto <[email protected]>
Fixes 0d9cfc9b8cb1 ("net: usbnet: Avoid potential RCU stall on LINK_CHANGE event")

---

v2:
- Rebase on top of the latest netdev/net tree. The previous patch was based on 0d9cfc9b8cb1. Line numbers have changed since then.

 drivers/net/usb/usbnet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/usb/usbnet.c +3 -3
index a38ffbf4b3f0..a1827684b92c 100644
@@ -1114,31 +1114,31 @@ static const struct ethtool_ops usbnet_ethtool_ops = {
 };
 
 /*-------------------------------------------------------------------------*/
 
 static void __handle_link_change(struct usbnet *dev)
 {
 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
 		return;
 
 	if (!netif_carrier_ok(dev->net)) {
+		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
+			netif_carrier_on(dev->net);
+
 		/* kill URBs for reading packets to save bus bandwidth */
 		unlink_urbs(dev, &dev->rxq);
 
 		/*
 		 * tx_timeout will unlink URBs for sending packets and
 		 * tx queue is stopped by netcore after link becomes off
 		 */
 	} else {
-		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
-			netif_carrier_on(dev->net);
-
 		/* submitting URBs for reading packets */
 		queue_work(system_bh_wq, &dev->bh_work);
 	}
 
 	/* hard_mtu or rx_urb_size may change during link change */
 	usbnet_update_max_qlen(dev);
 
 	clear_bit(EVENT_LINK_CHANGE, &dev->flags);
 }
 
Signature · 1 line
Ammar Faizi
reviewer
#1 lore raw

+ John Ernberg

Quoted 64 lines · Ammar Faizi

The commit in the Fixes tag breaks my laptop (found by git bisect). My home RJ45 LAN cable cannot connect after that commit.

The call to netif_carrier_on() should be done when netif_carrier_ok() is false. Not when it's true. Because calling netif_carrier_on() when __LINK_STATE_NOCARRIER is not set actually does nothing.

Cc Armando Budianto <[email protected]>
Fixes 0d9cfc9b8cb1 ("net: usbnet: Avoid potential RCU stall on LINK_CHANGE event")

---

v2:
- Rebase on top of the latest netdev/net tree. The previous patch was based on 0d9cfc9b8cb1. Line numbers have changed since then.

 drivers/net/usb/usbnet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/usb/usbnet.c +3 -3
index a38ffbf4b3f0..a1827684b92c 100644
@@ -1114,31 +1114,31 @@ static const struct ethtool_ops usbnet_ethtool_ops = {
 };
 
 /*-------------------------------------------------------------------------*/
 
 static void __handle_link_change(struct usbnet *dev)
 {
 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
 		return;
 
 	if (!netif_carrier_ok(dev->net)) {
+		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
+			netif_carrier_on(dev->net);
+
 		/* kill URBs for reading packets to save bus bandwidth */
 		unlink_urbs(dev, &dev->rxq);
 
 		/*
 		 * tx_timeout will unlink URBs for sending packets and
 		 * tx queue is stopped by netcore after link becomes off
 		 */
 	} else {
-		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
-			netif_carrier_on(dev->net);
-
 		/* submitting URBs for reading packets */
 		queue_work(system_bh_wq, &dev->bh_work);
 	}
 
 	/* hard_mtu or rx_urb_size may change during link change */
 	usbnet_update_max_qlen(dev);
 
 	clear_bit(EVENT_LINK_CHANGE, &dev->flags);
 }
 
Signature · 1 line
Ammar Faizi
reviewer Reviewed-by
#2 lore raw

+ Linus

Quoted 23 lines · Simon Horman

+ John Ernberg

Quoted 20 lines · Ammar Faizi

The commit in the Fixes tag breaks my laptop (found by git bisect). My home RJ45 LAN cable cannot connect after that commit.

The call to netif_carrier_on() should be done when netif_carrier_ok() is false. Not when it's true. Because calling netif_carrier_on() when __LINK_STATE_NOCARRIER is not set actually does nothing.

Cc Armando Budianto <[email protected]>
Fixes 0d9cfc9b8cb1 ("net: usbnet: Avoid potential RCU stall on LINK_CHANGE event")

---

v2:
- Rebase on top of the latest netdev/net tree. The previous patch was based on 0d9cfc9b8cb1. Line numbers have changed since then.

 drivers/net/usb/usbnet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

It seems this has escalated a bit as it broke things for Linus while he was travelling. He tested this patch and it resolved the problem. Which I think counts for something.

https://lore.kernel.org/netdev/CAHk-=wgkvNuGCDUMMs9bW9Mz5o=LcMhcDK_b2ThO6_T7cquoEQ@mail.gmail.com/

I have looked over the patch and it appears to me that it addresses a straightforward logic error: a check was added to turn the carrier on only if it is already on. Which seems a bit nonsensical. And presumably the intention was to add the check for the opposite case.

This patch addresses that problem.

So let me try and nudge this on a bit by providing a tag.

Quoted 45 lines
Quoted 44 lines
drivers/net/usb/usbnet.c +3 -3
index a38ffbf4b3f0..a1827684b92c 100644
@@ -1114,31 +1114,31 @@ static const struct ethtool_ops usbnet_ethtool_ops = {
 };
 
 /*-------------------------------------------------------------------------*/
 
 static void __handle_link_change(struct usbnet *dev)
 {
 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
 		return;
 
 	if (!netif_carrier_ok(dev->net)) {
+		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
+			netif_carrier_on(dev->net);
+
 		/* kill URBs for reading packets to save bus bandwidth */
 		unlink_urbs(dev, &dev->rxq);
 
 		/*
 		 * tx_timeout will unlink URBs for sending packets and
 		 * tx queue is stopped by netcore after link becomes off
 		 */
 	} else {
-		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
-			netif_carrier_on(dev->net);
-
 		/* submitting URBs for reading packets */
 		queue_work(system_bh_wq, &dev->bh_work);
 	}
 
 	/* hard_mtu or rx_urb_size may change during link change */
 	usbnet_update_max_qlen(dev);
 
 	clear_bit(EVENT_LINK_CHANGE, &dev->flags);
 }
 
Signature · 1 line
Ammar Faizi
#3 lore raw
Quoted 11 lines · Simon Horman

It seems this has escalated a bit as it broke things for Linus while he was travelling. He tested this patch and it resolved the problem. Which I think counts for something.

https://lore.kernel.org/netdev/CAHk-=wgkvNuGCDUMMs9bW9Mz5o=LcMhcDK_b2ThO6_T7cquoEQ@mail.gmail.com/

I have looked over the patch and it appears to me that it addresses a straightforward logic error: a check was added to turn the carrier on only if it is already on. Which seems a bit nonsensical. And presumably the intention was to add the check for the opposite case.

Quoted 5 lines

This patch addresses that problem.

So let me try and nudge this on a bit by providing a tag.

Hi Linus,

Given that Reviewed-by tag and the simplicity of the patch, it would be great if you can take this patch sooner to your tree. The fix is very critical for network connectivity. Especially for laptop users.

https://lore.kernel.org/all/[email protected]/

Signature · 1 line
Ammar Faizi
#4 lore raw
In reply to Simon Horman
Quoted 7 lines · Simon Horman

I have looked over the patch and it appears to me that it addresses a straightforward logic error: a check was added to turn the carrier on only if it is already on. Which seems a bit nonsensical. And presumably the intention was to add the check for the opposite case.

This patch addresses that problem.

So I agree that there was a logic error.

I'm not 100% sure about the "straightforward" part.

In particular, the whole *rest* of the code in that

        if (!netif_carrier_ok(dev->net)) {

no longer makes sense after we've turned the link on with that

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);

sequence.

Put another way - once we've turned the carrier on, now that whole

                /* kill URBs for reading packets to save bus bandwidth */
                unlink_urbs(dev, &dev->rxq);

                /*
                 * tx_timeout will unlink URBs for sending packets and
                 * tx queue is stopped by netcore after link becomes off
                 */

thing makes no sense.

So my gut feel is that the

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);

should actually be done outside that if-statement entirely, because it literally ends up changing the thing that if-statement is testing.

And no, I didn't actually test that version, because I was hoping that somebody who actually knows this code better would pipe up.

Linus

maintainer
#5 lore raw
Quoted 7 lines · On Wed, 6 Aug 2025 01:40:37 +0300 Linus Torvalds wrote:

So my gut feel is that the

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);

should actually be done outside that if-statement entirely, because it literally ends up changing the thing that if-statement is testing.

Right. I think it should be before the if (!netif_carrier_ok(dev->net))

Ammar, could you retest and repost that, since we haven't heard from John?

#6 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 6 Aug 2025 01:40:37 +0300 Linus Torvalds wrote:

So my gut feel is that the

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);
Quoted 7 lines
Quoted 2 lines

should actually be done outside that if-statement entirely, because it literally ends up changing the thing that if-statement is testing.

Right. I think it should be before the if (!netif_carrier_ok(dev->net))

Ammar, could you retest and repost that, since we haven't heard from John?

OK, I'll send a v3 shortly.

Signature · 1 line
Ammar Faizi
#7 lore raw
In reply to Jakub Kicinski

Hi Jakub, Linus, Ammar,

(sorry for the delay, on vacation, wasn't paying attention to the internet)

Quoted 6 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 6 Aug 2025 01:40:37 +0300 Linus Torvalds wrote:

So my gut feel is that the

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);
Quoted 7 lines
Quoted 2 lines

should actually be done outside that if-statement entirely, because it literally ends up changing the thing that if-statement is testing.

Right. I think it should be before the if (!netif_carrier_ok(dev->net))

Ammar, could you retest and repost that, since we haven't heard from John?

I can't verify the suggested change until sometime in September, after I return to office, but it feels correct.

However... I'm almost inclined to suggest a full revert of my patch as the testing was clearly royally botched. Booting it on the boards I have would have shown the failure immediately.

(I did see v3 of this patch being applied)

Apologies for the mess // John Ernberg

#8 lore raw
In reply to Linus Torvalds
Quoted 21 lines · Linus Torvalds

In particular, the whole *rest* of the code in that

        if (!netif_carrier_ok(dev->net)) {

no longer makes sense after we've turned the link on with that

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);

sequence.

Put another way - once we've turned the carrier on, now that whole

                /* kill URBs for reading packets to save bus bandwidth */
                unlink_urbs(dev, &dev->rxq);

                /*
                 * tx_timeout will unlink URBs for sending packets and
                 * tx queue is stopped by netcore after link becomes off
                 */
Quoted 1 line

thing makes no sense.

After taking a look further, I agree with you. I git-blamed the unlink_urbs()'s line and it's indeed expected to be called after link becomes off. So yes, it makes no sense to call that when we're turning the link on.

commit 4b49f58fff00e6e9b24eaa31d4c6324393d76b0a
Author: Ming Lei <[email protected]>
Date: Thu Apr 11 04:40:40 2013 +0000

usbnet: handle link change

The link change is detected via the interrupt pipe, and bulk pipes are responsible for transfering packets, so it is reasonable to stop bulk transfer after link is reported as off.

Even though my patch works on my machine. Something may go wrong.

Quoted 7 lines

So my gut feel is that the

                if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
                        netif_carrier_on(dev->net);

should actually be done outside that if-statement entirely, because it literally ends up changing the thing that if-statement is testing.

Apart from moving it outside that if-statement, unlink_urbs() call should probably also be guarded as we agreed it makes no sense to call it when we're turning the link on.

Signature · 1 line
Ammar Faizi
#9 lore raw
Quoted 3 lines · Ammar Faizi

Apart from moving it outside that if-statement, unlink_urbs() call should probably also be guarded as we agreed it makes no sense to call it when we're turning the link on.

Oh, no.

I just realized, it does need to be guarded because if netif_carrier_on() is placed before the if (!netif_carrier_ok(dev->net)), it already clears __LINK_STATE_NOCARRIER.

Signature · 1 line
Ammar Faizi
#10 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

And no, I didn't actually test that version, because I was hoping that somebody who actually knows this code better would pipe up.

Bah. Since I'm obviously horribly jetlagged, I decided to just test to make sure I understand the code.

And yeah, the attached patch also fixes the problem for me and makes more sense to me.

But again, it would be good to get comments from people who *actually* know the code.

Linus

 drivers/net/usb/usbnet.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/usb/usbnet.c +3 -3
index a38ffbf4b3f0..511c4154cf74 100644
@@ -1120,6 +1120,9 @@ static void __handle_link_change(struct usbnet *dev)
 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
 		return;
 
+	if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
+		netif_carrier_on(dev->net);
+
 	if (!netif_carrier_ok(dev->net)) {
 		/* kill URBs for reading packets to save bus bandwidth */
 		unlink_urbs(dev, &dev->rxq);
@@ -1129,9 +1132,6 @@ static void __handle_link_change(struct usbnet *dev)
 		 * tx queue is stopped by netcore after link becomes off
 		 */
 	} else {
-		if (test_and_clear_bit(EVENT_LINK_CARRIER_ON, &dev->flags))
-			netif_carrier_on(dev->net);
-
 		/* submitting URBs for reading packets */
 		queue_work(system_bh_wq, &dev->bh_work);
 	}
#11 lore raw
Quoted 3 lines · Linus Torvalds

And yeah, the attached patch also fixes the problem for me and makes more sense to me.

Ok, crossed emails because I was reading things in odd orders and going back to bed trying to get over jetlag.

Anyway, I've applied Ammar's v3 that ended up the same patch that I also tested,

Linus

#12 lore raw
Quoted 1 line · Linus Torvalds

Anyway, I've applied Ammar's v3 that ended up the same patch that I also tested,

Yesterday, I synced with your tree, but couldn't boot. Crashed with this call trace:

https://gist.githubusercontent.com/ammarfaizi2/3ba41f13517be4bae70cde869347d259/raw/0ac09b3e1d90d51c3fed14ca9f837f45d7730f0a/crash.jpg

This morning, I synced with your tree again, still the same result.

I'll try to bisect it and report to approriate subsystem once I get the first bad commit. I suspect it's related to pci or nvme (based on that call trace).

Signature · 1 line
Ammar Faizi