Skip to content

[PW_SID:1087459] riscv, bpf: Fix signed operations and add 32 bit atomics#1840

Closed
linux-riscv-bot wants to merge 5 commits into
workflow__riscv__fixesfrom
pw1087459
Closed

[PW_SID:1087459] riscv, bpf: Fix signed operations and add 32 bit atomics#1840
linux-riscv-bot wants to merge 5 commits into
workflow__riscv__fixesfrom
pw1087459

Conversation

@linux-riscv-bot
Copy link
Copy Markdown

PR for series 1087459 applied to workflow__riscv__fixes

Name: riscv, bpf: Fix signed operations and add 32 bit atomics
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1087459
Version: 1

nathanchance and others added 5 commits April 24, 2026 07:25
…_TYPED_FUNC_START

After commit 67bdd7b ("riscv: Split out measure_cycles() for
reuse") and commit c03ad15 ("riscv: Reuse measure_cycles() in
check_vector_unaligned_access()"), there are CFI failure when booting
kernels with CONFIG_CFI=y:

  CFI failure at measure_cycles+0x38/0xe0 (target: __riscv_copy_words_unaligned+0x0/0x50; expected type: ...)
  CFI failure at measure_cycles+0x38/0xe0 (target: __riscv_copy_vec_words_unaligned+0x0/0x24; expected type: ...)

The __riscv_copy_*_unaligned() functions are now called indirectly but
they are not defined with SYM_TYPED_FUNC_START, which is required for
assembly functions called indirectly from C to pass CFI checking. Switch
to SYM_TYPED_FUNC_START to clear up the CFI failures.

Fixes: 67bdd7b ("riscv: Split out measure_cycles() for reuse")
Fixes: c03ad15 ("riscv: Reuse measure_cycles() in check_vector_unaligned_access()")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Nam Cao <namcao@linutronix.de>
Link: https://patch.msgid.link/20260406-measure_cycles-cfi-failure-v1-1-03e0234ae02f@kernel.org
Signed-off-by: Paul Walmsley <pjw@kernel.org>
The current rv32 bpf jit compiler incorrectly treats BPF_SDIV and
BPF_SMOD as unsigned operations. The BPF instruction set allows
signed division and modulo by reusing the BPF_DIV and BPF_MOD opcodes
with the instruction offset set to 1.

Update the emit_alu_r32() function to accept an 'is_sdiv' variable and
emit the correct div and rem instructions when the offset is 1.

Before this patch:
[   44.161771] test_bpf: #165 ALU_SDIV_X: -6 / 2 = -3 jited:1 ret 2147483645 != -3 (0x7ffffffd != 0xfffffffd)FAIL (1 times)
[   44.167385] test_bpf: #166 ALU_SDIV_K: -6 / 2 = -3 jited:1 ret 2147483645 != -3 (0x7ffffffd != 0xfffffffd)FAIL (1 times)
[   44.171053] test_bpf: #169 ALU_SMOD_X: -7 % 2 = -1 jited:1 ret 1 != -1 (0x1 != 0xffffffff)FAIL (1 times)
[   44.172081] test_bpf: #170 ALU_SMOD_K: -7 % 2 = -1 jited:1 ret 1 != -1 (0x1 != 0xffffffff)FAIL (1 times)

After this patch:
[   16.002192] test_bpf: #165 ALU_SDIV_X: -6 / 2 = -3 jited:1 95 PASS
[   16.002983] test_bpf: #166 ALU_SDIV_K: -6 / 2 = -3 jited:1 1059 PASS
[   16.017167] test_bpf: #169 ALU_SMOD_X: -7 % 2 = -1 jited:1 136 PASS
[   16.023002] test_bpf: #170 ALU_SMOD_K: -7 % 2 = -1 jited:1 109 PASS

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The current rv32 bpf jit compiler incorrectly treats BPF_MOVSX as a
standard zero-extended move operation. The bpf instruction set allows
sign-extension moves by reusing the BPF_MOV opcode with the instruction
offset set to 8, 16, or 32.

Update the bpf_jit_emit_insn() function to check the offset field for
both ALU and ALU64 MOV operations. If the offset is non-zero, emit the
correct slli and srai instructions to perform the sign extension.

Before this patch:
[   19.549705] test_bpf: #82 ALU_MOVSX | BPF_B jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times)
[   19.551354] test_bpf: #83 ALU_MOVSX | BPF_H jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times)
[   19.552576] test_bpf: #84 ALU64_MOVSX | BPF_B jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times)
[   19.553542] test_bpf: #85 ALU64_MOVSX | BPF_H jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times)
[   19.554807] test_bpf: #86 ALU64_MOVSX | BPF_W jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times)

After this patch:
[   17.931172] test_bpf: #82 ALU_MOVSX | BPF_B jited:1 125 PASS
[   17.932198] test_bpf: #83 ALU_MOVSX | BPF_H jited:1 124 PASS
[   17.933039] test_bpf: #84 ALU64_MOVSX | BPF_B jited:1 124 PASS
[   17.933918] test_bpf: #85 ALU64_MOVSX | BPF_H jited:1 124 PASS
[   17.934751] test_bpf: #86 ALU64_MOVSX | BPF_W jited:1 122 PASS

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The RV32 BPF JIT compiler currently only supports the BPF_ADD atomic
operation. Other 32 bit atomic operations (and, or, xor, xchg) and
their BPF_FETCH variants are not supported and gracefully fall back to
the interpreter.

Since the RISC-V A extension is required for Linux on RV32, we can
natively support these 32-bit BPF atomic operations by mapping them
directly to the corresponding RISC-V amo*.w instructions.

Implement BPF_ADD, BPF_AND, BPF_OR, BPF_XOR, and BPF_XCHG with and
without BPF_FETCH. BPF_CMPXCHG requires a more complex lr.w/sc.w
loop and is left to fall back to the interpreter.

Before this patch:
[  138.862161] test_bpf: Summary: 1054 PASSED, 0 FAILED, [843/1042 JIT'ed]

After this patch:
[  157.024124] test_bpf: Summary: 1054 PASSED, 0 FAILED, [902/1042 JIT'ed]

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 137.09 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 924.27 seconds
Result: ERROR
Output:

Redirect to /build/tmp.Nx3ciL4xW2 and /build/tmp.tQMf0RoOMC
Tree base:
01805bc4f902e ("Adding CI files")
Building the whole tree with the patch
error:
Warning: /build/tmp5lx3_1f4/drivers/pmdomain/mediatek/mtk-mfg-pmdomain.c:244 struct __packed mtk_mfg_opp_entry { __le32 freq_khz; __le32 voltage_core; __le32 voltage_sram; __le32 posdiv; __le32 voltage_margin; __le32 power_mw; }; error: Cannot parse struct or union!
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_bytes_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_bytes_unaligned'; recompile with -fPIC



real	15m15.232s
user	570m44.695s
sys	107m47.639s

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1353.64 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 25.75 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 25.83 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.77 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 85.59 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
kdoc
Desc: Detects for kdoc errors
Duration: 0.91 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
module-param
Desc: Detect module_param changes
Duration: 0.26 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[bpf-next,1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.30 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 138.02 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 930.75 seconds
Result: ERROR
Output:

Redirect to /build/tmp.Z7CAI5D5Kq and /build/tmp.1yEzCCHeLz
Tree base:
90938c174fca2 ("riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT")
Building the whole tree with the patch
error:
Warning: /build/tmp7uo31kq3/drivers/pmdomain/mediatek/mtk-mfg-pmdomain.c:244 struct __packed mtk_mfg_opp_entry { __le32 freq_khz; __le32 voltage_core; __le32 voltage_sram; __le32 posdiv; __le32 voltage_margin; __le32 power_mw; }; error: Cannot parse struct or union!
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_bytes_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_bytes_unaligned'; recompile with -fPIC



real	15m21.237s
user	572m39.852s
sys	108m22.614s

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1355.94 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 25.00 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 26.18 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.83 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 85.37 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
kdoc
Desc: Detects for kdoc errors
Duration: 0.87 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
module-param
Desc: Detect module_param changes
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[bpf-next,2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.31 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 138.63 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 928.62 seconds
Result: ERROR
Output:

Redirect to /build/tmp.ZF7WiMZtFm and /build/tmp.gmdgf0oNyY
Tree base:
ca03e34f60648 ("riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT")
Building the whole tree with the patch
error:
Warning: /build/tmpzx43lliy/drivers/pmdomain/mediatek/mtk-mfg-pmdomain.c:244 struct __packed mtk_mfg_opp_entry { __le32 freq_khz; __le32 voltage_core; __le32 voltage_sram; __le32 posdiv; __le32 voltage_margin; __le32 power_mw; }; error: Cannot parse struct or union!
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_bytes_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_words_unaligned'; recompile with -fPIC
ld.lld: error: relocation R_RISCV_32 cannot be used against symbol '__kcfi_typeid___riscv_copy_vec_bytes_unaligned'; recompile with -fPIC



real	15m19.675s
user	573m33.636s
sys	108m42.266s

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1351.73 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 25.84 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 26.81 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 0.77 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 84.75 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
kdoc
Desc: Detects for kdoc errors
Duration: 0.85 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
module-param
Desc: Detect module_param changes
Duration: 0.28 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[bpf-next,3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.34 seconds
Result: PASS

@linux-riscv-bot linux-riscv-bot force-pushed the workflow__riscv__fixes branch from 01805bc to 94a07a2 Compare April 30, 2026 05:50
@linux-riscv-bot linux-riscv-bot deleted the pw1087459 branch April 30, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants