Skip to contentSkip to conversation

amba: fix refcount underflow if amba_device_add() fails

PATCH Guenter Roeck 2 participants 2 messages May 24, 2022

lore
Share & tools
lore

Guenter Roeck

author Tested-by
#0 lore raw
Quoted 8 lines · Kefeng Wang

"ARM: 9192/1: amba: fix memory leak in amba_device_try_add()" leads to a refcount underflow if amba_device_add() fails, which called by of_amba_device_create(), the of_amba_device_create() already exists the error handling, so amba_put_device() only need to be added into amba_deferred_retry().

Thanks,
Guenter

Quoted 34 lines
---
 drivers/amba/bus.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/amba/bus.c +2 -6
index 0e3ed5eb367b..0cb20324da16 100644
@@ -493,13 +493,8 @@ static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
 		goto skip_probe;
 
 	ret = amba_read_periphid(dev);
-	if (ret) {
-		if (ret != -EPROBE_DEFER) {
-			amba_device_put(dev);
-			goto err_out;
-		}
+	if (ret)
 		goto err_release;
-	}
 
 skip_probe:
 	ret = device_add(&dev->dev);
@@ -546,6 +541,7 @@ static int amba_deferred_retry(void)
 			continue;
 
 		list_del_init(&ddev->node);
+		amba_device_put(ddev->dev);
 		kfree(ddev);
 	}
 
Signature · 1 line
2.35.3
#1 lore raw
Quoted 10 lines · Guenter Roeck
Quoted 8 lines · Kefeng Wang

"ARM: 9192/1: amba: fix memory leak in amba_device_try_add()" leads to a refcount underflow if amba_device_add() fails, which called by of_amba_device_create(), the of_amba_device_create() already exists the error handling, so amba_put_device() only need to be added into amba_deferred_retry().

Thanks.

PS: I also test with kmemleak, this could also fix previous memory leak issue.

Quoted 39 lines

Thanks,
Guenter

Quoted 34 lines
---
  drivers/amba/bus.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/amba/bus.c +2 -6
index 0e3ed5eb367b..0cb20324da16 100644
@@ -493,13 +493,8 @@ static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
  		goto skip_probe;
 
  	ret = amba_read_periphid(dev);
-	if (ret) {
-		if (ret != -EPROBE_DEFER) {
-			amba_device_put(dev);
-			goto err_out;
-		}
+	if (ret)
  		goto err_release;
-	}
 
  skip_probe:
  	ret = device_add(&dev->dev);
@@ -546,6 +541,7 @@ static int amba_deferred_retry(void)
  			continue;
 
  		list_del_init(&ddev->node);
+		amba_device_put(ddev->dev);
  		kfree(ddev);
  	}
 
Signature · 1 line
2.35.3

.