Skip to contentSkip to conversation

LoongArch: Avoid in-place string operation on FDT content

PATCH Applied Yao Zi 3 participants 3 messages Jul 5, 2025 – Jul 8, 2025 LoongArch

lore
Share & tools
lore

Yao Zi

author
#0 lore raw

PATCH 1/1 LoongArch: Avoid in-place string operation on FDT content

In init_cpu_fullname, a constant pointer to model property is retrieved. It's later modified by the strsep function, which is illegal and corrupts kernel's FDT copy. This is shown by dmesg,

OF: fdt: not creating '/sys/firmware/fdt': CRC check failed

Create a mutable copy of the model property and do in-place operations on it instead. loongson_sysconf.cpuname lives across the kernel lifetime, thus manually releasing isn't necessary.

Also move of_node_put() for the root node after the usage of its property, since of_node_put() decreases the reference counter thus usage after the call is unsafe.

Fixes 44a01f1f726a ("LoongArch: Parsing CPU-related information from DTS")
---
 arch/loongarch/kernel/env.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/loongarch/kernel/env.c +7 -4
index 27144de5c5fe..fba141472ab9 100644
@@ -40,15 +40,18 @@ void __init init_environ(void)
 static int __init init_cpu_fullname(void)
 {
 	struct device_node *root;
+	const char *model;
 	int cpu, ret;
-	char *model;
+	char *tmp;
 
 	/* Parsing cpuname from DTS model property */
 	root = of_find_node_by_path("/");
-	ret = of_property_read_string(root, "model", (const char **)&model);
+	ret = of_property_read_string(root, "model", &model);
+	if (ret == 0) {
+		tmp = kstrdup(model, GFP_KERNEL);
+		loongson_sysconf.cpuname = strsep(&tmp, " ");
+	}
 	of_node_put(root);
-	if (ret == 0)
-		loongson_sysconf.cpuname = strsep(&model, " ");
 
 	if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
 		for (cpu = 0; cpu < NR_CPUS; cpu++)
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · 在2025年7月5日周六 下午1:57,Yao Zi写道:

In init_cpu_fullname, a constant pointer to model property is retrieved. It's later modified by the strsep function, which is illegal and corrupts kernel's FDT copy. This is shown by dmesg,

OF: fdt: not creating '/sys/firmware/fdt': CRC check failed

Create a mutable copy of the model property and do in-place operations on it instead. loongson_sysconf.cpuname lives across the kernel lifetime, thus manually releasing isn't necessary.

Quoted 5 lines

Also move of_node_put() for the root node after the usage of its property, since of_node_put() decreases the reference counter thus usage after the call is unsafe.

Fixes 44a01f1f726a ("LoongArch: Parsing CPU-related information from DTS")
Quoted 35 lines
---
 arch/loongarch/kernel/env.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/loongarch/kernel/env.c +7 -4
index 27144de5c5fe..fba141472ab9 100644
@@ -40,15 +40,18 @@ void __init init_environ(void)
 static int __init init_cpu_fullname(void)
 {
 	struct device_node *root;
+	const char *model;
 	int cpu, ret;
-	char *model;
+	char *tmp;
 
 	/* Parsing cpuname from DTS model property */
 	root = of_find_node_by_path("/");
-	ret = of_property_read_string(root, "model", (const char **)&model);
+	ret = of_property_read_string(root, "model", &model);
+	if (ret == 0) {
+		tmp = kstrdup(model, GFP_KERNEL);
+		loongson_sysconf.cpuname = strsep(&tmp, " ");
+	}
 	of_node_put(root);
-	if (ret == 0)
-		loongson_sysconf.cpuname = strsep(&model, " ");
 
 	if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, 
 "Loongson", 8)) {

for (cpu = 0; cpu < NR_CPUS; cpu++)

Signature · 1 line
2.49.0
Signature · 1 line
- Jiaxun
maintainer Applied
#2 lore raw

Applied, thanks.

Huacai

Quoted 60 lines · Jiaxun Yang
Quoted 10 lines · 在2025年7月5日周六 下午1:57,Yao Zi写道:

In init_cpu_fullname, a constant pointer to model property is retrieved. It's later modified by the strsep function, which is illegal and corrupts kernel's FDT copy. This is shown by dmesg,

OF: fdt: not creating '/sys/firmware/fdt': CRC check failed

Create a mutable copy of the model property and do in-place operations on it instead. loongson_sysconf.cpuname lives across the kernel lifetime, thus manually releasing isn't necessary.

Quoted 5 lines

Also move of_node_put() for the root node after the usage of its property, since of_node_put() decreases the reference counter thus usage after the call is unsafe.

Fixes 44a01f1f726a ("LoongArch: Parsing CPU-related information from DTS")
Quoted 33 lines
---
 arch/loongarch/kernel/env.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/loongarch/kernel/env.c +7 -4
index 27144de5c5fe..fba141472ab9 100644
@@ -40,15 +40,18 @@ void __init init_environ(void)
 static int __init init_cpu_fullname(void)
 {
      struct device_node *root;
+     const char *model;
      int cpu, ret;
-     char *model;
+     char *tmp;
 
      /* Parsing cpuname from DTS model property */
      root = of_find_node_by_path("/");
-     ret = of_property_read_string(root, "model", (const char **)&model);
+     ret = of_property_read_string(root, "model", &model);
+     if (ret == 0) {
+             tmp = kstrdup(model, GFP_KERNEL);
+             loongson_sysconf.cpuname = strsep(&tmp, " ");
+     }
      of_node_put(root);
-     if (ret == 0)
-             loongson_sysconf.cpuname = strsep(&model, " ");
 
      if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname,
 "Loongson", 8)) {
              for (cpu = 0; cpu < NR_CPUS; cpu++)
Quoted 2 lines

--
2.49.0

--
- Jiaxun