Skip to contentSkip to conversation

cpuidle: riscv-sbi: fix device node release in early exit of for_each_possible_cpu

PATCH Applied Javier Carrasco 4 participants 4 messages Nov 15, 2024 – Jan 9, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cpuidle: riscv-sbi: fix device node release in early exit of for_each_possible_cpu

The 'np' device_node is initialized via of_cpu_device_node_get(), which requires explicit calls to of_node_put() when it is no longer required to avoid leaking the resource.

Instead of adding the missing calls to of_node_put() in all execution paths, use the cleanup attribute for 'np' by means of the __free() macro, which automatically calls of_node_put() when the variable goes out of scope. Given that 'np' is only used within the for_each_possible_cpu(), reduce its scope to release the nood after every iteration of the loop.

Fixes 6abf32f1d9c5 ("cpuidle: Add RISC-V SBI CPU idle driver")

---
Changes in v3:
- Unwrap line (100 char width).
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Squash patches for mainline solution without intermediate steps.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 drivers/cpuidle/cpuidle-riscv-sbi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpuidle/cpuidle-riscv-sbi.c +2 -2
index 14462c092039..0c92a628bbd4 100644
@@ -504,12 +504,12 @@ static int sbi_cpuidle_probe(struct platform_device *pdev)
 	int cpu, ret;
 	struct cpuidle_driver *drv;
 	struct cpuidle_device *dev;
-	struct device_node *np, *pds_node;
+	struct device_node *pds_node;
 
 	/* Detect OSI support based on CPU DT nodes */
 	sbi_cpuidle_use_osi = true;
 	for_each_possible_cpu(cpu) {
-		np = of_cpu_device_node_get(cpu);
+		struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
 		if (np &&
 		    of_property_present(np, "power-domains") &&
 		    of_property_present(np, "power-domain-names")) {

---
base-commit: 744cf71b8bdfcdd77aaf58395e068b7457634b2c
change-id: 20241029-cpuidle-riscv-sbi-cleanup-e9b3cb96e16d

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
#1 lore raw

Hi Anup,

Quoted 50 lines · Javier Carrasco

The 'np' device_node is initialized via of_cpu_device_node_get(), which requires explicit calls to of_node_put() when it is no longer required to avoid leaking the resource.

Instead of adding the missing calls to of_node_put() in all execution paths, use the cleanup attribute for 'np' by means of the __free() macro, which automatically calls of_node_put() when the variable goes out of scope. Given that 'np' is only used within the for_each_possible_cpu(), reduce its scope to release the nood after every iteration of the loop.

Fixes 6abf32f1d9c5 ("cpuidle: Add RISC-V SBI CPU idle driver")

---
Changes in v3:
- Unwrap line (100 char width).
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Squash patches for mainline solution without intermediate steps.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/cpuidle/cpuidle-riscv-sbi.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpuidle/cpuidle-riscv-sbi.c +2 -3
index 14462c092039..0c92a628bbd4 100644
@@ -504,12 +504,12 @@ static int sbi_cpuidle_probe(struct platform_device *pdev)
  	int cpu, ret;
  	struct cpuidle_driver *drv;
  	struct cpuidle_device *dev;
-	struct device_node *np, *pds_node;
+	struct device_node *pds_node;
 
  	/* Detect OSI support based on CPU DT nodes */
  	sbi_cpuidle_use_osi = true;
  	for_each_possible_cpu(cpu) {
-		np = of_cpu_device_node_get(cpu);
+		struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
  		if (np &&
  		    of_property_present(np, "power-domains") &&
  		    of_property_present(np, "power-domain-names")) {
 
---

base-commit: 744cf71b8bdfcdd77aaf58395e068b7457634b2c
change-id: 20241029-cpuidle-riscv-sbi-cleanup-e9b3cb96e16d

Quoted 1 line

Best regards,

Will you make a PR for this? Or should this go through the riscv tree?

Thanks,

Alex

#2 lore raw
Quoted 50 lines · Alexandre Ghiti

Hi Anup,

Quoted 46 lines · Javier Carrasco

The 'np' device_node is initialized via of_cpu_device_node_get(), which requires explicit calls to of_node_put() when it is no longer required to avoid leaking the resource.

Instead of adding the missing calls to of_node_put() in all execution paths, use the cleanup attribute for 'np' by means of the __free() macro, which automatically calls of_node_put() when the variable goes out of scope. Given that 'np' is only used within the for_each_possible_cpu(), reduce its scope to release the nood after every iteration of the loop.

Fixes 6abf32f1d9c5 ("cpuidle: Add RISC-V SBI CPU idle driver")

---
Changes in v3:
- Unwrap line (100 char width).
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Squash patches for mainline solution without intermediate steps.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
  drivers/cpuidle/cpuidle-riscv-sbi.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpuidle/cpuidle-riscv-sbi.c +2 -2
index 14462c092039..0c92a628bbd4 100644
@@ -504,12 +504,12 @@ static int sbi_cpuidle_probe(struct platform_device *pdev)
      int cpu, ret;
      struct cpuidle_driver *drv;
      struct cpuidle_device *dev;
-     struct device_node *np, *pds_node;
+     struct device_node *pds_node;
 
      /* Detect OSI support based on CPU DT nodes */
      sbi_cpuidle_use_osi = true;
      for_each_possible_cpu(cpu) {
-             np = of_cpu_device_node_get(cpu);
+             struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
              if (np &&
                  of_property_present(np, "power-domains") &&
                  of_property_present(np, "power-domain-names")) {
Quoted 8 lines
Quoted 5 lines

---
base-commit: 744cf71b8bdfcdd77aaf58395e068b7457634b2c
change-id: 20241029-cpuidle-riscv-sbi-cleanup-e9b3cb96e16d

Best regards,

Will you make a PR for this? Or should this go through the riscv tree?

It's not KVM related so better take it through the RISC-V tree.

Regards,
Anup

#3 lore raw
In reply to Javier Carrasco
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 4 lines · On Sat, 16 Nov 2024 00:32:39 +0100 you wrote:

The 'np' device_node is initialized via of_cpu_device_node_get(), which requires explicit calls to of_node_put() when it is no longer required to avoid leaking the resource.

Quoted 8 lines

Instead of adding the missing calls to of_node_put() in all execution paths, use the cleanup attribute for 'np' by means of the __free() macro, which automatically calls of_node_put() when the variable goes out of scope. Given that 'np' is only used within the for_each_possible_cpu(), reduce its scope to release the nood after every iteration of the loop.

[...]

Here is the summary with links:
- [v3] cpuidle: riscv-sbi: fix device node release in early exit of for_each_possible_cpu

    https://git.kernel.org/riscv/c/7e25044b8045

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html