Skip to contentSkip to conversation

: fix phy_device_free memory leak

PATCH In review Petr Malat 2 participants 2 messages Feb 6, 2013 – Feb 8, 2013

Subsystems (3)
lore
Share & tools
lore

Petr Malat

author
#0 lore raw

PATCH 1/1 : fix phy_device_free memory leak

Hi,
there is a memory leak in phy_device handling:
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. I fixed this by the attached patch, so far it's working fine.

Patch description:
Name is freed by phy_device_free(). In the case a device is released trough kobject_cleanup()->device_release()->phy_device_release(), the name is set to NULL and it is not freed by phy_device_free(), because it will be freed later by kobject_cleanup().

Please put me on CC, I'm not signed into the mailing list.

Petr

drivers/net/phy/phy_device.c +4 -0
@@ -41,12 +41,16 @@ MODULE_LICENSE("GPL");
 
 void phy_device_free(struct phy_device *phydev)
 {
+	kfree(phydev->dev.kobj.name);
 	kfree(phydev);
 }
 EXPORT_SYMBOL(phy_device_free);
 
 static void phy_device_release(struct device *dev)
 {
+	/* Name will be freed by kobject_cleanup() */
+	dev->kobj.name = NULL; 
+
 	phy_device_free(to_phy_device(dev));
 }
maintainer
#1 lore raw

From: Petr Malat <[email protected]>
Date: Thu, 7 Feb 2013 00:07:57 +0100

Quoted 9 lines

Hi,
there is a memory leak in phy_device handling:
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. I fixed this by the attached patch, so far it's working fine.

Quoted 7 lines

Patch description:
Name is freed by phy_device_free(). In the case a device is released trough kobject_cleanup()->device_release()->phy_device_release(), the name is set to NULL and it is not freed by phy_device_free(), because it will be freed later by kobject_cleanup().

Please put me on CC, I'm not signed into the mailing list.

Please follow the directions in Documentation/SubmittingPatches, in particular the part about providing a proper signoff.

Do not send an updated version of this patch as a reply to this thread, instead create a completely new mailing list posting for that purpose.

Thanks.