Skip to contentSkip to conversation

powerpc/dt_cpu_ftrs: Avoid separate strlen() in scan_callback()

PATCH Superseded Thorsten Blum 2 participants 3 messages Jun 30 – Jul 1 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/dt_cpu_ftrs: Avoid separate strlen() in scan_callback()

Use the return value of strscpy() when copying the display name instead of checking the source string length with strlen() first.

Keep dt_cpu_name static but move it into dt_cpu_ftrs_scan_callback(), where it is assigned.

---
 arch/powerpc/kernel/dt_cpu_ftrs.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/dt_cpu_ftrs.c +2 -5
index 3af6c06af02f..8d8e94e4d2bc 100644
@@ -90,8 +90,6 @@ static void __restore_cpu_cpufeatures(void)
 		init_pmu_registers();
 }
 
-static char dt_cpu_name[64];
-
 static struct cpu_spec __initdata base_cpu_spec = {
 	.cpu_name		= NULL,
 	.cpu_features		= CPU_FTRS_DT_CPU_BASE,
@@ -1069,6 +1067,7 @@ static int __init count_cpufeatures_subnodes(unsigned long node,
 static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 					    *uname, int depth, void *data)
 {
+	static char dt_cpu_name[64];
 	const __be32 *prop;
 	int count, i;
 	u32 isa;
@@ -1106,10 +1105,8 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 	}
 
 	prop = of_get_flat_dt_prop(node, "display-name", NULL);
-	if (prop && strlen((char *)prop) != 0) {
-		strscpy(dt_cpu_name, (char *)prop, sizeof(dt_cpu_name));
+	if (prop && strscpy(dt_cpu_name, (char *)prop) != 0)
 		cur_cpu_spec->cpu_name = dt_cpu_name;
-	}
 
 	cpufeatures_setup_finished();
 
#1 lore raw
Quoted 33 lines · On Tue, 30 Jun 2026 17:46:56 +0200 Thorsten Blum <[email protected]>…

Use the return value of strscpy() when copying the display name instead of checking the source string length with strlen() first.

Keep dt_cpu_name static but move it into dt_cpu_ftrs_scan_callback(), where it is assigned.

---
 arch/powerpc/kernel/dt_cpu_ftrs.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -2
index 3af6c06af02f..8d8e94e4d2bc 100644
@@ -90,8 +90,6 @@ static void __restore_cpu_cpufeatures(void)
 		init_pmu_registers();
 }
 
-static char dt_cpu_name[64];
-
 static struct cpu_spec __initdata base_cpu_spec = {
 	.cpu_name		= NULL,
 	.cpu_features		= CPU_FTRS_DT_CPU_BASE,
@@ -1069,6 +1067,7 @@ static int __init count_cpufeatures_subnodes(unsigned long node,
 static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 					    *uname, int depth, void *data)
 {
+	static char dt_cpu_name[64];
 	const __be32 *prop;
 	int count, i;
 	u32 isa;

arch/powerpc/kernel/dt_cpu_ftrs.c static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char · line 1108

Quoted 6 lines
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -2
@@ -1106,10 +1105,8 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 	}
 
 	prop = of_get_flat_dt_prop(node, "display-name", NULL);
-	if (prop && strlen((char *)prop) != 0) {
-		strscpy(dt_cpu_name, (char *)prop, sizeof(dt_cpu_name));
+	if (prop && strscpy(dt_cpu_name, (char *)prop) != 0)

That is different code.
The old code only did the copy if the new string was non-empty. So you want to replace the strlen() with *(char *)prop != 0. Checks for strlen() == 0 are silly (but surprisingly common). A search might show up this gem:

	str[strlen(str)] = 0;

-- David

Quoted 6 lines
arch/powerpc/kernel/dt_cpu_ftrs.c +0 -1
 		cur_cpu_spec->cpu_name = dt_cpu_name;
-	}
 
 	cpufeatures_setup_finished();
 
#2 lore raw
Quoted 38 lines · David Laight
Quoted 35 lines · On Tue, 30 Jun 2026 17:46:56 +0200 Thorsten Blum <[email protected]>…

Use the return value of strscpy() when copying the display name instead of checking the source string length with strlen() first.

Keep dt_cpu_name static but move it into dt_cpu_ftrs_scan_callback(), where it is assigned.

---
 arch/powerpc/kernel/dt_cpu_ftrs.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -2
index 3af6c06af02f..8d8e94e4d2bc 100644
@@ -90,8 +90,6 @@ static void __restore_cpu_cpufeatures(void)
 		init_pmu_registers();
 }
 
-static char dt_cpu_name[64];
-
 static struct cpu_spec __initdata base_cpu_spec = {
 	.cpu_name		= NULL,
 	.cpu_features		= CPU_FTRS_DT_CPU_BASE,
@@ -1069,6 +1067,7 @@ static int __init count_cpufeatures_subnodes(unsigned long node,
 static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 					    *uname, int depth, void *data)
 {
+	static char dt_cpu_name[64];
 	const __be32 *prop;
 	int count, i;
 	u32 isa;
@@ -1106,10 +1105,8 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
 	}
 
Quoted 7 lines

arch/powerpc/kernel/dt_cpu_ftrs.c static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char · line 1108

Quoted 4 lines
arch/powerpc/kernel/dt_cpu_ftrs.c +1 -2
 	prop = of_get_flat_dt_prop(node, "display-name", NULL);
-	if (prop && strlen((char *)prop) != 0) {
-		strscpy(dt_cpu_name, (char *)prop, sizeof(dt_cpu_name));
+	if (prop && strscpy(dt_cpu_name, (char *)prop) != 0)

That is different code.
The old code only did the copy if the new string was non-empty.

Good catch, thanks!

I just sent a v2:
https://lore.kernel.org/lkml/[email protected]/