Skip to contentSkip to conversation

arm64: bpf: zero upper bits after rev32

PATCH In review Artem Savkov 7 participants 10 messages Mar 13, 2024 – Mar 21, 2024

Subsystems (5)
Networking Network drivers BPF arm64 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: bpf: zero upper bits after rev32

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")
---
 arch/arm64/net/bpf_jit_comp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +2 -1
index c5b461dda4385..e86e5ba74dca2 100644
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
 			break;
 		case 32:
 			emit(A64_REV32(is64, dst, dst), ctx);
-			/* upper 32 bits already cleared */
+			/* zero-extend 32 bits into 64 bits */
+			emit(A64_UXTW(is64, dst, dst), ctx);
 			break;
 		case 64:
 			emit(A64_REV64(dst, dst), ctx);
Signature · 1 line
2.44.0
#1 lore raw
Quoted 26 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")
---
 arch/arm64/net/bpf_jit_comp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
                        break;
                case 32:
                        emit(A64_REV32(is64, dst, dst), ctx);
-                       /* upper 32 bits already cleared */
+                       /* zero-extend 32 bits into 64 bits */
+                       emit(A64_UXTW(is64, dst, dst), ctx);

What does arm64 ISA say about rev32?
Since rev16 is special, it kinda makes sense, but still.

Puranjay,
could you please help review this fix?

reviewer
#2 lore raw
In reply to Artem Savkov
Quoted 25 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")
---
  arch/arm64/net/bpf_jit_comp.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  			break;
  		case 32:
  			emit(A64_REV32(is64, dst, dst), ctx);
-			/* upper 32 bits already cleared */
+			/* zero-extend 32 bits into 64 bits */
+			emit(A64_UXTW(is64, dst, dst), ctx);

I think the problem only occurs when is64 == 1. In this case, the generated rev32 insn reverses byte order in both high and low 32-bit word. To fix it, we could just set the first arg to 0 for A64_REV32:

emit(A64_REV32(0, dst, dst), ctx);

No need to add an extra uxtw isnn.

Quoted 3 lines
arch/arm64/net/bpf_jit_comp.c +0 -0
  			break;
  		case 64:
  			emit(A64_REV64(dst, dst), ctx);
#3 lore raw
Quoted 37 lines · Xu Kuohai
Quoted 25 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")
---
  arch/arm64/net/bpf_jit_comp.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  			break;
  		case 32:
  			emit(A64_REV32(is64, dst, dst), ctx);
-			/* upper 32 bits already cleared */
+			/* zero-extend 32 bits into 64 bits */
+			emit(A64_UXTW(is64, dst, dst), ctx);

I think the problem only occurs when is64 == 1. In this case, the generated rev32 insn reverses byte order in both high and low 32-bit word. To fix it, we could just set the first arg to 0 for A64_REV32:

Quoted 3 lines

emit(A64_REV32(0, dst, dst), ctx);

No need to add an extra uxtw isnn.

I can confirm this approach fixes the test issue as well.

Quoted 6 lines
Quoted 4 lines
arch/arm64/net/bpf_jit_comp.c +0 -0
  			break;
  		case 64:
  			emit(A64_REV64(dst, dst), ctx);
Signature · 1 line
 Artem
Tested-by Acked-by
#4 lore raw
Quoted 38 lines · Artem Savkov
Quoted 37 lines · Xu Kuohai
Quoted 25 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")
---
  arch/arm64/net/bpf_jit_comp.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  			break;
  		case 32:
  			emit(A64_REV32(is64, dst, dst), ctx);
-			/* upper 32 bits already cleared */
+			/* zero-extend 32 bits into 64 bits */
+			emit(A64_UXTW(is64, dst, dst), ctx);

I think the problem only occurs when is64 == 1. In this case, the generated rev32 insn reverses byte order in both high and low 32-bit word. To fix it, we could just set the first arg to 0 for A64_REV32:

Quoted 5 lines
Quoted 3 lines

emit(A64_REV32(0, dst, dst), ctx);

No need to add an extra uxtw isnn.

I can confirm this approach fixes the test issue as well.

Yes, the following diff fixes the issue:

arch/arm64/net/bpf_jit_comp.c +1 -1
index bc16eb694..64deff221 100644
@@ -943,7 +943,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
                        emit(A64_UXTH(is64, dst, dst), ctx);
                        break;
                case 32:
-                       emit(A64_REV32(is64, dst, dst), ctx);
+                       emit(A64_REV32(0, dst, dst), ctx);
                        /* upper 32 bits already cleared */
                        break;
                case 64:

All tests pass with this change:

test_bpf: Summary: 1049 PASSED, 0 FAILED, [1037/1037 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]
test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED

When you send a patch please add:

Thanks,
Puranjay

#5 lore raw
In reply to Artem Savkov
Quoted 14 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Quoted 1 line
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")

This tag is not right. It's unlikely that the bug has been around for 9 years.

Maybe you meant 1104247f3f979 ("bpf, arm64: Support unconditional bswap")?

Quoted 10 lines
---
 arch/arm64/net/bpf_jit_comp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
                        break;
                case 32:
                        emit(A64_REV32(is64, dst, dst), ctx);
-                       /* upper 32 bits already cleared */
+                       /* zero-extend 32 bits into 64 bits */
+                       emit(A64_UXTW(is64, dst, dst), ctx);

The fix can pass the tests, but emitting an extra instruction is unnecessary as the bug applies only to unconditional bswap.

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +0 -1
                        break;
                case 64:
                        emit(A64_REV64(dst, dst), ctx);
--

2.44.0

#6 lore raw
Quoted 15 lines · Xi Wang
Quoted 14 lines · Artem Savkov

Commit d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs") added upper bits zeroing to byteswap operations, but it assumes they will be already zeroed after rev32, which is not the case on some systems at least:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Quoted 6 lines
Quoted 1 line
Fixes d63903bbc30c7 ("arm64: bpf: fix endianness conversion bugs")

This tag is not right. It's unlikely that the bug has been around for 9 years.

Maybe you meant 1104247f3f979 ("bpf, arm64: Support unconditional bswap")?

Agree, thanks for pointing it out.

Quoted 27 lines
Quoted 10 lines
---
  arch/arm64/net/bpf_jit_comp.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..e86e5ba74dca2 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 948

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +2 -1
@@ -944,7 +944,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
                         break;
                 case 32:
                         emit(A64_REV32(is64, dst, dst), ctx);
-                       /* upper 32 bits already cleared */
+                       /* zero-extend 32 bits into 64 bits */
+                       emit(A64_UXTW(is64, dst, dst), ctx);

The fix can pass the tests, but emitting an extra instruction is unnecessary as the bug applies only to unconditional bswap.

Quoted 6 lines
arch/arm64/net/bpf_jit_comp.c +0 -1
                         break;
                 case 64:
                         emit(A64_REV64(dst, dst), ctx);
--

2.44.0

#7 lore raw

PATCH v2 1/1 arm64: bpf: fix 32bit unconditional bswap

In reply to Artem Savkov

In case when is64 == 1 in emit(A64_REV32(is64, dst, dst), ctx) the generated insn reverses byte order for both high and low 32-bit words, resuling in an incorrect swap as indicated by the jit test:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS

Fix this by forcing 32bit variant of rev32.

Fixes 1104247f3f979 ("bpf, arm64: Support unconditional bswap")
---
 arch/arm64/net/bpf_jit_comp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +1 -1
index c5b461dda4385..c3ededd23cbf6 100644
@@ -943,7 +943,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
 			emit(A64_UXTH(is64, dst, dst), ctx);
 			break;
 		case 32:
-			emit(A64_REV32(is64, dst, dst), ctx);
+			emit(A64_REV32(0, dst, dst), ctx);
 			/* upper 32 bits already cleared */
 			break;
 		case 64:
Signature · 1 line
2.44.0
reviewer Acked-by on v2 1/1
#8 lore raw

Re: [PATCH bpf-next v2] arm64: bpf: fix 32bit unconditional bswap

Quoted 28 lines · Artem Savkov

In case when is64 == 1 in emit(A64_REV32(is64, dst, dst), ctx) the generated insn reverses byte order for both high and low 32-bit words, resuling in an incorrect swap as indicated by the jit test:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS

Fix this by forcing 32bit variant of rev32.

Fixes 1104247f3f979 ("bpf, arm64: Support unconditional bswap")
---
  arch/arm64/net/bpf_jit_comp.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/net/bpf_jit_comp.c +0 -0
index c5b461dda4385..c3ededd23cbf6 100644

arch/arm64/net/bpf_jit_comp.c static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, · line 949

Quoted 8 lines
arch/arm64/net/bpf_jit_comp.c +1 -1
@@ -943,7 +943,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  			emit(A64_UXTH(is64, dst, dst), ctx);
  			break;
  		case 32:
-			emit(A64_REV32(is64, dst, dst), ctx);
+			emit(A64_REV32(0, dst, dst), ctx);
  			/* upper 32 bits already cleared */
  			break;
  		case 64:
#9 lore raw

Re: [PATCH bpf-next v2] arm64: bpf: fix 32bit unconditional bswap

In reply to Artem Savkov
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 13 lines · On Thu, 21 Mar 2024 09:18:09 +0100 you wrote:

In case when is64 == 1 in emit(A64_REV32(is64, dst, dst), ctx) the generated insn reverses byte order for both high and low 32-bit words, resuling in an incorrect swap as indicated by the jit test:

[ 9757.262607] test_bpf: #312 BSWAP 16: 0x0123456789abcdef -> 0xefcd jited:1 8 PASS
[ 9757.264435] test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
[ 9757.266260] test_bpf: #314 BSWAP 64: 0x0123456789abcdef -> 0x67452301 jited:1 8 PASS
[ 9757.268000] test_bpf: #315 BSWAP 64: 0x0123456789abcdef >> 32 -> 0xefcdab89 jited:1 8 PASS
[ 9757.269686] test_bpf: #316 BSWAP 16: 0xfedcba9876543210 -> 0x1032 jited:1 8 PASS
[ 9757.271380] test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)
[ 9757.273022] test_bpf: #318 BSWAP 64: 0xfedcba9876543210 -> 0x98badcfe jited:1 7 PASS
[ 9757.274721] test_bpf: #319 BSWAP 64: 0xfedcba9876543210 >> 32 -> 0x10325476 jited:1 9 PASS
Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next,v2] arm64: bpf: fix 32bit unconditional bswap

    https://git.kernel.org/bpf/bpf/c/a51cd6bf8e10

You are awesome, thank you!

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