Skip to contentSkip to conversation

net: phy: fix cleanup after probe failure

PATCH Superseded Xuanqiang Luo 4 participants 18 messages Aug 19 – Aug 21

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover net: phy: fix cleanup after probe failure

From: Xuanqiang Luo <[email protected]>

phy_probe() initializes the PHY driver, ports, SFP upstream, and LEDs in stages. Its error paths do not always release only the resources acquired at each stage. It can also mark the PHY ready before all setup succeeds.

Port setup also leaves SFP cleanup split between phy_sfp_probe(), phy_setup_ports(), and phy_probe(), and default port setup ignores errors from attaching the port to the PHY driver.

This series makes each initialization layer own its cleanup and propagates setup failures to the caller.

Patch 1 splits the phy_probe() cleanup by initialization stage.

Patch 2 makes SFP and port setup unwind their resources in the required order.

Patch 3 sets PHY_READY only after LED setup succeeds.

Patch 4 calls the PHY driver remove callback after later probe failures.

Patch 5 propagates errors from default port setup.

---
Changes:
v3:
Patch 1:
- Rename cleanup labels to include verbs describing their actions. (Jakub Kicinski.)

Patch 3:
- Do not clear phydev->drv before device-core teardown completes; this can expose NULL dereferences in concurrent attach paths and devres callbacks. Set PHY_READY only after LED setup succeeds and update the Fixes tag. (Sashiko, Jakub Kicinski.)

v2: https://lore.kernel.org/all/[email protected]/
Patch 1:
- Limit this patch to splitting phy_probe() error paths, moving the SFP teardown fixes to Patch 2.

Patch 2 (new):
- makes SFP and port setup unwind their resources in the required order.
- Add phy_sfp_release() for complete SFP teardown instead of open-coding sfp_bus_del_upstream(). (Andrew Lunn, Maxime Chevallier.)

Patch 3 (new):
- Restore PHY_DOWN and clear phydev->drv after probe failure.

Patch 4:
- Move the former Patch 2 to Patch 4; no functional changes.

Patch 5 (new):
- Propagate errors from default port setup.

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

Xuanqiang Luo (5):
net: phy: split phy_probe() error paths
net: phy: unregister SFP upstream before port cleanup
net: phy: set PHY_READY after LED setup
net: phy: call driver remove when core initialization fails
net: phy: propagate errors from default port setup

 drivers/net/phy/phy_device.c | 80 ++++++++++++++++++++++++++----------
 1 file changed, 58 insertions(+), 22 deletions(-)

base-commit: f5bbbfec59b4e2fb7520a91de3df8a6174325d6a

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/5 net: phy: split phy_probe() error paths

From: Xuanqiang Luo <[email protected]>

phy_probe() uses one cleanup path for failures at every initialization stage. This runs cleanup for resources that have not been initialized.

Split the cleanup by initialization stage so each failure path unwinds only the resources that may have been initialized.

---
 drivers/net/phy/phy_device.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/net/phy/phy_device.c +11 -9
index 0615228459ef4..59ee5a76af2ba 100644
@@ -3681,7 +3681,7 @@ static int phy_probe(struct device *dev)
 	if (phydev->drv->probe) {
 		err = phydev->drv->probe(phydev);
 		if (err)
-			goto out;
+			goto out_reset;
 	}
 
 	phy_disable_interrupts(phydev);
@@ -3702,7 +3702,7 @@ static int phy_probe(struct device *dev)
 		err = genphy_read_abilities(phydev);
 
 	if (err)
-		goto out;
+		goto out_reset;
 
 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
 			       phydev->supported))
@@ -3719,7 +3719,7 @@ static int phy_probe(struct device *dev)
 
 	err = phy_setup_ports(phydev);
 	if (err)
-		goto out;
+		goto out_sfp_release;
 
 	phy_advertise_supported(phydev);
 
@@ -3728,7 +3728,7 @@ static int phy_probe(struct device *dev)
 	 */
 	err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
 	if (err)
-		goto out;
+		goto out_sfp_release;
 
 	/* Get the EEE modes we want to prohibit. */
 	of_set_phy_eee_broken(phydev);
@@ -3781,20 +3781,22 @@ static int phy_probe(struct device *dev)
 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) {
 		err = of_phy_leds(phydev);
 		if (err)
-			goto out;
+			goto out_unreg_led_triggers;
 	}
 
 	return 0;
 
-out:
+out_unreg_led_triggers:
+	if (!phydev->is_on_sfp_module)
+		phy_led_triggers_unregister(phydev);
+
+out_sfp_release:
 	sfp_bus_del_upstream(phydev->sfp_bus);
 	phydev->sfp_bus = NULL;
 
 	phy_cleanup_ports(phydev);
 
-	if (!phydev->is_on_sfp_module)
-		phy_led_triggers_unregister(phydev);
-
+out_reset:
 	/* Re-assert the reset signal on error */
 	phy_device_reset(phydev, 1);
 
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH net v3 1/5] net: phy: split phy_probe() error paths

Quoted 13 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() uses one cleanup path for failures at every initialization stage. This runs cleanup for resources that have not been initialized.

Split the cleanup by initialization stage so each failure path unwinds only the resources that may have been initialized.

drivers/net/phy/phy_device.c +3 -1
-out:
+out_unreg_led_triggers:
+	if (!phydev->is_on_sfp_module)
+		phy_led_triggers_unregister(phydev);

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3797

Quoted 10 lines
drivers/net/phy/phy_device.c +2 -3
+
+out_sfp_release:
 	sfp_bus_del_upstream(phydev->sfp_bus);
 	phydev->sfp_bus = NULL;
 
 	phy_cleanup_ports(phydev);
 
-	if (!phydev->is_on_sfp_module)
-		phy_led_triggers_unregister(phydev);
-

It would be nice to mention this reorder in the commit message. The commit message is about "Why?". Explain why the order has been changed.

Andrew

---
pw-bot: cr

#3 lore raw

Re: [PATCH net v3 1/5] net: phy: split phy_probe() error paths

Quoted 12 lines · Andrew Lunn
Quoted 11 lines · Xuanqiang Luo
drivers/net/phy/phy_device.c +5 -1
-out:
+out_unreg_led_triggers:
+	if (!phydev->is_on_sfp_module)
+		phy_led_triggers_unregister(phydev);
+
+out_sfp_release:
  	sfp_bus_del_upstream(phydev->sfp_bus);
  	phydev->sfp_bus = NULL;
 
  	phy_cleanup_ports(phydev);
Quoted 8 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3797

Quoted 3 lines
drivers/net/phy/phy_device.c +0 -3
-	if (!phydev->is_on_sfp_module)
-		phy_led_triggers_unregister(phydev);
-

It would be nice to mention this reorder in the commit message. The commit message is about "Why?". Explain why the order has been changed.

Andrew

Sure, I'll explain it in more detail in the next revision.

Thanks,
Xuanqiang

#4 lore raw

PATCH v3 2/5 net: phy: unregister SFP upstream before port cleanup

In reply to Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

Commit 4497f5028675 ("net: phy: Clean the phy_ports after unregistering the downstream SFP bus") established that an SFP upstream must be unregistered before its phy_ports are destroyed because SFP callbacks may access these ports.

phy_setup_ports() does not follow this order when a later port setup step fails after phy_sfp_probe() succeeds. It destroys the SFP phy_port and leaves phy_probe() to unregister the upstream later, creating a race between port destruction and SFP upstream callbacks.

The error unwind is also split across three functions. If phy_setup_sfp_port() fails, phy_sfp_probe() leaves the upstream registered and relies on phy_probe() to remove it after phy_setup_ports() returns.

Make each layer unwind the resources it successfully set up. Unregister only the upstream in phy_sfp_probe() when SFP port setup fails, since the failed port has already been destroyed. Add phy_sfp_release() for a successful SFP probe, and make phy_setup_ports() use it before cleaning up the remaining ports. Once phy_setup_ports() has rolled back all port setup, make phy_probe() skip this cleanup.

Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")
---
 drivers/net/phy/phy_device.c | 49 ++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 11 deletions(-)
drivers/net/phy/phy_device.c +38 -11
index 59ee5a76af2ba..538e9ca44d093 100644
@@ -1723,12 +1723,41 @@ static int phy_sfp_probe(struct phy_device *phydev)
 			phydev->sfp_bus = NULL;
 	}
 
-	if (!ret && phydev->sfp_bus)
+	if (!ret && phydev->sfp_bus) {
 		ret = phy_setup_sfp_port(phydev);
+		if (ret) {
+			sfp_bus_del_upstream(phydev->sfp_bus);
+			phydev->sfp_bus = NULL;
+		}
+	}
 
 	return ret;
 }
 
+/**
+ * phy_sfp_release - release resources set up by phy_sfp_probe()
+ * @phydev: the PHY device
+ *
+ * Release the SFP resources set up by a successful phy_sfp_probe(). Unregister
+ * the upstream before destroying its phy_port, so SFP upstream callbacks cannot
+ * race with port destruction.
+ */
+static void phy_sfp_release(struct phy_device *phydev)
+{
+	struct phy_port *port, *tmp;
+
+	sfp_bus_del_upstream(phydev->sfp_bus);
+	phydev->sfp_bus = NULL;
+
+	list_for_each_entry_safe(port, tmp, &phydev->ports, head) {
+		if (!port->is_sfp)
+			continue;
+
+		phy_del_port(phydev, port);
+		phy_port_destroy(port);
+	}
+}
+
 static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
 {
 	return phydrv->config_intr && phydrv->handle_interrupt;
@@ -3522,13 +3551,13 @@ static int phy_setup_ports(struct phy_device *phydev)
 	if (!phydev->is_genphy_driven) {
 		ret = phy_sfp_probe(phydev);
 		if (ret)
-			goto out;
+			goto err_ports;
 	}
 
 	if (phydev->n_ports < phydev->max_n_ports) {
 		ret = phy_default_setup_single_port(phydev);
 		if (ret)
-			goto out;
+			goto err_sfp;
 	}
 
 	linkmode_zero(ports_supported);
@@ -3555,7 +3584,9 @@ static int phy_setup_ports(struct phy_device *phydev)
 
 	return 0;
 
-out:
+err_sfp:
+	phy_sfp_release(phydev);
+err_ports:
 	phy_cleanup_ports(phydev);
 	return ret;
 }
@@ -3719,7 +3750,7 @@ static int phy_probe(struct device *dev)
 
 	err = phy_setup_ports(phydev);
 	if (err)
-		goto out_sfp_release;
+		goto out_reset;
 
 	phy_advertise_supported(phydev);
 
@@ -3791,9 +3822,7 @@ static int phy_probe(struct device *dev)
 		phy_led_triggers_unregister(phydev);
 
 out_sfp_release:
-	sfp_bus_del_upstream(phydev->sfp_bus);
-	phydev->sfp_bus = NULL;
-
+	phy_sfp_release(phydev);
 	phy_cleanup_ports(phydev);
 
 out_reset:
@@ -3817,9 +3846,7 @@ static int phy_remove(struct device *dev)
 
 	phydev->state = PHY_DOWN;
 
-	sfp_bus_del_upstream(phydev->sfp_bus);
-	phydev->sfp_bus = NULL;
-
+	phy_sfp_release(phydev);
 	phy_cleanup_ports(phydev);
 
 	if (phydev->drv && phydev->drv->remove)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 2/5
#5 lore raw

Re: [PATCH net v3 2/5] net: phy: unregister SFP upstream before port cleanup

Quoted 24 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

Commit 4497f5028675 ("net: phy: Clean the phy_ports after unregistering the downstream SFP bus") established that an SFP upstream must be unregistered before its phy_ports are destroyed because SFP callbacks may access these ports.

phy_setup_ports() does not follow this order when a later port setup step fails after phy_sfp_probe() succeeds. It destroys the SFP phy_port and leaves phy_probe() to unregister the upstream later, creating a race between port destruction and SFP upstream callbacks.

The error unwind is also split across three functions. If phy_setup_sfp_port() fails, phy_sfp_probe() leaves the upstream registered and relies on phy_probe() to remove it after phy_setup_ports() returns.

Make each layer unwind the resources it successfully set up. Unregister only the upstream in phy_sfp_probe() when SFP port setup fails, since the failed port has already been destroyed. Add phy_sfp_release() for a successful SFP probe, and make phy_setup_ports() use it before cleaning up the remaining ports. Once phy_setup_ports() has rolled back all port setup, make phy_probe() skip this cleanup.

Quoted 2 lines
Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")

Andrew

#6 lore raw

PATCH v3 3/5 net: phy: set PHY_READY after LED setup

In reply to Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() sets PHY_READY before calling of_phy_leds(). If LED setup fails, the error path releases the initialized resources while the PHY state remains READY even though probing failed.

Set PHY_READY only after LED setup succeeds.

Fixes 01e5b728e9e4 ("net: phy: Add a binding for PHY LEDs")
---
 drivers/net/phy/phy_device.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/phy/phy_device.c +3 -3
index 538e9ca44d093..8fd6fbf378f0a 100644
@@ -3799,9 +3799,6 @@ static int phy_probe(struct device *dev)
 				 phydev->supported);
 	}
 
-	/* Set the state to READY by default */
-	phydev->state = PHY_READY;
-
 	/* Register the PHY LED triggers */
 	if (!phydev->is_on_sfp_module)
 		phy_led_triggers_register(phydev);
@@ -3815,6 +3812,9 @@ static int phy_probe(struct device *dev)
 			goto out_unreg_led_triggers;
 	}
 
+	/* Set the state to READY by default */
+	phydev->state = PHY_READY;
+
 	return 0;
 
 out_unreg_led_triggers:
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 3/5
#7 lore raw

Re: [PATCH net v3 3/5] net: phy: set PHY_READY after LED setup

Quoted 10 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() sets PHY_READY before calling of_phy_leds(). If LED setup fails, the error path releases the initialized resources while the PHY state remains READY even though probing failed.

Set PHY_READY only after LED setup succeeds.

Fixes 01e5b728e9e4 ("net: phy: Add a binding for PHY LEDs")

Andrew

#8 lore raw

Re: [PATCH net v3 3/5] net: phy: set PHY_READY after LED setup

In reply to Xuanqiang Luo
Quoted 11 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() sets PHY_READY before calling of_phy_leds(). If LED setup fails, the error path releases the initialized resources while the PHY state remains READY even though probing failed.

Set PHY_READY only after LED setup succeeds.

Fixes 01e5b728e9e4 ("net: phy: Add a binding for PHY LEDs")

I noticed that Sashiko found two new issues:

Quoted 24 lines
Quoted 5 lines
drivers/net/phy/phy_device.c +0 -0
index 538e9ca44d093..8fd6fbf378f0a 100644

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3804

Quoted 9 lines
drivers/net/phy/phy_device.c +0 -3
@@ -3799,9 +3799,6 @@ static int phy_probe(struct device *dev)
                    phydev->supported);
       }
 
-    /* Set the state to READY by default */
-    phydev->state = PHY_READY;
-
       /* Register the PHY LED triggers */
       if (!phydev->is_on_sfp_module)
           phy_led_triggers_register(phydev);

[Severity: Critical]
This is a pre-existing issue, but by ignoring the return value of phy_led_triggers_register() here, could we trigger a double-free later?

In drivers/net/phy/phy_led_triggers.c, if phy_led_triggers_register() fails, it frees phy->phy_led_triggers but leaves it as a dangling pointer and sets phy_num_led_triggers = 0. Since the return value is ignored here, phy_probe() continues execution.

Quoted 5 lines

If of_phy_leds() then fails, the code jumps to out_unreg_led_triggers and calls phy_led_triggers_unregister(). Inside that function, the loop is skipped because phy_num_led_triggers is 0, but it unconditionally calls kfree(phy->phy_led_triggers). This results in a double-free of the dangling pointer.

Although this part was not introduced by the current patch, I think it still falls within the scope of probe failure handling, so I plan to add a separate patch in the next revision to fix it.

Quoted 17 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3817

Quoted 7 lines
drivers/net/phy/phy_device.c +3 -0
@@ -3815,6 +3812,9 @@ static int phy_probe(struct device *dev)
 			goto out_unreg_led_triggers;
 	}
 
+	/* Set the state to READY by default */
+	phydev->state = PHY_READY;
+

[Severity: High]
Does moving this lockless state assignment to the end of phy_probe() create a race condition with concurrent MAC driver attachments?

The driver core sets dev->driver before invoking phy_probe(). While phy_probe() is still running and potentially blocked in of_phy_leds(), a concurrent network driver probe could find this PHY and call phy_attach_direct().

Quoted 9 lines

Because dev->driver is already set, phy_attach_direct() proceeds without acquiring the PHY's device lock and sets the state to PHY_READY. The MAC driver then immediately calls phy_start(), which acquires phydev->lock and transitions the state from PHY_READY to PHY_UP.

If this lockless assignment completes after the MAC driver has already called phy_start(), won't it silently clobber the PHY_UP state back to PHY_READY and stall the PHY state machine?

This appears to be a pre-existing issue, since phy_attach_direct() and phy_probe() are not serialized.

This patch does widen the race window, but phydev->state should still be set to PHY_READY before phy_probe() returns.

Thanks,
Xuanqiang

maintainer
#9 lore raw

Re: [PATCH net v3 3/5] net: phy: set PHY_READY after LED setup

Quoted 43 lines · Xuanqiang Luo
Quoted 11 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() sets PHY_READY before calling of_phy_leds(). If LED setup fails, the error path releases the initialized resources while the PHY state remains READY even though probing failed.

Set PHY_READY only after LED setup succeeds.

Fixes 01e5b728e9e4 ("net: phy: Add a binding for PHY LEDs")

I noticed that Sashiko found two new issues:

Quoted 22 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3804

Quoted 13 lines
drivers/net/phy/phy_device.c +0 -2
index 538e9ca44d093..8fd6fbf378f0a 100644
@@ -3799,9 +3799,6 @@ static int phy_probe(struct device *dev)
                    phydev->supported);
       }
 > -    /* Set the state to READY by default */
-    phydev->state = PHY_READY;
-
       /* Register the PHY LED triggers */
       if (!phydev->is_on_sfp_module)
           phy_led_triggers_register(phydev);

[Severity: Critical]
This is a pre-existing issue, but by ignoring the return value of phy_led_triggers_register() here, could we trigger a double-free later?

In drivers/net/phy/phy_led_triggers.c, if phy_led_triggers_register() fails, it frees phy->phy_led_triggers but leaves it as a dangling pointer and sets phy_num_led_triggers = 0. Since the return value is ignored here, phy_probe() continues execution.

Quoted 5 lines

If of_phy_leds() then fails, the code jumps to out_unreg_led_triggers and calls phy_led_triggers_unregister(). Inside that function, the loop is skipped because phy_num_led_triggers is 0, but it unconditionally calls kfree(phy->phy_led_triggers). This results in a double-free of the dangling pointer.

Quoted 14 lines

Although this part was not introduced by the current patch, I think it still falls within the scope of probe failure handling, so I plan to add a separate patch in the next revision to fix it.

Quoted 10 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3816

Quoted 6 lines
drivers/net/phy/phy_device.c +2 -0
@@ -3815,6 +3812,9 @@ static int phy_probe(struct device *dev)
 			goto out_unreg_led_triggers;
 	}
 > +	/* Set the state to READY by default */
+	phydev->state = PHY_READY;
+

[Severity: High]
Does moving this lockless state assignment to the end of phy_probe() create a race condition with concurrent MAC driver attachments?

I'm not sure this is correct. If phy_probe() is still ongoing, can you even call phy_attach()?

In the normal case the MAC driver registers the MDIO bus during its probe. The PHYs will be scanned and probed synchronously. Once the MDIO is registered, the MAC driver will then continue. Generally the PHY is attached in open() but it could be later in probe. So think this serialisation is performed by general code structure.

It gets more interesting with a shared MDIO bus, which has two or more PHYs on it. The first MAC driver registers its MDIO bus as above, and later attaches its PHY. However in parallel a second MAC driver can probe, and use a phandle to point to the PHY on the MDIO bus, and try to attach to it. But i _guess_ following the phandle does not work until the PHY probe has completed?

Quoted 5 lines

This appears to be a pre-existing issue, since phy_attach_direct() and phy_probe() are not serialized.

This patch does widen the race window, but phydev->state should still be set to PHY_READY before phy_probe() returns.

I would agree it is an old issues, if it is an issue. So i would say it is out of scope of this patchset. You can look deeper if you want, but it is not required.

Andrew

#10 lore raw

PATCH v3 4/5 net: phy: call driver remove when core initialization fails

In reply to Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() may fail while querying features or completing other core initialization after the PHY driver probe callback has succeeded. The driver core does not run the remove path after a probe error, so resources that the PHY driver releases in its remove callback are leaked.

Call the PHY driver remove callback on these failures.

Fixes efbdfdc29bdd ("net: phy: Add support for asking the PHY its abilities")
---
 drivers/net/phy/phy_device.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +6 -2
index 8fd6fbf378f0a..77318659ff985 100644
@@ -3733,7 +3733,7 @@ static int phy_probe(struct device *dev)
 		err = genphy_read_abilities(phydev);
 
 	if (err)
-		goto out_reset;
+		goto out_remove;
 
 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
 			       phydev->supported))
@@ -3750,7 +3750,7 @@ static int phy_probe(struct device *dev)
 
 	err = phy_setup_ports(phydev);
 	if (err)
-		goto out_reset;
+		goto out_remove;
 
 	phy_advertise_supported(phydev);
 
@@ -3825,6 +3825,10 @@ static int phy_probe(struct device *dev)
 	phy_sfp_release(phydev);
 	phy_cleanup_ports(phydev);
 
+out_remove:
+	if (phydev->drv->remove)
+		phydev->drv->remove(phydev);
+
 out_reset:
 	/* Re-assert the reset signal on error */
 	phy_device_reset(phydev, 1);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 4/5
#11 lore raw

Re: [PATCH net v3 4/5] net: phy: call driver remove when core initialization fails

Quoted 8 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() may fail while querying features or completing other core initialization after the PHY driver probe callback has succeeded. The driver core does not run the remove path after a probe error, so resources that the PHY driver releases in its remove callback are leaked.

Quoted 4 lines

Call the PHY driver remove callback on these failures.

Fixes efbdfdc29bdd ("net: phy: Add support for asking the PHY its abilities")

Andrew

#12 lore raw

PATCH v3 5/5 net: phy: propagate errors from default port setup

In reply to Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_default_setup_single_port() ignores errors from phy_add_port() and always reports success. If a PHY driver attach_mdi_port() callback fails, the phy_port is leaked and PHY probing continues without the expected default port.

Destroy the port and return the error.

Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")
---
 drivers/net/phy/phy_device.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +5 -2
index 77318659ff985..22a5af92c620a 100644
@@ -3458,6 +3458,7 @@ static int phy_default_setup_single_port(struct phy_device *phydev)
 {
 	struct phy_port *port = phy_port_alloc();
 	unsigned long mode;
+	int ret;
 
 	if (!port)
 		return -ENOMEM;
@@ -3484,9 +3485,11 @@ static int phy_default_setup_single_port(struct phy_device *phydev)
 		port->pairs = max_t(int, port->pairs,
 				    ethtool_linkmode_n_pairs(mode));
 
-	phy_add_port(phydev, port);
+	ret = phy_add_port(phydev, port);
+	if (ret)
+		phy_port_destroy(port);
 
-	return 0;
+	return ret;
 }
 
 static int of_phy_ports(struct phy_device *phydev)
Signature · 1 line
2.43.0
Reviewed-by on v3 5/5
#13 lore raw

Re: [PATCH net v3 5/5] net: phy: propagate errors from default port setup

Hi,

Quoted 11 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_default_setup_single_port() ignores errors from phy_add_port() and always reports success. If a PHY driver attach_mdi_port() callback fails, the phy_port is leaked and PHY probing continues without the expected default port.

Destroy the port and return the error.

Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")

Maxime

Quoted 30 lines
---
 drivers/net/phy/phy_device.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +5 -2
index 77318659ff985..22a5af92c620a 100644
@@ -3458,6 +3458,7 @@ static int phy_default_setup_single_port(struct phy_device *phydev)
 {
 	struct phy_port *port = phy_port_alloc();
 	unsigned long mode;
+	int ret;
 
 	if (!port)
 		return -ENOMEM;
@@ -3484,9 +3485,11 @@ static int phy_default_setup_single_port(struct phy_device *phydev)
 		port->pairs = max_t(int, port->pairs,
 				    ethtool_linkmode_n_pairs(mode));
 
-	phy_add_port(phydev, port);
+	ret = phy_add_port(phydev, port);
+	if (ret)
+		phy_port_destroy(port);
 
-	return 0;
+	return ret;
 }
 
 static int of_phy_ports(struct phy_device *phydev)
maintainer
#14 lore raw
In reply to Xuanqiang Luo
Quoted 15 lines · Xuanqiang Luo

From: Xuanqiang Luo <[email protected]>

phy_probe() initializes the PHY driver, ports, SFP upstream, and LEDs in stages. Its error paths do not always release only the resources acquired at each stage. It can also mark the PHY ready before all setup succeeds.

Port setup also leaves SFP cleanup split between phy_sfp_probe(), phy_setup_ports(), and phy_probe(), and default port setup ignores errors from attaching the port to the PHY driver.

This series makes each initialization layer own its cleanup and propagates setup failures to the caller.

Patch 1 splits the phy_probe() cleanup by initialization stage.

Quoted 8 lines

Patch 2 makes SFP and port setup unwind their resources in the required order.

Patch 3 sets PHY_READY only after LED setup succeeds.

Patch 4 calls the PHY driver remove callback after later probe failures.

Patch 5 propagates errors from default port setup.

The scope of these patches has increased quite a bit. It is now more like ongoing development work than a actual fix. Does this bother anybody?

Please submit for net-next, once it reopens.

Andrew

maintainer
#15 lore raw
Quoted 1 line · On Wed, 19 Aug 2026 15:33:28 +0200 Andrew Lunn wrote:

Please submit for net-next, once it reopens.

FWIW I'd prefer to take these sort of error path fixes to net during the merge window. No point backlogging borderline stuff. Any particular reason you want this in net-next?

maintainer
#16 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 1 line · On Wed, 19 Aug 2026 15:33:28 +0200 Andrew Lunn wrote:

Please submit for net-next, once it reopens.

FWIW I'd prefer to take these sort of error path fixes to net during the merge window. No point backlogging borderline stuff. Any particular reason you want this in net-next?

I don't think it bothers anybody, so does not meet stable rules.

But i've nothing against it being merged now.

Andrew

#17 lore raw
Quoted 11 lines · Andrew Lunn
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Wed, 19 Aug 2026 15:33:28 +0200 Andrew Lunn wrote:

Please submit for net-next, once it reopens.

FWIW I'd prefer to take these sort of error path fixes to net during the merge window. No point backlogging borderline stuff. Any particular reason you want this in net-next?

I don't think it bothers anybody, so does not meet stable rules.

But i've nothing against it being merged now.

Andrew

Thanks for the discussion.

Based on the feedback, I’ll continue targeting net and post a revised version in a follow-up.

Thanks,
Xuanqiang