Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Markus Stockhausen" <markus.stockhausen@gmx.de>
Cc: conor+dt@kernel.org, devicetree@vger.kernel.org, robh@kernel.org
Subject: Re: [PATCH net-next v7 3/8] net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus
Date: Mon, 27 Jul 2026 07:18:45 +0000	[thread overview]
Message-ID: <20260727071845.6C2741F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260726071751.1359156-4-markus.stockhausen@gmx.de>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Calling `phy_attach_direct()` on an already-attached PHY destructively tears down the existing attachment and corrupts module refcounts.
--

--- Patch [3]: [PATCH net-next v7 3/8] net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus ---
commit f981bcd4f5183a24448425da1f5f3cc2787f456b
Author: Daniel Golle <daniel@makrotopia.org>
Subject: net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus

This commit adds notify_phy_attach() and notify_phy_detach() hooks to
struct mii_bus. This allows MDIO bus drivers to be informed when a PHY
is attached or detached. The teardown logic from phy_detach() is moved
into a new __phy_detach() helper to handle the notification conditionally.

Link: https://github.com/openwrt/openwrt/pull/21515#discussion_r2714069716
Link: https://lore.kernel.org/netdev/cover.1769053496.git.daniel@makrotopia.org/

> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 94b2e85e00a37..233622e9ba61f 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c

[ ... ]

> @@ -1890,8 +1986,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
>  	return err;
>  
>  error:
> -	/* phy_detach() does all of the cleanup below */
> -	phy_detach(phydev);
> +	/* cleanup without bus notification */
> +	__phy_detach(phydev, false);

[Severity: High]
This is a pre-existing issue, but is it safe to call __phy_detach() here
if we reached this error path because the PHY was already attached?

Earlier in phy_attach_direct(), if the PHY is already attached, it jumps
to the error path:

	if (phydev->attached_dev) {
		dev_err(&dev->dev, "PHY already attached\n");
		err = -EBUSY;
		goto error;
	}

By calling __phy_detach(), it destructively tears down the existing attachment.
It nullifies phydev->attached_dev and removes links:

drivers/net/phy/phy_device.c:__phy_detach() {
	...
	phydev->attached_dev->phydev = NULL;
	phydev->attached_dev = NULL;
	phy_link_topo_del_phy(dev, phydev);
	...
}

Could this lead to unexpected device detachment and potential use-after-free
or null-pointer dereferences in the original owner's driver?

Does this also corrupt the module refcount for bus->owner? The module_get()
in phy_attach_direct() is conditional on the new device's owner, while
the module_put() in __phy_detach() relies on the old device's owner.

Would it be better to add a separate error label that skips __phy_detach()
for cases where the attachment hasn't been established yet?

>  	return err;
>  
>  error_module_put:

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260726071751.1359156-1-markus.stockhausen@gmx.de?part=3

  reply	other threads:[~2026-07-27  7:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-26  7:17 [PATCH net-next v7 0/8] net: mdio: realtek-rtl9300: Add RTL83xx support Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 1/8] dt-bindings: net: realtek,rtl9301-mdio: Add RTL83xx series Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 2/8] net: mdio: realtek-rtl9300: Add polling documentation Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 3/8] net: phy: add (*notify_phy_attach/detach)() hooks to struct mii_bus Markus Stockhausen
2026-07-27  7:18   ` sashiko-bot [this message]
2026-07-26  7:17 ` [PATCH net-next v7 4/8] net: mdio: realtek-rtl9300: Configure hardware polling during probing Markus Stockhausen
2026-07-27  7:18   ` sashiko-bot
2026-07-26  7:17 ` [PATCH net-next v7 5/8] net: mdio: realtek-rtl9300: Add page tracking Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 6/8] net: mdio: realtek-rtl9300: Increase MDIO timeout Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 7/8] net: mdio: realtek-rtl9300: Add support for RTL838x Markus Stockhausen
2026-07-26  7:17 ` [PATCH net-next v7 8/8] net: mdio: realtek-rtl9300: Add support for RTL839x 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=20260727071845.6C2741F00A3A@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