From: Pu Lehui <pulehui@huawei.com>
To: Chen Pei <cp0613@linux.alibaba.com>, <ast@kernel.org>,
<daniel@iogearbox.net>, <andrii@kernel.org>, <memxor@gmail.com>,
<bjorn@kernel.org>, <puranjay@kernel.org>
Cc: <eddyz87@gmail.com>, <martin.lau@linux.dev>, <song@kernel.org>,
<yonghong.song@linux.dev>, <jolsa@kernel.org>,
<emil@etsalapatis.com>, <pjw@kernel.org>, <palmer@dabbelt.com>,
<shuah@kernel.org>, <guoren@kernel.org>, <bpf@vger.kernel.org>,
<linux-riscv@lists.infradead.org>,
<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next 1/2] bpf, riscv: Add support for signed arena loads
Date: Tue, 18 Aug 2026 22:27:15 +0800 [thread overview]
Message-ID: <c5441b7e-3d20-4e4e-a225-7478f7fb9d6d@huawei.com> (raw)
In-Reply-To: <3445fb0231f9c72496ae840e9dca675b15899433.1786708960.git.cp0613@linux.alibaba.com>
On 2026/8/17 15:24, Chen Pei wrote:
> Signed loads from arena memory are currently rejected on riscv64, as
> bpf_jit_supports_insn() refuses BPF_MEMSX loads when in_arena is set,
> while x86 and arm64 gained support for them in v6.18. Compilers such
> as GCC-14 are free to generate signed loads into arena memory, which
> breaks loading of otherwise valid BPF programs on riscv64.
>
> Implement BPF_PROBE_MEM32SX support in the RV64 JIT by reusing the
> existing arena handling: the arena base (RV_REG_ARENA) is added to
> the source register and the load is emitted with sign extension
> (lb/lh/lw). Add BPF_PROBE_MEM32SX to the add_exception_handler()
> mode gate so that faulting loads get an exception table entry which
> clears the destination register and resumes execution.
>
> Verified by running the arena LDSX selftests (arena_ldsx_disasm,
> arena_ldsx_exception, arena_ldsx_s8/s16/s32) on riscv64 QEMU, all
> passing.
>
> Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
> ---
> arch/riscv/net/bpf_jit_comp64.c | 15 +++++++++------
> 1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> index f9d5347ba966..5786f7dfc8a7 100644
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
> @@ -777,6 +777,7 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
> if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
> BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
> BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
> + BPF_MODE(insn->code) != BPF_PROBE_MEM32SX &&
> BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
> return 0;
>
> @@ -1902,13 +1903,19 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
> + /* LDX | PROBE_MEM32SX: dst = *(signed size *)(src + RV_REG_ARENA + off) */
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B:
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H:
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W:
> {
> bool sign_ext;
>
> sign_ext = BPF_MODE(insn->code) == BPF_MEMSX ||
> - BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
> + BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
> + BPF_MODE(insn->code) == BPF_PROBE_MEM32SX;
>
> - if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
> + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32 ||
> + BPF_MODE(insn->code) == BPF_PROBE_MEM32SX) {
> emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
> rs = RV_REG_T2;
> }
> @@ -2126,10 +2133,6 @@ bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
> if (insn->imm == BPF_CMPXCHG)
> return rv_ext_enabled(ZACAS);
> break;
> - case BPF_LDX | BPF_MEMSX | BPF_B:
> - case BPF_LDX | BPF_MEMSX | BPF_H:
> - case BPF_LDX | BPF_MEMSX | BPF_W:
> - return false;
> }
> }
>
Reviewed-by: Pu Lehui <pulehui@huawei.com>
WARNING: multiple messages have this Message-ID (diff)
From: Pu Lehui <pulehui@huawei.com>
To: Chen Pei <cp0613@linux.alibaba.com>, <ast@kernel.org>,
<daniel@iogearbox.net>, <andrii@kernel.org>, <memxor@gmail.com>,
<bjorn@kernel.org>, <puranjay@kernel.org>
Cc: <eddyz87@gmail.com>, <martin.lau@linux.dev>, <song@kernel.org>,
<yonghong.song@linux.dev>, <jolsa@kernel.org>,
<emil@etsalapatis.com>, <pjw@kernel.org>, <palmer@dabbelt.com>,
<shuah@kernel.org>, <guoren@kernel.org>, <bpf@vger.kernel.org>,
<linux-riscv@lists.infradead.org>,
<linux-kselftest@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next 1/2] bpf, riscv: Add support for signed arena loads
Date: Tue, 18 Aug 2026 22:27:15 +0800 [thread overview]
Message-ID: <c5441b7e-3d20-4e4e-a225-7478f7fb9d6d@huawei.com> (raw)
In-Reply-To: <3445fb0231f9c72496ae840e9dca675b15899433.1786708960.git.cp0613@linux.alibaba.com>
On 2026/8/17 15:24, Chen Pei wrote:
> Signed loads from arena memory are currently rejected on riscv64, as
> bpf_jit_supports_insn() refuses BPF_MEMSX loads when in_arena is set,
> while x86 and arm64 gained support for them in v6.18. Compilers such
> as GCC-14 are free to generate signed loads into arena memory, which
> breaks loading of otherwise valid BPF programs on riscv64.
>
> Implement BPF_PROBE_MEM32SX support in the RV64 JIT by reusing the
> existing arena handling: the arena base (RV_REG_ARENA) is added to
> the source register and the load is emitted with sign extension
> (lb/lh/lw). Add BPF_PROBE_MEM32SX to the add_exception_handler()
> mode gate so that faulting loads get an exception table entry which
> clears the destination register and resumes execution.
>
> Verified by running the arena LDSX selftests (arena_ldsx_disasm,
> arena_ldsx_exception, arena_ldsx_s8/s16/s32) on riscv64 QEMU, all
> passing.
>
> Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
> ---
> arch/riscv/net/bpf_jit_comp64.c | 15 +++++++++------
> 1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> index f9d5347ba966..5786f7dfc8a7 100644
> --- a/arch/riscv/net/bpf_jit_comp64.c
> +++ b/arch/riscv/net/bpf_jit_comp64.c
> @@ -777,6 +777,7 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
> if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
> BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
> BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
> + BPF_MODE(insn->code) != BPF_PROBE_MEM32SX &&
> BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
> return 0;
>
> @@ -1902,13 +1903,19 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
> case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
> + /* LDX | PROBE_MEM32SX: dst = *(signed size *)(src + RV_REG_ARENA + off) */
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B:
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H:
> + case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W:
> {
> bool sign_ext;
>
> sign_ext = BPF_MODE(insn->code) == BPF_MEMSX ||
> - BPF_MODE(insn->code) == BPF_PROBE_MEMSX;
> + BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
> + BPF_MODE(insn->code) == BPF_PROBE_MEM32SX;
>
> - if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
> + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32 ||
> + BPF_MODE(insn->code) == BPF_PROBE_MEM32SX) {
> emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
> rs = RV_REG_T2;
> }
> @@ -2126,10 +2133,6 @@ bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
> if (insn->imm == BPF_CMPXCHG)
> return rv_ext_enabled(ZACAS);
> break;
> - case BPF_LDX | BPF_MEMSX | BPF_B:
> - case BPF_LDX | BPF_MEMSX | BPF_H:
> - case BPF_LDX | BPF_MEMSX | BPF_W:
> - return false;
> }
> }
>
Reviewed-by: Pu Lehui <pulehui@huawei.com>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-08-18 14:27 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-17 7:24 [PATCH bpf-next 0/2] bpf, riscv: Add support for signed arena loads Chen Pei
2026-08-17 7:24 ` Chen Pei
2026-08-17 7:24 ` [PATCH bpf-next 1/2] " Chen Pei
2026-08-17 7:24 ` Chen Pei
2026-08-17 8:16 ` bot+bpf-ci
2026-08-17 8:16 ` bot+bpf-ci
2026-08-18 14:27 ` Pu Lehui [this message]
2026-08-18 14:27 ` Pu Lehui
2026-08-19 3:41 ` Pu Lehui
2026-08-19 3:41 ` Pu Lehui
2026-08-19 8:23 ` Chen Pei
2026-08-19 8:23 ` Chen Pei
2026-08-17 7:24 ` [PATCH bpf-next 2/2] selftests/bpf: Enable arena LDSX tests for riscv64 Chen Pei
2026-08-17 7:24 ` Chen Pei
2026-08-18 14:27 ` Pu Lehui
2026-08-18 14:27 ` Pu Lehui
2026-08-18 14:28 ` [PATCH bpf-next 0/2] bpf, riscv: Add support for signed arena loads Pu Lehui
2026-08-18 14:28 ` Pu Lehui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c5441b7e-3d20-4e4e-a225-7478f7fb9d6d@huawei.com \
--to=pulehui@huawei.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cp0613@linux.alibaba.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=guoren@kernel.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=puranjay@kernel.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.