Skip to contentSkip to conversation

dma: qcom: hidma_mgmt: Add of_node_put() before goto

PATCH Superseded Nishka Dasgupta 3 participants 5 messages Jul 23, 2019 – Jul 24, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma: qcom: hidma_mgmt: Add of_node_put() before goto

Each iteration of for_each_available_child_of_node puts the previous node, but in the case of a goto from the middle of the loop, there is no put, thus causing a memory leak. Add an of_node_put before the goto in 4 places.
Issue found with Coccinelle.

---
 drivers/dma/qcom/hidma_mgmt.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/dma/qcom/hidma_mgmt.c +10 -3
index 3022d66e7a33..209adc6ceabe 100644
@@ -362,16 +362,22 @@ static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
 		struct platform_device *new_pdev;
 
 		ret = of_address_to_resource(child, 0, &res[0]);
-		if (!ret)
+		if (!ret) {
+			of_node_put(child);
 			goto out;
+		}
 
 		ret = of_address_to_resource(child, 1, &res[1]);
-		if (!ret)
+		if (!ret) {
+			of_node_put(child);
 			goto out;
+		}
 
 		ret = of_irq_to_resource(child, 0, &res[2]);
-		if (ret <= 0)
+		if (ret <= 0) {
+			of_node_put(child);
 			goto out;
+		}
 
 		memset(&pdevinfo, 0, sizeof(pdevinfo));
 		pdevinfo.fwnode = &child->fwnode;
@@ -386,6 +392,7 @@ static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
 		new_pdev = platform_device_register_full(&pdevinfo);
 		if (IS_ERR(new_pdev)) {
 			ret = PTR_ERR(new_pdev);
+			of_node_put(child);
 			goto out;
 		}
 		of_node_get(child);
Signature · 1 line
2.19.1
#1 lore raw
Quoted 4 lines · Nishka Dasgupta

Each iteration of for_each_available_child_of_node puts the previous node, but in the case of a goto from the middle of the loop, there is no put, thus causing a memory leak. Add an of_node_put before the goto in 4 places.

Why not just add it once at the "out" label itself? (Consider the conditions for the loop terminating naturally)

And if you're cleaning up the refcounting here anyway then I'd also note that the reference held by the loop iterator makes the extra get/put inside that loop entirely redundant. It's always worth taking a look at the wider context rather than just blindly focusing on what a given script picks up - it's fairly rare that a piece of code has one obvious issue but is otherwise perfect.

Robin.

Quoted 46 lines

Issue found with Coccinelle.

---
  drivers/dma/qcom/hidma_mgmt.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
drivers/dma/qcom/hidma_mgmt.c +10 -3
index 3022d66e7a33..209adc6ceabe 100644
@@ -362,16 +362,22 @@ static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
  		struct platform_device *new_pdev;
 
  		ret = of_address_to_resource(child, 0, &res[0]);
-		if (!ret)
+		if (!ret) {
+			of_node_put(child);
  			goto out;
+		}
 
  		ret = of_address_to_resource(child, 1, &res[1]);
-		if (!ret)
+		if (!ret) {
+			of_node_put(child);
  			goto out;
+		}
 
  		ret = of_irq_to_resource(child, 0, &res[2]);
-		if (ret <= 0)
+		if (ret <= 0) {
+			of_node_put(child);
  			goto out;
+		}
 
  		memset(&pdevinfo, 0, sizeof(pdevinfo));
  		pdevinfo.fwnode = &child->fwnode;
@@ -386,6 +392,7 @@ static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
  		new_pdev = platform_device_register_full(&pdevinfo);
  		if (IS_ERR(new_pdev)) {
  			ret = PTR_ERR(new_pdev);
+			of_node_put(child);
  			goto out;
  		}
  		of_node_get(child);
maintainer
#2 lore raw
Quoted 3 lines · Robin Murphy

Why not just add it once at the "out" label itself? (Consider the conditions for the loop terminating naturally)

+1

Quoted 6 lines
Quoted 6 lines

---
  drivers/dma/qcom/hidma_mgmt.c | 13 ++++++++++---

  1 file changed, 10 insertions(+), 3 deletions(-)
Quoted 12 lines

drivers/dma/qcom/hidma_mgmt.c static int __init · line 366

Quoted 12 lines
drivers/dma/qcom/hidma_mgmt.c +2 -1
index 3022d66e7a33..209adc6ceabe 100644
@@ -362,16 +362,22 @@ static int __init
 hidma_mgmt_of_populate_channels(struct device_node *np)
           struct platform_device *new_pdev;
             ret = of_address_to_resource(child, 0, &res[0]);
-        if (!ret)
+        if (!ret) {
+            of_node_put(child);

The spacing on this also looks weird.

Quoted 2 lines
Quoted 2 lines
drivers/dma/qcom/hidma_mgmt.c +1 -0
               goto out;
+        } 
#3 lore raw
In reply to Robin Murphy
Quoted 7 lines · Robin Murphy
Quoted 4 lines · Nishka Dasgupta

Each iteration of for_each_available_child_of_node puts the previous node, but in the case of a goto from the middle of the loop, there is no put, thus causing a memory leak. Add an of_node_put before the goto in 4 places.

Why not just add it once at the "out" label itself? (Consider the conditions for the loop terminating naturally)

If the loop terminates naturally then, as far as I understand, child will be put by the loop itself; then an extra of_node_put() under the out label would put the child node even though it has already been put. If I'm understanding this correctly (and I might not be) is it okay to decrement refcount more times that it is incremented?

Quoted 1 line

And if you're cleaning up the refcounting here anyway then I'd also note that the reference held by the loop iterator makes the extra get/put inside that loop entirely redundant. It's always worth taking a look at the wider context rather than just blindly focusing on what a given script picks up - it's fairly rare that a piece of code has one obvious issue but is otherwise perfect.

Thank you for pointing this out; I've added it in v2.

Thanking you,
Nishka

Quoted 46 lines

Robin.

Quoted 44 lines

Issue found with Coccinelle.

---
  drivers/dma/qcom/hidma_mgmt.c | 13 ++++++++++---

  1 file changed, 10 insertions(+), 3 deletions(-)
drivers/dma/qcom/hidma_mgmt.c +10 -3
index 3022d66e7a33..209adc6ceabe 100644
@@ -362,16 +362,22 @@ static int __init 
 hidma_mgmt_of_populate_channels(struct device_node *np)
           struct platform_device *new_pdev;
           ret = of_address_to_resource(child, 0, &res[0]);
-        if (!ret)
+        if (!ret) {
+            of_node_put(child);
               goto out;
+        }
           ret = of_address_to_resource(child, 1, &res[1]);
-        if (!ret)
+        if (!ret) {
+            of_node_put(child);
               goto out;
+        }
           ret = of_irq_to_resource(child, 0, &res[2]);
-        if (ret <= 0)
+        if (ret <= 0) {
+            of_node_put(child);
               goto out;
+        }
           memset(&pdevinfo, 0, sizeof(pdevinfo));
           pdevinfo.fwnode = &child->fwnode;
@@ -386,6 +392,7 @@ static int __init 
 hidma_mgmt_of_populate_channels(struct device_node *np)
           new_pdev = platform_device_register_full(&pdevinfo);
           if (IS_ERR(new_pdev)) {
               ret = PTR_ERR(new_pdev);
+            of_node_put(child);
               goto out;
           }

          of_node_get(child);

#4 lore raw
Quoted 10 lines · Nishka Dasgupta
Quoted 7 lines · Robin Murphy
Quoted 4 lines · Nishka Dasgupta

Each iteration of for_each_available_child_of_node puts the previous node, but in the case of a goto from the middle of the loop, there is no put, thus causing a memory leak. Add an of_node_put before the goto in 4 places.

Why not just add it once at the "out" label itself? (Consider the conditions for the loop terminating naturally)

If the loop terminates naturally then, as far as I understand, child will be put by the loop itself; then an extra of_node_put() under the out label would put the child node even though it has already been put. If I'm understanding this correctly (and I might not be) is it okay to decrement refcount more times that it is incremented?

Ah, but is it really the same thing being put both times? The loop *iterator* will indeed drop its reference on the last valid child node, but what's the actual termination condition, and thus the state afterwards? ;)

Robin.

Quoted 53 lines
Quoted 1 line

And if you're cleaning up the refcounting here anyway then I'd also note that the reference held by the loop iterator makes the extra get/put inside that loop entirely redundant. It's always worth taking a look at the wider context rather than just blindly focusing on what a given script picks up - it's fairly rare that a piece of code has one obvious issue but is otherwise perfect.

Thank  you for pointing this out; I've added it in v2.

Thanking you,
Nishka

Quoted 46 lines

Robin.

Quoted 44 lines

Issue found with Coccinelle.

---
  drivers/dma/qcom/hidma_mgmt.c | 13 ++++++++++---

  1 file changed, 10 insertions(+), 3 deletions(-)
drivers/dma/qcom/hidma_mgmt.c +10 -3
index 3022d66e7a33..209adc6ceabe 100644
@@ -362,16 +362,22 @@ static int __init 
 hidma_mgmt_of_populate_channels(struct device_node *np)
           struct platform_device *new_pdev;
           ret = of_address_to_resource(child, 0, &res[0]);
-        if (!ret)
+        if (!ret) {
+            of_node_put(child);
               goto out;
+        }
           ret = of_address_to_resource(child, 1, &res[1]);
-        if (!ret)
+        if (!ret) {
+            of_node_put(child);
               goto out;
+        }
           ret = of_irq_to_resource(child, 0, &res[2]);
-        if (ret <= 0)
+        if (ret <= 0) {
+            of_node_put(child);
               goto out;
+        }
           memset(&pdevinfo, 0, sizeof(pdevinfo));
           pdevinfo.fwnode = &child->fwnode;
@@ -386,6 +392,7 @@ static int __init 
 hidma_mgmt_of_populate_channels(struct device_node *np)
           new_pdev = platform_device_register_full(&pdevinfo);
           if (IS_ERR(new_pdev)) {
               ret = PTR_ERR(new_pdev);
+            of_node_put(child);
               goto out;
           }

          of_node_get(child);