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 11/12] net: phy: mux: Add the mii-mux-auto driver
Date: Sat, 05 Dec 2026 12:18:39 +0000 [thread overview]
Message-ID: <179647311958.3146.5048859446669166535@kernel.org> (raw)
In-Reply-To: <20261204110622.1106492-12-maxime.chevallier@bootlin.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 5 · Low: 0
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] The mii-mux-auto binding accepts DTs that the mii-mux-auto
driver refuses to probe, and the driver gives no reason.
- [Medium] On mii-mux-auto hardware (Turris Omnia), the mux select line
is the SFP cage's MOD_DEF0.
- [Medium] MII_MUX_AUTO is tristate, and nothing orders the MAC after the
mux.
- [Medium] net: phy: unsynchronized pl->mux access and detach vs
phy_mux_unregister()
- [Medium] net: phy: mux: unsynchronized mii_mux_auto_remove() vs
phy_mux_detach()
--
net: phy: mux: Add the mii-mux-auto driver
This adds a platform driver for MII multiplexers that the hardware switches
on its own, described by the mii-mux-auto binding. It registers the two mux
ports with the phy_mux core. It also reads takes-precedence so the core
knows which port the hardware prefers, as on the Turris Omnia WAN port.
> diff --git a/drivers/net/phy/mux/Kconfig b/drivers/net/phy/mux/Kconfig
> new file mode 100644
> index 0000000000000..cbfd49fab2ea2
> --- /dev/null
> +++ b/drivers/net/phy/mux/Kconfig
> @@ -0,0 +1,18 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +if PHY_MUX
> +
> +config MII_MUX_AUTO
> + tristate "MII multiplexer switched by the hardware"
[Severity: Medium]
With MII_MUX_AUTO built as a module, what happens if the MAC connects
before mii_mux_auto has probed and called phy_mux_register()?
Nothing seems to order the MAC after the mux. drivers/of/property.c has no
fw_devlink supplier binding for mii-mux-handle. The device link in
phy_mux_attach() is only created after a successful attach.
In that window fwnode_phy_mux_attach() returns -EPROBE_DEFER, and
phylink_attach_mux() turns it into -ENODEV:
drivers/net/phy/phylink.c:phylink_attach_mux() {
...
ret = fwnode_phy_mux_attach(mux_fwnode, pl->netdev, flags,
&phylink_mux_ops, pl);
fwnode_handle_put(mux_fwnode);
return ret == -EPROBE_DEFER ? -ENODEV : ret;
}
phylink_fwnode_phy_connect() then returns -ENODEV and does not fall back to
phy-handle.
On Turris Omnia, mvneta connects from ndo_open:
mvneta_open()
mvneta_mdio_probe()
phylink_of_phy_connect()
phylink_attach_mux()
fwnode_phy_mux_attach() <- -EPROBE_DEFER, returned as -ENODEV
So if eth2 is brought up before udev has loaded mii_mux_auto, wouldn't
ndo_open fail with -ENODEV, with no retry once the mux registers? A MAC
that connects at probe time would fail probe for good, since the error is
no longer -EPROBE_DEFER.
Converting to -ENODEV is reasonable in ndo_open context. The conversion
was added earlier in this series in "net: phylink: Support PHYs reached
through a phy_mux". This patch adds the first mux provider and makes it
modular, which makes the path reachable.
Should there be some ordering or deferral for this new dependency?
> + help
> + Support for the two-port MII multiplexers that the hardware
> + switches by itself, without any software control.
[ ... ]
> diff --git a/drivers/net/phy/mux/mii_mux_auto.c b/drivers/net/phy/mux/mii_mux_auto.c
> new file mode 100644
> index 0000000000000..980cc2c242ba7
> --- /dev/null
> +++ b/drivers/net/phy/mux/mii_mux_auto.c
> @@ -0,0 +1,99 @@
[ ... ]
> +static int mii_mux_auto_init_port(struct phy_mux_port *port,
> + struct fwnode_handle *child)
> +{
> + port->has_precedence = fwnode_property_read_bool(child,
> + "takes-precedence");
[Severity: Medium]
Is has_precedence enough to describe how this hardware switches?
On Turris Omnia the select line is MOD_DEF0, so the hardware routes the
MAC lane to the cage as soon as a module is seated. The core only treats
the SFP port as available once phylink accepts the module. Presence is set
after EEPROM parsing and cleared again if the optical config fails:
drivers/net/phy/phylink.c:phylink_sfp_module_insert() {
...
phylink_sfp_set_present(pl, true);
...
ret = phylink_sfp_config_optical(pl);
if (ret)
phylink_sfp_set_present(pl, false);
...
}
phy_mux_choose_port() then falls back to the PHY port, which is always
available:
xa_for_each(&mux->ports, id, port)
if (port->available)
return port;
That leaves hardware and software disagreeing in three cases:
- for a short time, between MOD_DEF0 asserting and sfp_module_insert()
running
- permanently, if the EEPROM can't be read and sfp.c ends in
SFP_MOD_ERROR without ever calling module_insert
- permanently, for a module that phylink rejects
In each case the PHY stays selected. phylink_mux_select() brings the MAC up
in the PHY's phy-mode and starts the PHY.
With a copper cable also plugged in, can the PHY report link up while the
analog switch has moved the lane to the cage, so no traffic flows?
"net: phylink: Support an SFP cage behind a phy_mux" mentions this ("with
an unsupported module we're left with the PHY port selected while the MAC
is wired to the cage ... left for later"). Neither this commit message nor
the Turris Omnia DTS commit ("we follow the module at runtime") mentions
it.
Should this limitation be documented here? Or should a raw-presence
callback exist before this configuration is enabled on real hardware?
> +
> + return 0;
> +}
> +
> +static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
> +{
> + struct fwnode_handle *child;
> + int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
> +
> + fwnode_for_each_available_child_node(fwnode, child) {
> + if (fwnode_property_read_bool(child, "takes-precedence"))
> + n_prec++;
> +
> + if (!fwnode_property_present(child, "sfp")) {
> + n_phy++;
> + continue;
> + }
> +
> + n_sfp++;
> + if (fwnode_property_read_bool(child, "takes-precedence"))
> + n_sfp_prec++;
> + }
> +
> + if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
> + return -EOPNOTSUPP;
[Severity: Medium]
This check only accepts one SFP port and one PHY port, with
takes-precedence on the SFP port. Does that match the mii-mux-auto
binding?
In Documentation/devicetree/bindings/net/mii-mux-auto.yaml, each port can
be a PHY port or an SFP port through mii-mux.yaml $defs/port. The oneOf
only requires takes-precedence on exactly one of port@0/port@1, whatever
the port type:
oneOf:
- properties:
port@0:
required:
- takes-precedence
- properties:
port@1:
required:
- takes-precedence
So these DTs pass dtbs_check but fail probe here with -EOPNOTSUPP:
- takes-precedence on the PHY port
- two PHY ports
- two SFP ports
There is no dev_err(), so the only output is the driver core's generic
"probe ... failed with error -95".
The restriction seems to come from the core. phy_mux_register_port() sets
port->available = true on PHY ports and nothing clears it. A PHY port with
precedence would always win in phy_mux_choose_port():
xa_for_each(&mux->ports, id, port)
if (port->has_precedence && port->available)
return port;
The commit message says the driver "looks for the takes-precedence property
to tell the core which port the hardware is going to switch to".
mii_mux_auto_init_port() also reads the property on any port type. Yet
only one layout can probe.
Could the binding be narrowed to match the driver? And could the driver
print why it rejects the ports?
> +
> + return 0;
> +}
[ ... ]
> @@ -0,0 +1,99 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +
> +#include <linux/module.h>
> +#include <linux/phy_mux.h>
> +#include <linux/platform_device.h>
> +#include <linux/property.h>
> +
> +static int mii_mux_auto_init_port(struct phy_mux_port *port,
> + struct fwnode_handle *child)
> +{
> + port->has_precedence = fwnode_property_read_bool(child,
> + "takes-precedence");
> +
> + return 0;
> +}
> +
> +static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
> +{
> + struct fwnode_handle *child;
> + int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
> +
> + fwnode_for_each_available_child_node(fwnode, child) {
> + if (fwnode_property_read_bool(child, "takes-precedence"))
> + n_prec++;
> +
> + if (!fwnode_property_present(child, "sfp")) {
> + n_phy++;
> + continue;
> + }
> +
> + n_sfp++;
> + if (fwnode_property_read_bool(child, "takes-precedence"))
> + n_sfp_prec++;
> + }
> +
> + if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
> + return -EOPNOTSUPP;
> +
> + return 0;
> +}
> +
> +static int mii_mux_auto_probe(struct platform_device *pdev)
> +{
> + struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
> + struct phy_mux *mux;
> + int ret;
> +
> + ret = mii_mux_auto_check_ports(fwnode);
> + if (ret)
> + return ret;
> +
> + mux = phy_mux_alloc(&pdev->dev, 2);
> + if (IS_ERR(mux))
> + return PTR_ERR(mux);
> +
> + ret = phy_mux_fwnode_add_ports(mux, fwnode, mii_mux_auto_init_port);
> + if (ret)
> + goto err_free;
> +
> + ret = phy_mux_register(mux);
> + if (ret)
> + goto err_free;
> +
> + platform_set_drvdata(pdev, mux);
> +
> + return 0;
> +
> +err_free:
> + phy_mux_free(mux);
> + return ret;
> +}
> +
> +static void mii_mux_auto_remove(struct platform_device *pdev)
> +{
> + struct phy_mux *mux = platform_get_drvdata(pdev);
> +
> + phy_mux_unregister(mux);
> + phy_mux_free(mux);
> +}
> +
> +static const struct of_device_id mii_mux_auto_of_match[] = {
> + { .compatible = "mii-mux-auto" },
> + { }
> +};
> +MODULE_DEVICE_TABLE(of, mii_mux_auto_of_match);
> +
> +static struct platform_driver mii_mux_auto_driver = {
> + .probe = mii_mux_auto_probe,
> + .remove = mii_mux_auto_remove,
> + .driver = {
> + .name = "mii-mux-auto",
> + .of_match_table = mii_mux_auto_of_match,
> + },
> +};
> +module_platform_driver(mii_mux_auto_driver);
> +
> +MODULE_DESCRIPTION("Ethernet MII multiplexer switched by the hardware");
> +MODULE_AUTHOR("Maxime Chevallier <maxime.chevallier@bootlin.com>");
> +MODULE_LICENSE("GPL");
[Severity: Medium]
What keeps the mux alive while a MAC is still attached to it?
phy_mux_attach() adds the device link with DL_FLAG_STATELESS. That
link is not managed, so device_links_unbind_consumers() skips it.
Unbinding mii-mux-auto therefore does not unbind the MAC first. struct
phy_mux and struct phylink are not refcounted either, and
phy_mux_attach() takes no module reference.
So a sysfs unbind, or rmmod mii_mux_auto, runs mii_mux_auto_remove()
while the MAC and its phylink are still bound. As soon as
phy_mux_unregister() returns, phy_mux_free() destroys the ports and
kfree()s the mux.
phy_mux_unregister() relies only on rtnl_lock() to serialize against
the parent. phy_mux_detach() takes neither rtnl_lock() nor
phy_mux_mutex. Some MAC teardown paths reach it without rtnl_lock():
- phylink_destroy() -> phylink_detach_mux(). pl->mux can still be
set here after a failed mvpp2_open() or ucc_geth_open(). The call
comes from mvpp2_port_remove() or ucc_geth_remove().
- mtk_remove(), which calls mtk_stop() and phylink_disconnect_phy()
from platform remove context without rtnl_lock(). If the mux was
started, phy_mux_stop() there uses cancel_work() and can race
with phy_mux_state_machine().
If one of these runs concurrently with mii_mux_auto_remove():
- phy_mux_detach() can touch a mux and ports that phy_mux_free() is
freeing.
- phy_mux_unregister() can pass its mux->netdev && mux->pops check
and then find mux->pops cleared. It can also call ->detach() on a
phylink that phylink_destroy() has already freed.
- both sides can run phy_mux_detach() at once. That calls
phy_detach() and put_device(&phydev->mdio.dev) twice, and drops
the device link twice through device_link_del().
This needs root to trigger, but it ends in a use-after-free.
Should the MAC be unbound before the mux goes away, for example through
a managed device link? Or should the mux be refcounted so that it is
not freed while a phylink still points at it? In either case, should
phy_mux_detach() take the same lock as phy_mux_unregister()?
[Severity: Medium]
mii_mux_auto_remove() is the first in-tree caller of
phy_mux_unregister() and phy_mux_free(). That makes two races in the
phy_mux core reachable. In both, pl->mux is read or torn down without
the lock that phy_mux_unregister() relies on.
phy_mux_unregister() drops phy_mux_mutex right after list_del(). It
then holds only rtnl_lock() around mux->pops->detach(). That call ends
in phylink_detach_mux(), which clears pl->mux.
First, some MACs call phylink_fwnode_phy_connect() without
rtnl_lock(). Two examples:
- ocelot_port_phylink_create(), at probe time
- dsa_user_phy_setup(), which runs before dsa_user_create() takes
rtnl_lock()
fwnode_phy_mux_attach() sets pl->mux under phy_mux_mutex and then
drops the mutex. phylink_fwnode_phy_connect() later tests pl->mux with
no lock held:
drivers/net/phy/phylink.c:phylink_fwnode_phy_connect() {
...
ret = phylink_attach_mux(pl, fwnode, flags);
if (ret)
return ret;
if (pl->mux)
return 0;
...
}
If mii_mux_auto_remove() runs in between, the test can see NULL and
fall through to fwnode_get_phy_node(). The connect then does one of
three things:
- returns -ENODEV
- returns 0 with nothing attached
- attaches a fallback phy-handle while pl->sfp_behind_mux is still
set
Second, phylink_destroy() must be called without rtnl_lock(). It calls
phylink_detach_mux() -> phy_mux_detach(pl->mux) and then kfree(pl).
It holds neither rtnl_lock() nor phy_mux_mutex while doing so.
pl->mux can still be set at that point if ndo_open failed after the
connect and never called phylink_disconnect_phy(). ucc_geth_open()
does this when ucc_geth_init_mac() or request_irq() fails. mvpp2_open()
does the same when request_irq() fails.
If that races with mii_mux_auto_remove():
- both sides can run phy_mux_detach() on the same mux. That calls
phy_detach(), put_device() and device_link_del() twice.
- phy_mux_free() can kfree() the mux while phylink_destroy() is
still inside phy_mux_detach().
- phylink_destroy() can kfree(pl) just before phy_mux_unregister()
calls mux->pops->detach(mux, mux->parent_priv) with that pointer.
Unbinding the driver or unloading the module needs root. Still, the
result is an oops or memory corruption, not a clean failure.
The locking was added earlier in this series, in "net: phy: phy_mux:
Allow attaching a mux to a net_device" and "net: phylink: Support PHYs
reached through a phy_mux". This patch is what makes it reachable.
Should the pl->mux test in phylink_fwnode_phy_connect() and the detach
in phylink_destroy() be serialized against phy_mux_unregister()? Or
should the mux be pinned while a phylink is attached to it?
--
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:18 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
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 [this message]
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=179647311958.3146.5048859446669166535@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