From: Aleksei Sviridkin <f@lex.la>
To: Andrew Lunn <andrew@lunn.ch>,
Heiner Kallweit <hkallweit1@gmail.com>,
Russell King <linux@armlinux.org.uk>,
netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
linux-kernel@vger.kernel.org,
Florian Fainelli <f.fainelli@gmail.com>,
Mao Wenan <maowenan@huawei.com>,
Woojung Huh <woojung.huh@microchip.com>,
Vladimir Oltean <vladimir.oltean@nxp.com>,
Maxime Chevallier <maxime.chevallier@bootlin.com>
Subject: [PATCH net v3 2/4] net: phy: put the driver module the attach took
Date: Fri, 25 Sep 2026 00:59:48 +0300 [thread overview]
Message-ID: <20260924215951.2127682-3-f@lex.la> (raw)
In-Reply-To: <20260924215951.2127682-1-f@lex.la>
phy_attach_direct() pins the PHY driver module through d->driver, and
phy_detach() releases it by reading d->driver again. Unbinding the PHY
driver while the PHY is attached clears that pointer, so a detach that
runs while the driver is still unbound skips the put and the module can
no longer be unloaded; if a different driver binds in between, the put
lands on a module that was never pinned. The NULL test added by
commit c2b727df7caa ("net: phy: Avoid NPD upon phy_detach() when driver
is unbound") avoids the oops but skips the put.
Found by reading phy_detach() while chasing a PHY driver unbind race on
a Keenetic KN-1012 (MT7981, air_en8811h built as a module). The leak
itself was not observed there: unbinding air_en8811h under the attached
lan4 DSA port and then unbinding the switch faults earlier on that
board, in phy_free_interrupt() or under phy_stop(), before
phy_detach() gets to the put.
Remember which module was pinned and release that one.
Fixes: cafe8df8b9bc ("net: phy: Fix lack of reference count on PHY driver")
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
---
drivers/net/phy/phy_device.c | 8 +++++---
include/linux/phy.h | 2 ++
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 7046976c5b6a..29d65f6cfd59 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1793,6 +1793,7 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
err = -EIO;
goto error_put_device;
}
+ phydev->drv_owner = d->driver->owner;
if (phydev->is_genphy_driven) {
err = d->driver->probe(d);
@@ -1894,7 +1895,8 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
return err;
error_module_put:
- module_put(d->driver->owner);
+ module_put(phydev->drv_owner);
+ phydev->drv_owner = NULL;
phydev->is_genphy_driven = 0;
d->driver = NULL;
error_put_device:
@@ -1955,8 +1957,8 @@ void phy_detach(struct phy_device *phydev)
phydev->phy_link_change = NULL;
phydev->phylink = NULL;
- if (phydev->mdio.dev.driver)
- module_put(phydev->mdio.dev.driver->owner);
+ module_put(phydev->drv_owner);
+ phydev->drv_owner = NULL;
/* If the device had no specific driver before (i.e. - it
* was using the generic driver), we unbind the device
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 5f8d65868e0f..33a207ac5c30 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -560,6 +560,7 @@ struct phy_oatc14_sqi_capability {
*
* @mdio: MDIO bus this PHY is on
* @drv: Pointer to the driver for this PHY instance
+ * @drv_owner: Driver module phy_attach_direct() took a reference on
* @devlink: Create a link between phy dev and mac dev, if the external phy
* used by current mac interface is managed by another mac interface.
* @phyindex: Unique id across the phy's parent tree of phys to address the PHY
@@ -671,6 +672,7 @@ struct phy_device {
/* Information about the PHY type */
/* And management functions */
const struct phy_driver *drv;
+ struct module *drv_owner;
struct device_link *devlink;
--
2.53.0
next prev parent reply other threads:[~2026-09-24 22:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 21:59 [PATCH net v3 0/4] net: phy: make PHY driver unbind safe against attach and use Aleksei Sviridkin
2026-09-24 21:59 ` [PATCH net v3 1/4] net: phy: refuse a second attach before touching the PHY Aleksei Sviridkin
2026-09-24 21:59 ` Aleksei Sviridkin [this message]
2026-09-24 21:59 ` [PATCH net v3 3/4] net: phy: serialise attach and detach with PHY driver bind and unbind Aleksei Sviridkin
2026-09-30 0:46 ` Jakub Kicinski
2026-09-24 21:59 ` [PATCH net v3 4/4] net: phy: make an unbind wait for the attached consumer to detach Aleksei Sviridkin
2026-09-30 0:44 ` [PATCH net v3 0/4] net: phy: make PHY driver unbind safe against attach and use Jakub Kicinski
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=20260924215951.2127682-3-f@lex.la \
--to=f@lex.la \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=f.fainelli@gmail.com \
--cc=hkallweit1@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=maowenan@huawei.com \
--cc=maxime.chevallier@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=vladimir.oltean@nxp.com \
--cc=woojung.huh@microchip.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
all inboxes | Powered by JetHome®