[PATCH v2] riscv: cacheinfo: Fix node reference leak in populate_cache_leaves

Zishun Yi posted 1 patch 1 month ago
arch/riscv/kernel/cacheinfo.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2] riscv: cacheinfo: Fix node reference leak in populate_cache_leaves
Posted by Zishun Yi 1 month ago
Currently, the while loop drops the reference to prev in each iteration.
If the loop terminates early due to a break, the final of_node_put(np)
correctly drops the reference to the current node.

However, if the loop terminates naturally because np == NULL, calling
of_node_put(np) is a no-op. This leaves the last valid node stored in
prev without its reference dropped, resulting in a node reference leak.

Fix this by changing the final `of_node_put(np)` to `of_node_put(prev)`.

Fixes: 94f9bf118f1e ("RISC-V: Fix of_node_* refcount")
Cc: stable@vger.kernel.org
Assisted-by: Gemini:gemini-3.1-pro
Signed-off-by: Zishun Yi <vulab@iscas.ac.cn>
---
Changes in v2:
- Added 'Assisted-by' tag.

 arch/riscv/kernel/cacheinfo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/cacheinfo.c b/arch/riscv/kernel/cacheinfo.c
index 26b085dbdd07..6c9a1ef2d45a 100644
--- a/arch/riscv/kernel/cacheinfo.c
+++ b/arch/riscv/kernel/cacheinfo.c
@@ -133,7 +133,7 @@ int populate_cache_leaves(unsigned int cpu)
 			ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
 		levels = level;
 	}
-	of_node_put(np);
+	of_node_put(prev);
 
 	return 0;
 }
-- 
2.51.2
Re: [PATCH v2] riscv: cacheinfo: Fix node reference leak in populate_cache_leaves
Posted by Paul Walmsley 3 weeks ago
On Sat, 9 May 2026, Zishun Yi wrote:

> Currently, the while loop drops the reference to prev in each iteration.
> If the loop terminates early due to a break, the final of_node_put(np)
> correctly drops the reference to the current node.
> 
> However, if the loop terminates naturally because np == NULL, calling
> of_node_put(np) is a no-op. This leaves the last valid node stored in
> prev without its reference dropped, resulting in a node reference leak.
> 
> Fix this by changing the final `of_node_put(np)` to `of_node_put(prev)`.
> 
> Fixes: 94f9bf118f1e ("RISC-V: Fix of_node_* refcount")
> Cc: stable@vger.kernel.org
> Assisted-by: Gemini:gemini-3.1-pro
> Signed-off-by: Zishun Yi <vulab@iscas.ac.cn>

Thanks, queued for v7.1-rc.


- Paul