* [PATCH net v4] net: ibm: emac: attach and start the fixed-link PHY
@ 2026-11-27 1:47 Linkui Xiao
2026-11-28 2:39 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Linkui Xiao @ 2026-11-27 1:47 UTC (permalink / raw)
To: Andrew Lunn, netdev
Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Rosen Penev, Christian Lamparter, Jacob Keller,
Kees Cook, Daniel Machon, Heiner Kallweit, linux-kernel,
Linkui Xiao
From: Linkui Xiao <xiaolinkui@kylinos.cn>
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.
Fixes: baab9de385a8 ("net: ibm: emac: use netdev's phydev directly")
Fixes: d99b408ed8e5 ("net: phy: fixed_phy: remove not needed initialization of phy_device members")
Signed-off-by: Linkui Xiao <xiaolinkui@kylinos.cn>
---
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/20261124083941.2480237-1-xiaolinkui@126.com/
- 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/20261123093201.770552-1-xiaolinkui@126.com/
- 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/20261112075321.3903875-1-xiaolinkui@126.com/
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.
diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
index 1b95b4d5deff..27b79f897e36 100644
--- a/drivers/net/ethernet/ibm/emac/core.c
+++ b/drivers/net/ethernet/ibm/emac/core.c
@@ -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))
--
2.25.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH net v4] net: ibm: emac: attach and start the fixed-link PHY
2026-11-27 1:47 [PATCH net v4] net: ibm: emac: attach and start the fixed-link PHY Linkui Xiao
@ 2026-11-28 2:39 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 2:39 UTC (permalink / raw)
To: xiaolinkui
Cc: andrew+netdev, netdev, andrew, davem, edumazet, kuba, pabeni,
rosenp, chunkeey, jacob.e.keller, kees+treewide, daniel.machon,
hkallweit1, linux-kernel, xiaolinkui
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().
> diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> index 1b95b4d5deff5..27b79f897e36f 100644
> --- a/drivers/net/ethernet/ibm/emac/core.c
> +++ b/drivers/net/ethernet/ibm/emac/core.c
> @@ -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);
[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?
> + 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.
> @@ -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 yet
register_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 lweventlist
emac_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?
> @@ -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.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127014748.2908781-1-xiaolinkui%40126.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-28 2:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 1:47 [PATCH net v4] net: ibm: emac: attach and start the fixed-link PHY Linkui Xiao
2026-11-28 2:39 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®