* [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
@ 2026-11-12 7:53 Linkui Xiao
2026-11-13 13:50 ` Andrew Lunn
2026-11-16 13:51 ` netdev-bot+sashiko
0 siblings, 2 replies; 3+ messages in thread
From: Linkui Xiao @ 2026-11-12 7:53 UTC (permalink / raw)
To: andrew+netdev, davem, edumazet, kuba, pabeni, rosenp,
jacob.e.keller, daniel.machon, chunkeey, kees+treewide
Cc: netdev, linux-kernel, Linkui Xiao
From: Linkui Xiao <xiaolinkui@kylinos.cn>
emac_init_phy() handles a "fixed-link" property by registering a fixed
PHY for the MAC's own OF node and then assigning net_device->phydev to
it by hand:
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);
net_device->phydev is meant to be set by phy_attach_direct(), which is
what the of_phy_connect() helper reaches. Assigning it by hand skips
everything attach does: phydev->attached_dev stays NULL, no link change
handler is wired up, the PHY never enters the netdev's phy_link_topology,
and the put_device() drops the reference the driver just took, so the
phy_device is kept alive afterwards only by the fixed MDIO bus it is
registered on.
The missing topology entry is what prompted this. ethnl_req_get_phydev()
returns dev->phydev as-is when an ethtool netlink request carries no
phy-index, so ETHTOOL_MSG_PHY_GET on an EMAC port with a fixed-link finds
a phydev and then looks it up in a link_topo that was never allocated:
struct phy_link_topology *topo = reply_data->dev->link_topo;
pdn = xa_load(&topo->phys, phydev->phyindex);
which dereferences NULL in phy_prepare_data().
Every other phylib driver with fixed-link support registers the fixed PHY
and then passes the MAC's own OF node to of_phy_connect(): gianfar, fec,
davinci_emac and nixge all do it, and so does emac_dt_phy_connect() for
the phy-handle case. Do the same here. of_phy_connect() takes the attach
reference and drops the of_phy_find_device() one, so the explicit
put_device() goes away. emac_adjust_link() is still called once by hand
afterwards and the PHY state machine is still not started, because a
fixed link does not change. The rest of the driver already treats
dev->phy.address < 0 as "no PHY to poll" and never reaches
dev->phy.def->ops, so nothing else changes.
The hand assignment itself dates back to commit 08e39982ef64 ("net: emac:
fix fixed-link setup for the RTL8363SB switch"), but it only started
affecting the net_device when commit baab9de385a8 ("net: ibm: emac: use
netdev's phydev directly") moved the pointer from the driver private
dev->phy_dev into ndev->phydev, so that is the commit fixed here.
No in-tree DTS pairs "ibm,emac" with a "fixed-link" property, so this is
latent in mainline; it is out-of-tree users such as the Netgear WNDAP620
that hit it. There is no such hardware here, so the change is verified by
cross compilation and inspection only.
emac_remove() still does not call phy_disconnect(), which the phy-handle
path gets wrong today as well. That is left to a separate change.
Fixes: baab9de385a8 ("net: ibm: emac: use netdev's phydev directly")
Signed-off-by: Linkui Xiao <xiaolinkui@kylinos.cn>
---
drivers/net/ethernet/ibm/emac/core.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
index 640618c..6e8b711 100644
--- a/drivers/net/ethernet/ibm/emac/core.c
+++ b/drivers/net/ethernet/ibm/emac/core.c
@@ -2674,11 +2674,14 @@ 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 -EINVAL;
+
+ emac_adjust_link(ndev);
}
return 0;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
2026-11-12 7:53 [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect() Linkui Xiao
@ 2026-11-13 13:50 ` Andrew Lunn
2026-11-16 13:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Lunn @ 2026-11-13 13:50 UTC (permalink / raw)
To: Linkui Xiao
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, rosenp,
jacob.e.keller, daniel.machon, chunkeey, kees+treewide, netdev,
linux-kernel, Linkui Xiao
On Thu, Nov 12, 2026 at 03:53:21PM +0800, Linkui Xiao wrote:
> From: Linkui Xiao <xiaolinkui@kylinos.cn>
>
> emac_init_phy() handles a "fixed-link" property by registering a fixed
> PHY for the MAC's own OF node and then assigning net_device->phydev to
> it by hand:
>
> 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);
>
> net_device->phydev is meant to be set by phy_attach_direct(), which is
> what the of_phy_connect() helper reaches. Assigning it by hand skips
> everything attach does: phydev->attached_dev stays NULL, no link change
> handler is wired up, the PHY never enters the netdev's phy_link_topology,
> and the put_device() drops the reference the driver just took, so the
> phy_device is kept alive afterwards only by the fixed MDIO bus it is
> registered on.
>
> The missing topology entry is what prompted this. ethnl_req_get_phydev()
> returns dev->phydev as-is when an ethtool netlink request carries no
> phy-index, so ETHTOOL_MSG_PHY_GET on an EMAC port with a fixed-link finds
> a phydev and then looks it up in a link_topo that was never allocated:
>
> struct phy_link_topology *topo = reply_data->dev->link_topo;
> pdn = xa_load(&topo->phys, phydev->phyindex);
>
> which dereferences NULL in phy_prepare_data().
>
> Every other phylib driver with fixed-link support registers the fixed PHY
> and then passes the MAC's own OF node to of_phy_connect(): gianfar, fec,
> davinci_emac and nixge all do it, and so does emac_dt_phy_connect() for
> the phy-handle case. Do the same here. of_phy_connect() takes the attach
> reference and drops the of_phy_find_device() one, so the explicit
> put_device() goes away. emac_adjust_link() is still called once by hand
> afterwards and the PHY state machine is still not started, because a
> fixed link does not change. The rest of the driver already treats
> dev->phy.address < 0 as "no PHY to poll" and never reaches
> dev->phy.def->ops, so nothing else changes.
>
> The hand assignment itself dates back to commit 08e39982ef64 ("net: emac:
> fix fixed-link setup for the RTL8363SB switch"), but it only started
> affecting the net_device when commit baab9de385a8 ("net: ibm: emac: use
> netdev's phydev directly") moved the pointer from the driver private
> dev->phy_dev into ndev->phydev, so that is the commit fixed here.
>
> No in-tree DTS pairs "ibm,emac" with a "fixed-link" property, so this is
> latent in mainline; it is out-of-tree users such as the Netgear WNDAP620
> that hit it. There is no such hardware here, so the change is verified by
> cross compilation and inspection only.
>
> emac_remove() still does not call phy_disconnect(), which the phy-handle
> path gets wrong today as well. That is left to a separate change.
This commit message is overly verbose and contains a lot of irrelevant
information. That is typical of LMM generated commit messages.
Please try writing the commit message by hand. Its O.K. to write it in
your first language and use a LLM to translate it to English.
The commit message is about "Why?". And it is there to answer
questions the reviewer might have. This last sentence about
phy_disconnect() was one of the things i thought about while looking
at the change, but because the commit messages was so long, i had
given up reading it....
> Fixes: baab9de385a8 ("net: ibm: emac: use netdev's phydev directly")
> Signed-off-by: Linkui Xiao <xiaolinkui@kylinos.cn>
> ---
> drivers/net/ethernet/ibm/emac/core.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> index 640618c..6e8b711 100644
> --- a/drivers/net/ethernet/ibm/emac/core.c
> +++ b/drivers/net/ethernet/ibm/emac/core.c
> @@ -2674,11 +2674,14 @@ 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 -EINVAL;
> +
> + emac_adjust_link(ndev);
Why is emacs_adjust_link() still called?
Andrew
---
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
2026-11-12 7:53 [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect() Linkui Xiao
2026-11-13 13:50 ` Andrew Lunn
@ 2026-11-16 13:51 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:51 UTC (permalink / raw)
To: xiaolinkui
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, rosenp,
jacob.e.keller, daniel.machon, chunkeey, kees+treewide, netdev,
linux-kernel, xiaolinkui
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 2 · Low: 0
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] The patch attaches the fixed-link PHY with of_phy_connect() in
emac_init_phy() (drivers/net/ethernet/ibm/emac/core.c:2680) but never…
- [Medium] In emac_init_phy()
(drivers/net/ethernet/ibm/emac/core.c:2684), the fixed-link path calls…
- [Medium] net: ibm: emac: fixed-link PHY leak in emac_init_phy() on
connect error
--
net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
This commit changes the fixed-link branch of emac_init_phy(). Instead of
setting ndev->phydev by hand, it now calls of_phy_connect(), so the fixed
PHY is properly attached and enters the netdev's phy_link_topology. This
avoids a NULL link_topo dereference in phy_prepare_data() for
ETHTOOL_MSG_PHY_GET.
> diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> index 640618c0a09e9..6e8b71138bb9a 100644
> --- a/drivers/net/ethernet/ibm/emac/core.c
> +++ b/drivers/net/ethernet/ibm/emac/core.c
> @@ -2674,11 +2674,14 @@ 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 -EINVAL;
[Severity: Medium]
When of_phy_connect() fails here, the fixed PHY that
of_phy_register_fixed_link(np) registered a few lines above is never
cleaned up. emac_init_phy() returns -EINVAL immediately. Neither this
function nor the emac_probe() error path calls
of_phy_deregister_fixed_link(np).
of_phy_register_fixed_link() goes through fixed_phy_register(), which
does three things:
- takes one of the NUM_FP slots in the fixed PHY table;
- takes a reference on np with of_node_get();
- registers a phy_device on the global fixed MDIO bus.
All of this survives the failed probe.
of_phy_connect() can return NULL here in several ways:
- of_phy_find_device() does not find the device;
- phy_attach_direct() fails in try_module_get(),
phy_link_topo_add_phy() or phy_init_hw();
- phy_attach_direct() hits the -EBUSY "PHY already attached" case.
The last case ties in with the missing phy_disconnect() in
emac_remove(). On a rebind the first fixed PHY is still attached. Every
later probe attempt then registers another fixed PHY for the same
of_node, fails to connect, and leaks it. The fixed PHY table has only
NUM_FP slots and is shared by the whole system. Wouldn't a few
unbind/bind cycles exhaust it, so that fixed-link users of other
drivers fail too?
The old code had a similar leak when of_phy_find_device() returned
NULL. This patch rewrites this error handling anyway. Could the failure
branch call of_phy_deregister_fixed_link(np) before returning? The
drivers named in the commit message deregister the fixed link on their
probe error paths. The same unwinding would also be needed when probe
fails after emac_init_phy() has succeeded.
[Severity: High]
This path now really attaches the PHY. Does anything ever detach it? I
couldn't find phy_disconnect(), phy_detach() or
of_phy_deregister_fixed_link() anywhere under
drivers/net/ethernet/ibm/emac/.
Two paths leave the attach in place. In emac_probe(), a register_netdev()
failure jumps straight to err_detach_tah:
err = register_netdev(ndev);
if (err) {
...
goto err_detach_tah;
}
and emac_remove() only does:
unregister_netdev(dev->ndev);
...
mal_unregister_commac(dev->mal, &dev->commac);
emac_put_deps(dev);
The ndev comes from devm_alloc_etherdev(), so devres frees it on unbind or
on a failed probe. The fixed PHY stays registered on the global fixed MDIO
bus. Its attached_dev, adjust_link and phy_link_change fields still point
at the freed netdev. Before this patch, attached_dev stayed NULL on this
path.
Can this lead to a use-after-free on system suspend? The path would be:
mdio_bus_phy_suspend()
mdio_bus_phy_may_suspend()
if (netdev->ethtool->wol_enabled)
return false;
where netdev is the stale attached_dev. phy_suspend() also reads
netdev->ethtool->wol_enabled.
Does rebinding break too? On unbind/bind, a module reload, or a retry
after a failed register_netdev(), of_phy_register_fixed_link(np) registers
a second fixed PHY with the same of_node. But of_phy_find_device(np)
inside of_phy_connect() returns the first one, which is still attached.
phy_attach_direct() then fails here:
if (phydev->phy_link_change) {
phydev_err(phydev, "PHY already attached\n");
err = -EBUSY;
so emac_init_phy() returns -EINVAL on every later probe. The old code
rebound without an error, which doesn't seem to match this part of the
commit message:
so nothing else changes.
Each bind also seems to leak a few things:
- the device reference;
- the try_module_get() on the fixed_phy module, which then can't be
unloaded;
- the module reference on the genphy driver;
- the phy_device_node allocated by phy_link_topo_add_phy(), because
netdev_free_phy_link_topology() only calls xa_destroy() on topo->phys.
The of_phy_connect() kernel-doc says:
The refcount must be dropped by calling phy_disconnect() or phy_detach().
The drivers named in the commit message (gianfar, fec, davinci_emac,
nixge) pair of_phy_connect() with phy_disconnect() and
of_phy_deregister_fixed_link(), both on teardown and on the error paths.
The commit message leaves phy_disconnect() to a separate change. But this
patch is what creates the attach state on the fixed-link path. Should
phy_disconnect() and of_phy_deregister_fixed_link() be added here, both to
the emac_probe() error path after emac_init_phy() and to emac_remove()?
> +
> + emac_adjust_link(ndev);
[Severity: Medium]
Does phydev hold valid speed, duplex and pause values at this point?
fixed_phy_register() only stores the status in fmb_fixed_phys[] for
register emulation:
fmb_fixed_phys[phy_addr].status = *status;
fmb_fixed_phys[phy_addr].status.link = true;
and phy_device_create() starts with:
dev->speed = SPEED_UNKNOWN;
dev->duplex = DUPLEX_UNKNOWN;
dev->pause = false;
None of phy_attach_direct(), phy_probe(), phy_init_hw() or phy_resume()
reads link status, and the state machine is not started.
So emac_adjust_link() copies SPEED_UNKNOWN, DUPLEX_UNKNOWN and pause = 0
into dev->phy. That overwrites the dev->phy.pause = 1 set earlier in this
branch, and the SPEED_100/DUPLEX_FULL defaults from emac_probe().
emac_open() then calls emac_configure(). Because duplex is not
DUPLEX_FULL, it leaves out EMAC_MR1_FDE / EMAC_MR1_MWSW_001 and the flow
control bits. It takes the default case in the dev->phy.speed switch, and
it passes SPEED_UNKNOWN to rgmii_set_speed() / zmii_set_speed().
Wouldn't that leave the MAC at 10 Mb/s half duplex whatever the
fixed-link node says, for example 1000/full to an RTL8363SB switch?
The old hand-assignment code behaved the same way, but the commit message
relies on this call to configure the link:
emac_adjust_link() is still called once by hand
afterwards and the PHY state machine is still not started, because a
fixed link does not change.
Now that the PHY is attached, could phy_read_status(ndev->phydev) be
called under phydev->lock before emac_adjust_link()? Or could phylib drive
the handler instead?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112075321.3903875-1-xiaolinkui%40126.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-16 13:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 7:53 [PATCH net] net: ibm: emac: attach the fixed-link PHY with of_phy_connect() Linkui Xiao
2026-11-13 13:50 ` Andrew Lunn
2026-11-16 13:51 ` 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