Skip to contentSkip to conversation

A few miscellaneous fixes

PATCH Applied Andrew Jones 3 participants 10 messages Oct 14, 2020 – Nov 11, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover A few miscellaneous fixes

While recently working on kvm-unit-tests I found a few bugs. Posting the patches now since the series I'm working on may not get merged for awhile.

Andrew Jones (3):
lib/string: Fix getenv name matching
scripts: Save rematch before calling out of for_each_unittest arm/arm64: Change dcache_line_size to ulong

 lib/arm/asm/processor.h   | 2 +-
 lib/arm/setup.c           | 2 +-
 lib/arm64/asm/processor.h | 2 +-
 lib/string.c              | 5 ++++-
 scripts/common.bash       | 4 +++-
 5 files changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/3 lib/string: Fix getenv name matching

Without confirming that the name length exactly matches too, then the string comparison would return the same value for VAR* as for VAR, when VAR came first in the environ array.

---
 lib/string.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/string.c +4 -1
index 018dcc879516..3a0562120f12 100644
@@ -171,10 +171,13 @@ extern char **environ;
 char *getenv(const char *name)
 {
     char **envp = environ, *delim;
+    int len;
 
     while (*envp) {
         delim = strchr(*envp, '=');
-        if (delim && strncmp(name, *envp, delim - *envp) == 0)
+        assert(delim);
+        len = delim - *envp;
+        if (strlen(name) == len && strncmp(name, *envp, len) == 0)
             return delim + 1;
         ++envp;
     }
Signature · 1 line
2.26.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH kvm-unit-tests 1/3] lib/string: Fix getenv name matching

Quoted 19 lines · Andrew Jones

Without confirming that the name length exactly matches too, then the string comparison would return the same value for VAR* as for VAR, when VAR came first in the environ array.

---
 lib/string.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/string.c +1 -0
index 018dcc879516..3a0562120f12 100644
@@ -171,10 +171,13 @@ extern char **environ;
 char *getenv(const char *name)
 {
     char **envp = environ, *delim;
+    int len;

lib/string.c extern char **environ; · line 183

Quoted 10 lines
lib/string.c +3 -1
 
     while (*envp) {
         delim = strchr(*envp, '=');
-        if (delim && strncmp(name, *envp, delim - *envp) == 0)
+        assert(delim);
+        len = delim - *envp;
+        if (strlen(name) == len && strncmp(name, *envp, len) == 0)
             return delim + 1;
         ++envp;
     }
maintainer
#3 lore raw

Re: [PATCH kvm-unit-tests 1/3] lib/string: Fix getenv name matching

In reply to Andrew Jones
Quoted 5 lines · Andrew Jones

+ if (strlen(name) == len && strncmp(name, *envp, len) == 0)

             return delim + 1;
         ++envp;
     }

-- 2.26.2

Slightly more efficient:

+ if (memcmp(name, *envp, len) == 0 && !name[len])

(strncmp is not needed since we know there's no NULL between *envp and *envp+len, and after memcmp we know the same about the first len characters of name).

Paolo

#4 lore raw

PATCH 2/3 scripts: Save rematch before calling out of for_each_unittest

In reply to Andrew Jones

If we don't save BASH_REMATCH before calling another function, and that other function also uses [[...]], then we'll lose the test.

---
 scripts/common.bash | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/common.bash +3 -1
index a6044b7c6c35..7b983f7d6dd6 100644
@@ -13,15 +13,17 @@ function for_each_unittest()
 	local check
 	local accel
 	local timeout
+	local rematch
 
 	exec {fd}<"$unittests"
 
 	while read -r -u $fd line; do
 		if [[ "$line" =~ ^\[(.*)\]$ ]]; then
+			rematch=${BASH_REMATCH[1]}
 			if [ -n "${testname}" ]; then
 				$(arch_cmd) "$cmd" "$testname" "$groups" "$smp" "$kernel" "$opts" "$arch" "$check" "$accel" "$timeout"
 			fi
-			testname=${BASH_REMATCH[1]}
+			testname=$rematch
 			smp=1
 			kernel=""
 			opts=""
Signature · 1 line
2.26.2
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH kvm-unit-tests 2/3] scripts: Save rematch before calling out of for_each_unittest

Quoted 23 lines · Andrew Jones

If we don't save BASH_REMATCH before calling another function, and that other function also uses [[...]], then we'll lose the test.

---
 scripts/common.bash | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/common.bash +1 -0
index a6044b7c6c35..7b983f7d6dd6 100644
@@ -13,15 +13,17 @@ function for_each_unittest()
 	local check
 	local accel
 	local timeout
+	local rematch
 
 	exec {fd}<"$unittests"
 
 	while read -r -u $fd line; do

scripts/common.bash function for_each_unittest() · line 29

Quoted 10 lines
scripts/common.bash +2 -1
 		if [[ "$line" =~ ^\[(.*)\]$ ]]; then
+			rematch=${BASH_REMATCH[1]}
 			if [ -n "${testname}" ]; then
 				$(arch_cmd) "$cmd" "$testname" "$groups" "$smp" "$kernel" "$opts" "$arch" "$check" "$accel" "$timeout"
 			fi
-			testname=${BASH_REMATCH[1]}
+			testname=$rematch
 			smp=1
 			kernel=""
 			opts=""
#6 lore raw

PATCH 3/3 arm/arm64: Change dcache_line_size to ulong

In reply to Andrew Jones

dcache_line_size is treated like a long in assembly, so make it one.

---
 lib/arm/asm/processor.h   | 2 +-
 lib/arm/setup.c           | 2 +-
 lib/arm64/asm/processor.h | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
lib/arm/asm/processor.h +1 -1
index e26ef89000a8..273366d1fe1c 100644
@@ -89,6 +89,6 @@ static inline u32 get_ctr(void)
 	return read_sysreg(CTR);
 }
 
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* _ASMARM_PROCESSOR_H_ */
lib/arm/setup.c +1 -1
index 78562e47c01c..ea714d064afa 100644
@@ -42,7 +42,7 @@ static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1];
 struct mem_region *mem_regions = __initial_mem_regions;
 phys_addr_t __phys_offset, __phys_end;
 
-u32 dcache_line_size;
+unsigned long dcache_line_size;
 
 int mpidr_to_cpu(uint64_t mpidr)
 {
lib/arm64/asm/processor.h +1 -1
index 02665b84cc7e..6ee7c1260b6b 100644
@@ -115,7 +115,7 @@ static inline u64 get_ctr(void)
 	return read_sysreg(ctr_el0);
 }
 
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASMARM64_PROCESSOR_H_ */
Signature · 1 line
2.26.2
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH kvm-unit-tests 3/3] arm/arm64: Change dcache_line_size to ulong

Quoted 40 lines · Andrew Jones

dcache_line_size is treated like a long in assembly, so make it one.

---
 lib/arm/asm/processor.h   | 2 +-
 lib/arm/setup.c           | 2 +-
 lib/arm64/asm/processor.h | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
lib/arm/asm/processor.h +1 -1
index e26ef89000a8..273366d1fe1c 100644
@@ -89,6 +89,6 @@ static inline u32 get_ctr(void)
 	return read_sysreg(CTR);
 }
 
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* _ASMARM_PROCESSOR_H_ */
lib/arm/setup.c +1 -1
index 78562e47c01c..ea714d064afa 100644
@@ -42,7 +42,7 @@ static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1];
 struct mem_region *mem_regions = __initial_mem_regions;
 phys_addr_t __phys_offset, __phys_end;
 
-u32 dcache_line_size;
+unsigned long dcache_line_size;
 
 int mpidr_to_cpu(uint64_t mpidr)
 {
lib/arm64/asm/processor.h +0 -0
index 02665b84cc7e..6ee7c1260b6b 100644

lib/arm64/asm/processor.h static inline u64 get_ctr(void) · line 121

Quoted 8 lines
lib/arm64/asm/processor.h +1 -1
@@ -115,7 +115,7 @@ static inline u64 get_ctr(void)
 	return read_sysreg(ctr_el0);
 }
 
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASMARM64_PROCESSOR_H_ */
maintainer Applied
#8 lore raw

Re: [PATCH kvm-unit-tests 3/3] arm/arm64: Change dcache_line_size to ulong

Quoted 43 lines · Thomas Huth
Quoted 42 lines · Andrew Jones

dcache_line_size is treated like a long in assembly, so make it one.

---
 lib/arm/asm/processor.h   | 2 +-
 lib/arm/setup.c           | 2 +-
 lib/arm64/asm/processor.h | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
lib/arm/asm/processor.h +1 -1
index e26ef89000a8..273366d1fe1c 100644
@@ -89,6 +89,6 @@ static inline u32 get_ctr(void)
 	return read_sysreg(CTR);
 }
 
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* _ASMARM_PROCESSOR_H_ */
lib/arm/setup.c +1 -1
index 78562e47c01c..ea714d064afa 100644
@@ -42,7 +42,7 @@ static struct mem_region __initial_mem_regions[NR_INITIAL_MEM_REGIONS + 1];
 struct mem_region *mem_regions = __initial_mem_regions;
 phys_addr_t __phys_offset, __phys_end;
 
-u32 dcache_line_size;
+unsigned long dcache_line_size;
 
 int mpidr_to_cpu(uint64_t mpidr)
 {
lib/arm64/asm/processor.h +0 -0
index 02665b84cc7e..6ee7c1260b6b 100644
@@ -115,7 +115,7 @@ static inline u64 get_ctr(void)
 	return read_sysreg(ctr_el0);
 }
 
Quoted 9 lines

lib/arm64/asm/processor.h static inline u64 get_ctr(void) · line 121

Quoted 6 lines
lib/arm64/asm/processor.h +1 -1
-extern u32 dcache_line_size;
+extern unsigned long dcache_line_size;
 
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASMARM64_PROCESSOR_H_ */

Queued all three, thanks.

Paolo

#9 lore raw

Re: [PATCH kvm-unit-tests 3/3] arm/arm64: Change dcache_line_size to ulong

Quoted 3 lines · Paolo Bonzini

Queued all three, thanks.

Hi Paolo,

Can you please push these?

Thanks,
drew