From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796473122; b=yDwnCSwMAeNrPFJglXwgtD/Uew+aBHlOkv8gGdpYggR7AU2Q8jkDl1Awc9n1dUoxUcUw um8g4HB24rgEaqpOD9Izm67EE0ENsbfAOVdaqCb2ZuHPD7IOR+z7LXqP9rxZewFyxrwSb gg3om+6NRz2TwMZKzUxXdZcu/b86OMMQAvPBA0jbZSIZ1qW5cVlqUxmk/gcq3MuAoqWEV SSxnJLka7MRda29+kldDOXCe05dvbTMv+HSC82uW55NrhsaYdXCdodlfSB/ZBq8gOXx4O 62zzp5+7cHNISu1oY14FnImsbmrV0SER0RpV0H6NbPzKO6Q6r9dGPCajDpLqT4BTR0Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796473122; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=FtI/MmbM803qOn+Ff/mwmvYecfrfySBRqepAvgjkch0=; b=ibiEk1nylLCYMti1k8/zLy+RzFuMeO/W6Au+zKm7B8wTJSJ4PA0UfsEj620jPmXDf758 GrI1AdeztVDvK0ctMaPDk2TG8nil3F46stbvjMDPLSUSrcn2XoaXXH3FL/APEzYTYbbs7 neux6WYNz2sEVyiCc0WrcZt4MYgpBW/uvX55vzHgIFUONBVowluZiLb2eqzBkh+FAUtk7 jpRx1/P3sDrB1HBtl7yZGNqRWUQkORIDBFxHJtJHRAzJyWcrRNCwhqqBjqIphcS5bactj U0btp+GXia2rd7RpQQVtfLz7NubL4zlQog38FPp7neVXYfTBCmRNELeELD1IG8jaZsA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TCUwlEHQ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10830606A691; Sat, 5 Dec 2026 12:18:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TCUwlEHQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0082F1F00A10; Sat, 5 Dec 2026 12:18:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796473121; bh=FtI/MmbM803qOn+Ff/mwmvYecfrfySBRqepAvgjkch0=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=TCUwlEHQjqmUC9ZFAzYyARurJVg5kb8UwF1L4VG3OL6OZS9pIzLc/OLcWqgw0JXYB zQNJNKW8pPHjiEGnRRqu3qmutmJLNDXCJSYuTbGEF3A642MYEbJlEXzhBVqCQqbcjY 7MCcbSs19Yzp+iFRTVRhQIfIQ3TcFjEV9+hAxF9ZKgflAOYS1syfGQqbIOhTOf8PeS V7VnoAMN62ob2iKAyMw45Uj+/rIz0KPHEupHkEoarRra0vyNAXqDJ5BDYViUllxExf q1E0HkuaYGYGlwZDPBYsxbbzU13q1Su2NDNRKt4vFssAKRi+D62ITOvjOZaF7fYOcp tvUZ1T8fIWjMg== Subject: Re: [PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver 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 Date: Sat, 05 Dec 2026 12:18:39 +0000 Message-ID: <179647311958.3146.5048859446669166535@kernel.org> In-Reply-To: <20261204110622.1106492-12-maxime.chevallier@bootlin.com> References: <20261204110622.1106492-12-maxime.chevallier@bootlin.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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 > +#include > +#include > +#include > + > +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 "); > +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