Re: [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic
From: Maxime Chevallier
Date: Sun Dec 06 2026 - 09:43:32 EST
On 12/5/26 13:18, netdev-bot+sashiko@xxxxxxxxxx wrote:
> Thank you for your contribution! Sashiko AI review found 7 potential
> issue(s) to consider.
>
> Critical: 0 · High: 0 · Medium: 3 · Low: 4
>
> 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).
>
> - [Medium] phy_mux_start() (drivers/net/phy/phy_mux.c) sets mux->state =
> PHY_MUX_STARTED and then calls phy_mux_select(mux, port), but it…
> - [Medium] phy_mux_attach() now runs phy_mux_port_deselect() on every
> port right after phy_attach_direct().
> - [Medium] net: phy: unlocked phy_mux_detach() races with unregister and
> ethtool
> - [Low] Concern: phy_mux_unregister() calls pops->detach() and only then
> phy_mux_stop(), so a deselect callback could reach a parent that has…
> - [Low] The mux's own bookkeeping, phy_mux_port_set_parent_phy(), depends
> on which optional parent callbacks exist, and the two directions check…
> - [Low] Concern: phy_mux_start() does not check that the mux is attached.
> - [Low] Concern: phy_mux_detach() and phy_mux_stop() read mux->state with
> no lock under a conditional rtnl contract, so a phy_mux_start() coming…
>
[...]
> [Severity: Medium]
> This loop calls phy_mux_phy_power_down() -> phy_suspend() on every PHY
> right after phy_attach_direct() resumed it with phy_resume(). On genphy
> that sets BMCR_PDOWN.
>
> phy_mux_port_ops has no select or resume op, so the mux never powers a
> PHY back up itself. Later in the series this only happens in
> phylink_mux_select() -> phy_start(), which runs from phylink_start() ->
> phy_mux_start() -> state_work.
>
> Also later in the series, phylink_attach_mux() passes PHY_F_RXC_ALWAYS_ON
> to fwnode_phy_mux_attach() when pl->config->mac_requires_rxc is set. Does
> this power-down cancel out that flag?
>
> phylink_fwnode_phy_connect()
> phylink_attach_mux() /* flags |= PHY_F_RXC_ALWAYS_ON */
> fwnode_phy_mux_attach()
> phy_mux_attach()
> phy_attach_direct() /* phy_resume() */
> phy_mux_port_deselect()
> phy_mux_phy_power_down()
> phy_suspend()
>
> Some MACs need the PHY's RX clock between connect and phylink_start(),
> for example for a DMA soft reset in ndo_open. Behind a mux, such a MAC
> could fail to open.
>
> The only user so far (mvneta on Turris Omnia) doesn't set
> mac_requires_rxc. The failure also depends on the PHY stopping RXC while
> powered down.
I have never seen a mux on RGMII, even less so on stmmac...
The ones I've seen are on RMII or serialized MII.
>
> [ ... ]
>
>> +void phy_mux_start(struct phy_mux *mux)
>> +{
>> + struct phy_mux_port *port;
>> + unsigned long id = 0;
>> +
>> + mux->state = PHY_MUX_STARTED;
>> +
>> + port = xa_find(&mux->ports, &id, ULONG_MAX, XA_PRESENT);
>> + phy_mux_select(mux, port);
>> +}
>
> [Severity: Medium]
> What happens here if the parent's ->select() fails?
>
> __phy_mux_select_port() rolls back netdev->phydev and phy_link_change.
> phy_mux_select() then returns the error without setting
> mux->current_port. phy_mux_start() drops that return value, and it is
> declared void in include/linux/phy_mux.h, so the parent can't see the
> failure either.
>
> The mux is left in PHY_MUX_STARTED with current_port == NULL. Every PHY is
> still powered down from phy_mux_attach(), and nothing retries the
> selection.
>
> The rest of the series doesn't change this. phy_mux_start() later only
> queues state_work, and phy_mux_state_machine() also ignores the result of
> phy_mux_select(mux, phy_mux_choose_port(mux)). The only later re-trigger
> is an SFP availability change.
>
> With phylink as the parent, phylink_mux_select() fails if
> phylink_bringup_phy() fails. That can happen when port->interface fails
> validation, or on an MDIO error from phy_eee_rx_clock_stop(). The
> interface would then be up with no PHY and no link until it is taken down
> and brought up again.
>
> Should the error be returned to the caller, or should the selection be
> retried?
For now, this works well with phylink. I don't know if retrying brings
anything ?
>
> [ ... ]
>
>> @@ -188,13 +281,15 @@ EXPORT_SYMBOL_GPL(fwnode_phy_mux_attach);
>> * phy_mux_detach() - Detach the mux from its parent
>> * @mux: The mux
>> *
>> - * The PHYs behind the mux are detached.
>> + * The mux is stopped and its PHYs are detached. Caller must hold rtnl when
>> + * the mux was started.
>> */
>> void phy_mux_detach(struct phy_mux *mux)
>> {
>> if (!mux->netdev)
>> return;
>>
>> + phy_mux_stop(mux);
>> phy_mux_detach_phys(mux);
>>
>> if (mux->devlink) {
>
> [Severity: Medium]
> This kernel-doc makes rtnl optional for phy_mux_detach() as long as the
> mux is stopped. In that case phy_mux_detach() does all of the following
> without holding rtnl or phy_mux_mutex:
>
> - runs phy_mux_detach_phys()
> - calls device_link_del(mux->devlink)
> - clears mux->pops, mux->parent_priv and mux->netdev
>
> phy_mux_unregister() drops phy_mux_mutex right after list_del(). It
> then relies only on rtnl to read mux->netdev and mux->pops and to call
> mux->pops->detach(). An unlocked phy_mux_detach() therefore shares no
> lock with it.
>
> The two can also run at the same time. The device link added in
> phy_mux_attach() is DL_FLAG_STATELESS, so unbinding the mux driver
> doesn't unbind the MAC first. mii_mux_auto_remove(), later in the
> series, can then run phy_mux_unregister() and phy_mux_free()
> concurrently with MAC removal.
>
> Later in the series, phylink_destroy() calls phylink_detach_mux() ->
> phy_mux_detach() without rtnl, since phylink_destroy() must not be
> called with rtnl held. That path is reachable with a stopped mux, for
> example:
>
> mvpp2_open()
> phylink_fwnode_phy_connect() /* mux attached, PHY_MUX_STOPPED */
> request_irq() fails /* no phylink_disconnect_phy() */
> ...
> mvpp2_port_remove()
> phylink_destroy()
> phylink_detach_mux()
> phy_mux_detach() /* no rtnl */
>
> ucc_geth ends up in the same place when ucc_geth_open() fails in
> ucc_geth_init_mac() or request_irq() and ucc_geth_remove() runs
> later.
>
> If the mux driver is unbound at that point, both CPUs can be inside
> phy_mux_detach(). One of them gets there through mux->pops->detach()
> -> phylink_mux_detach(). Possible results are:
>
> - a double phy_detach() and device_link_del()
> - a NULL dereference of mux->pops
> - a use-after-free of the phylink passed as parent_priv
> - a use-after-free of the mux and its ports after phy_mux_free()
>
> ucc_geth registers its netdev with devm_register_netdev(), so it is
> still NETREG_REGISTERED when phylink_destroy() runs. Two things follow:
>
> - phy_detach() reaches the rcu_dereference_protected() on dev->hwprov
> in phy_detach_internal() without rtnl, and lockdep will complain.
> - phy_link_topo_del_phy() kfree()s the phy_device_node while an
> ethtool netlink request in ethnl_req_get_phydev() may be looking it
> up under rtnl.
>
> The attach side doesn't help either. fwnode_phy_mux_attach() only
> takes phy_mux_mutex, and callers such as dsa_user_create() and
> ocelot_probe_port() don't hold rtnl. phy_mux_detach() takes neither
> lock. So mux->netdev, mux->pops, mux->parent_priv and mux->devlink
> have no single lock covering attach, detach and unregister.
>
> Triggering this needs a privileged driver unbind or module unload on
> top of an ndo_open() error path, so it isn't easy to hit. Nothing
> later in the series changes it, though.
>
> Should phy_mux_detach() always require rtnl, with phylink_destroy()
> taking it around phylink_detach_mux()? Or should phy_mux_mutex protect
> the mux state across attach, detach and unregister?
Already answered on the previous patch, I'll add RTNL requirement here
>