Skip to contentSkip to conversation

net: ibm: emac: attach the fixed-link PHY with of_phy_connect()

PATCH Superseded Linkui Xiao 3 participants 7 messages Nov 12 – Nov 24

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ibm: emac: attach the fixed-link PHY with of_phy_connect()

From: Linkui Xiao <[email protected]>

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")
---
 drivers/net/ethernet/ibm/emac/core.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +8 -5
index 640618c..6e8b711 100644
@@ -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;
 	}
Signature · 1 line
2.25.1
#1 lore raw
Quoted 48 lines · Linkui Xiao

From: Linkui Xiao <[email protected]>

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.

Quoted 7 lines

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....

Quoted 17 lines
Fixes baab9de385a8 ("net: ibm: emac: use netdev's phydev directly")
---
 drivers/net/ethernet/ibm/emac/core.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +0 -3
index 640618c..6e8b711 100644
@@ -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;

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 +8 -2
-			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

#2 lore raw

Hi Andrew,

Thanks for the review, and for pointing out the missing teardown.

Quoted 2 lines

This commit message is overly verbose and contains a lot of irrelevant information.

Agreed. v2 1/2 is three paragraphs: what breaks, why of_phy_connect(), and why the new phy_disconnect() calls belong in the same change. I dropped the walk through phy_prepare_data(), the list of other drivers, the archaeology about 08e39982ef64, and the "no in-tree DTS does this" paragraph. The cover letter notes the AI assistance per Documentation/process/generated-content.rst.

Quoted 1 line

Why is emacs_adjust_link() still called?

of_phy_connect() only stores the handler in phydev->adjust_link, attach never calls it. The state machine is not started on this path either - emac_mdio_init_phy() runs only for the phy-handle case - so nothing else fills in dev->phy, which emac_configure() reads to build MR1. A fixed link cannot change, so calling it once is enough.

Answering that turned up a second bug. Since d99b408ed8e5 ("net: phy:
fixed_phy: remove not needed initialization of phy_device members") the phy_device no longer carries the DT status, so that call currently copies SPEED_UNKNOWN and DUPLEX_UNKNOWN and the MAC is programmed for 10 Mbit/s half duplex. That is 2/2 of v2, which adds a phy_read_status() after the attach. emac_mdio_read_link() already does exactly that for the phy-handle path.

Quoted 3 lines

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....

Fair, and leaving it out was the wrong call: this patch is what creates the attached state, so it should also tear it down. v2 calls phy_disconnect() in emac_remove() and at err_detach_tah, and of_phy_deregister_fixed_link() in both places, plus the connect failure branch. That also fixes the phy-handle path, which called phy_start() without ever calling phy_stop(). emac_remove() unregisters the netdev first, so detaching without RTNL is fine.

v2 is posted as a new thread: [PATCH net v2 0/2].

Cross compiled for powerpc with W=1 out of ppc44x_defconfig, clean. No hardware here, so no runtime test.

Thanks,
Linkui

#3 lore raw
Quoted 5 lines

of_phy_connect() only stores the handler in phydev->adjust_link, attach never calls it. The state machine is not started on this path either - emac_mdio_init_phy() runs only for the phy-handle case - so nothing else fills in dev->phy, which emac_configure() reads to build MR1. A fixed link cannot change, so calling it once is enough.

The whole point of fixed-link is that it works like a real PHY. You should not special case it other than when creating it and destroying it. You should call phy_start/phy_stop. When start is called, phylib will call the adjust link callback, same as a real PHY, and setup the MAC as needed.

Andrew

#4 lore raw

Hi Andrew,

Thanks for the review.

Quoted 5 lines

The whole point of fixed-link is that it works like a real PHY. You should not special case it other than when creating it and destroying it. You should call phy_start/phy_stop. When start is called, phylib will call the adjust link callback, same as a real PHY, and setup the MAC as needed.

Done. v3 drops both the hand called emac_adjust_link() and v2's phy_read_status(): the fixed PHY is attached with of_phy_connect() and started with phy_start(), and phylib resolves the link and calls the callback. phy_stop() comes from phy_disconnect() in emac_remove() and on the probe error path. The only special casing left is creating and destroying the fixed PHY.

phy_start() stays in emac_init_phy() rather than moving to emac_open(), because that is where this driver already starts the phy-handle PHY (emac_mdio_init_phy()). Moving only the fixed link to open/close would special case it again, and moving both paths is a larger change to code I cannot test here. Say the word and I will send that to net-next separately.

I was wrong that a fixed link has no reason to run the state machine. emac_adjust_link() does not program the MAC either, it only copies speed, duplex and pause into dev->phy, which emac_configure() reads at open. phy_start() is called from emac_init_phy(), before register_netdev(), so the state machine is queued before userspace can bring the interface up.

v3 is posted as a new thread.

Cross compiled for powerpc out of ppc44x_defconfig with W=1, clean. No hardware here, so no runtime test.

Thanks,
Linkui

#5 lore raw
In reply to Linkui Xiao
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

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.

Quoted 9 lines
drivers/net/ethernet/ibm/emac/core.c +0 -1
index 640618c0a09e9..6e8b71138bb9a 100644
@@ -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);

drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2682

Quoted 10 lines
drivers/net/ethernet/ibm/emac/core.c +6 -4
-			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()?

drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2684

Quoted 2 lines
drivers/net/ethernet/ibm/emac/core.c +2 -0
+
+			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?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112075321.3903875-1-xiaolinkui%40126.com
#6 lore raw

Thanks for the review. All three issues are legitimate; v2 is a two patch series posted as a new thread, [PATCH net v2 0/2].

- [High] The fixed-link PHY is attached but never detached.

Fixed in 1/2. emac_remove() and err_detach_tah in emac_probe() now call phy_disconnect() and of_phy_deregister_fixed_link(), so the stale attached_dev, the NUM_FP slot, the of_node reference and the module references are all released. emac_remove() already unregisters the netdev before this point, and phy_detach_internal() explicitly allows detaching without RTNL once dev is unregistered, so the suspend use-after-free on the stale attached_dev is gone.

The rebind analysis is correct: without the disconnect the second of_phy_find_device() returns the still-attached PHY and phy_attach_direct() fails with -EBUSY. Adding phy_disconnect() also stops the PHY, which the phy-handle path never did - emac_mdio_init_phy() calls phy_start() and nothing calls phy_stop().

You are also right that "the rest of the driver already treats dev->phy.address < 0 as no PHY to poll, so nothing else changes" was too strong, since the attach state does change. That claim is gone from the v2 commit message.

- [Medium] of_phy_connect() failure leaves the fixed PHY registered.

Fixed in 1/2: the failure branch calls of_phy_deregister_fixed_link(np) before returning. The teardown paths guard the call with of_phy_is_fixed_link(), which is the pattern ravb, nixge, davinci_emac and cpsw use; of_phy_deregister_fixed_link() is also a no-op when no fixed PHY is registered, so reaching it twice is safe.

- [Medium] emac_adjust_link() copies SPEED_UNKNOWN/DUPLEX_UNKNOWN.

This one is worse than "the old code behaved the same way", so thank you for pushing on it. The regression comes from d99b408ed8e5 ("net: phy:
fixed_phy: remove not needed initialization of phy_device members"), which stopped copying the device tree status into the phy_device on the assumption that the state machine fills those members in once the PHY is started. EMAC never starts it on the fixed-link path, so emac_configure() builds MR1 without EMAC_MR1_FDE and without EMAC_MR1_MF_1000 / EMAC_MR1_MF_100, and flow control is left off.

Fixed in 2/2 with phy_read_status() after the attach, which is what emac_mdio_read_link() already does on the phy-handle path. The fixed PHY is bound to genphy, so genphy_read_status() resolves speed, duplex and pause from the BMSR/LPA/STAT1000/ESTATUS registers that swphy_read_reg() emulates out of the device tree status. phy_read_status() rather than phy_start() because a fixed link cannot change and there is no reason to run the state machine for it.

Cross compiled for powerpc with W=1 out of ppc44x_defconfig, clean. No hardware here, so no runtime test.

Regards,
Linkui

pw-bot: cr