Skip to contentSkip to conversation

riscv, lib: Fix Zbb strncmp

PATCH Applied Björn Töpel 5 participants 7 messages Feb 28, 2023 – Mar 1, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv, lib: Fix Zbb strncmp

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

  strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

  strncmp("/dev/vda", "/dev/vda\0", 8);

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")
---
 arch/riscv/lib/strncmp.S | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/lib/strncmp.S +3 -1
index ee49595075be..efd3f3150a54 100644
@@ -78,11 +78,13 @@ strncmp_zbb:
 	/* Main loop for aligned string.  */
 	.p2align 3
 1:
-	bgt	a0, t6, 3f
+	bge	a0, t6, 3f
 	REG_L	t0, 0(a0)
 	REG_L	t1, 0(a1)
 	orc.b	t3, t0
 	bne	t3, t5, 2f
+	orc.b	t3, t1
+	bne	t3, t5, 2f
 	addi	a0, a0, SZREG
 	addi	a1, a1, SZREG
 	beq	t0, t1, 1b

base-commit: eb9be8310c58c166f9fae3b71c0ad9d6741b4897

Signature · 7 lines
2.37.2


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Tested-by on v1 1/1
#1 lore raw
Quoted 22 lines · Björn Töpel

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

  strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

Quoted 7 lines
  strncmp("/dev/vda", "/dev/vda\0", 8);

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")

With your new selftest stuff & on a JH7110:

Also, given the reporter, this should be:

no?

Thanks,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#2 lore raw
Quoted 31 lines · Conor Dooley
Quoted 22 lines · Björn Töpel

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

  strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

Quoted 7 lines
  strncmp("/dev/vda", "/dev/vda\0", 8);

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")
Quoted 5 lines

With your new selftest stuff & on a JH7110:

Also, given the reporter, this should be:

no?

Indeed. Sorry, Guenter, for missing that!

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw
In reply to Conor Dooley
Quoted 31 lines · Conor Dooley
Quoted 22 lines · Björn Töpel

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

   strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

Quoted 7 lines
   strncmp("/dev/vda", "/dev/vda\0", 8);

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")
Quoted 6 lines

With your new selftest stuff & on a JH7110:

Also, given the reporter, this should be:

no?

No worries. I'll give the patch a try.

Guenter

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Tested-by on v1 1/1
#4 lore raw
In reply to Björn Töpel
Quoted 22 lines · Björn Töpel

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

   strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

Quoted 7 lines
   strncmp("/dev/vda", "/dev/vda\0", 8);

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")
Quoted 24 lines
---
  arch/riscv/lib/strncmp.S | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/lib/strncmp.S +3 -1
index ee49595075be..efd3f3150a54 100644
@@ -78,11 +78,13 @@ strncmp_zbb:
  	/* Main loop for aligned string.  */
  	.p2align 3
  1:
-	bgt	a0, t6, 3f
+	bge	a0, t6, 3f
  	REG_L	t0, 0(a0)
  	REG_L	t1, 0(a1)
  	orc.b	t3, t0
  	bne	t3, t5, 2f
+	orc.b	t3, t1
+	bne	t3, t5, 2f
  	addi	a0, a0, SZREG
  	addi	a1, a1, SZREG
  	beq	t0, t1, 1b
Quoted 1 line

base-commit: eb9be8310c58c166f9fae3b71c0ad9d6741b4897

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#5 lore raw
Quoted 25 lines · On Tue, 28 Feb 2023 13:18:12 PST (-0800), [email protected] wrote:
Quoted 24 lines · Björn Töpel

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

The Zbb strncmp has two issues in the fast path:

Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example:

   strncmp("/dev/vda", "/dev/", 5);

Correct by changing "bgt" to "bge".

Missing NULL checks in the second string: This could lead to incorrect results for:

   strncmp("/dev/vda", "/dev/vda\0", 8);
Quoted 7 lines
Quoted 5 lines

Correct by adding an additional check.

Fixes b6fcdb191e36 ("RISC-V: add zbb support to string functions")

Thanks guys, I'm going to apply this ASAP as it's a pretty concrete breakage in new code from this merge window. I just stuck it in my staging branch, it should show up on for-next after the next batch of tests.

Quoted 25 lines
Quoted 25 lines
---
  arch/riscv/lib/strncmp.S | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/lib/strncmp.S +3 -1
index ee49595075be..efd3f3150a54 100644
@@ -78,11 +78,13 @@ strncmp_zbb:
  	/* Main loop for aligned string.  */
  	.p2align 3
  1:
-	bgt	a0, t6, 3f
+	bge	a0, t6, 3f
  	REG_L	t0, 0(a0)
  	REG_L	t1, 0(a1)
  	orc.b	t3, t0
  	bne	t3, t5, 2f
+	orc.b	t3, t1
+	bne	t3, t5, 2f
  	addi	a0, a0, SZREG
  	addi	a1, a1, SZREG
  	beq	t0, t1, 1b
Quoted 1 line
Quoted 1 line

base-commit: eb9be8310c58c166f9fae3b71c0ad9d6741b4897

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw
In reply to Björn Töpel
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 9 lines · On Tue, 28 Feb 2023 19:42:10 +0100 you wrote:

From: Björn Töpel <[email protected]>

The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration.

The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path.

[...]

Here is the summary with links:
- riscv, lib: Fix Zbb strncmp

    https://git.kernel.org/riscv/c/81a1dd10b072

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv