net: ibm: emac: attach and start the fixed-link PHY
Subsystems (2)
PATCH v4 1/1 net: ibm: emac: attach and start the fixed-link PHY
From: Linkui Xiao <[email protected]>
emac_init_phy() sets ndev->phydev by hand for a fixed link, so phy_attach_direct() never runs. The PHY is missing from the netdev's PHY link topology and ETHTOOL_MSG_PHY_GET dereferences the NULL link_topo in phy_prepare_data().
Attach it with of_phy_connect(), as this driver already does for the phy-handle case and as gianfar, fec, davinci_emac and nixge do, and start it through emac_mdio_init_phy(), the handler that starts a real PHY here. phylib then resolves the link and calls emac_adjust_link(), which fills in the dev->phy speed, duplex and pause that emac_configure() reads to build MR1. Nothing else fills those in since commit d99b408ed8e5 ("net: phy:fixed_phy: remove not needed initialization of phy_device members"), so today a fixed link is programmed as 10 Mbit/s half duplex with flow control off, whatever the fixed-link property says.
phy_start() only queues the state machine, so an emac_open() that wins the race against the first emac_adjust_link() still builds MR1 from the probe defaults, and a fixed link never changes state again for a second callback to correct it. That window is the one the phy-handle path has always had, this driver starts its PHY at probe time rather than in ndo_open, and the next ifdown/ifup closes it.
Attaching also means detaching, and commit baab9de385a8 ("net: ibm: emac:
use netdev's phydev directly") dropped the phy_disconnect() from emac_remove() together with dev->phy_dev. Since then neither a fixed link nor a phy-handle PHY is detached: unbind leaves phydev->attached_dev pointing at a freed netdev, and the next probe fails with -EBUSY from phy_attach_direct(). Restore it, and deregister the fixed PHY on the same paths.
The PHY is stopped before unregister_netdev() rather than left to phy_disconnect(). phy_stop() turns the carrier off, which queues a linkwatch event, and unregister_netdev() is what drains the queue. phy_disconnect() cannot move up with it, detaching a PHY from a registered netdev wants RTNL.
Found during an audit of net/ethtool/ carried out with an AI coding assistant (Codex), not on hardware.
--- drivers/net/ethernet/ibm/emac/core.c | 33 +++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
v4:
- start the fixed PHY through emac_mdio_init_phy(), the mii_phy_ops init handler this driver already starts a real PHY with, instead of a bare phy_start() (Andrew Lunn)
- stop the PHY in emac_remove() before unregister_netdev(): phy_stop() turns the carrier off, and a carrier change after unregister_netdev() leaves this netdev on the linkwatch list for the linkwatch work to pick up after devres has freed it (Sashiko AI review)
- name commit baab9de385a8 ("net: ibm: emac: use netdev's phydev directly") in the commit message as the one that dropped phy_disconnect() from emac_remove() (Sashiko AI review)
- state how the issue was found and that there is no runtime test, plus the generated-content disclosure (netdev-bot+sinfo)
v3: https://lore.kernel.org/netdev/[email protected]/
- dropped 2/2: no phy_read_status() and no hand called emac_adjust_link(); the fixed link is started and phylib calls the adjust link callback like it does for a real PHY (Andrew Lunn)
- the of_phy_connect() failure branch no longer deregisters the fixed PHY itself: the err_detach_tah cleanup covers every probe failure
v2: https://lore.kernel.org/netdev/[email protected]/
- 1/2: rewrote the commit message by hand (Andrew Lunn)
- 1/2: disconnect the PHY in emac_remove() and on probe failure, and deregister the fixed PHY when of_phy_connect() fails (Andrew Lunn, Sashiko AI review)
- 2/2: new, read the link status so emac_adjust_link() no longer copies
SPEED_UNKNOWN into dev->phy (Sashiko AI review)
v1: https://lore.kernel.org/netdev/[email protected]/
Cross compiled for powerpc out of ppc44x_defconfig with W=1, clean. No hardware here, so no runtime test.
Per Documentation/process/generated-content.rst: the change and the commit message were developed with an AI coding assistant; the analysis, the build testing and the review are mine.
drivers/net/ethernet/ibm/emac/core.c +28 -5
@@ -2673,11 +2673,17 @@ static int emac_init_phy(struct emac_instance *dev)return res; res =of_phy_register_fixed_link (np); - ndev->phydev =of_phy_find_device (np); -if (res || !ndev->phydev) -return res ? res : -EINVAL ; -emac_adjust_link (dev->ndev); -put_device (&ndev->phydev->mdio.dev); +if (res) +return res; + +if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , + dev->phy_mode)) +return -ENODEV ; + +/* Start it the same way as a real PHY. */ + res =emac_mdio_init_phy (&dev->phy); +if (res) +return res; }return 0 ; }
@@ -3190,6 +3196,10 @@ static int emac_probe(struct platform_device *ofdev)/* I have a bad feeling about this ... */ err_detach_tah: +if (ndev->phydev) +phy_disconnect (ndev->phydev); +if (of_phy_is_fixed_link (np)) +of_phy_deregister_fixed_link (np);if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))tah_detach (dev->tah_dev, dev->tah_port); err_detach_rgmii:
@@ -3214,6 +3224,14 @@ static void emac_remove(struct platform_device *ofdev)DBG (dev,"remove" NL ); +/* Stop the PHY first: phy_stop() turns the carrier off and queues a +* linkwatch event, which unregister_netdev() then drains. +* phy_disconnect() has to stay below, detaching a PHY from a +* registered netdev wants RTNL and we do not hold it here. +*/ +if (dev->ndev->phydev) +phy_stop (dev->ndev->phydev); +/* Unregister network device before tearing down hardware * to prevent use-after-free during deferred cleanup. This ensures * the network stack stops all operations before hardware resources
@@ -3223,6 +3241,11 @@ static void emac_remove(struct platform_device *ofdev)cancel_work_sync (&dev->reset_work); +if (dev->ndev->phydev) +phy_disconnect (dev->ndev->phydev); +if (of_phy_is_fixed_link (ofdev->dev.of_node)) +of_phy_deregister_fixed_link (ofdev->dev.of_node); +if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))tah_detach (dev->tah_dev, dev->tah_port);if (emac_has_feature (dev,EMAC_FTR_HAS_RGMII ))
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · Low: 2
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] Use-after-free on the probe error path.
- [Medium] On the fixed-link path, the MAC link parameters are now filled in asynchronously and without a lock shared with the readers.
- [Low] Since the fixed-link PHY is now attached and started at probe, two writers manage the carrier on the fixed-link path and nothing…
- [Low] emac_remove() now calls phy_stop() before unregister_netdev().
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the PHY- less/fixed-link branch of emac_init_phy() returns before it sets…
--
net: ibm: emac: attach and start the fixed-link PHY
This patch attaches the fixed-link PHY in emac_init_phy() with of_phy_connect() and starts it through emac_mdio_init_phy(), so phylib fills in dev->phy via emac_adjust_link(). It also restores phy_disconnect() and of_phy_deregister_fixed_link() in emac_remove() and on the probe error path, and stops the PHY before unregister_netdev().
Quoted 12 lines
drivers/net/ethernet/ibm/emac/core.c +0
-4@@ -2673,11 +2673,17 @@ static int emac_init_phy(struct emac_instance *dev)return res; res =of_phy_register_fixed_link (np); - ndev->phydev =of_phy_find_device (np); -if (res || !ndev->phydev) -return res ? res : -EINVAL ; -emac_adjust_link (dev->ndev);
drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2684
Quoted 10 lines
drivers/net/ethernet/ibm/emac/core.c +9
-1-put_device (&ndev->phydev->mdio.dev); +if (res) +return res; + +if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , + dev->phy_mode)) +return -ENODEV ; + +/* Start it the same way as a real PHY. */ + res =emac_mdio_init_phy (&dev->phy);
[Severity: Medium]
Before this patch, emac_adjust_link() was called synchronously here, so dev->phy was filled in by the time probe returned. Now phy_start() only queues phy_state_machine(). The first emac_adjust_link() runs later from the workqueue, holding only phydev->lock:
emac_adjust_link() {
dev->phy.autoneg = phy->autoneg;
dev->phy.speed = phy->speed;
dev->phy.duplex = phy->duplex;
dev->phy.pause = phy->pause;
dev->phy.asym_pause = phy->asym_pause;
...
}On the fixed-link path, emac_open() holds RTNL and dev->link_lock but never refreshes these fields before it builds MR1:
emac_open() {
...
if (dev->phy.address >= 0) {
...
} else
netif_carrier_on(dev->ndev);
...
emac_configure(dev);
...
}emac_configure() reads dev->phy.speed, duplex, pause and asym_pause, and the two lock sets do not overlap. If emac_open() runs before the first state machine pass, MR1 gets the probe defaults (SPEED_100, DUPLEX_FULL). If it runs during that pass, it can see a mix of old and new fields.
emac_adjust_link() never reprograms MR1, and a fixed link without link-gpios never changes state again. Can the wrong MR1 then stay in place until the next ifdown/ifup?
The commit message says:
That window is the one the phy-handle path has always had, this driver starts its PHY at probe time rather than in ndo_open, and the next ifdown/ifup closes it.
Is that accurate? On the phy-handle path (dev->phy.address >= 0), emac_open() reads the link synchronously through poll_link()/read_link(), which ends up in phy_read_status(). emac_link_timer() also reprograms the MAC on link changes. A fixed link has neither, so a fixed link can still be programmed with the wrong speed, duplex or pause, which is the bug this patch is meant to fix.
[Severity: Low]
Now that the fixed-link PHY is started here, two writers set the carrier on this path. phylib's phy_link_change() calls netif_carrier_on()/off() from the state machine work. The driver still calls netif_carrier_off() in emac_probe() before register_netdev(), netif_carrier_on() in emac_open() for dev->phy.address < 0, and netif_carrier_off() in emac_close().
Nothing orders these writers. phy_start() here runs before the netif_carrier_off() in emac_probe(). If the work runs after that call but while reg_state is still NETREG_UNINITIALIZED, netif_carrier_on() clears __LINK_STATE_NOCARRIER without a linkwatch event. The netdev then registers with the carrier on while it is administratively down. The later netif_carrier_on() in emac_open() is a no-op, so no linkwatch event fires on open.
Could operstate then stay at UNKNOWN instead of moving to UP?
If the work runs first instead, the netif_carrier_off() in emac_probe() overrides phylib, and phylib then thinks the link is up while the carrier is off. With link-gpios, phylib could also turn the carrier back on for a closed interface.
Should the driver leave the carrier to phylib in the fixed-link case?
drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2689
Quoted 5 lines
drivers/net/ethernet/ibm/emac/core.c +2
-0+if (res) +return res; }return 0 ; }
[Severity: High]
This isn't a bug introduced by this patch, but the PHY-less/fixed-link branch of emac_init_phy() returns before these assignments further down:
dev->phy.mdio_read = emac_mdio_read; dev->phy.mdio_write = emac_mdio_write; ... dev->phy.gpcs_address = dev->gpcs_address;
The private area is zeroed, so for a fixed link dev->phy.mdio_read is NULL and dev->phy.gpcs_address is 0. With phy-mode sgmii, tbi or rtbi, emac_open()->emac_configure() reaches:
if (emac_phy_gpcs(dev->phy.mode)) {
if (dev->phy.gpcs_address != 0xffffffff)
emac_mii_reset_gpcs(&dev->phy);Then emac_mii_reset_gpcs()->gpcs_phy_read() does:
return phy->mdio_read(phy->dev, phy->gpcs_address, reg);
Does this call a NULL function pointer with RTNL and dev->link_lock held? emac_reset_work() and the reinit path in emac_link_timer() reach emac_configure() the same way.
There is a second effect now that the real fixed-link speed reaches dev->phy. For a 1000 Mbit/s GPCS fixed link, MR1 is programmed with EMAC_MR1_MF_IPPA(0) instead of the gpcs-address from DT. The oops above would happen first, though.
drivers/net/ethernet/ibm/emac/core.c static int emac_probe(struct platform_device *ofdev) · line 3202
Quoted 8 lines
drivers/net/ethernet/ibm/emac/core.c +4
-0@@ -3190,6 +3196,10 @@ static int emac_probe(struct platform_device *ofdev)/* I have a bad feeling about this ... */ err_detach_tah: +if (ndev->phydev) +phy_disconnect (ndev->phydev); +if (of_phy_is_fixed_link (np)) +of_phy_deregister_fixed_link (np);
[Severity: High]
Can this unwind leave a linkwatch event queued on a netdev that devres then frees?
The fixed-link PHY is started in emac_init_phy() before register_netdev(), so the state machine can run during registration:
emac_init_phy()
emac_mdio_init_phy()
phy_start()
phy_state_machine()
phy_link_up()->phy_link_change(true)
netif_carrier_on() <- clears NOCARRIER, no event yetregister_netdev()
register_netdevice()
netdev_register_kobject() fails <- reg_state = NETREG_UNREGISTERED
goto err_detach_tah
phy_disconnect()
phy_stop()
phy_link_down()->phy_link_change(false)
netif_carrier_off()
linkwatch_fire_event() <- netdev_hold(), added to lweventlistemac_probe() returns the error
devm action: free_netdev()
put_device() <- net_device freed
netif_carrier_off() only suppresses the event for NETREG_UNINITIALIZED:
if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
if (dev->reg_state == NETREG_UNINITIALIZED)
return;
atomic_inc(&dev->carrier_down_count);
linkwatch_fire_event(dev);free_netdev() does not wait for the linkwatch reference, so linkwatch_event() would later walk lweventlist and dereference the freed net_device. A NETDEV_REGISTER notifier veto ends in the same state. If the state machine first runs after the failure, its own netif_carrier_on() queues the event in the same way.
The commit message describes this hazard for emac_remove(), but the probe unwind does not seem to be covered. Would starting the PHY only after register_netdev() succeeds, or from ndo_open/ndo_stop, avoid this?
drivers/net/ethernet/ibm/emac/core.c static void emac_remove(struct platform_device *ofdev) · line 3233
Quoted 11 lines
drivers/net/ethernet/ibm/emac/core.c +7
-0@@ -3214,6 +3224,14 @@ static void emac_remove(struct platform_device *ofdev)DBG (dev,"remove" NL ); +/* Stop the PHY first: phy_stop() turns the carrier off and queues a +* linkwatch event, which unregister_netdev() then drains. +* phy_disconnect() has to stay below, detaching a PHY from a +* registered netdev wants RTNL and we do not hold it here. +*/ +if (dev->ndev->phydev) +phy_stop (dev->ndev->phydev);
[Severity: Low]
For a phy-handle PHY, phy_stop() also powers the PHY down. PHY_HALTED returns PHY_STATE_WORK_SUSPEND, _phy_state_machine_post_work() calls phy_suspend(), and genphy_suspend() sets BMCR_PDOWN. Here that happens while the MAC is still running, since emac_close() only runs later inside unregister_netdev().
emac_close() then calls emac_rx_disable()/emac_tx_disable(), which busy wait for the idle bits:
while (!(in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
udelay(1);
--n;
}
if (unlikely(!n))
emac_report_timeout_error(dev, "RX disable timeout");If the powered-down PHY stops supplying RX_CLK/TX_CLK, could these waits run to the full timeout and log "RX/TX disable timeout" on unbind?
The link-loss handling in emac_link_timer() (emac_rx_clk_tx() and emac_reinitialize()) is also skipped here, because phylib has already cleared the carrier.
Before commit baab9de385a8, the PHY was disconnected only after unregister_netdev(), so the MAC was stopped while the PHY still had power.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127014748.2908781-1-xiaolinkui%40126.com
Quoted 6 lines · Linkui Xiao
From: Linkui Xiao <[email protected]>
emac_init_phy()setsndev->phydevby hand for a fixed link, sophy_attach_direct()never runs. The PHY is missing from the netdev's PHY link topology andETHTOOL_MSG_PHY_GETdereferences the NULLlink_topoinphy_prepare_data().
The size and complexity of this change is snowballing. Without having hardware to test on, i'm wondering if it is better to just leave it broken. I don't think any user has reported it, so it does not bother anybody.
What really needs to happen is a complete rewrite, and that definitely needs hardware.
Andrew
Quoted 8 lines · Andrew Lunn
Quoted 6 lines · Linkui Xiao
From: Linkui Xiao <[email protected]>
emac_init_phy()setsndev->phydevby hand for a fixed link, sophy_attach_direct()never runs. The PHY is missing from the netdev's PHY link topology andETHTOOL_MSG_PHY_GETdereferences the NULLlink_topoinphy_prepare_data().
Quoted 7 lines
The size and complexity of this change is snowballing. Without having hardware to test on, i'm wondering if it is better to just leave it broken. I don't think any user has reported it, so it does not bother anybody.
What really needs to happen is a complete rewrite, and that definitely needs hardware.
Agreed. Each round has moved more of the PHY lifecycle around (start, stop, disconnect, the probe unwind and the remove ordering), and I have no 4xx hardware to check any of it, so I will not respin this one. The rewrite is not something I can take on without access to a board.
[OPTIONAL, see notes: delete this paragraph if you do not want to offer the alternative]
One much smaller option, in case it is of any use: the oops itself is phy_prepare_data() in net/ethtool/phy.c doing xa_load() on a NULL dev->link_topo. A two-line "if (!topo) return -EOPNOTSUPP;" there would turn ethtool --show-phys on such a netdev into a clean error instead of an oops, without touching emac at all. I will only send that if you want it; otherwise I will leave the whole thing alone.
pw-bot: rejected
Quoted 8 lines
[OPTIONAL, see notes: delete this paragraph if you do not want to offer the alternative]
One much smaller option, in case it is of any use: the oops itself isphy_prepare_data()innet/ethtool/phy.cdoingxa_load()on a NULLdev->link_topo. A two-line "if (!topo) return-EOPNOTSUPP;" there would turnethtool --show-physon such a netdev into a clean error instead of an oops, without touching emac at all. I will only send that if you want it; otherwise I will leave the whole thing alone.
I say leave it alone. If anybody can trigger this, they have the hardware. We can then propose patches and get them tested. By adding such a test we are just hiding other broken drivers. The kernel opps'ing can be good, it makes it clear something is broken, and the stack trace often makes it clear where.
Andrew