[PATCH net] net: phylink: unwind the PHY binding when bringup fails late
From: Aleksei Sviridkin
Date: Thu Sep 17 2026 - 21:55:35 EST
phylink_bringup_phy() records the PHY in pl->phydev before its last
fallible step: on a MAC whose phylink ops implement LPI,
phy_eee_rx_clock_stop() can fail with a real MDIO error. The callers
unwind with phy_detach(), which knows nothing about pl->phydev, so a
pointer to a PHY that is no longer attached outlives the failed
connect.
What that costs depends on how the caller got here.
phylink_connect_phy() goes through phylink_attach_phy(), which refuses
to attach while pl->phydev is set, turning a transient MDIO error into
a permanent -EBUSY. The SFP path is worse than that: sfp_sm_probe_phy()
answers the failure with phy_device_remove() and phy_device_free(), and
it assigns sfp->mod_phy only past that error return, so nothing clears
pl->phydev and it is left pointing at a freed phy_device that
phylink_resolve() and the ethtool helpers go on reading.
phylink_fwnode_phy_connect() has no such check, so a later connect
overwrites the stale pointer and hides the problem. A disconnect does
not: phylink_disconnect_phy() hands that pointer to phy_disconnect(),
and the second phy_detach() on the same PHY drops references the first
one already released.
Clear the binding on the failure path. This is the same operation
phylink_disconnect_phy() performs, so both now share a helper. The
PHY-side fields are left to phy_detach(), which every caller already
runs on this path.
Fixes: 03abf2a7c654 ("net: phylink: add EEE management")
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@xxxxxx>
Reviewed-by: Andrew Lunn <andrew@xxxxxxx>
---
Notes:
Split out of the [PATCH net v7 0/2] pair (20260909204306.2374562-1-f@xxxxxx):
2/2 is being reworked per review, and this patch has nothing left to wait
for.
Reviewed-by was given on v2 (net-next v2 1/2), lore message-id
006ad9d6-b51b-46ed-8c5d-3649b70a4f62@xxxxxxx.
Code is unchanged from the v7 posting, patch-id 293623f600b8; only the
hunk offsets moved with the rebase onto current net/main.
drivers/net/phy/phylink.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index a1458da8111b..1efb45ce8534 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -2085,6 +2085,18 @@ static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
return phylink_validate(pl, supported, state);
}
+/* Disassociate @phy from @pl. Caller must hold pl->phydev_mutex. */
+static void phylink_clear_phydev(struct phylink *pl, struct phy_device *phy)
+{
+ mutex_lock(&phy->lock);
+ mutex_lock(&pl->state_mutex);
+ pl->phydev = NULL;
+ pl->phy_enable_tx_lpi = false;
+ pl->mac_tx_clk_stop = false;
+ mutex_unlock(&pl->state_mutex);
+ mutex_unlock(&phy->lock);
+}
+
static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
phy_interface_t interface)
{
@@ -2199,6 +2211,12 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
if (ret == 0 && phy_interrupt_is_valid(phy))
phy_request_interrupt(phy);
+ if (ret) {
+ mutex_lock(&pl->phydev_mutex);
+ phylink_clear_phydev(pl, phy);
+ mutex_unlock(&pl->phydev_mutex);
+ }
+
return ret;
}
@@ -2349,15 +2367,8 @@ void phylink_disconnect_phy(struct phylink *pl)
mutex_lock(&pl->phydev_mutex);
phy = pl->phydev;
- if (phy) {
- mutex_lock(&phy->lock);
- mutex_lock(&pl->state_mutex);
- pl->phydev = NULL;
- pl->phy_enable_tx_lpi = false;
- pl->mac_tx_clk_stop = false;
- mutex_unlock(&pl->state_mutex);
- mutex_unlock(&phy->lock);
- }
+ if (phy)
+ phylink_clear_phydev(pl, phy);
mutex_unlock(&pl->phydev_mutex);
if (phy) {
base-commit: 3b95a04eb5f95bf6a016a1bb9ff37d3eee48de63
--
2.53.0