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

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] net: phylink: stale sfp_link_down after stop/remove/detach
  breaks LOS gating
- [Medium] net: phylink: phylink_start() runs resolve before mux selects
  SFP port
- [Medium] net: phylink: phylink_mux_select() clears DISABLE_LINK before
  reconfiguring
- [Medium] net: phylink: missing sfp_mod_present rollback on
  may_have_phy module failures
- [Medium] net: phylink: phylink_mux_deselect() does not bring down
  active SFP link
- [Medium] net: phylink: data race on mux_sfp_port in
  phylink_mux_attach()
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
  detach
--

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

net: phylink: Support an SFP cage behind a phy_mux

This commit teaches phylink to discover an SFP cage described on a
phy_mux port, report module availability to the mux, and handle
selecting and deselecting the SFP port.

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

[ ... ]

> @@ -2150,6 +2179,7 @@ static void phylink_detach_mux(struct phylink *pl)
>  
>  	phy_mux_detach(pl->mux);
>  	pl->mux = NULL;
> +	pl->mux_sfp_port = NULL;
>  }

[Severity: Medium]
Can phylink_destroy() race with phy_mux_state_machine() and cause a
use-after-free of pl when calling phylink_detach_mux(pl) without
rtnl_lock()?

phylink_destroy() runs without rtnl_lock() and calls
sfp_bus_del_upstream(pl->sfp_bus) followed by phylink_detach_mux(pl)
and kfree(pl).  With this patch, phylink_sfp_detach() calls
phy_mux_port_set_available(pl->mux_sfp_port, false), which queues
mux->state_work right before sfp_bus_del_upstream() drops rtnl_lock().
Because phy_mux_stop() inside phy_mux_detach() uses cancel_work()
rather than cancel_work_sync(), phylink_destroy() can free pl while
the worker is still running:

CPU0                                CPU1 (kworker)
phylink_destroy()
  sfp_bus_del_upstream()
    rtnl_lock()
    phylink_sfp_detach()
      phy_mux_port_set_available(..., false)
        phy_mux_trigger(mux)
    rtnl_unlock()
                                    phy_mux_state_machine()
                                      rtnl_lock()
  phylink_detach_mux(pl)              phy_mux_select(...)
    phy_mux_detach(pl->mux)
      phy_mux_stop(mux)
        cancel_work(&mux->state_work)
  kfree(pl)
                                        mux->pops->select(..., pl)
                                          <-- use-after-free of pl

Drivers such as ucc_geth (ucc_geth_remove()) or lan743x (error path in
lan743x_netdev_open()) can reach phylink_destroy() with pl->mux still
attached.  Independently, phy_mux_unregister() can also run
phylink_detach_mux(pl) concurrently under rtnl_lock(), risking double
phy_detach() / device_link_del() or use-after-free.  Wrapping
phylink_detach_mux(pl) in rtnl_lock() / rtnl_unlock() inside
phylink_destroy() would serialize both paths before kfree(pl).

[ ... ]

> @@ -2766,6 +2796,9 @@ void phylink_start(struct phylink *pl)
>  		phy_start(pl->phydev);
>  	if (pl->sfp_bus)
>  		sfp_upstream_start(pl->sfp_bus);
> +	if (pl->mux_sfp_port)
> +		phy_mux_port_set_available(pl->mux_sfp_port,
> +					   pl->sfp_mod_present);
>  	if (pl->mux)
>  		phy_mux_start(pl->mux);
>  }

[Severity: Medium]
Can phylink_resolve() bring the link up prematurely here before the mux
selects the SFP port?

Earlier in phylink_start(), phylink_enable_and_run_resolve(pl,
PHYLINK_DISABLE_STOPPED) clears PHYLINK_DISABLE_STOPPED and schedules
pl->resolve.  Because phylink_sfp_link_down() returns early without
setting PHYLINK_DISABLE_LINK while !pl->sfp_selected,
pl->phylink_disable_state becomes 0.

When an SFP module is already inserted with pl->sfp_link_down true,
phy_mux_port_set_available() runs while mux->state is still
PHY_MUX_STOPPED, so phy_mux_start() defers port selection to
phy_mux_state_machine().  That worker must wait for rtnl_lock(),
whereas phylink_resolve() does not:

CPU0 (under rtnl_lock)              CPU1 (kworker)
phylink_start()
  phylink_enable_and_run_resolve(
      pl, PHYLINK_DISABLE_STOPPED)
  phy_mux_port_set_available(...)
  phy_mux_start(pl->mux)
    phy_mux_trigger(mux)
                                    phylink_resolve()
                                      phylink_disable_state == 0
                                      <-- premature link-up
rtnl_unlock()
                                    phy_mux_state_machine()
                                      rtnl_lock()
                                      phylink_mux_select(SFP)
                                        set PHYLINK_DISABLE_LINK

This allows a spurious carrier-up and flap before SFP startup and LOS
checks complete.

[ ... ]

> @@ -4096,18 +4143,27 @@ static int phylink_sfp_module_insert(void *upstream,
>  	pl->sfp_may_have_phy = caps->may_have_phy;
>  	pl->sfp_port = caps->port;
>  
> +	phylink_sfp_set_present(pl, true);
> +
>  	/* If this module may have a PHY connecting later, defer until later */
>  	if (pl->sfp_may_have_phy)
>  		return 0;
>  
> -	return phylink_sfp_config_optical(pl);
> +	ret = phylink_sfp_config_optical(pl);
> +	if (ret)
> +		phylink_sfp_set_present(pl, false);
> +
> +	return ret;
>  }

[Severity: Medium]
Does this leave pl->sfp_mod_present and pl->mux_sfp_port->available
stuck true when a may_have_phy module fails deferred setup?

For copper or other may_have_phy modules, validation is deferred to
phylink_sfp_connect_phy() or phylink_sfp_module_start().  Neither
function calls phylink_sfp_set_present(pl, false) on failure (for
example when !phy->drv or phylink_sfp_config_optical() fails), and
sfp_sm_main() transitions straight to SFP_S_FAIL without calling
sfp_module_remove().  That leaves the mux stuck on the rejected SFP
port until the module is physically removed.

Also, calling phylink_sfp_set_present(pl, true) before
phylink_sfp_config_optical() synchronously switches the mux and stops
an active PHY port before discovering that an optical module is
unsupported, causing an unnecessary link bounce when it rolls back.

[ ... ]

> @@ -4268,6 +4332,7 @@ static void phylink_mux_attach(struct phy_mux *mux, void *priv)
>  		phy_mux_set_mac_managed_pm(mux);
>  
>  	pl->mux = mux;
> +	pl->mux_sfp_port = phy_mux_get_sfp_port(mux);
>  }

[Severity: Medium]
Is there a data race on pl->mux_sfp_port between phylink_mux_attach()
and the SFP callbacks?

phylink_create() registers the SFP upstream before
phylink_of_phy_connect() attaches the mux, and probe-time callers such
as dsa_user_create() and ocelot_port_phylink_create() call
phylink_of_phy_connect() without holding rtnl_lock().  In that window,
phylink_mux_attach() writes pl->mux_sfp_port under phy_mux_mutex while
phylink_sfp_set_present() and phylink_sfp_detach() read it under
rtnl_lock():

CPU0 (probe, phy_mux_mutex)         CPU1 (SFP work, rtnl_lock)
phylink_of_phy_connect()
  phylink_attach_mux()
    phylink_mux_attach()
      pl->mux_sfp_port = ...        phylink_sfp_module_insert()
                                      phylink_sfp_set_present()
                                        if (pl->mux_sfp_port)
                                          <-- data race

> @@ -4277,6 +4342,19 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
>  	struct phy_device *phy;
>  	int ret;
>  
> +	if (port->type == PHY_MUX_PORT_SFP) {
> +		if (!pl->sfp_bus)
> +			return -ENODEV;
> +
> +		pl->sfp_selected = true;
> +		if (pl->sfp_link_down)
> +			phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
> +		else
> +			phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
> +
> +		return 0;
> +	}
> +
>  	if (port->type != PHY_MUX_PORT_PHY)
>  		return -EOPNOTSUPP;

[Severity: Medium]
Can pl->sfp_link_down stay stale false after phylink_stop(),
phylink_sfp_module_remove(), or phylink_sfp_detach(), bypassing SFP
LOS link gating?

Once an SFP link comes up, phylink_sfp_link_up() clears
pl->sfp_link_down.  When phylink_stop() calls sfp_upstream_stop(),
sfp_sm_device() changes sm_dev_state to SFP_DEV_DOWN before
sfp_sm_main() runs, so sfp_sm_main() skips sfp_sm_link_down() (which
is gated on sm_dev_state == SFP_DEV_UP) while resetting sm_state to
SFP_S_DOWN.  Neither phylink_stop(), phylink_sfp_module_remove(), nor
phylink_sfp_detach() resets pl->sfp_link_down = true.

If the interface is later reopened with RX_LOS asserted or a new
module inserted, phylink_mux_select() sees stale pl->sfp_link_down ==
false and clears PHYLINK_DISABLE_LINK.  Because
sfp_sm_link_check_los() moves straight to SFP_S_WAIT_LOS without
calling sfp_sm_link_down(), PHYLINK_DISABLE_LINK remains cleared and
carrier can come up while LOS is asserted.

> @@ -4291,7 +4369,10 @@ static int phylink_mux_select(struct phy_mux *mux, struct phy_mux_port *port,
>  		return ret;
>  	}
>  
> +	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
> +
>  	pl->link_config.interface = port->interface;
> +	pl->req_link_an_mode = pl->cfg_link_an_mode;
>  	phylink_mac_initial_config(pl, false);
>  	phy_start(phy);

[Severity: Medium]
Should PHYLINK_DISABLE_LINK be cleared after
phylink_mac_initial_config() rather than before it?

Once PHYLINK_DISABLE_LINK is cleared here, any MAC, PCS, or PHY
interrupt can schedule phylink_resolve() on another CPU without
rtnl_lock().  Because pl->link_config.interface and
pl->req_link_an_mode are updated without pl->state_mutex,
phylink_resolve() can race with those writes and poll the PCS under
stale SFP act_link_an_mode and interface settings before
phylink_mac_initial_config() reconfigures the MAC/PCS.  Updating the
config under pl->state_mutex and clearing PHYLINK_DISABLE_LINK after
phylink_mac_initial_config(pl, false) would close this window.

> @@ -4304,7 +4385,10 @@ static void phylink_mux_deselect(struct phy_mux *mux,
>  	struct phy_device *phy;
>  
> -	if (pl->phydev != port->phydev)
> +	if (port->type == PHY_MUX_PORT_SFP)
> +		pl->sfp_selected = false;
> +
> +	if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
>  		return;

[Severity: Medium]
Should phylink_mux_deselect() bring the link down when deselecting
PHY_MUX_PORT_SFP?

Unbinding the phy_mux driver while the SFP link is up calls
phy_mux_unregister() -> phylink_mux_detach() -> phy_mux_stop() ->
phylink_mux_deselect() without stopping the netdev.  Because
phylink_mux_deselect() only clears pl->sfp_selected without calling
phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK), the link
stays up and later phylink_sfp_link_down() calls return early while
pl->sfp_behind_mux && !pl->sfp_selected, leaving carrier stuck up.

Unbinding the sfp driver while an optical link is up similarly leaves
the link up across the mux switch, so phylink_mux_select() calls
phylink_mac_initial_config() without mac_link_down() first.  Also, if
a copper SFP PHY is still attached when deselected, pl->phydev remains
populated and selecting a PHY port fails with -EBUSY.

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

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