Re: [PATCH v3 2/5] LoongArch: BPF: Add the default case in emit_atomic() and rename it
From: Hengqi Chen
Date: Tue Apr 14 2026 - 22:27:34 EST
On Mon, Apr 13, 2026 at 12:05 PM Tiezhu Yang <yangtiezhu@xxxxxxxxxxx> wrote:
>
> Like the other archs such as x86 and riscv, add the default case
> in emit_atomic() to print an error message for the invalid opcode
> and return -EINVAL , then make its return type as int.
>
> While at it, given that all of the instructions in emit_atomic()
> are only read-modify-write instructions, rename emit_atomic() to
> emit_atomic_rmw() to make it clear, because there will be a new
> function emit_atomic_ld_st() for load-acquire and store-release
> instructions in the later patch.
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
> ---
Acked-by: Hengqi Chen <hengqi.chen@xxxxxxxxx>
> arch/loongarch/net/bpf_jit.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 9cb796e16379..fefda4050a20 100644
> --- a/arch/loongarch/net/bpf_jit.c
> +++ b/arch/loongarch/net/bpf_jit.c
> @@ -344,7 +344,7 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx, int insn)
> #undef jmp_offset
> }
>
> -static void emit_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
> +static int emit_atomic_rmw(const struct bpf_insn *insn, struct jit_ctx *ctx)
> {
> const u8 t1 = LOONGARCH_GPR_T1;
> const u8 t2 = LOONGARCH_GPR_T2;
> @@ -448,7 +448,12 @@ static void emit_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
> emit_zext_32(ctx, r0, true);
> }
> break;
> + default:
> + pr_err_once("bpf-jit: invalid atomic read-modify-write opcode %02x\n", imm);
> + return -EINVAL;
> }
> +
> + return 0;
> }
>
> static bool is_signed_bpf_cond(u8 cond)
> @@ -1256,7 +1261,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
>
> case BPF_STX | BPF_ATOMIC | BPF_W:
> case BPF_STX | BPF_ATOMIC | BPF_DW:
> - emit_atomic(insn, ctx);
> + ret = emit_atomic_rmw(insn, ctx);
> + if (ret)
> + return ret;
> break;
>
> /* Speculation barrier */
> --
> 2.42.0
>