From: sashiko-bot@kernel.org
To: "Markus Stockhausen" <markus.stockhausen@gmx.de>
Cc: conor+dt@kernel.org, robh@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH net-next v15 04/13] net: phy: add phy_detach_internal() helper
Date: Tue, 01 Sep 2026 14:35:35 +0000 [thread overview]
Message-ID: <20260901143536.368891F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260831143439.2404484-5-markus.stockhausen@gmx.de>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] phy_attach_direct() error path destructively tears down pre-existing PHY attachments and corrupts module refcounts.
--
commit 9c0668542a6d1a738f40f7d8a8ebdeecb5d1257f
Author: Markus Stockhausen <markus.stockhausen@gmx.de>
Subject: net: phy: add phy_detach_internal() helper
This patch carves out the phy_detach() code into a new helper
phy_detach_internal() and makes phy_detach() a short stub that calls the
new helper. This prepares for upcoming MDIO bus notification enhancements
that need to distinguish between normal detach and cleanup paths.
> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 8cb0d60fcbba9..69b04a92b7766 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -1763,6 +1763,98 @@ static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
> return phydrv->config_intr && phydrv->handle_interrupt;
> }
>
> +/**
> + * phy_detach_internal - detach a PHY device from its network device
> + * @phydev: target phy_device struct
> + *
> + * This detaches the phy device from its network device and the phy
> + * driver, and drops the reference count taken in phy_attach_direct().
> + */
> +static void phy_detach_internal(struct phy_device *phydev)
> +{
> + struct net_device *dev = phydev->attached_dev;
> + struct module *ndev_owner = NULL;
> + struct mii_bus *bus;
[ ... ]
> + phy_suspend(phydev);
> + if (dev) {
> + struct hwtstamp_provider *hwprov;
> +
> + /* hwprov may technically be protected by ops lock but
> + * not for devices with a phydev, see phy_link_topo_add_phy()
> + */
> + hwprov = rtnl_dereference(dev->hwprov);
> + /* Disable timestamp if it is the one selected */
> + if (hwprov && hwprov->phydev == phydev) {
> + rcu_assign_pointer(dev->hwprov, NULL);
> + kfree_rcu(hwprov, rcu_head);
> + }
> +
> + phydev->attached_dev->phydev = NULL;
> + phydev->attached_dev = NULL;
> + phy_link_topo_del_phy(dev, phydev);
> + }
[Severity: High]
This isn't a bug introduced by this patch, but this logic can tear down
an existing attachment when an error occurs during phy_attach_direct().
If phy_attach_direct() is called on a PHY that is already attached to
another device, it hits this check:
drivers/net/phy/phy_device.c:phy_attach_direct() {
if (phydev->attached_dev) {
dev_err(&dev->dev, "PHY already attached\n");
err = -EBUSY;
goto error;
}
...
error:
/* phy_detach() does all of the cleanup below */
phy_detach(phydev);
return err;
}
This calls phy_detach_internal() on the old attached_dev, completely
destroying the pre-existing attachment (clearing its phydev pointer,
suspending the PHY, etc.) without notifying it. Can we abort harmlessly
instead of destructively modifying the pre-existing state?
> +
> + phydev->phy_link_change = NULL;
> + phydev->phylink = NULL;
> +
> + if (phydev->mdio.dev.driver)
> + module_put(phydev->mdio.dev.driver->owner);
> +
> + /* If the device had no specific driver before (i.e. - it
> + * was using the generic driver), we unbind the device
> + * from the generic driver so that there's a chance a
> + * real driver could be loaded
> + */
> + if (phydev->is_genphy_driven) {
> + device_release_driver(&phydev->mdio.dev);
> + phydev->is_genphy_driven = 0;
> + }
> +
> + /* Assert the reset signal */
> + phy_device_reset(phydev, 1);
> +
> + /*
> + * The phydev might go away on the put_device() below, so avoid
> + * a use-after-free bug by reading the underlying bus first.
> + */
> + bus = phydev->mdio.bus;
> +
> + put_device(&phydev->mdio.dev);
> + if (dev)
> + ndev_owner = dev->dev.parent->driver->owner;
> + if (ndev_owner != bus->owner)
> + module_put(bus->owner);
> +}
[Severity: High]
This is a pre-existing issue, but this balances the bus owner refcount
based on the old device's driver owner.
In the case where phy_attach_direct() fails due to the PHY already being
attached, this module_put() executes with the old caller's module owner.
Could this lead to module refcount underflows or leaks because it balances
the new caller's try_module_get() using the old caller's module?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260831143439.2404484-1-markus.stockhausen@gmx.de?part=4
next prev parent reply other threads:[~2026-09-01 14:35 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 14:34 [PATCH net-next v15 00/13] net: mdio: realtek-rtl9300: Add RTL83xx support Markus Stockhausen
2026-08-31 14:34 ` [PATCH net-next v15 01/13] dt-bindings: net: realtek,rtl9301-mdio: Add RTL83xx series Markus Stockhausen
2026-08-31 14:34 ` [PATCH net-next v15 02/13] net: mdio: realtek-rtl9300: Add polling documentation Markus Stockhausen
2026-08-31 14:34 ` [PATCH net-next v15 03/13] net: mdio: realtek-rtl9300: deny C45 over C22 access Markus Stockhausen
2026-09-02 0:08 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,03/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 04/13] net: phy: add phy_detach_internal() helper Markus Stockhausen
2026-09-01 14:35 ` sashiko-bot [this message]
2026-09-02 0:09 ` Andrew Lunn
2026-08-31 14:34 ` [PATCH net-next v15 05/13] net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus Markus Stockhausen
2026-09-02 0:10 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,05/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 06/13] net: mdio: realtek-rtl9300: suppress sysfs bind/unbind attributes Markus Stockhausen
2026-09-01 14:35 ` sashiko-bot
2026-09-02 0:12 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,06/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 07/13] net: mdio: realtek-rtl9300: Configure hardware polling during probing Markus Stockhausen
2026-09-01 14:35 ` sashiko-bot
2026-09-02 0:14 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,07/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 08/13] net: mdio: realtek-rtl9300: Add page tracking Markus Stockhausen
2026-09-02 0:16 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,08/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 09/13] net: mdio: realtek-rtl9300: Increase MDIO timeout Markus Stockhausen
2026-08-31 14:34 ` [PATCH net-next v15 10/13] net: mdio: realtek-rtl9300: Open up C22 and C45 space in parallel Markus Stockhausen
2026-09-01 14:35 ` sashiko-bot
2026-09-02 0:16 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,10/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 11/13] net: mdio: realtek-rtl9300: Add support for RTL838x Markus Stockhausen
2026-09-02 0:17 ` Andrew Lunn
2026-09-02 5:36 ` [net-next,v15,11/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 12/13] net: mdio: realtek-rtl9300: Add support for RTL839x Markus Stockhausen
2026-09-02 0:18 ` Andrew Lunn
2026-09-02 5:32 ` AW: " Markus Stockhausen
2026-09-02 5:36 ` [net-next,v15,12/13] " netdev-bot+sashiko
2026-08-31 14:34 ` [PATCH net-next v15 13/13] net: mdio: realtek-rtl9300: reword Kconfig and module description Markus Stockhausen
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=20260901143536.368891F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=markus.stockhausen@gmx.de \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox