Skip to contentSkip to conversation

firmware: psci: Fix refcount leak in psci_dt_init

PATCH Applied Miaoqian Lin 5 participants 8 messages Mar 18, 2025 – Apr 29, 2025 Power state coordination interface (psci)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: psci: Fix refcount leak in psci_dt_init

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Fixes bff60792f994 ("arm64: psci: factor invocation code to drivers")
---
 drivers/firmware/psci/psci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/psci/psci.c +3 -1
index a1ebbe9b73b1..38ca190d4a22 100644
@@ -804,8 +804,10 @@ int __init psci_dt_init(void)
 
 	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
-	if (!np || !of_device_is_available(np))
+	if (!np || !of_device_is_available(np)) {
+		of_node_put(np);
 		return -ENODEV;
+	}
 
 	init_fn = (psci_initcall_t)matched_np->data;
 	ret = init_fn(np);
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

Hi Miaoqian,

Quoted 9 lines · Miaoqian Lin

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Fixes bff60792f994 ("arm64: psci: factor invocation code to drivers")
---
  drivers/firmware/psci/psci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

I'm wandering if the fix tag is correct enough because !of_device_is_available(np) wasn't added by bff60792f994.

drivers/firmware/psci/psci.c int __init psci_dt_init(void) · line 810

Quoted 13 lines
drivers/firmware/psci/psci.c +3 -1
index a1ebbe9b73b1..38ca190d4a22 100644
@@ -804,8 +804,10 @@ int __init psci_dt_init(void)
 
  	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
-	if (!np || !of_device_is_available(np))
+	if (!np || !of_device_is_available(np)) {
+		of_node_put(np);
  		return -ENODEV;
+	}

The fix looks good to me. The duplicated of_node_put() can be avoided with a 'out' tag added, something like below.

	if (!np || !of_device_is_available(np)) {
		ret = -ENODEV;
		goto out;
	}

	:

out:

	of_node_put(np);
	return ret;

drivers/firmware/psci/psci.c int __init psci_dt_init(void) · line 813

Quoted 3 lines
drivers/firmware/psci/psci.c +0 -0
  	init_fn = (psci_initcall_t)matched_np->data;
  	ret = init_fn(np);

Thanks,
Gavin

#2 lore raw
Quoted 38 lines · Gavin Shan

Hi Miaoqian,

Quoted 9 lines · Miaoqian Lin

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Fixes bff60792f994 ("arm64: psci: factor invocation code to drivers")
---
  drivers/firmware/psci/psci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

I'm wandering if the fix tag is correct enough because !of_device_is_available(np) wasn't added by bff60792f994.

drivers/firmware/psci/psci.c int __init psci_dt_init(void) · line 808

Quoted 11 lines
drivers/firmware/psci/psci.c +3 -1
index a1ebbe9b73b1..38ca190d4a22 100644
@@ -804,8 +804,10 @@ int __init psci_dt_init(void)
  	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
-	if (!np || !of_device_is_available(np))
+	if (!np || !of_device_is_available(np)) {
+		of_node_put(np);
  		return -ENODEV;
+	}

The fix looks good to me. The duplicated of_node_put() can be avoided with a 'out' tag added, something like below.

	if (!np || !of_device_is_available(np)) {
		ret = -ENODEV;
		goto out;
	}

	:
Quoted 7 lines

out:

	of_node_put(np);
	return ret;
Quoted 2 lines
drivers/firmware/psci/psci.c +0 -0
  	init_fn = (psci_initcall_t)matched_np->data;
  	ret = init_fn(np);

Any reason why we can't move to the new scoped usage like below?

Signature · 4 lines
Regards,
Sudeep

-->8
drivers/firmware/psci/psci.c +2 -4
index a1ebbe9b73b1..a4269078b2a2 100644
@@ -797,12 +797,11 @@ static const struct of_device_id psci_of_match[] __initconst = {
 
 int __init psci_dt_init(void)
 {
-       struct device_node *np;
        const struct of_device_id *matched_np;
        psci_initcall_t init_fn;
        int ret;
-
-       np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
+       struct device_node *np __free(device_node) =
+               of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
        if (!np || !of_device_is_available(np))
                return -ENODEV;
@@ -810,7 +809,6 @@ int __init psci_dt_init(void)
        init_fn = (psci_initcall_t)matched_np->data;
        ret = init_fn(np);
 
-       of_node_put(np);
        return ret;
 }
 
maintainer
#3 lore raw
Quoted 24 lines · Sudeep Holla

On Wed, Mar 19, 2025 at 08:28:38PM +1000, Gavin Shan wrote:

Any reason why we can't move to the new scoped usage like below?

drivers/firmware/psci/psci.c +2 -3
index a1ebbe9b73b1..a4269078b2a2 100644
@@ -797,12 +797,11 @@ static const struct of_device_id psci_of_match[] __initconst = {
 
 int __init psci_dt_init(void)
 {
-       struct device_node *np;
        const struct of_device_id *matched_np;
        psci_initcall_t init_fn;
        int ret;
-
-       np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
+       struct device_node *np __free(device_node) =
+               of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
        if (!np || !of_device_is_available(np))
                return -ENODEV;

drivers/firmware/psci/psci.c int __init psci_dt_init(void) · line 813

Quoted 6 lines
drivers/firmware/psci/psci.c +0 -1
@@ -810,7 +809,6 @@ int __init psci_dt_init(void)
        init_fn = (psci_initcall_t)matched_np->data;
        ret = init_fn(np);
 
-       of_node_put(np);
        return ret;
 }

IIUC the bug has existed since before the scoped helpers were introduced, so for the sake of keeping the backport simple I reckon we should take Miaoqian Lin's patch as-is, and we can consider moving to the scoped helpers as a later cleanup.

Mark.

#4 lore raw
Quoted 28 lines · Mark Rutland
Quoted 27 lines · Sudeep Holla

On Wed, Mar 19, 2025 at 08:28:38PM +1000, Gavin Shan wrote:

Any reason why we can't move to the new scoped usage like below?

drivers/firmware/psci/psci.c +2 -3
index a1ebbe9b73b1..a4269078b2a2 100644
@@ -797,12 +797,11 @@ static const struct of_device_id psci_of_match[] __initconst = {
 
 int __init psci_dt_init(void)
 {
-       struct device_node *np;
        const struct of_device_id *matched_np;
        psci_initcall_t init_fn;
        int ret;
-
-       np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
+       struct device_node *np __free(device_node) =
+               of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
        if (!np || !of_device_is_available(np))
                return -ENODEV;
@@ -810,7 +809,6 @@ int __init psci_dt_init(void)
        init_fn = (psci_initcall_t)matched_np->data;
        ret = init_fn(np);
 
Quoted 9 lines

drivers/firmware/psci/psci.c int __init psci_dt_init(void) · line 813

Quoted 3 lines
drivers/firmware/psci/psci.c +0 -1
-       of_node_put(np);
        return ret;
 }

IIUC the bug has existed since before the scoped helpers were introduced, so for the sake of keeping the backport simple I reckon we should take Miaoqian Lin's patch as-is, and we can consider moving to the scoped helpers as a later cleanup.

Agreed.

Signature · 2 lines
Regards,
Sudeep
maintainer Acked-by on v1 1/1
#5 lore raw
In reply to Miaoqian Lin
Quoted 5 lines · Miaoqian Lin

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Fixes bff60792f994 ("arm64: psci: factor invocation code to drivers")

The fixes tag is wrong. As of commit bff60792f994 the code was:

|       np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
|
|       if (!np)
|               return -ENODEV;

... which was correct.

The bug was introduced later in commit:

d09a0011ec0d511b ("drivers: psci: Allow PSCI node to be disabled")

... which added the of_device_is_available() check.

Other than that, this looks fine. With the fixes tag corrected:

Mark.

Quoted 23 lines
---
 drivers/firmware/psci/psci.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/psci/psci.c +3 -1
index a1ebbe9b73b1..38ca190d4a22 100644
@@ -804,8 +804,10 @@ int __init psci_dt_init(void)
 
 	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
-	if (!np || !of_device_is_available(np))
+	if (!np || !of_device_is_available(np)) {
+		of_node_put(np);
 		return -ENODEV;
+	}
 
 	init_fn = (psci_initcall_t)matched_np->data;
 	ret = init_fn(np);
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v1 1/1
#6 lore raw
In reply to Miaoqian Lin
Quoted 9 lines · Miaoqian Lin

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Fixes bff60792f994 ("arm64: psci: factor invocation code to drivers")
---
  drivers/firmware/psci/psci.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

With the fix tag corrected to the one, suggested by Mark.

Quoted 16 lines
drivers/firmware/psci/psci.c +3 -1
index a1ebbe9b73b1..38ca190d4a22 100644
@@ -804,8 +804,10 @@ int __init psci_dt_init(void)
 
  	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
 
-	if (!np || !of_device_is_available(np))
+	if (!np || !of_device_is_available(np)) {
+		of_node_put(np);
  		return -ENODEV;
+	}
 
  	init_fn = (psci_initcall_t)matched_np->data;
  	ret = init_fn(np);
Applied
#7 lore raw
In reply to Miaoqian Lin
Quoted 4 lines · Miaoqian Lin

Fix a reference counter leak in psci_dt_init() where of_node_put(np) was missing after of_find_matching_node_and_match() when np is unavailable.

Applied to arm64 (for-next/psci), thanks!

[1/1] firmware: psci: Fix refcount leak in psci_dt_init

      https://git.kernel.org/arm64/c/7ff37d29fd5c

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev