Skip to contentSkip to conversation

net: fix device reference leaks

PATCH In review Johan Hovold 3 participants 8 messages Nov 3, 2016 – Nov 9, 2016

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover net: fix device reference leaks

This series fixes a number of device reference leaks (and one of_node leak) due to failure to drop the references taken by bus_find_device() and friends.

Note that the final two patches have been compile tested only.

Thanks,
Johan

v2
- hold reference to cpsw-phy-sel device while accessing private data as requested by David. Also update the commit message. (patch 1/4)
- add linux-omap on CC where appropriate

Johan Hovold (4):
phy: fix device reference leaks
net: ethernet: ti: cpsw: fix device and of_node leaks
net: ethernet: ti: davinci_emac: fix device reference leak
net: hns: fix device reference leaks

 drivers/net/ethernet/hisilicon/hns/hnae.c |  8 +++++++-
 drivers/net/ethernet/ti/cpsw-phy-sel.c    |  3 +++
 drivers/net/ethernet/ti/davinci_emac.c    | 10 ++++++----
 drivers/net/phy/phy_device.c              |  2 ++
 4 files changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
2.7.3
#1 lore raw

PATCH v2 1/4 phy: fix device reference leaks

Make sure to drop the reference taken by bus_find_device_by_name() before returning from phy_connect() and phy_attach().

Note that both function still take a reference to the phy device through phy_attach_direct().

Fixes e13934563db0 ("[PATCH] PHY Layer fixup")
Cc Florian Fainelli <[email protected]>
---
 drivers/net/phy/phy_device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/phy_device.c +2 -0
index e977ba931878..1a4bf8acad78 100644
@@ -723,6 +723,7 @@ struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
 	phydev = to_phy_device(d);
 
 	rc = phy_connect_direct(dev, phydev, handler, interface);
+	put_device(d);
 	if (rc)
 		return ERR_PTR(rc);
 
@@ -953,6 +954,7 @@ struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
 	phydev = to_phy_device(d);
 
 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
+	put_device(d);
 	if (rc)
 		return ERR_PTR(rc);
 
Signature · 1 line
2.7.3
#2 lore raw

PATCH v2 2/4 net: ethernet: ti: cpsw: fix device and of_node leaks

In reply to Johan Hovold

Make sure to drop the references taken by of_get_child_by_name() and bus_find_device() before returning from cpsw_phy_sel().

Note that holding a reference to the cpsw-phy-sel device does not prevent the devres-managed private data from going away.

Fixes 5892cd135e16 ("drivers: net: cpsw-phy-sel: Add new driver...")
Cc Mugunthan V N <[email protected]>
Cc Grygorii Strashko <[email protected]>
---
 drivers/net/ethernet/ti/cpsw-phy-sel.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/ti/cpsw-phy-sel.c +3 -0
index 054a8dd23dae..ba1e45ff6aae 100644
@@ -176,9 +176,12 @@ void cpsw_phy_sel(struct device *dev, phy_interface_t phy_mode, int slave)
 	}
 
 	dev = bus_find_device(&platform_bus_type, NULL, node, match);
+	of_node_put(node);
 	priv = dev_get_drvdata(dev);
 
 	priv->cpsw_phy_sel(priv, phy_mode, slave);
+
+	put_device(dev);
 }
 EXPORT_SYMBOL_GPL(cpsw_phy_sel);
 
Signature · 1 line
2.7.3
#5 lore raw

PATCH v2 3/4 net: ethernet: ti: davinci_emac: fix device reference leak

In reply to Johan Hovold

Make sure to drop the references taken by bus_find_device() before returning from emac_dev_open().

Note that phy_connect still takes a reference to the phy device.

Fixes 5d69e0076a72 ("net: davinci_emac: switch to new mdio")
Cc Mugunthan V N <[email protected]>
Cc Grygorii Strashko <[email protected]>
---
 drivers/net/ethernet/ti/davinci_emac.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/ethernet/ti/davinci_emac.c +6 -4
index 2fd94a5bc1f3..84fbe5714f8b 100644
@@ -1410,6 +1410,7 @@ static int emac_dev_open(struct net_device *ndev)
 	int i = 0;
 	struct emac_priv *priv = netdev_priv(ndev);
 	struct phy_device *phydev = NULL;
+	struct device *phy = NULL;
 
 	ret = pm_runtime_get_sync(&priv->pdev->dev);
 	if (ret < 0) {
@@ -1488,19 +1489,20 @@ static int emac_dev_open(struct net_device *ndev)
 
 	/* use the first phy on the bus if pdata did not give us a phy id */
 	if (!phydev && !priv->phy_id) {
-		struct device *phy;
-
 		phy = bus_find_device(&mdio_bus_type, NULL, NULL,
 				      match_first_device);
-		if (phy)
+		if (phy) {
 			priv->phy_id = dev_name(phy);
+			if (!priv->phy_id || !*priv->phy_id)
+				put_device(phy);
+		}
 	}
 
 	if (!phydev && priv->phy_id && *priv->phy_id) {
 		phydev = phy_connect(ndev, priv->phy_id,
 				     &emac_adjust_link,
 				     PHY_INTERFACE_MODE_MII);
-
+		put_device(phy);	/* reference taken by bus_find_device */
 		if (IS_ERR(phydev)) {
 			dev_err(emac_dev, "could not connect to phy %s\n",
 				priv->phy_id);
Signature · 1 line
2.7.3
#6 lore raw

PATCH v2 4/4 net: hns: fix device reference leaks

In reply to Johan Hovold

Make sure to drop the reference taken by class_find_device() in hnae_get_handle() on errors and when later releasing the handle.

Fixes 6fe6611ff275 ("net: add Hisilicon Network Subsystem...")
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
---
 drivers/net/ethernet/hisilicon/hns/hnae.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/hisilicon/hns/hnae.c +7 -1
index c54c6fac0d1d..b6ed818f78ff 100644
@@ -332,8 +332,10 @@ struct hnae_handle *hnae_get_handle(struct device *owner_dev,
 		return ERR_PTR(-ENODEV);
 
 	handle = dev->ops->get_handle(dev, port_id);
-	if (IS_ERR(handle))
+	if (IS_ERR(handle)) {
+		put_device(&dev->cls_dev);
 		return handle;
+	}
 
 	handle->dev = dev;
 	handle->owner_dev = owner_dev;
@@ -356,6 +358,8 @@ struct hnae_handle *hnae_get_handle(struct device *owner_dev,
 	for (j = i - 1; j >= 0; j--)
 		hnae_fini_queue(handle->qs[j]);
 
+	put_device(&dev->cls_dev);
+
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(hnae_get_handle);
@@ -377,6 +381,8 @@ void hnae_put_handle(struct hnae_handle *h)
 		dev->ops->put_handle(h);
 
 	module_put(dev->owner);
+
+	put_device(&dev->cls_dev);
 }
 EXPORT_SYMBOL(hnae_put_handle);
 
Signature · 1 line
2.7.3
maintainer
#7 lore raw
In reply to Johan Hovold

From: Johan Hovold <[email protected]>
Date: Thu, 3 Nov 2016 18:40:18 +0100

Quoted 5 lines

This series fixes a number of device reference leaks (and one of_node leak) due to failure to drop the references taken by bus_find_device() and friends.

Note that the final two patches have been compile tested only.

...

Quoted 4 lines

v2
- hold reference to cpsw-phy-sel device while accessing private data as requested by David. Also update the commit message. (patch 1/4)
- add linux-omap on CC where appropriate

Series applied, thanks.

Reviewed-by on v2 2/4
#3 lore raw

Re: [PATCH net v2 2/4] net: ethernet: ti: cpsw: fix device and of_node leaks

In reply to Johan Hovold
Quoted 6 lines · Johan Hovold

Make sure to drop the references taken by of_get_child_by_name() and bus_find_device() before returning from cpsw_phy_sel().

Note that holding a reference to the cpsw-phy-sel device does not prevent the devres-managed private data from going away.

Quoted 6 lines
Fixes 5892cd135e16 ("drivers: net: cpsw-phy-sel: Add new driver...")
Cc Mugunthan V N <[email protected]>
Cc Grygorii Strashko <[email protected]>

---

Quoted 21 lines
 drivers/net/ethernet/ti/cpsw-phy-sel.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/ti/cpsw-phy-sel.c +3 -0
index 054a8dd23dae..ba1e45ff6aae 100644
@@ -176,9 +176,12 @@ void cpsw_phy_sel(struct device *dev, phy_interface_t phy_mode, int slave)
 	}
 
 	dev = bus_find_device(&platform_bus_type, NULL, node, match);
+	of_node_put(node);
 	priv = dev_get_drvdata(dev);
 
 	priv->cpsw_phy_sel(priv, phy_mode, slave);
+
+	put_device(dev);
 }
 EXPORT_SYMBOL_GPL(cpsw_phy_sel);
 
Signature · 2 lines
regards,
-grygorii
#4 lore raw

Re: [PATCH net v2 2/4] net: ethernet: ti: cpsw: fix device and of_node leaks

Quoted 9 lines · Grygorii Strashko
Quoted 6 lines · Johan Hovold

Make sure to drop the references taken by of_get_child_by_name() and bus_find_device() before returning from cpsw_phy_sel().

Note that holding a reference to the cpsw-phy-sel device does not prevent the devres-managed private data from going away.

Quoted 8 lines
Quoted 6 lines
Fixes 5892cd135e16 ("drivers: net: cpsw-phy-sel: Add new driver...")
Cc Mugunthan V N <[email protected]>
Cc Grygorii Strashko <[email protected]>

---

Thanks for the review. Note that David has already applied these, though.

Johan