Skip to contentSkip to conversation

net: zynq_gem: plug resource leaks in .probe error and .remove paths

PATCH Michal Simek 1 participant 2 messages Nov 3 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 4 lines · Graeme Smecher

zynq_gem_probe() allocates receive buffers, buffer descriptor space and (usually) an MDIO bus, but these are leaked on some error paths.

zynq_gem_remove() never freed priv->tx_bd and priv->rxbuffers at all.

And this is one patch.

Quoted 6 lines

With CONFIG_DM_ETH_PHY, priv->bus may instead come from eth_phy_get_mdio_bus(), in which case it belongs to another controller and must not be unregistered or freed. Track whether the bus was allocated here and only tear it down in that case, in both the probe error path and remove.

And this is another one.

Quoted 58 lines

---
Changes in v2:
- Move mdio_free() out of the mdio_register_seq() failure branch and into the error label ladder (Michal Simek)
- With CONFIG_DM_ETH_PHY, priv->bus may be a shared bus obtained from eth_phy_get_mdio_bus(). Track whether we allocated it and only unregister/free it in that case, in both probe and remove (Michal Simek)

  drivers/net/zynq_gem.c | 28 +++++++++++++++++++++-------
  1 file changed, 21 insertions(+), 7 deletions(-)
drivers/net/zynq_gem.c +8 -2
index f570ae9ee73..986e6918253 100644
@@ -257,6 +257,7 @@ struct zynq_gem_priv {
  	struct phy_device *phydev;
  	ofnode phy_of_node;
  	struct mii_dev *bus;
+	bool own_bus;
  	struct clk rx_clk;
  	struct clk tx_clk;
  	struct clk pclk;
@@ -998,13 +999,18 @@ static int zynq_gem_probe(struct udevice *dev)
 
  	if (!priv->bus) {
  		priv->bus = mdio_alloc();
+		if (!priv->bus) {
+			ret = -ENOMEM;
+			goto err2;
+		}
+		priv->own_bus = true;
  		priv->bus->read = zynq_gem_miiphy_read;
  		priv->bus->write = zynq_gem_miiphy_write;
  		priv->bus->priv = priv;
 
  		ret = mdio_register_seq(priv->bus, dev_seq(dev));
  		if (ret)
-			goto err2;
+			goto err3;
  	}
 
  	if (IS_ENABLED(CONFIG_DM_ETH_PHY))
@@ -1012,7 +1018,7 @@ static int zynq_gem_probe(struct udevice *dev)
 
  	ret = zynq_phy_init(dev);
  	if (ret)
-		goto err3;
+		goto err4;
 
  	if (priv->interface == PHY_INTERFACE_MODE_SGMII &&
  	    generic_phy_valid(&phy)) {
@@ -1024,13 +1030,13 @@ static int zynq_gem_probe(struct udevice *dev)
  					dev_err
  					(dev,

drivers/net/zynq_gem.c static int zynq_gem_probe(struct udevice *dev) · line 1039

Quoted 10 lines
drivers/net/zynq_gem.c +2 -2
  					 "Failed to set gem dynamic config\n");
-					return ret;
+					goto err4;
  				}
  			}
  		}
  		ret = generic_phy_power_on(&phy);
  		if (ret)
-			return ret;
+			goto err4;

You are changing this but in error patch I can't see error path for phy handling. I think it would be good to clean it up when you are on it.

drivers/net/zynq_gem.c static int zynq_gem_probe(struct udevice *dev) · line 1048

Quoted 8 lines
drivers/net/zynq_gem.c +1 -0
  	}
 
  	printf("\nZYNQ GEM: %lx, mdio bus %lx, phyaddr %d, interface %s\n",
@@ -1039,8 +1045,12 @@ static int zynq_gem_probe(struct udevice *dev)
 
  	return ret;
 
+err4:

Don't you miss also clean up from zynq_phy_init()

       if (!IS_ERR_OR_NULL(priv->phydev))
               free(priv->phydev);
Quoted 14 lines
drivers/net/zynq_gem.c +4 -2
+	if (priv->own_bus)
+		mdio_unregister(priv->bus);
  err3:
-	mdio_unregister(priv->bus);
+	if (priv->own_bus)
+		mdio_free(priv->bus);
  err2:
  	free(priv->tx_bd);
  err1:
@@ -1053,8 +1063,12 @@ static int zynq_gem_remove(struct udevice *dev)
  	struct zynq_gem_priv *priv = dev_get_priv(dev);
 
  	free(priv->phydev);
-	mdio_unregister(priv->bus);

drivers/net/zynq_gem.c static int zynq_gem_remove(struct udevice *dev) · line 1074

Quoted 10 lines
drivers/net/zynq_gem.c +6 -1
-	mdio_free(priv->bus);
+	if (priv->own_bus) {
+		mdio_unregister(priv->bus);
+		mdio_free(priv->bus);
+	}
+	free(priv->tx_bd);
+	free(priv->rxbuffers);
 
  	return 0;
  }

M

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Re: [PATCH v2 2/2] net: eth-phy: release the reset GPIO on device removal

Quoted 21 lines · Graeme Smecher

eth_phy_of_to_plat() claims the PHY reset GPIO with gpio_request_by_name(), but nothing releases the claim when the device is removed. When retrying a wedged PHY, any follow-up probes fail because the GPIO has already been claimed.

---
Changes in v2:
- None

  drivers/net/eth-phy-uclass.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
drivers/net/eth-phy-uclass.c +0 -0
index 1dae26878e6..e93a358a2a6 100644
@@ -181,11 +181,25 @@ static int eth_phy_pre_probe(struct udevice *dev)
  	return 0;
  }
 

drivers/net/eth-phy-uclass.c static int eth_phy_pre_probe(struct udevice *dev) · line 193

Quoted 10 lines
drivers/net/eth-phy-uclass.c +10 -0
+static int eth_phy_pre_remove(struct udevice *dev)
+{
+	struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev);
+
+	if (!CONFIG_IS_ENABLED(DM_GPIO))
+		return 0;
+
+	if (dm_gpio_is_valid(&uc_priv->reset_gpio))
+		dm_gpio_free(dev, &uc_priv->reset_gpio);
+

don't you also want to call reset_free() when you are touching this?

M