Skip to contentSkip to conversation

ata: pata_octeon_cf: Fix refcount leak in octeon_cf_probe

PATCH Superseded Miaoqian Lin 3 participants 3 messages May 11, 2022 libata

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ata: pata_octeon_cf: Fix refcount leak in octeon_cf_probe

of_find_device_by_node() takes reference, we should use put_device() to release it when not need anymore.
Add missing put_device() in error path to avoid refcount leak.

Fixes 43f01da0f279 ("MIPS/OCTEON/ata: Convert pata_octeon_cf.c to use device tree.")
---
 drivers/ata/pata_octeon_cf.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/ata/pata_octeon_cf.c +2 -0
index 6b5ed3046b44..65688459acf1 100644
@@ -857,12 +857,14 @@ static int octeon_cf_probe(struct platform_device *pdev)
 				res_dma = platform_get_resource(dma_dev, IORESOURCE_MEM, 0);
 				if (!res_dma) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);
 					return -EINVAL;
 				}
 				cf_port->dma_base = (u64)devm_ioremap(&pdev->dev, res_dma->start,
 									 resource_size(res_dma));
 				if (!cf_port->dma_base) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);
 					return -EINVAL;
 				}
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 20 lines · Miaoqian Lin

of_find_device_by_node() takes reference, we should use put_device() to release it when not need anymore.
Add missing put_device() in error path to avoid refcount leak.

Fixes 43f01da0f279 ("MIPS/OCTEON/ata: Convert pata_octeon_cf.c to use device tree.")
---
 drivers/ata/pata_octeon_cf.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/ata/pata_octeon_cf.c +1 -0
index 6b5ed3046b44..65688459acf1 100644
@@ -857,12 +857,14 @@ static int octeon_cf_probe(struct platform_device *pdev)
 				res_dma = platform_get_resource(dma_dev, IORESOURCE_MEM, 0);
 				if (!res_dma) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);

drivers/ata/pata_octeon_cf.c static int octeon_cf_probe(struct platform_device *pdev) · line 870

Quoted 10 lines
drivers/ata/pata_octeon_cf.c +1 -0
 					return -EINVAL;
 				}
 				cf_port->dma_base = (u64)devm_ioremap(&pdev->dev, res_dma->start,
 									 resource_size(res_dma));
 				if (!cf_port->dma_base) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);
 					return -EINVAL;
 				}
 

The extra reference should also be dropped at the end of the "if (dma_node)" too, no ? Otherwise, early returns due to errors after that "if (dma_node)" will also leak the extra ref.

Note that looking around at places where of_find_device_by_node() is being used, very few users call put_device()... Looks like there are lots of device ref leaks with that.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
In reply to Miaoqian Lin

Hello!

Quoted 20 lines · Miaoqian Lin

of_find_device_by_node() takes reference, we should use put_device() to release it when not need anymore.
Add missing put_device() in error path to avoid refcount leak.

Fixes 43f01da0f279 ("MIPS/OCTEON/ata: Convert pata_octeon_cf.c to use device tree.")
---
 drivers/ata/pata_octeon_cf.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/ata/pata_octeon_cf.c +1 -0
index 6b5ed3046b44..65688459acf1 100644
@@ -857,12 +857,14 @@ static int octeon_cf_probe(struct platform_device *pdev)
 				res_dma = platform_get_resource(dma_dev, IORESOURCE_MEM, 0);
 				if (!res_dma) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);

drivers/ata/pata_octeon_cf.c static int octeon_cf_probe(struct platform_device *pdev) · line 870

Quoted 10 lines
drivers/ata/pata_octeon_cf.c +1 -0
 					return -EINVAL;
 				}
 				cf_port->dma_base = (u64)devm_ioremap(&pdev->dev, res_dma->start,
 									 resource_size(res_dma));
 				if (!cf_port->dma_base) {
 					of_node_put(dma_node);
+					put_device(&dma_dev->dev);
 					return -EINVAL;
 				}
 

It seems you forgot to call put_device() at the end of the enclosing *if*, where of_node_put()
is also called...

MBR, Sergey