[PATCH] riscv: cpuinfo: Fix NULL dereference and inconsistent error code

Mohamed Ayman posted 1 patch 1 month, 1 week ago
There is a newer version of this series
arch/riscv/kernel/cpu.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
[PATCH] riscv: cpuinfo: Fix NULL dereference and inconsistent error code
Posted by Mohamed Ayman 1 month, 1 week ago
Fix a potential NULL pointer dereference in c_show() by ensuring
the device tree node returned by of_get_cpu_node() is checked before use.

Also replace a non-standard return value (-1) in
riscv_of_parent_hartid() with -ENODEV to follow Linux kernel
conventions for "not found" error returns.

Both issues improve robustness and consistency with existing RISC-V CPU info handling code.
---
 arch/riscv/kernel/cpu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
index 3dbc8cc557dd..e0bc7526861f 100644
--- a/arch/riscv/kernel/cpu.c
+++ b/arch/riscv/kernel/cpu.c
@@ -135,7 +135,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
 		}
 	}
 
-	return -1;
+	return -ENODEV;
 }
 
 unsigned long __init riscv_get_marchid(void)
@@ -348,12 +348,13 @@ static int c_show(struct seq_file *m, void *v)
 
 	if (acpi_disabled) {
 		node = of_get_cpu_node(cpu_id, NULL);
+		if (node) {
+			if (!of_property_read_string(node, "compatible", &compat) &&
+				strcmp(compat, "riscv"))
+				seq_printf(m, "uarch\t\t: %s\n", compat);
 
-		if (!of_property_read_string(node, "compatible", &compat) &&
-		    strcmp(compat, "riscv"))
-			seq_printf(m, "uarch\t\t: %s\n", compat);
-
-		of_node_put(node);
+			of_node_put(node);
+		}
 	}
 
 	seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
-- 
2.34.1
Re: [PATCH] riscv: cpuinfo: Fix NULL dereference and inconsistent error code
Posted by Conor Dooley 1 month, 1 week ago
Hey,

On Sat, May 02, 2026 at 07:02:57PM +0300, Mohamed Ayman wrote:
> Fix a potential NULL pointer dereference in c_show() by ensuring
> the device tree node returned by of_get_cpu_node() is checked before use.
> 
> Also replace a non-standard return value (-1) in
> riscv_of_parent_hartid() with -ENODEV to follow Linux kernel
> conventions for "not found" error returns.
> 
> Both issues improve robustness and consistency with existing RISC-V CPU info handling code.


These two things should be two different patches, since they're
completely unrelated.

Additionally, the patch is missing a signoff.

> ---
>  arch/riscv/kernel/cpu.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
> index 3dbc8cc557dd..e0bc7526861f 100644
> --- a/arch/riscv/kernel/cpu.c
> +++ b/arch/riscv/kernel/cpu.c
> @@ -135,7 +135,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
>  		}
>  	}
>  
> -	return -1;
> +	return -ENODEV;
>  }
>  
>  unsigned long __init riscv_get_marchid(void)
> @@ -348,12 +348,13 @@ static int c_show(struct seq_file *m, void *v)
>  
>  	if (acpi_disabled) {
>  		node = of_get_cpu_node(cpu_id, NULL);
> +		if (node) {

Is it actually possible for this to fail?

Cheers,
Conor.

> +			if (!of_property_read_string(node, "compatible", &compat) &&
> +				strcmp(compat, "riscv"))
> +				seq_printf(m, "uarch\t\t: %s\n", compat);
>  
> -		if (!of_property_read_string(node, "compatible", &compat) &&
> -		    strcmp(compat, "riscv"))
> -			seq_printf(m, "uarch\t\t: %s\n", compat);
> -
> -		of_node_put(node);
> +			of_node_put(node);
> +		}
>  	}
>  
>  	seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
> -- 
> 2.34.1
>