From: netdev-bot+sashiko@kernel.org
To: maxime.chevallier@bootlin.com
Cc: davem@davemloft.net, andrew+netdev@lunn.ch, kuba@kernel.org,
edumazet@kernel.org, willemb@google.com, pabeni@redhat.com,
linux@armlinux.org.uk, kabel@kernel.org,
sebastian.hesselbarth@gmail.com, gregory.clement@bootlin.com,
hkallweit1@gmail.com, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
thomas.petazzoni@bootlin.com, devicetree@vger.kernel.org,
conor+dt@kernel.org, krzk+dt@kernel.org, robh@kernel.org,
christophe.leroy@csgroup.eu, herve.codina@bootlin.com,
f.fainelli@gmail.com, vladimir.oltean@nxp.com,
kory.maincent@bootlin.com, o.rempel@pengutronix.de,
nicveronese@gmail.com, horms@kernel.org,
marcin.s.wojtas@gmail.com, daniel@makrotopia.org,
dimitri.fedrau@liebherr.com, frank.wunderlich@linux.dev,
p.ameruoso@live.it, f@lex.la
Subject: Re: [PATCH net-next 10/12] net: phylink: Support an SFP cage behind a phy_mux
Date: Sat, 05 Dec 2026 12:18:37 +0000 [thread overview]
Message-ID: <179647311708.3146.9182940426072971501@kernel.org> (raw)
In-Reply-To: <20261204110622.1106492-11-maxime.chevallier@bootlin.com>
Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 5 · Low: 2
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] phylink_sfp_module_insert() (drivers/net/phy/phylink.c) calls
phylink_sfp_set_present(pl, true) before it has accepted the module.
- [Medium] phylink_register_mux_sfp() sets pl->sfp_behind_mux = true and
pl->sfp_link_down = true inside phylink_create().
- [Medium] net: phylink: phylink_start() runs resolve before mux selects
SFP port
- [Medium] net: phylink: phylink_mux_deselect() does not bring down
active SFP link
- [Medium] net: phylink: missing rtnl_lock() in phylink_destroy() mux
detach
- [Low] phylink_speed_down() and phylink_speed_up() change from
`!pl->sfp_bus && pl->phydev` to `pl->phydev && (!pl->sfp_bus ||…
- [Low] On the PHY-port path, phylink_mux_select() calls
clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state) first,…
--
net: phylink: Support an SFP cage behind a phy_mux
This patch lets phylink find an SFP cage that is described in one of the
ports of a phy_mux referenced by mii-mux-handle, and register as its
upstream. Module presence is reported to the mux with
phy_mux_port_set_available() so the mux can switch between the SFP and PHY
ports. SFP link events are gated on whether the SFP port is selected.
> 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
[ ... ]
> @@ -1876,6 +1881,30 @@ static int phylink_register_sfp(struct phylink *pl,
> return ret;
> }
>
> +static int phylink_register_mux_sfp(struct phylink *pl,
> + const struct fwnode_handle *fwnode)
> +{
> + struct fwnode_handle *mux_fwnode, *sfp_fwnode;
> + int ret;
> +
> + mux_fwnode = fwnode_find_reference(fwnode, "mii-mux-handle", 0);
> + if (IS_ERR(mux_fwnode))
> + return phylink_register_sfp(pl, fwnode);
> +
> + sfp_fwnode = fwnode_phy_mux_find_sfp_port(mux_fwnode);
> + fwnode_handle_put(mux_fwnode);
> + if (!sfp_fwnode)
> + return phylink_register_sfp(pl, fwnode);
> +
> + pl->sfp_behind_mux = true;
> + pl->sfp_link_down = true;
[Severity: Medium]
Should deselecting the SFP port also take the link down?
For an SFP port, phylink_mux_deselect() only clears sfp_selected. It
does not set PHYLINK_DISABLE_LINK. It also leaves an SFP module PHY
in pl->phydev if one is attached.
Take a mux driver unbind while the SFP link is up. The device link
created by phy_mux_attach() is DL_FLAG_STATELESS, so the netdev stays
open:
phy_mux_unregister()
phylink_mux_detach()
phylink_detach_mux()
phy_mux_detach()
phy_mux_stop()
phy_mux_select(mux, NULL)
phylink_mux_deselect()
The link is left up. sfp_behind_mux stays true while sfp_selected is
now false, so every later phylink_sfp_link_down() takes the early
return. Wouldn't the carrier then stay up even after the fibre or
the module is pulled?
Removing the SFP socket has a similar problem. sfp_unregister_bus()
calls phylink_sfp_detach() first, which marks the mux port
unavailable and queues mux->state_work.
It then calls socket_ops->stop(), which moves sm_dev_state to
SFP_DEV_DOWN. As a result sfp_sm_main() skips sfp_sm_link_down():
if (sfp->sm_state == SFP_S_LINK_UP &&
sfp->sm_dev_state == SFP_DEV_UP)
sfp_sm_link_down(sfp);
When the mux work runs, phylink_mux_deselect() leaves the link up.
phylink_mux_select() for the PHY port then calls
phylink_mac_initial_config() -> phylink_major_config(), and
mac_link_down() has never been called. Is the MAC/PCS expected to
cope with a major reconfiguration while the link is up?
Also, a copper module's PHY may still be in pl->phydev when the SFP
port is deselected. In that case the following phylink_mux_select()
for the PHY port would fail with -EBUSY.
Would calling phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK)
from the SFP branch of phylink_mux_deselect() be enough here? And
what should happen to an attached module PHY?
[Severity: Medium]
Can phylink_resolve() bring the link up here before the mux has
selected the SFP port?
While sfp_selected is false, phylink_sfp_link_down() only records
sfp_link_down. It returns without setting PHYLINK_DISABLE_LINK.
Suppose a module is already in the cage when the interface is opened.
By the time phylink_start() runs, the only bit left in
phylink_disable_state is PHYLINK_DISABLE_STOPPED.
phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED) clears
that bit. The state drops to zero and pl->resolve is queued.
Only after that does phylink_start() call
phy_mux_port_set_available() and phy_mux_start(). At that point
mux->state is still PHY_MUX_STOPPED, so the port is not selected
synchronously. The selection is deferred to mux->state_work, and
phy_mux_state_machine() has to take rtnl_lock(). The caller of
phylink_start() still holds it:
rtnl_lock();
switch (mux->state) {
...
case PHY_MUX_STARTED:
phy_mux_select(mux, phy_mux_choose_port(mux));
break;
}
phylink_resolve() does not need RTNL. It can run first, with
phylink_disable_state == 0 even though sfp_link_down is still true.
That is before the SFP state machine has finished its LOS checks.
When phylink_mux_select() finally runs for the SFP port, it calls
phylink_run_resolve_and_disable(). Could the carrier come up and
then drop straight away?
Would it make sense to set PHYLINK_DISABLE_LINK up front when
sfp_behind_mux is set? phylink_mux_select() could then clear it for
whichever port ends up selected. Or should the mux be started before
the resolve is enabled?
[Severity: Medium]
sfp_behind_mux is set here in phylink_create() based only on the firmware
description. The mux itself is attached later, through
phylink_fwnode_phy_connect() -> phylink_attach_mux(). What happens if that
attach never takes place?
phylink_attach_mux() returns 0 without attaching anything for non-netdev
users:
if (pl->config->type != PHYLINK_NETDEV)
return 0;
There are other ways to end up without a mux:
- phylink_fwnode_phy_connect() returns early for fixed-link.
- A MAC driver may call phylink_connect_phy() directly when there is no
phy-handle.
- If the mux driver is unbound, phylink_detach_mux() clears pl->mux and
pl->mux_sfp_port but leaves sfp_behind_mux set.
In all of these cases sfp_selected never becomes true, so
phylink_sfp_link_up() always takes the new early return:
pl->sfp_link_down = false;
if (pl->sfp_behind_mux && !pl->sfp_selected)
return;
Would the module then be detected and configured while
PHYLINK_DISABLE_LINK is never cleared? That would leave the carrier down
with no error reported.
> +
> + ret = phylink_register_sfp(pl, sfp_fwnode);
> + fwnode_handle_put(sfp_fwnode);
> +
> + return ret;
> +}
[ ... ]
> @@ -3842,7 +3875,8 @@ int phylink_speed_down(struct phylink *pl, bool sync)
>
> ASSERT_RTNL();
>
> - if (!pl->sfp_bus && pl->phydev)
> + if (pl->phydev &&
> + (!pl->sfp_bus || (pl->sfp_behind_mux && !pl->sfp_selected)))
> ret = phy_speed_down(pl->phydev, sync);
[Severity: Low]
Does !sfp_selected reliably mean that pl->phydev is the mux's copper PHY?
phylink_mux_deselect() clears sfp_selected for an SFP port, but it only
clears pl->phydev for PHY_MUX_PORT_PHY ports:
if (port->type == PHY_MUX_PORT_SFP)
pl->sfp_selected = false;
if (port->type != PHY_MUX_PORT_PHY || pl->phydev != port->phydev)
return;
Suppose the mux is detached while a copper SFP module's PHY is attached,
for example on mux driver unbind:
phy_mux_unregister()
phylink_mux_detach()
phy_mux_detach()
phy_mux_stop()
pl->phydev is then still the SFP module PHY, and sfp_selected is false.
A later phylink_speed_down() would call phy_speed_down() on that SFP
module PHY. Both the kernel-doc ("set the non-SFP PHY to lowest speed")
and the old check rule that out. The same applies to phylink_speed_up().
The commit message also doesn't mention this change to the WoL/suspend
behaviour.
>
> return ret;
[ ... ]
> @@ -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;
[Severity: Medium]
The port is reported as available here before phylink has accepted the
module. The commit message says:
"we only report the modules that phylink accepted"
and:
"with an unsupported module we're left with the PHY port selected"
Does the code match that?
When the mux is started, phy_mux_port_set_available(port, true) selects
the port synchronously:
if (available && mux->state == PHY_MUX_STARTED) {
phy_mux_select(mux, phy_mux_choose_port(mux));
return;
}
phy_mux_trigger(mux);
That deselects the copper port before phylink_sfp_config_optical() has
run. phylink_mux_deselect() stops the PHY, clears pl->phydev and frees
the interrupt, and then the mux powers the PHY down.
For an unsupported optical module, the rollback to false only queues
phy_mux_trigger(). Would every such insertion drop and renegotiate the
copper link?
For modules with sfp_may_have_phy set, this returns 0 with the port still
available, and nothing later seems to undo it.
phylink_sfp_connect_phy() can fail:
if (!phy->drv) {
...
return -EINVAL;
}
...
return phylink_sfp_config_phy(pl, phy);
phylink_sfp_module_start() can also fail in phylink_sfp_config_optical(),
and that path only removes the mod port:
ret = phylink_sfp_config_optical(pl);
if (ret)
goto del_mod_port;
In sfp_sm_main(), both failures go to SFP_S_FAIL without calling
module_remove. That would leave the SFP mux port selected, the copper PHY
suspended with pl->phydev NULL, and PHYLINK_DISABLE_LINK set.
Is there then no link on either medium until the module is pulled out?
> - return phylink_sfp_config_optical(pl);
> + ret = phylink_sfp_config_optical(pl);
> + if (ret)
> + phylink_sfp_set_present(pl, false);
> +
> + return ret;
> }
[Severity: Medium]
This gives the mux a new way to be touched while phylink_destroy()
is tearing things down without RTNL.
phylink_destroy() calls sfp_bus_del_upstream(), which takes RTNL and
ends up in phylink_sfp_detach(). With this patch, that now calls
phy_mux_port_set_available(pl->mux_sfp_port, false). If a module was
present, this goes through phy_mux_trigger() and queues
mux->state_work.
As soon as sfp_bus_del_upstream() drops RTNL,
phy_mux_state_machine() can take it. It then calls
phylink_mux_select() or phylink_mux_deselect() with pl as the
private pointer.
Meanwhile phylink_destroy() runs phylink_detach_mux() ->
phy_mux_detach() -> phy_mux_stop() without RTNL, although both of
those expect it to be held.
phy_mux_stop() only does cancel_work() on mux->state_work. Nothing
waits for a running phy_mux_state_machine() before kfree(pl). Can
that lead to a use-after-free of pl?
The same unlocked phylink_detach_mux() can also race with
phy_mux_unregister(). On mux driver unbind, that calls
phylink_mux_detach() under RTNL. Both sides could then run
phy_mux_detach() at the same time, calling phy_detach() and
device_link_del() twice.
This needs phylink_destroy() to be entered with pl->mux still set.
That happens when the MAC driver doesn't call
phylink_disconnect_phy() first:
- ucc_geth_remove() calls phylink_destroy() before devres
unregisters the netdev.
- The error paths in lan743x_netdev_open() and mvpp2_open() skip the
disconnect after the PHY was connected.
The unlocked call was added earlier in the series. Should
phylink_destroy() take RTNL around phylink_detach_mux(), or detach
the mux before sfp_bus_del_upstream()?
Note that phy_mux_stop() can't simply switch to cancel_work_sync()
while it is called under RTNL, since the work itself takes
rtnl_lock().
[ ... ]
> @@ -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);
[Severity: Low]
Can clearing PHYLINK_DISABLE_LINK before these stores race with
phylink_resolve()?
When switching back from the SFP port after the module is removed,
phylink_sfp_link_down() has set DISABLE_LINK. This clear_bit() can bring
phylink_disable_state back to 0. A resolve queued by phylink_pcs_change()
or phylink_mac_change() can then run from the workqueue.
phylink_resolve() reads, and may write, pl->link_config while holding
phydev_mutex, phy->lock and state_mutex. Here pl->link_config.interface
and pl->req_link_an_mode are written under RTNL only.
The link should be down during that window, so the practical impact may
be small. Would moving the clear_bit() after phylink_mac_initial_config()
avoid the race?
> phy_start(phy);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204110622.1106492-1-maxime.chevallier%40bootlin.com
next prev parent reply other threads:[~2026-12-05 12:19 UTC|newest]
Thread overview: 22+ 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 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 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 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 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 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 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 12:18 ` netdev-bot+sashiko [this message]
2026-12-04 11:06 ` [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver Maxime Chevallier
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 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=179647311708.3146.9182940426072971501@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=christophe.leroy@csgroup.eu \
--cc=conor+dt@kernel.org \
--cc=daniel@makrotopia.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=dimitri.fedrau@liebherr.com \
--cc=edumazet@kernel.org \
--cc=f.fainelli@gmail.com \
--cc=f@lex.la \
--cc=frank.wunderlich@linux.dev \
--cc=gregory.clement@bootlin.com \
--cc=herve.codina@bootlin.com \
--cc=hkallweit1@gmail.com \
--cc=horms@kernel.org \
--cc=kabel@kernel.org \
--cc=kory.maincent@bootlin.com \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=marcin.s.wojtas@gmail.com \
--cc=maxime.chevallier@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=nicveronese@gmail.com \
--cc=o.rempel@pengutronix.de \
--cc=p.ameruoso@live.it \
--cc=pabeni@redhat.com \
--cc=robh@kernel.org \
--cc=sebastian.hesselbarth@gmail.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=vladimir.oltean@nxp.com \
--cc=willemb@google.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox