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 09/12] net: phylink: Support PHYs reached through a phy_mux
Date: Sat, 05 Dec 2026 11:08:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-251450@kernel.org> (raw)
In-Reply-To: <20261204110622.1106492-10-maxime.chevallier@bootlin.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] net: phylink: phylink_mux_select() configures suspended PHY
- [High] net: phylink: phylink_resolve() left enabled across phy_mux
  port transitions
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
- [Medium] net: phylink: phy_mux_stop() in phylink_stop() breaks PM WoL
  and RXC resume
- [Medium] net: phylink: unlocked pl->mux read in
  phylink_fwnode_phy_connect()
- [Medium] net: phylink: armed PHY IRQ in PHY_HALTED races with mux
  select/deselect
--

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

net: phylink: Support PHYs reached through a phy_mux

This commit adds phy_mux parent support to phylink so that MACs referencing a
mii-mux-handle attach to the mux in phylink_fwnode_phy_connect() and manage
PHY port selection through phy_mux_parent_ops.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 03e255f5ceb17..702614adcb945 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -2141,6 +2143,15 @@ struct phylink *phylink_create(struct phylink_config *config,
>  }
>  EXPORT_SYMBOL_GPL(phylink_create);
>  
> +static void phylink_detach_mux(struct phylink *pl)
> +{
> +	if (!pl->mux)
> +		return;
> +
> +	phy_mux_detach(pl->mux);
> +	pl->mux = NULL;
> +}
> +
>  /**
>   * phylink_destroy() - cleanup and destroy the phylink instance
>   * @pl: a pointer to a &struct phylink returned from phylink_create()
> @@ -2153,6 +2164,7 @@ EXPORT_SYMBOL_GPL(phylink_create);
>  void phylink_destroy(struct phylink *pl)
>  {
>  	sfp_bus_del_upstream(pl->sfp_bus);
> +	phylink_detach_mux(pl);
>  	phylink_destroy_sfp_cage_port(pl);
>  	if (pl->link_gpio)
>  		gpiod_put(pl->link_gpio);

[Severity: Medium]
Can calling phylink_detach_mux() here without rtnl_lock() race with
phy_mux_unregister() or phy_mux_state_machine()?

phylink_destroy() is called without rtnl_lock() held, unlike both neighboring
calls (sfp_bus_del_upstream() and phylink_destroy_sfp_cage_port()), which
acquire it internally.  In drivers/net/phy/phy_mux.c, phy_mux_unregister() and
phy_mux_state_machine() rely on rtnl_lock() to serialize mux teardown (and
phy_mux_stop() only calls non-synchronous cancel_work(&mux->state_work)).

CPU0                              CPU1
phylink_destroy()                 phy_mux_unregister()
  phylink_detach_mux()              rtnl_lock()
    if (!pl->mux)                     mux->pops->detach()
                                        phylink_detach_mux()
                                          phy_mux_detach(mux)
                                          pl->mux = NULL
    phy_mux_detach(pl->mux)         rtnl_unlock()
      <-- double detach / UAF       phy_mux_free(mux)

If phylink_destroy() runs while pl->mux is still attached, this can double
detach or use-after-free pl or mux, and if the mux is still started,
phy_mux_detach() -> phy_mux_stop() -> phylink_mux_deselect() ->
phylink_clear_phydev() also hits ASSERT_RTNL().  Acquiring rtnl_lock() around
phylink_detach_mux(pl) in phylink_destroy() (and asserting ASSERT_RTNL() in
phylink_detach_mux()) would close this.

[ ... ]

> @@ -2516,6 +2560,13 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
>  	if (!phylink_expects_phy(pl))
>  		return 0;
>  
> +	ret = phylink_attach_mux(pl, fwnode, flags);
> +	if (ret)
> +		return ret;
> +
> +	if (pl->mux)
> +		return 0;
> +
>  	phy_fwnode = fwnode_get_phy_node(fwnode);
>  	if (IS_ERR(phy_fwnode)) {
>  		/* PHY mode requires a PHY to be specified. */

[Severity: Medium]
Can this unlocked read of pl->mux race with a concurrent phy_mux_unregister()?

Callers such as dsa_user_create() invoke phylink_fwnode_phy_connect() without
rtnl_lock().  fwnode_phy_mux_attach() releases phy_mux_mutex before returning:

CPU0                              CPU1
phylink_fwnode_phy_connect()
  phylink_attach_mux()
    fwnode_phy_mux_attach()
      mutex_lock(&phy_mux_mutex)
      pl->mux = mux
      mutex_unlock(&phy_mux_mutex)
                                  phy_mux_unregister()
                                    rtnl_lock()
                                    phylink_mux_detach()
                                      pl->mux = NULL
                                    rtnl_unlock()
  if (pl->mux)  <-- sees NULL
    return 0;
  fwnode_get_phy_node()

If phy_mux_unregister() clears pl->mux in that window, this falls through to
fwnode_get_phy_node() as if no mux existed, returning -ENODEV in MLO_AN_PHY
mode or returning 0 with neither mux nor PHY in MLO_AN_INBAND mode.

[ ... ]

> @@ -2708,6 +2766,8 @@ void phylink_start(struct phylink *pl)
>  		phy_start(pl->phydev);
>  	if (pl->sfp_bus)
>  		sfp_upstream_start(pl->sfp_bus);
> +	if (pl->mux)
> +		phy_mux_start(pl->mux);
>  }
>  EXPORT_SYMBOL_GPL(phylink_start);

[Severity: High]
Does leaving phylink_resolve() enabled while no mux port is selected allow
spurious link-up and unlocked reconfiguration in MLO_AN_INBAND mode?

phylink_start() clears PHYLINK_DISABLE_STOPPED and queues pl->resolve before
phy_mux_start() schedules mux->state_work (which blocks on rtnl_lock()).
phylink_mux_deselect() also clears pl->phydev without setting a
PHYLINK_DISABLE_* bit, so the retriggered resolve work runs with pl->phydev
NULL.

CPU0                              kworker
phylink_start()
  phylink_enable_and_run_resolve()
    queue_work(pl->resolve)
  phy_mux_start(pl->mux)
    schedule_work(mux->state_work)
                                  phylink_resolve()
                                    /* pl->phydev == NULL */
                                    phylink_mac_pcs_get_state()
                                    phylink_link_up()  <-- no port selected

In MLO_AN_INBAND mode, phylink_resolve() skips gating on pl->phy_state.link
when pl->phydev is NULL, so a live PCS link triggers phylink_link_up() with
no mux port selected.

When phylink_mux_select() then runs, calling phylink_mac_initial_config() ->
phylink_major_config() while pl->old_link_state is true reconfigures the MAC
and PCS without a preceding phylink_link_down().

Because phylink_resolve() is still enabled, phylink_mux_select() also writes
pl->link_config.interface without pl->state_mutex or phy->lock, and
phylink_mac_initial_config() reads pl->req_link_an_mode, pl->phy_state, and
pl->link_config before taking phy->lock, racing with a concurrent resolve.

> @@ -2727,6 +2787,8 @@ void phylink_stop(struct phylink *pl)
>  {
>  	ASSERT_RTNL();
>  
> +	if (pl->mux)
> +		phy_mux_stop(pl->mux);
>  	if (pl->sfp_bus)
>  		sfp_upstream_stop(pl->sfp_bus);
>  	if (pl->phydev)

[Severity: Medium]
Does calling phy_mux_stop() in phylink_stop() break PHY Wake-on-LAN and
mac_requires_rxc resume?

phy_mux_stop() deselects the active port via phylink_mux_deselect(), which
clears pl->phydev to NULL and frees the PHY interrupt.  In
phylink_suspend(pl, false), phylink_stop() runs before
phylink_phy_pm_speed_ctrl() checks pl->phydev, so phylink_speed_down() (and
phylink_speed_up() on resume) is always skipped, and freeing the PHY IRQ
breaks IRQ-based PHY Wake-on-LAN wakeups.

Clearing pl->phydev also turns phylink_prepare_resume() into a no-op, so MACs
with mac_requires_rxc (such as stmmac in __stmmac_open()) cannot wake the
suspended PHY to restore RXC before DMA reset.

[ ... ]

> +static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
> +			      void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +	int ret;
> +
> +	if (port->type != PHY_MUX_PORT_PHY)
> +		return -EOPNOTSUPP;
> +
> +	if (pl->phydev)
> +		return -EBUSY;
> +
> +	phy = port->phydev;
> +
> +	ret = phylink_bringup_phy(pl, phy, port->interface);
> +	if (ret) {
> +		phy->phylink = NULL;
> +		return ret;
> +	}
> +
> +	pl->link_config.interface = port->interface;
> +	phylink_mac_initial_config(pl, false);
> +	phy_start(phy);
> +
> +	return 0;
> +}

[Severity: High]
Isn't port->phydev still suspended when phylink_bringup_phy() and
phylink_mac_initial_config() configure it here?

phy_mux_attach() and port deselection both suspend PHY ports via
phy_mux_phy_power_down() -> phy_suspend(), and phy_mux does not call
phy_resume() on selection.  phylink_bringup_phy() performs MDIO accesses for
EEE and enables interrupts via phy_request_interrupt(), and
phylink_major_config() calls phy_config_inband(), all before phy_start(phy)
resumes the PHY.

On PHY drivers that ignore non-BMCR writes in power-down or reset registers on
resume (such as bcm54xx_resume() in drivers/net/phy/broadcom.c or
kszphy_resume() in drivers/net/phy/micrel.c), interrupt and EEE configuration
can be silently discarded or fail, leaving hardware IRQs masked while polling
is disabled so the link never comes up.  Resuming the PHY before
phylink_bringup_phy() (and re-suspending on error), or resuming the port in
phy_mux before pops->select(), would avoid this.

> +static void phylink_mux_deselect(struct phy_mux *mux,
> +				 struct phy_mux_port *port, void *priv)
> +{
> +	struct phylink *pl = priv;
> +	struct phy_device *phy;
> +
> +	if (pl->phydev != port->phydev)
> +		return;
> +
> +	phy = port->phydev;
> +
> +	if (phy_is_started(phy) || phy->state == PHY_ERROR)
> +		phy_stop(phy);
> +
> +	phylink_clear_phydev(pl);
> +
> +	if (phy_interrupt_is_valid(phy))
> +		phy_free_interrupt(phy);
> +
> +	phy->phylink = NULL;
> +}

[Severity: Medium]
Can a PHY or shared interrupt firing while phy->state is PHY_HALTED re-suspend
the PHY after phy_start() or race with teardown?

Because a muxed PHY stays attached across port switches, phy_stop() leaves it
in PHY_HALTED rather than PHY_READY.  In phylink_mux_deselect(), phy_stop()
cancels phy->state_queue before phylink_clear_phydev() flushes pl->resolve and
before phy_free_interrupt() frees the IRQ, without canceling phy->state_queue
afterward.  On re-selection in phylink_mux_select(), phylink_bringup_phy()
also calls phy_request_interrupt() while phy->state is still PHY_HALTED.

CPU0                              kworker
phylink_mux_select()
  phylink_bringup_phy()
    phy_request_interrupt(phy)
    /* IRQ fires in PHY_HALTED */
                                  phy_state_machine()
                                    mutex_lock(&phy->lock)
                                    _phy_state_machine() -> WORK_SUSPEND
                                    mutex_unlock(&phy->lock)
  phy_start(phy)
    mutex_lock(&phy->lock)
    __phy_resume(phy)
    phy->state = PHY_UP
    mutex_unlock(&phy->lock)
                                    phy_suspend(phy)  <-- re-suspends PHY

Because phy_state_machine() drops phy->lock before
_phy_state_machine_post_work() calls phy_suspend(), a stale PHY_HALTED work
item can re-set BMCR_PDOWN and phy->suspended after phy_start() resumes the
PHY, leaving the link stuck down until another stop/start cycle.  During
deselection, the queued work can also race with phy_mux_phy_power_down() or
phy_detach().

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

  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
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 [this message]
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-251450@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.