net: phy: some cleanups following phy_port SFP
Subsystems (3)
v2 cover net: phy: some cleanups following phy_port SFP
While posting the v11 of phy_port netlink, sashiko found some pre-existing issues, and following the tentative fix [1], Nicolai found some more :)
Patch 1 and 2 add some cleanup in the phy_probe error paths, to discard all SFP and phy_port that have been set-up when probing fails further down the road.
Patch 3 fixes a sashiko-reported issue that I was able to reproduce, where we hit a deadlock when trying to setup PHY-driven SFP ports while using genphy. This is because the sfp init logic for PHYs is now generic, instead of per-driver, and genphy has the particularity of running its .probe() under RTNL, which clashes with the SFP code.
One more reason to try and cleanup RTNL handling in PHY/SFP :)
Thanks Nicolai for the feedback on V1,
Maxime
[1] : https://lore.kernel.org/r/[email protected]
Maxime Chevallier (3):
net: phy: clean the sfp upstream if phy probing fails
net: phy: remove phy ports upon probe failure
net: phy: don't try to setup PHY-driven SFP cages when using genphy
drivers/net/phy/phy_device.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/3 net: phy: clean the sfp upstream if phy probing fails
Sashiko reported that we don't call sfp_bus_del_upstream() in the probe failure path, so let's add it, otherwise the sfp-bus is left with a dangling 'upstream' field, that may be used later on during SFP events.
This issue existed before the generic phylib sfp support, back when drivers were calling phy_sfp_probe themselves.
---
V2: Null-ify phydev->sfp_bus upon sfp_bus_add_upstream failure
drivers/net/phy/phy_device.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/phy/phy_device.c +6 -0
@@ -1718,6 +1718,9 @@ static int phy_sfp_probe(struct phy_device *phydev) ret =sfp_bus_add_upstream (bus, phydev, &sfp_phydev_ops);sfp_bus_put (bus); + +if (ret) + phydev->sfp_bus =NULL ; }if (!ret && phydev->sfp_bus)
@@ -3775,6 +3778,9 @@ static int phy_probe(struct device *dev)return 0 ; out: +sfp_bus_del_upstream (phydev->sfp_bus); + phydev->sfp_bus =NULL ; +if (!phydev->is_on_sfp_module)phy_led_triggers_unregister (phydev);
Signature · 1 line
2.54.0
Re: [PATCH net v2 1/3] net: phy: clean the sfp upstream if phy probing fails
Quoted 31 lines · Maxime Chevallier
Sashiko reported that we don't call
sfp_bus_del_upstream()in the probe failure path, so let's add it, otherwise the sfp-bus is left with a dangling 'upstream' field, that may be used later on during SFP events.This issue existed before the generic phylib sfp support, back when drivers were calling
phy_sfp_probethemselves.Fixes 298e54fa810e ("net: phy: add core phylib sfp support")Signed-off-by Maxime Chevallier <[email protected]>---
V2: Null-ifyphydev->sfp_busuponsfp_bus_add_upstreamfailuredrivers/net/phy/phy_device.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/net/phy/phy_device.c +3
-0@@ -1718,6 +1718,9 @@ static int phy_sfp_probe(struct phy_device *phydev) ret =sfp_bus_add_upstream (bus, phydev, &sfp_phydev_ops);sfp_bus_put (bus); + +if (ret) + phydev->sfp_bus =NULL ; }drivers/net/phy/phy_device.c +0
-0if (!ret && phydev->sfp_bus)
drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3785
Quoted 8 lines
drivers/net/phy/phy_device.c +3
-0@@ -3775,6 +3778,9 @@ static int phy_probe(struct device *dev)return 0 ; out: +sfp_bus_del_upstream (phydev->sfp_bus); + phydev->sfp_bus =NULL ; +if (!phydev->is_on_sfp_module)phy_led_triggers_unregister (phydev);
Thanks
Nicolai
Jakub Kicinski
maintainerRe: [PATCH net v2 1/3] net: phy: clean the sfp upstream if phy probing fails
In reply to Maxime ChevallierThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: clean the sfp upstream if phy probing fails
This commit adds a call to sfp_bus_del_upstream() in the probe failure path to ensure the sfp-bus is not left with a dangling upstream field that could be used during subsequent SFP events.
Quoted 3 lines
drivers/net/phy/phy_device.c +0
-0
[ ... ]
drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3783
Quoted 7 lines
drivers/net/phy/phy_device.c +3
-0@@ -3775,6 +3778,9 @@ static int phy_probe(struct device *dev)return 0 ; out: +sfp_bus_del_upstream (phydev->sfp_bus); + phydev->sfp_bus =NULL ; +
[Severity: High]
This is a pre-existing issue, but if a subsequent initialization step fails and we jump to the out label, should we be calling the specific PHY driver's remove callback?
If phydev->drv->probe() succeeds early in phy_probe(), but something like phy_setup_ports() or of_phy_leds() fails later, the execution jumps to this error path. Without calling phydev->drv->remove(phydev), any resources allocated by the driver are leaked.
Also, drivers like dp83640 register the phydev with external subsystems, such as adding it to shared lists like clock->phylist during their probe. Leaving dangling pointers to this unbound phydev in those subsystems could lead to use-after-free crashes later.
Quoted 3 lines
drivers/net/phy/phy_device.c +0
-0if (!phydev->is_on_sfp_module)phy_led_triggers_unregister (phydev);
Signature · 1 line
pw-bot: cr
Re: [PATCH net v2 1/3] net: phy: clean the sfp upstream if phy probing fails
Quoted 33 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: clean the sfp upstream if phy probing failsThis commit adds a call to
sfp_bus_del_upstream()in the probe failure path to ensure the sfp-bus is not left with a dangling upstream field that could be used during subsequent SFP events.Quoted 3 lines
drivers/net/phy/phy_device.c +0
-0[ ... ]
drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3783
Quoted 7 lines
drivers/net/phy/phy_device.c +3
-0@@ -3775,6 +3778,9 @@ static int phy_probe(struct device *dev)return 0 ; out: +sfp_bus_del_upstream (phydev->sfp_bus); + phydev->sfp_bus =NULL ; +[Severity: High]
This is a pre-existing issue, but if a subsequent initialization step fails and we jump to the out label, should we be calling the specific PHY driver's remove callback?If
phydev->drv->probe()succeeds early inphy_probe(), but something likephy_setup_ports()orof_phy_leds()fails later, the execution jumps to this error path. Without callingphydev->drv->remove(phydev), any resources allocated by the driver are leaked.
Quoted 4 lines
Also, drivers like dp83640 register the phydev with external subsystems, such as adding it to shared lists like
clock->phylistduring their probe. Leaving dangling pointers to this unbound phydev in those subsystems could lead to use-after-free crashes later.
Good point too. I've sent a V3 already, so if V3 is accepted I'll send a standalone patch for that, or include that in V4 should it be needed :)
Maxime
Quoted 4 lines
Quoted 4 lines
drivers/net/phy/phy_device.c +0
-0if (!phydev->is_on_sfp_module)phy_led_triggers_unregister (phydev);
PATCH v2 2/3 net: phy: remove phy ports upon probe failure
In reply to Maxime ChevallierWhen phy_probe fails, let's clean the phy_ports that were successfully added already.
--- drivers/net/phy/phy_device.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/net/phy/phy_device.c +2 -0
@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev)return 0 ; out: +phy_cleanup_ports (phydev); +sfp_bus_del_upstream (phydev->sfp_bus); phydev->sfp_bus =NULL ;
Signature · 1 line
2.54.0
Re: [PATCH net v2 2/3] net: phy: remove phy ports upon probe failure
Quoted 15 lines · Maxime Chevallier
When
phy_probefails, let's clean thephy_portsthat were successfully added already.Suggested-by Nicolai Buchwitz <[email protected]>Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")Signed-off-by Maxime Chevallier <[email protected]>--- drivers/net/phy/phy_device.c | 2 ++ 1 file changed, 2 insertions(+)drivers/net/phy/phy_device.c +0
-0
drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3784
Quoted 7 lines
drivers/net/phy/phy_device.c +2
-0@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev)return 0 ; out: +phy_cleanup_ports (phydev); +sfp_bus_del_upstream (phydev->sfp_bus); phydev->sfp_bus =NULL ;
Thanks
Nicolai
Re: [PATCH net v2 2/3] net: phy: remove phy ports upon probe failure
Hi Nicolai,
Quoted 18 lines · Nicolai Buchwitz
Quoted 17 lines · Maxime Chevallier
When
phy_probefails, let's clean thephy_portsthat were successfully added already.Suggested-by Nicolai Buchwitz <[email protected]>Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")Signed-off-by Maxime Chevallier <[email protected]>---
drivers/net/phy/phy_device.c | 2 ++1 file changed, 2 insertions(+)drivers/net/phy/phy_device.c +0
-0@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev) return 0;
Quoted 7 lines
Quoted 5 lines
out:
+phy_cleanup_ports(phydev);
+
sfp_bus_del_upstream(phydev->sfp_bus);
phydev->sfp_bus= NULL;Reviewed-by Nicolai Buchwitz <[email protected]>
I'll be moving phy_cleanup_ports(phydev); after the sfp cleanup, can I keep your review tag ?
Maxime
Quoted 3 lines
Thanks
Nicolai
Re: [PATCH net v2 2/3] net: phy: remove phy ports upon probe failure
Quoted 29 lines · Maxime Chevallier
Hi Nicolai,
Quoted 18 lines · Nicolai Buchwitz
Quoted 17 lines · Maxime Chevallier
When
phy_probefails, let's clean thephy_portsthat were successfully added already.Suggested-by Nicolai Buchwitz <[email protected]>Fixes 589e934d2735 ("net: phy: Introduce PHY ports representation")Signed-off-by Maxime Chevallier <[email protected]>---
drivers/net/phy/phy_device.c | 2 ++1 file changed, 2 insertions(+)drivers/net/phy/phy_device.c +0
-0@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev) return 0;Quoted 7 lines
Quoted 5 lines
out:
+phy_cleanup_ports(phydev);
+
sfp_bus_del_upstream(phydev->sfp_bus);
phydev->sfp_bus= NULL;Reviewed-by Nicolai Buchwitz <[email protected]>
Quoted 2 lines
I'll be moving
phy_cleanup_ports(phydev); after the sfp cleanup, can I keep your review tag ?
Sure.
Thanks,
Nicolai
Quoted 1 line
[...]
Jakub Kicinski
maintainerRe: [PATCH net v2 2/3] net: phy: remove phy ports upon probe failure
In reply to Maxime ChevallierThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: remove phy ports upon probe failure
When phy_probe fails, let's clean the phy_ports that were successfully added already.
drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3785
Quoted 13 lines
drivers/net/phy/phy_device.c +2
-0@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev)return 0 ; out: +phy_cleanup_ports (phydev); +sfp_bus_del_upstream (phydev->sfp_bus); phydev->sfp_bus =NULL ;
[Severity: Critical]
Is it safe to call phy_cleanup_ports() before sfp_bus_del_upstream() here?
sfp_bus_del_upstream() unregisters the upstream SFP callbacks and acts as a synchronization barrier by acquiring rtnl_lock(). Before it is called, the SFP bus is still active and can asynchronously fire events like phy_sfp_module_insert().
Because these SFP events locklessly iterate over the phydev->ports list via phy_get_sfp_port(), and phy_cleanup_ports() mutates the list and frees port structures without lock protection:
Could this concurrent access lead to list corruption and a use-after-free?
This same incorrect ordering is a pre-existing issue in the phy_remove() teardown path, but this patch introduces it into the phy_probe() error path.
Signature · 1 line
pw-bot: cr
Re: [PATCH net v2 2/3] net: phy: remove phy ports upon probe failure
Quoted 30 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: remove phy ports upon probe failureWhen
phy_probefails, let's clean thephy_portsthat were successfully added already.drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3785
Quoted 13 lines
drivers/net/phy/phy_device.c +2
-0@@ -3778,6 +3778,8 @@ static int phy_probe(struct device *dev)return 0 ; out: +phy_cleanup_ports (phydev); +sfp_bus_del_upstream (phydev->sfp_bus); phydev->sfp_bus =NULL ;[Severity: Critical]
Is it safe to callphy_cleanup_ports()beforesfp_bus_del_upstream()here?
sfp_bus_del_upstream()unregisters the upstream SFP callbacks and acts as a synchronization barrier by acquiringrtnl_lock(). Before it is called, the SFP bus is still active and can asynchronously fire events likephy_sfp_module_insert().
Quoted 8 lines
Because these SFP events locklessly iterate over the
phydev->portslist viaphy_get_sfp_port(), andphy_cleanup_ports()mutates the list and frees port structures without lock protection:Could this concurrent access lead to list corruption and a use-after-free?
This same incorrect ordering is a pre-existing issue in the
phy_remove()teardown path, but this patch introduces it into thephy_probe()error path.
Heh true indeed... Let me respin then, I'll fix the other callsite that has the same problem
Thanks,
Maxime
PATCH v2 3/3 net: phy: don't try to setup PHY-driven SFP cages when using genphy
In reply to Maxime ChevallierWe don't have support for PHY-driver SFP cages with the genphy code.
On top of that, it was found by sashiko that running sfp_bus_add_upstream() for genphy deadlocks, as for genphy the PHY probing runs under RTNL, which isn't the case for non-genphy drivers.
This problem was reproduced, and does lead to a deadlock on RTNL.
Before the blamed commit, the phy_sfp_probe() call was made by individual PHY drivers, so there was no way to get to the SFP probing path when using genphy.
Let's therefore only run phy_sfp_probe when not using genphy.
--- drivers/net/phy/phy_device.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/net/phy/phy_device.c +8 -3
@@ -3512,9 +3512,14 @@ static int phy_setup_ports(struct phy_device *phydev)if (ret)return ret; - ret =phy_sfp_probe (phydev); -if (ret) - goto out; +/* We don't support SFP with genphy drivers. Also, genphy driver binding +* occurs with RTNL help, wich will deadlock the sfp_bus_add_upstream(). +*/ +if (!phydev->is_genphy_driven) { + ret =phy_sfp_probe (phydev); +if (ret) + goto out; + }if (phydev->n_ports < phydev->max_n_ports) { ret =phy_default_setup_single_port (phydev);
Signature · 1 line
2.54.0
Re: [PATCH net v2 3/3] net: phy: don't try to setup PHY-driven SFP cages when using genphy
Quoted 36 lines · Maxime Chevallier
We don't have support for PHY-driver SFP cages with the genphy code.
On top of that, it was found by sashiko that running
sfp_bus_add_upstream()for genphy deadlocks, as for genphy the PHY probing runs under RTNL, which isn't the case for non-genphy drivers.This problem was reproduced, and does lead to a deadlock on RTNL.
Before the blamed commit, the
phy_sfp_probe()call was made by individual PHY drivers, so there was no way to get to the SFP probing path when using genphy.Let's therefore only run
phy_sfp_probewhen not using genphy.Fixes bad869b5e41a ("net: phy: Only rely on phy_port for PHY-driven SFP")Signed-off-by Maxime Chevallier <[email protected]>--- drivers/net/phy/phy_device.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/net/phy/phy_device.c +1
-3@@ -3512,9 +3512,14 @@ static int phy_setup_ports(struct phy_device *phydev)if (ret)return ret; - ret =phy_sfp_probe (phydev); -if (ret) - goto out; +/* We don't support SFP with genphy drivers. Also, genphy driver binding
drivers/net/phy/phy_device.c +1
-0+* occurs with RTNL help, wich will deadlock the
sfp_bus_add_upstream().
drivers/net/phy/phy_device.c static int phy_setup_ports(struct phy_device *phydev) · line 3525
Quoted 9 lines
drivers/net/phy/phy_device.c +6
-0+*/ +if (!phydev->is_genphy_driven) { + ret =phy_sfp_probe (phydev); +if (ret) + goto out; + }if (phydev->n_ports < phydev->max_n_ports) { ret =phy_default_setup_single_port (phydev);
Thanks
Nicolai
Jakub Kicinski
maintainerRe: [PATCH net v2 3/3] net: phy: don't try to setup PHY-driven SFP cages when using genphy
In reply to Maxime ChevallierThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: don't try to setup PHY-driven SFP cages when using genphy
This change skips phy_sfp_probe() when the genphy driver is bound, avoiding an RTNL deadlock in sfp_bus_add_upstream() since genphy binding runs under RTNL while non-genphy driver binding does not.
drivers/net/phy/phy_device.c static int phy_setup_ports(struct phy_device *phydev) · line 3516
Quoted 13 lines
drivers/net/phy/phy_device.c +2
-3@@ -3512,9 +3512,14 @@ static int phy_setup_ports(struct phy_device *phydev)if (ret)return ret; - ret =phy_sfp_probe (phydev); -if (ret) - goto out; +/* We don't support SFP with genphy drivers. Also, genphy driver binding +* occurs with RTNL help, wich will deadlock the sfp_bus_add_upstream().
^^^^
[Low]
There is a typo in the new comment, "wich" should be "which".
Quoted 9 lines
drivers/net/phy/phy_device.c +6
-0+*/ +if (!phydev->is_genphy_driven) { + ret =phy_sfp_probe (phydev); +if (ret) + goto out; + }if (phydev->n_ports < phydev->max_n_ports) { ret =phy_default_setup_single_port (phydev);
Re: [PATCH net v2 3/3] net: phy: don't try to setup PHY-driven SFP cages when using genphy
Quoted 24 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: phy: don't try to setup PHY-driven SFP cages when using genphyThis change skips
phy_sfp_probe()when the genphy driver is bound, avoiding an RTNL deadlock insfp_bus_add_upstream()since genphy binding runs under RTNL while non-genphy driver binding does not.drivers/net/phy/phy_device.c static int phy_setup_ports(struct phy_device *phydev) · line 3516
Quoted 13 lines
drivers/net/phy/phy_device.c +2
-3@@ -3512,9 +3512,14 @@ static int phy_setup_ports(struct phy_device *phydev)if (ret)return ret; - ret =phy_sfp_probe (phydev); -if (ret) - goto out; +/* We don't support SFP with genphy drivers. Also, genphy driver binding +* occurs with RTNL help, wich will deadlock the sfp_bus_add_upstream(). ^^^^
Quoted 2 lines
[Low]
There is a typo in the new comment, "wich" should be "which".
Sure thing, I need to respin anyway, let me fix this :)
Maxime
Quoted 10 lines
Quoted 10 lines
drivers/net/phy/phy_device.c +6
-0+*/ +if (!phydev->is_genphy_driven) { + ret =phy_sfp_probe (phydev); +if (ret) + goto out; + }if (phydev->n_ports < phydev->max_n_ports) { ret =phy_default_setup_single_port (phydev);