Skip to contentSkip to conversation

powerpc: powernv: Fix refcount leak bug in opal-powercap

PATCH In review Liang He 2 participants 5 messages Jun 17, 2022 – Jun 18, 2022 PowerPC

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 powerpc: powernv: Fix refcount leak bug in opal-powercap

In opal_powercap_init(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() in fail path or when it is not used anymore.

Besides, for_each_child_of_node() will automatically *inc* and *dec* refcount during iteration. However, we should add the of_node_put() if there is a break.

---
 arch/powerpc/platforms/powernv/opal-powercap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/platforms/powernv/opal-powercap.c +4 -1
index 64506b46e77b..b102477d3f95 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
 	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
 			GFP_KERNEL);
 	if (!pcaps)
-		return;
+		goto out_powercap;
 
 	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
 	if (!powercap_kobj) {
@@ -236,6 +236,9 @@ void __init opal_powercap_init(void)
 		kfree(pcaps[i].pg.name);
 	}
 	kobject_put(powercap_kobj);
+	of_node_put(node);
 out_pcaps:
 	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
 }
Signature · 1 line
2.25.1
#1 lore raw

Le 17/06/2022 à 06:20, Liang He a écrit :

Quoted 7 lines

In opal_powercap_init(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() in fail path or when it is not used anymore.

Besides, for_each_child_of_node() will automatically *inc* and *dec* refcount during iteration. However, we should add the of_node_put() if there is a break.

Hi,

I'm not sure that your patch is right here. Because of this *inc* and *dec* things, do we still need to of_node_put(powercap) once we have entered for_each_child_of_node?

I think that this reference will be released on the first iteration of the loop.

Maybe of_node_put(powercap) should be duplicated everywhere it is relevant and removed from the error handling path? Or an additional reference should be taken before the loop?
Or adding a new label with "powercap = NULL" and branching there when needed?

CJ

Quoted 29 lines
---
  arch/powerpc/platforms/powernv/opal-powercap.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/platforms/powernv/opal-powercap.c +4 -1
index 64506b46e77b..b102477d3f95 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
  	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
  			GFP_KERNEL);
  	if (!pcaps)
-		return;
+		goto out_powercap;
 
  	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
  	if (!powercap_kobj) {
@@ -236,6 +236,9 @@ void __init opal_powercap_init(void)
  		kfree(pcaps[i].pg.name);
  	}
  	kobject_put(powercap_kobj);
+	of_node_put(node);
  out_pcaps:
  	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
  }
author
#2 lore raw
Quoted 9 lines · Christophe JAILLET

Le 17/06/2022 à 06:20, Liang He a écrit :

Quoted 7 lines

In opal_powercap_init(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() in fail path or when it is not used anymore.

Besides, for_each_child_of_node() will automatically *inc* and *dec* refcount during iteration. However, we should add the of_node_put() if there is a break.

Quoted 9 lines

Hi,

I'm not sure that your patch is right here. Because of this *inc* and *dec* things, do we still need to of_node_put(powercap) once we have entered for_each_child_of_node?

I think that this reference will be released on the first iteration of the loop.

Hi, CJ,

Thanks for your reply and I want have a discuss.

Based on my review on the src of 'of_get_next_child', there is only *inc* for next and *dec* for pre as follow.

(|node| == powercap)
======__of_get_next_child( |node|, prev)======

     ...
        next = prev? prev->sibling:|node|->child;
	of_node_get(next);
	of_node_put(prev);
     ...

=========================

However, there is no any code to release the |node| (i.e., *powercap*).

Am I right? If I am wrong, please correct me, thanks.

Quoted 8 lines

Maybe of_node_put(powercap) should be duplicated everywhere it is relevant and removed from the error handling path?
Or an additional reference should be taken before the loop? Or adding a new label with "powercap = NULL" and branching there when needed?

CJ

If my understanding is right, I think current patch is right.

Otherwise, I will make a new patch to handle that, Thanks.

Liang

Quoted 30 lines
Quoted 30 lines
---
  arch/powerpc/platforms/powernv/opal-powercap.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/platforms/powernv/opal-powercap.c +4 -1
index 64506b46e77b..b102477d3f95 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
  	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
  			GFP_KERNEL);
  	if (!pcaps)
-		return;
+		goto out_powercap;
 
  	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
  	if (!powercap_kobj) {
@@ -236,6 +236,9 @@ void __init opal_powercap_init(void)
  		kfree(pcaps[i].pg.name);
  	}
  	kobject_put(powercap_kobj);
+	of_node_put(node);
  out_pcaps:
  	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
  }
#3 lore raw

Le 17/06/2022 à 07:42, Liang He a écrit :

Quoted 37 lines
Quoted 9 lines · Christophe JAILLET

Le 17/06/2022 à 06:20, Liang He a écrit :

Quoted 7 lines

In opal_powercap_init(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() in fail path or when it is not used anymore.

Besides, for_each_child_of_node() will automatically *inc* and *dec* refcount during iteration. However, we should add the of_node_put() if there is a break.

Quoted 9 lines

Hi,

I'm not sure that your patch is right here. Because of this *inc* and *dec* things, do we still need to of_node_put(powercap) once we have entered for_each_child_of_node?

I think that this reference will be released on the first iteration of the loop.

Hi, CJ,

Thanks for your reply and I want have a discuss.

Based on my review on the src of 'of_get_next_child', there is only *inc* for next and *dec* for pre as follow.

(|node| == powercap)
======__of_get_next_child( |node|, prev)======

      ...
         next = prev? prev->sibling:|node|->child;
	of_node_get(next);
	of_node_put(prev);
      ...
Quoted 5 lines

=========================

However, there is no any code to release the |node| (i.e., *powercap*).

Am I right? If I am wrong, please correct me, thanks.

You are right.
I mis-read __of_get_next_child(().

CJ

Quoted 46 lines
Quoted 9 lines

Maybe of_node_put(powercap) should be duplicated everywhere it is relevant and removed from the error handling path?
Or an additional reference should be taken before the loop? Or adding a new label with "powercap = NULL" and branching there when needed?

CJ

If my understanding is right, I think current patch is right.

Otherwise, I will make a new patch to handle that, Thanks.

Liang

Quoted 30 lines
Quoted 30 lines
---
   arch/powerpc/platforms/powernv/opal-powercap.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/platforms/powernv/opal-powercap.c +4 -1
index 64506b46e77b..b102477d3f95 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
   	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
   			GFP_KERNEL);
   	if (!pcaps)
-		return;
+		goto out_powercap;
 
   	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
   	if (!powercap_kobj) {
@@ -236,6 +236,9 @@ void __init opal_powercap_init(void)
   		kfree(pcaps[i].pg.name);
   	}
   	kobject_put(powercap_kobj);
+	of_node_put(node);
   out_pcaps:
   	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
   }
author
#4 lore raw
In reply to Christophe JAILLET
Quoted 44 lines · Christophe JAILLET

Le 17/06/2022 à 06:20, Liang He a écrit :

Quoted 7 lines

In opal_powercap_init(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() in fail path or when it is not used anymore.

Besides, for_each_child_of_node() will automatically *inc* and *dec* refcount during iteration. However, we should add the of_node_put() if there is a break.

Hi,

I'm not sure that your patch is right here. Because of this *inc* and *dec* things, do we still need to of_node_put(powercap) once we have entered for_each_child_of_node?

I think that this reference will be released on the first iteration of the loop.

Maybe of_node_put(powercap) should be duplicated everywhere it is relevant and removed from the error handling path?
Or an additional reference should be taken before the loop? Or adding a new label with "powercap = NULL" and branching there when needed?

CJ

Quoted 17 lines
---
  arch/powerpc/platforms/powernv/opal-powercap.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/platforms/powernv/opal-powercap.c +1 -1
index 64506b46e77b..b102477d3f95 100644
@@ -153,7 +153,7 @@ void __init opal_powercap_init(void)
  	pcaps = kcalloc(of_get_child_count(powercap), sizeof(*pcaps),
  			GFP_KERNEL);
  	if (!pcaps)
-		return;
+		goto out_powercap;
 
Quoted 12 lines

arch/powerpc/platforms/powernv/opal-powercap.c void __init opal_powercap_init(void) · line 244

Quoted 12 lines
arch/powerpc/platforms/powernv/opal-powercap.c +3 -0
  	powercap_kobj = kobject_create_and_add("powercap", opal_kobj);
  	if (!powercap_kobj) {
@@ -236,6 +236,9 @@ void __init opal_powercap_init(void)
  		kfree(pcaps[i].pg.name);
  	}
  	kobject_put(powercap_kobj);
+	of_node_put(node);
  out_pcaps:
  	kfree(pcaps);
+out_powercap:
+	of_node_put(powercap);
  }

Hi, CJ.

I think my patch is correct based on the old commit:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.19-rc2&id=09700c504d8e63faffd2a2235074e8c5d130cb8f

Bugs and fix solutions in this 09700c504d8e63-commit are very similar with mine.

Besides, I also find similar new bugs in other two files in the same directory 'powernv',
so I have merged all three files' patches into one commit. '[PATCH v2] powerpc: powernv: Fix refcount leak bug'.

Thanks.

Liang