Skip to contentSkip to conversation

getcpu01: Fix strtoul incorrectly returns 0

PATCH In review Ping Fang 2 participants 5 messages Mar 20, 2023 – Mar 22, 2023

lore
Share & tools
lore

Ping Fang

author
#0 lore raw

PATCH 1/1 getcpu01: Fix strtoul incorrectly returns 0

get_nodeid() find nodeid by iterate /sys/devices/system/node/nodeX/cpuY. But there are cpulist and cpumap, which will report 0 in strtoul. On multi-node numa system, the last iteration nodeX directory mismatch the cpu0 node id. Then fail the test. The following shows on a 4 node numa system. The last iteration stopped under /node1. so expected node id 1. Then failed.

getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node2 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node0 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node3 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node1 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:128: TFAIL: getcpu() returned wrong value expected node id:1 returned node id:0, cpu_set:0

---
 testcases/kernel/syscalls/getcpu/getcpu01.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/getcpu/getcpu01.c +4 -1
index f6fcc4fc1..9842c8999 100644
@@ -69,6 +69,7 @@ static unsigned int get_nodeid(unsigned int cpu_id)
 	DIR *directory_parent, *directory_node;
 	struct dirent *de, *dn;
 	char directory_path[PATH_MAX];
+	char *invalid_number;
 	unsigned int cpu;
 	int node_id = 0;
 
@@ -91,7 +92,9 @@ static unsigned int get_nodeid(unsigned int cpu_id)
 			while ((dn = readdir(directory_node)) != NULL) {
 				if (strncmp(dn->d_name, "cpu", 3))
 					continue;
-				cpu = strtoul(dn->d_name + 3, NULL, 0);
+				cpu = strtoul(dn->d_name + 3, &invalid_number, 0);
+				if (strcmp(invalid_number, "\0"))
+					continue;
 				if (cpu == cpu_id) {
 					node_id =
 					    strtoul(de->d_name + 4, NULL, 0);
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Ping,

Good catch!

Quoted 38 lines · Ping Fang

get_nodeid() find nodeid by iterate /sys/devices/system/node/nodeX/cpuY. But there are cpulist and cpumap, which will report 0 in strtoul. On multi-node numa system, the last iteration nodeX directory mismatch the cpu0 node id. Then fail the test. The following shows on a 4 node numa system. The last iteration stopped under /node1. so expected node id 1. Then failed.

getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node2 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node0 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node3 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node1 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:128: TFAIL: getcpu() returned wrong value expected node id:1 returned node id:0, cpu_set:0

---
 testcases/kernel/syscalls/getcpu/getcpu01.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/getcpu/getcpu01.c +1 -0
index f6fcc4fc1..9842c8999 100644
@@ -69,6 +69,7 @@ static unsigned int get_nodeid(unsigned int cpu_id)
        DIR *directory_parent, *directory_node;
        struct dirent *de, *dn;
        char directory_path[PATH_MAX];
+       char *invalid_number;
        unsigned int cpu;
        int node_id = 0;
 

testcases/kernel/syscalls/getcpu/getcpu01.c static unsigned int get_nodeid(unsigned int cpu_id) · line 99

Quoted 9 lines
testcases/kernel/syscalls/getcpu/getcpu01.c +3 -1
@@ -91,7 +92,9 @@ static unsigned int get_nodeid(unsigned int cpu_id)
                        while ((dn = readdir(directory_node)) != NULL) {
                                if (strncmp(dn->d_name, "cpu", 3))
                                        continue;
-                               cpu = strtoul(dn->d_name + 3, NULL, 0);
+                               cpu = strtoul(dn->d_name + 3,
 &invalid_number, 0);
+                               if (strcmp(invalid_number, "\0"))
+                                       continue;
 

Why only check if **endptr is '\0' on return? Shouldn't we consider a more generic situation that was no digits and *endptr stores the original string (or returns 0)?

Quoted 12 lines
testcases/kernel/syscalls/getcpu/getcpu01.c +0 -0
                                if (cpu == cpu_id) {
                                        node_id =
                                            strtoul(de->d_name + 4, NULL,

0);
--
2.31.1

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw
Quoted 43 lines · Li Wang

Hi Ping,

Good catch!

Quoted 37 lines · Ping Fang

get_nodeid() find nodeid by iterate /sys/devices/system/node/nodeX/cpuY. But there are cpulist and cpumap, which will report 0 in strtoul. On multi-node numa system, the last iteration nodeX directory mismatch the cpu0 node id. Then fail the test. The following shows on a 4 node numa system. The last iteration stopped under /node1. so expected node id 1. Then failed.

getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node2 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node0 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node3 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node1 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:128: TFAIL: getcpu() returned wrong value expected node id:1 returned node id:0, cpu_set:0

---
 testcases/kernel/syscalls/getcpu/getcpu01.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/getcpu/getcpu01.c +1 -0
index f6fcc4fc1..9842c8999 100644
@@ -69,6 +69,7 @@ static unsigned int get_nodeid(unsigned int cpu_id)
        DIR *directory_parent, *directory_node;
        struct dirent *de, *dn;
        char directory_path[PATH_MAX];
+       char *invalid_number;
        unsigned int cpu;
        int node_id = 0;
 
Quoted 15 lines

testcases/kernel/syscalls/getcpu/getcpu01.c static unsigned int get_nodeid(unsigned int cpu_id) · line 99

Quoted 10 lines
testcases/kernel/syscalls/getcpu/getcpu01.c +3 -1
@@ -91,7 +92,9 @@ static unsigned int get_nodeid(unsigned int cpu_id)
                        while ((dn = readdir(directory_node)) != NULL) {
                                if (strncmp(dn->d_name, "cpu", 3))
                                        continue;
-                               cpu = strtoul(dn->d_name + 3, NULL, 0);
+                               cpu = strtoul(dn->d_name + 3,
 &invalid_number, 0);
+                               if (strcmp(invalid_number, "\0"))
+                                       continue;
 

Why only check if **endptr is '\0' on return? Shouldn't we consider a more generic situation that was no digits and *endptr stores the original string (or returns 0)?

The strtoul will try to convert string into unsigned long integer. If successful the endptr will point to a "\0" string return the number, otherwise to the original string and return 0.
In the case of /sys/devices/system/node/node1/cpulist, endptr is "list\0" and return 0. The strcmp false skip to the next iteration.

Quoted 19 lines
Quoted 5 lines
testcases/kernel/syscalls/getcpu/getcpu01.c +0 -0
                                if (cpu == cpu_id) {
                                        node_id =
                                            strtoul(de->d_name + 4, NULL,
Quoted 9 lines

0);
--
2.31.1

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Regards,
Li Wang

Signature · 6 lines
*--*
Thanks & Regards,
*Ping Fang*

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#3 lore raw
Quoted 64 lines · Ping Fang
Quoted 43 lines · Li Wang

Hi Ping,

Good catch!

Quoted 37 lines · Ping Fang

get_nodeid() find nodeid by iterate /sys/devices/system/node/nodeX/cpuY. But there are cpulist and cpumap, which will report 0 in strtoul. On multi-node numa system, the last iteration nodeX directory mismatch the cpu0 node id. Then fail the test. The following shows on a 4 node numa system. The last iteration stopped under /node1. so expected node id 1. Then failed.

getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node2 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node0 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node3 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:98: TINFO: cpu:0, cpu_id:0, de->d_name:node1 getcpu01.c:99: TINFO: dn->d_name:cpulist
getcpu01.c:128: TFAIL: getcpu() returned wrong value expected node id:1 returned node id:0, cpu_set:0

---
 testcases/kernel/syscalls/getcpu/getcpu01.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/getcpu/getcpu01.c +1 -0
index f6fcc4fc1..9842c8999 100644
@@ -69,6 +69,7 @@ static unsigned int get_nodeid(unsigned int cpu_id)
        DIR *directory_parent, *directory_node;
        struct dirent *de, *dn;
        char directory_path[PATH_MAX];
+       char *invalid_number;
        unsigned int cpu;
        int node_id = 0;
 
Quoted 15 lines

testcases/kernel/syscalls/getcpu/getcpu01.c static unsigned int get_nodeid(unsigned int cpu_id) · line 99

Quoted 10 lines
testcases/kernel/syscalls/getcpu/getcpu01.c +3 -1
@@ -91,7 +92,9 @@ static unsigned int get_nodeid(unsigned int cpu_id)
                        while ((dn = readdir(directory_node)) != NULL) {
                                if (strncmp(dn->d_name, "cpu", 3))
                                        continue;
-                               cpu = strtoul(dn->d_name + 3, NULL, 0);
+                               cpu = strtoul(dn->d_name + 3,
 &invalid_number, 0);
+                               if (strcmp(invalid_number, "\0"))
+                                       continue;
 

Why only check if **endptr is '\0' on return? Shouldn't we consider a more generic situation that was no digits and *endptr stores the original string (or returns 0)?

Quoted 6 lines

The strtoul will try to convert string into unsigned long integer. If successful the endptr will point to a "\0" string return the number, otherwise to the original string and return 0.
In the case of /sys/devices/system/node/node1/cpulist, endptr is "list\0" and return 0. The strcmp false skip to the next iteration.

Ah, you're right. Here we're expecting to get a valid cpuid, the 'dn->d_name + 3' should be strictly equal to "N\0".

Thanks for the explanation.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Pushed, thanks!

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp