LoongArch: BPF: Fix unconditional bswap instructions
authorTiezhu Yang <yangtiezhu@loongson.cn>
Sat, 9 Dec 2023 07:49:16 +0000 (15:49 +0800)
committerHuacai Chen <chenhuacai@loongson.cn>
Sat, 9 Dec 2023 07:49:16 +0000 (15:49 +0800)
We can see that "bswap32: Takes an unsigned 32-bit number in either big-
or little-endian format and returns the equivalent number with the same
bit width but opposite endianness" in BPF Instruction Set Specification,
so it should clear the upper 32 bits in "case 32:" for both BPF_ALU and
BPF_ALU64.

[root@linux fedora]# echo 1 > /proc/sys/net/core/bpf_jit_enable
[root@linux fedora]# modprobe test_bpf

Before:
test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 ret 1460850314 != -271733879 (0x5712ce8a != 0xefcdab89)FAIL (1 times)
test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 ret -1460850316 != 271733878 (0xa8ed3174 != 0x10325476)FAIL (1 times)

After:
test_bpf: #313 BSWAP 32: 0x0123456789abcdef -> 0xefcdab89 jited:1 4 PASS
test_bpf: #317 BSWAP 32: 0xfedcba9876543210 -> 0x10325476 jited:1 4 PASS

Fixes: 4ebf9216e7df ("LoongArch: BPF: Support unconditional bswap instructions")
Acked-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
arch/loongarch/net/bpf_jit.c

index cd002f1..4fcd6cd 100644 (file)
@@ -774,8 +774,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
                        break;
                case 32:
                        emit_insn(ctx, revb2w, dst, dst);
-                       /* zero-extend 32 bits into 64 bits */
-                       emit_zext_32(ctx, dst, is32);
+                       /* clear the upper 32 bits */
+                       emit_zext_32(ctx, dst, true);
                        break;
                case 64:
                        emit_insn(ctx, revbd, dst, dst);