Skip to content

Commit

Permalink
phy: Fix phy_device_free memory leak
Browse files Browse the repository at this point in the history
Fix memory leak in phy_device_free() for the case when phy_device*
returned by phy_device_create() is not registered in the system.

Bug description:
phy_device_create() sets name of kobject using dev_set_name(), which
allocates memory using kvasprintf(), but this memory isn't freed if
the underlying device isn't registered properly, because kobject_cleanup()
is not called in that case. This can happen (and actually is happening on
our machines) if phy_device_register(), called by mdiobus_scan(), fails.

Patch description:
Embedded struct device is initialized in phy_device_create() and it
counterpart phy_device_free() just drops one reference to the device,
which leads to proper deinitialization including releasing the kobject
name memory.

Signed-off-by: Petr Malat <oss@malat.biz>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Petr Malat authored and David S. Miller committed Feb 28, 2013
1 parent d521de0 commit b2a4319
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions drivers/net/phy/phy_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ MODULE_LICENSE("GPL");

void phy_device_free(struct phy_device *phydev)
{
kfree(phydev);
put_device(&phydev->dev);
}
EXPORT_SYMBOL(phy_device_free);

static void phy_device_release(struct device *dev)
{
phy_device_free(to_phy_device(dev));
kfree(to_phy_device(dev));
}

static struct phy_driver genphy_driver;
Expand Down Expand Up @@ -201,6 +201,8 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
there's no driver _already_ loaded. */
request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));

device_initialize(&dev->dev);

return dev;
}
EXPORT_SYMBOL(phy_device_create);
Expand Down Expand Up @@ -363,9 +365,9 @@ int phy_device_register(struct phy_device *phydev)
/* Run all of the fixups for this PHY */
phy_scan_fixups(phydev);

err = device_register(&phydev->dev);
err = device_add(&phydev->dev);
if (err) {
pr_err("phy %d failed to register\n", phydev->addr);
pr_err("PHY %d failed to add\n", phydev->addr);
goto out;
}

Expand Down

0 comments on commit b2a4319

Please sign in to comment.