All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Maxime Chevallier" <maxime.chevallier@bootlin.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device
Date: Sat, 05 Dec 2026 11:08:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-251445@kernel.org> (raw)
In-Reply-To: <20261204110622.1106492-5-maxime.chevallier@bootlin.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] net: phy: unsynchronized phy_mux_detach() races with
  unregister and attach
- [Medium] net: phy: phy_mux: phy_mux_attach() clobbers dev->sfp_bus
- [Medium] net: phy: phy_mux_detach_phys() omits phy_stop_machine()
  before phy_detach()
--

commit 5ec0ab0887a678472a4f565b6b693b7e69aef8df
Author: Maxime Chevallier <maxime.chevallier@bootlin.com>

net: phy: phy_mux: Allow attaching a mux to a net_device

This commit adds support for attaching a phy_mux to a net_device and its
parent, binding and attaching the PHYs behind the mux and introducing
parent_ops attach and detach callbacks.

> diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
> index e259eb4e271a0..691bca09e0af1 100644
> --- a/drivers/net/phy/phy_mux.c
> +++ b/drivers/net/phy/phy_mux.c

[ ... ]

> +static void phy_mux_detach_phys(struct phy_mux *mux)
> +{
> +	struct phy_mux_port *port;
> +	unsigned long id;
> +
> +	xa_for_each(&mux->ports, id, port) {
> +		if (!port->attached)
> +			continue;
> +
> +		phy_detach(port->phydev);
> +		phy_mux_port_unbind_phy(port);
> +		port->attached = false;
> +	}
> +}

[Severity: Medium]
Should phy_mux_detach_phys() call phy_stop_machine() before phy_detach()?

phy_mux_attach() sets port->phydev->phy_link_change to
phy_mux_phy_link_change, which causes phy_uses_state_machine() to return
true even for unselected mux PHYs.  On MAC drivers without mac_managed_pm
(such as mvneta), mdio_bus_phy_resume() then calls phy_start_machine() and
queues phydev->state_queue.  For non-genphy drivers, phy_detach() does not
cancel state_queue.

If a deselected PHY is in PHY_HALTED with phydev->suspended still false
(for example, with WoL enabled), a queued phy_state_machine() work item
can race with detach and netdev teardown:

CPU0                              CPU1 (kworker)
                                  phy_state_machine()
                                    _phy_state_machine()  /* PHY_HALTED */
                                    mutex_unlock(&phydev->lock)
                                    phy_suspend()
                                      netdev = phydev->attached_dev
phy_mux_detach()
  phy_mux_detach_phys()
    phy_detach()
      phydev->attached_dev = NULL
      phy_device_reset(phydev, 1)
free_netdev(netdev)
                                      netdev->ethtool->wol_enabled
                                        <-- use-after-free

Even without WoL, a pending phy_suspend() can run after
phy_device_reset(phydev, 1) or race with a subsequent re-attach and power
down an active PHY.

[ ... ]

> +		ret = phy_attach_direct(dev, port->phydev, phy_flags,
> +					port->interface);
> +		if (ret) {
> +			phy_mux_port_unbind_phy(port);
> +			goto err_detach;
> +		}
> +
> +		port->phydev->phy_link_change = phy_mux_phy_link_change;
> +		port->attached = true;
> +		has_phys = true;
> +	}
> +
> +	if (has_phys)
> +		dev->phydev = NULL;

[Severity: Medium]
Can attaching a muxed PHY here clobber dev->sfp_bus?

Unlike phy_sysfs_create_links(), phy_attach_direct() unconditionally sets
dev->sfp_bus = phydev->sfp_bus when phydev->sfp_bus_attached is true, even
when phydev->mux_port is set.  Because deselected mux PHYs keep
phydev->attached_dev set to dev, later phy_sfp_attach() and
phy_sfp_detach() calls also overwrite or clear dev->sfp_bus.

On a mux where a PHY has a downstream SFP cage, this overwrites any
existing dev->sfp_bus (such as an SFP cage on another mux port), causing
ethtool -m to query the wrong cage.  In addition, phy_detach_internal()
clears phydev->attached_dev without clearing dev->sfp_bus, leaving a
dangling dev->sfp_bus pointer if the PHY and its SFP socket are later
unbound while dev stays registered.

[ ... ]

> +void phy_mux_detach(struct phy_mux *mux)
> +{
> +	if (!mux->netdev)
> +		return;
> +
> +	phy_mux_detach_phys(mux);
> +
> +	if (mux->devlink) {
> +		device_link_del(mux->devlink);
> +		mux->devlink = NULL;
> +	}
> +
> +	mux->pops = NULL;
> +	mux->parent_priv = NULL;
> +	mux->netdev = NULL;
> +}
> +EXPORT_SYMBOL_GPL(phy_mux_detach);

[ ... ]

>  void phy_mux_unregister(struct phy_mux *mux)
>  {
>  	mutex_lock(&phy_mux_mutex);
>  	list_del(&mux->node);
>  	mutex_unlock(&phy_mux_mutex);
> +
> +	rtnl_lock();
> +	if (mux->netdev && mux->pops && mux->pops->detach)
> +		mux->pops->detach(mux, mux->parent_priv);
> +	rtnl_unlock();
>  }

[Severity: Medium]
Can phy_mux_detach() race with phy_mux_unregister() and phy_mux_free()?

phy_mux_attach() adds mux->devlink with DL_FLAG_STATELESS, so unbinding
the mux does not unbind the MAC first.  phy_mux_unregister() drops
phy_mux_mutex and relies on rtnl_lock() to serialize mux->pops->detach()
before the mux driver calls phy_mux_free(mux), whereas phy_mux_detach()
acquires neither phy_mux_mutex nor rtnl_lock().

When phylink_destroy() calls phy_mux_detach() without RTNL (for instance
in mvpp2_port_remove() or ucc_geth_remove() after an open-time failure
leaves the mux attached), it can run concurrently with mux removal:

CPU0                              CPU1
phylink_destroy()  /* no RTNL */  phy_mux_unregister()
  phy_mux_detach(mux)               mutex_lock(&phy_mux_mutex)
    if (!mux->netdev) /* false */   list_del(&mux->node)
                                    mutex_unlock(&phy_mux_mutex)
                                    rtnl_lock()
                                    if (mux->netdev && mux->pops->detach)
                                      mux->pops->detach(mux, priv)
                                        phy_mux_detach(mux)
                                    rtnl_unlock()
                                  phy_mux_free(mux)
                                    kfree(mux)
    phy_mux_detach_phys(mux)
      <-- use-after-free / double detach

If CPU0 finishes phy_mux_detach() and frees phylink while CPU1 is past the
mux->netdev check, CPU1 can likewise dereference NULL mux->pops or call
pops->detach() on freed parent state.

Similarly, fwnode_phy_mux_attach() holds only phy_mux_mutex and is called
without RTNL by drivers such as dsa_user_create() and ocelot_probe_port(),
sharing no common lock with phy_mux_detach().  Calling phy_mux_detach()
without RTNL while dev is still registered (as in ucc_geth_remove()) also
violates the rcu_dereference_protected() check on dev->hwprov in
phy_detach_internal().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204110622.1106492-1-maxime.chevallier@bootlin.com?part=4

  reply	other threads:[~2026-12-05 11:08 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:06 [PATCH net-next 00/12] net: phy: Introduce MII multiplexers Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 01/12] dt-bindings: net: Add Ethernet " Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 03/12] net: phy: Don't create the sysfs links for PHYs behind a mux Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 04/12] net: phy: phy_mux: Allow attaching a mux to a net_device Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot [this message]
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 05/12] net: phy: phy_mux: Add port selection logic Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 06/12] net: phy: phy_mux: Support SFP cage ports Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 07/12] net: phylink: Factor out phylink_clear_phydev() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 08/12] net: phy: phy_mux: Add phy_mux_set_mac_managed_pm() Maxime Chevallier
2026-12-04 11:06 ` [PATCH net-next 09/12] net: phylink: Support PHYs reached through a phy_mux Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 10/12] net: phylink: Support an SFP cage behind " Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko
2026-12-04 11:06 ` [PATCH net-next 12/12] ARM: dts: turris-omnia: Describe the WAN port multiplexer Maxime Chevallier
2026-12-05 11:08   ` sashiko-bot
2026-12-05 12:18   ` netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251445@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.