[PATCH v3] Loongarch: env: fix missing NULL checks for kstrdup

Li Jun posted 1 patch 2 weeks, 4 days ago
arch/loongarch/kernel/env.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH v3] Loongarch: env: fix missing NULL checks for kstrdup
Posted by Li Jun 2 weeks, 4 days ago
1.Replacing `of_find_node_by_path("/")` with `of_root` avoids multiple
calls to `of_node_put()`.
2.Fixes potential kernel oops during early boot when memory
allocation fails while parsing CPU model from device tree.

--Replacing `of_find_node_by_path("/")` with `of_root`,
Fix missing NULL checks.

Signed-off-by: Li Jun <lijun01@kylinos.cn>
---
 arch/loongarch/kernel/env.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
index 841206fde3ab..2afc9b09f139 100644
--- a/arch/loongarch/kernel/env.c
+++ b/arch/loongarch/kernel/env.c
@@ -15,6 +15,7 @@
 #include <asm/loongson.h>
 #include <asm/setup.h>
 #include <asm/time.h>
+#include <linux/of.h>
 
 u64 efi_system_table;
 struct loongson_system_configuration loongson_sysconf;
@@ -42,16 +43,15 @@ static int __init init_cpu_fullname(void)
 	int cpu, ret;
 	char *cpuname;
 	const char *model;
-	struct device_node *root;
 
 	/* Parsing cpuname from DTS model property */
-	root = of_find_node_by_path("/");
-	ret = of_property_read_string(root, "model", &model);
+	ret = of_property_read_string(of_root, "model", &model);
 	if (ret == 0) {
 		cpuname = kstrdup(model, GFP_KERNEL);
+		if (!cpuname)
+			return -ENOMEM;
 		loongson_sysconf.cpuname = strsep(&cpuname, " ");
 	}
-	of_node_put(root);
 
 	if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
 		for (cpu = 0; cpu < NR_CPUS; cpu++)
-- 
2.34.1
Re: [PATCH v3] Loongarch: env: fix missing NULL checks for kstrdup
Posted by Huacai Chen 2 weeks, 4 days ago
Hi, Jun,

Please replace Loongarch with LoongArch, and...

On Thu, Mar 19, 2026 at 1:58 PM Li Jun <lijun01@kylinos.cn> wrote:
>
> 1.Replacing `of_find_node_by_path("/")` with `of_root` avoids multiple
> calls to `of_node_put()`.
> 2.Fixes potential kernel oops during early boot when memory
> allocation fails while parsing CPU model from device tree.
>
> --Replacing `of_find_node_by_path("/")` with `of_root`,
> Fix missing NULL checks.
Why say it again?

>
> Signed-off-by: Li Jun <lijun01@kylinos.cn>
> ---
>  arch/loongarch/kernel/env.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
> index 841206fde3ab..2afc9b09f139 100644
> --- a/arch/loongarch/kernel/env.c
> +++ b/arch/loongarch/kernel/env.c
> @@ -15,6 +15,7 @@
>  #include <asm/loongson.h>
>  #include <asm/setup.h>
>  #include <asm/time.h>
> +#include <linux/of.h>
I don't think this is needed, we already use of_find_node_by_path in
of.h, which means it has already been included implicitly.


Huacai

>
>  u64 efi_system_table;
>  struct loongson_system_configuration loongson_sysconf;
> @@ -42,16 +43,15 @@ static int __init init_cpu_fullname(void)
>         int cpu, ret;
>         char *cpuname;
>         const char *model;
> -       struct device_node *root;
>
>         /* Parsing cpuname from DTS model property */
> -       root = of_find_node_by_path("/");
> -       ret = of_property_read_string(root, "model", &model);
> +       ret = of_property_read_string(of_root, "model", &model);
>         if (ret == 0) {
>                 cpuname = kstrdup(model, GFP_KERNEL);
> +               if (!cpuname)
> +                       return -ENOMEM;
>                 loongson_sysconf.cpuname = strsep(&cpuname, " ");
>         }
> -       of_node_put(root);
>
>         if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
>                 for (cpu = 0; cpu < NR_CPUS; cpu++)
> --
> 2.34.1
>
>