Re: [PATCH net v3] net: ibm: emac: attach and start the fixed-link PHY
From: Linkui Xiao
Date: Thu Nov 26 2026 - 20:48:55 EST
Thanks for the review. All four are legitimate. v4 is posted as a new
thread; it also starts the fixed PHY through emac_mdio_init_phy(), which is
what Andrew asked for in his review of v3.
- [High] Unbind can leave a freed net_device queued on the linkwatch list.
Correct, including the chain, and the reason is not that the reference is
weak. unregister_netdev() -> netdev_run_todo() drains lweventlist through
linkwatch_sync_dev(), and it is also the only place that waits for the
refcount, in netdev_wait_allrefs_any(); it returns as soon as the count
reaches one. A carrier change after that queues an event nobody will ever
wait for again: linkwatch_add_event() does list_add_tail() + netdev_hold(),
and netdev_hold() is a real __dev_hold(), but free_netdev() does not wait
for refs and never takes dev->link_watch_list back off, it goes straight to
ref_tracker_dir_exit(), free_percpu(dev->pcpu_refcnt) and put_device() ->
netdev_release() -> kvfree(). devres frees the netdev as soon as
emac_remove() returns, and __linkwatch_run_queue() then walks the list,
calling netdev_tracker_free() and __dev_put() on freed memory.
Fixed by stopping the PHY before unregister_netdev():
if (dev->ndev->phydev)
phy_stop(dev->ndev->phydev);
phy_stop() is what turns the carrier off, so the event is queued while the
netdev is still registered and unregister_netdev() drains it.
phy_disconnect() stays below it: it only stops the PHY when
phy_is_started(), which is false by then, and moving it up would detach
from a registered netdev without RTNL, which phy_detach_internal() only
tolerates once reg_state != NETREG_REGISTERED.
I did not move phy_start()/phy_stop() into emac_open()/emac_close(). Andrew
asked for the fixed PHY to be started the way this driver starts a real
PHY, and that is emac_mdio_init_phy() from emac_init_phy(), not from
ndo_open. Splitting the two belongs to the phylib/phylink rewrite he also
asked for.
err_detach_tah keeps its order, with one caveat. A failure before
register_netdev() leaves reg_state at NETREG_UNINITIALIZED, where
netif_carrier_off() returns before it reaches linkwatch, so the
phy_disconnect() there cannot queue anything.
The caveat is a failed register_netdev(). It leaves the netdev
NETREG_UNREGISTERED, whether netdev_register_kobject() failed or the
NETDEV_REGISTER veto ran the rollback, so netif_carrier_off() no longer
returns early. If the state machine brought the carrier up between the
probe's own netif_carrier_off() and the failure, the phy_stop() inside
that phy_disconnect() queues one more event: after netdev_run_todo() has
drained the queue on the veto path, with no drain at all on the other.
That takes register_netdev() to fail, so I did not widen the error path
for it.
- [Medium] dev->phy is now filled in asynchronously.
The ordering is real: phy_start() only does phy_queue_state_machine(phydev,
0) and emac_adjust_link() just copies the resolved values into dev->phy, so
emac_open() can win the race and build MR1 from the probe defaults, and
since a fixed link never changes state again the callback will not fire a
second time to correct it.
I left the code alone in v4, but the commit message no longer implies the
speed is settled. It now carries this paragraph:
> 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.
Two things bound the window. It is not new: this driver has always started
its PHY from emac_init_phy() at probe time and never from ndo_open, so the
phy-handle path has the same race against its first ifup. And it is not
permanent: emac_open() runs emac_configure() on every open, so an
ifdown/ifup picks up the dev->phy values the callback filled in meanwhile.
What is permanent is the behaviour before this patch, where dev->phy.speed
stays SPEED_UNKNOWN, emac_configure() takes the default case, and the port
is programmed 10 Mbit/s half duplex with flow control off whatever the
fixed-link property says.
Making it deterministic means reading the link under link_lock before
emac_configure(), or reconfiguring from the callback, and the ethtool paths
already take link_lock before calling into phylib. That is the same
rewrite; I would rather do it there than bolt it onto a crash fix.
- [High, pre-existing] NULL mdio_read on the gpcs path.
Also correct. The branch returns before dev->phy.mdio_read, mdio_write and
gpcs_address are assigned, and emac_configure() reaches
emac_mii_reset_gpcs() -> gpcs_phy_read() on every open and reset_work, so a
sgmii/tbi/rtbi port in that branch dereferences NULL. arches.dts EMAC0
(sgmii, phy-map 0xffffffff, gpcs-address 0xa) is exactly that case. It is
untouched by this patch and predates it; I will send a separate fix.
- [Low] name baab9de385a8 in the commit message.
Done. The v4 commit message now says that baab9de385a8 ("net: ibm: emac:
use netdev's phydev directly") dropped
if (dev->phy_dev)
phy_disconnect(dev->phy_dev);
from emac_remove() along with dev->phy_dev, and that the emac_remove() hunk
restores it.
Cross compiled for powerpc out of ppc44x_defconfig with W=1, clean. No
hardware here, so no runtime test.
Regards,
Linkui
pw-bot: cr