[PATCH] net: ethernet: fix potential memory leak in gemini_ethernet_port_probe()

Lu Wei luwei32 at huawei.com
Tue Jul 28 23:46:06 EDT 2020


If some processes in gemini_ethernet_port_probe() fail,
free_netdev(dev) needs to be called to avoid a memory leak.

Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Reported-by: Hulk Robot <hulkci at huawei.com>
Signed-off-by: Lu Wei <luwei32 at huawei.com>
---
 drivers/net/ethernet/cortina/gemini.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 8d13ea370db1..5e93a1a570b6 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -2407,37 +2407,48 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
 	dmares = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!dmares) {
 		dev_err(dev, "no DMA resource\n");
+		free_netdev(netdev);
 		return -ENODEV;
 	}
 	port->dma_base = devm_ioremap_resource(dev, dmares);
-	if (IS_ERR(port->dma_base))
+	if (IS_ERR(port->dma_base)) {
+		free_netdev(netdev);
 		return PTR_ERR(port->dma_base);
+	}
 
 	/* GMAC config memory */
 	gmacres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 	if (!gmacres) {
 		dev_err(dev, "no GMAC resource\n");
+		free_netdev(netdev);
 		return -ENODEV;
 	}
 	port->gmac_base = devm_ioremap_resource(dev, gmacres);
-	if (IS_ERR(port->gmac_base))
+	if (IS_ERR(port->gmac_base)) {
+		free_netdev(netdev);
 		return PTR_ERR(port->gmac_base);
+	}
 
 	/* Interrupt */
 	irq = platform_get_irq(pdev, 0);
-	if (irq <= 0)
+	if (irq <= 0) {
+		free_netdev(netdev);
 		return irq ? irq : -ENODEV;
+	}
 	port->irq = irq;
 
 	/* Clock the port */
 	port->pclk = devm_clk_get(dev, "PCLK");
 	if (IS_ERR(port->pclk)) {
 		dev_err(dev, "no PCLK\n");
+		free_netdev(netdev);
 		return PTR_ERR(port->pclk);
 	}
 	ret = clk_prepare_enable(port->pclk);
-	if (ret)
+	if (ret) {
+		free_netdev(netdev);
 		return ret;
+	}
 
 	/* Maybe there is a nice ethernet address we should use */
 	gemini_port_save_mac_addr(port);
@@ -2446,6 +2457,7 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
 	port->reset = devm_reset_control_get_exclusive(dev, NULL);
 	if (IS_ERR(port->reset)) {
 		dev_err(dev, "no reset\n");
+		free_netdev(netdev);
 		return PTR_ERR(port->reset);
 	}
 	reset_control_reset(port->reset);
@@ -2501,8 +2513,10 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
 					IRQF_SHARED,
 					port_names[port->id],
 					port);
-	if (ret)
+	if (ret) {
+		free_netdev(netdev);
 		return ret;
+	}
 
 	ret = register_netdev(netdev);
 	if (!ret) {
-- 
2.17.1




More information about the linux-arm-kernel mailing list