Merge branch 'net-phy-fix-a-second-attach-of-an-attached-phy-and-the-module-put-on-detach'

Aleksei Sviridkin says:

====================
net: phy: fix a second attach of an attached PHY and the module put on detach

Two phylib fixes found on a Keenetic KN-1012 (MT7981) while chasing a
PHY driver unbind race. A second attach of an attached PHY tears down
the first consumer, and a detach after a driver unbind leaks the module
reference.

1: refuse a second attach before the driver module is taken or the
   generic driver bound.
2: put the module the attach pinned, not whatever driver is bound at
   detach.

Patch 1 goes first: with patch 2 alone, the second attach error path
leaves the first consumer's module reference pinned.
====================

Link: https://patch.msgid.link/20261013151200.2473464-1-f@lex.la
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski 2026-10-16 18:44:47 -07:00
commit 8b0244ca4f
2 changed files with 17 additions and 10 deletions

View File

@ -1728,8 +1728,8 @@ static void phy_detach_internal(struct phy_device *phydev, bool notify_bus)
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
@ -1794,6 +1794,7 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
struct mii_bus *bus = phydev->mdio.bus;
struct device *d = &phydev->mdio.dev;
struct module *ndev_owner = NULL;
struct module *drv_owner;
int irq = phydev->irq;
int err;
@ -1811,6 +1812,13 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
get_device(d);
/* Set by every attach, with or without a netdev */
if (phydev->phy_link_change) {
phydev_err(phydev, "PHY already attached\n");
err = -EBUSY;
goto error_put_device;
}
/* Assume that if there is no driver, that it doesn't
* exist, and we should use the genphy driver.
*/
@ -1823,11 +1831,13 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
phydev->is_genphy_driven = 1;
}
if (!try_module_get(d->driver->owner)) {
drv_owner = d->driver->owner;
if (!try_module_get(drv_owner)) {
phydev_err(phydev, "failed to get the device driver module\n");
err = -EIO;
goto error_put_device;
}
phydev->drv_owner = drv_owner;
if (phydev->is_genphy_driven) {
err = d->driver->probe(d);
@ -1838,12 +1848,6 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
goto error_module_put;
}
if (phydev->attached_dev) {
dev_err(&dev->dev, "PHY already attached\n");
err = -EBUSY;
goto error;
}
phydev->phy_link_change = phy_link_change;
if (dev) {
phydev->attached_dev = dev;
@ -1941,7 +1945,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->irq = irq;
phydev->is_genphy_driven = 0;
d->driver = NULL;

View File

@ -578,6 +578,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
@ -689,6 +690,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;