* [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow
2026-11-23 4:39 [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls Tiezhu Yang
@ 2026-11-23 4:39 ` Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 2/4] LoongArch: BPF: Add support for instructions array Tiezhu Yang
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-11-23 4:39 UTC (permalink / raw)
To: Huacai Chen, Hengqi Chen; +Cc: loongarch, bpf, linux-kernel
Separate BPF_PSEUDO_CALL handling into its own execution path by
emitting the jump instruction and breaking early. This eliminates
the need for a redundant 'insn->src_reg != BPF_PSEUDO_CALL' check
at the end of the main BPF_CALL case.
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/net/bpf_jit.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index d36891db1179..873b2b146f02 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1219,6 +1219,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
if (insn->src_reg == BPF_PSEUDO_CALL) {
tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(ctx->stack_size);
emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_SP, tcc_ptr_off);
+ move_addr(ctx, t1, func_addr);
+ emit_insn(ctx, jirl, LOONGARCH_GPR_RA, t1, 0);
+ break;
}
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
@@ -1245,8 +1248,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
* convention with the argument and return value in BPF_REG_AX,
* so skip moving the C return value into BPF_REG_0.
*/
- if (insn->src_reg != BPF_PSEUDO_CALL &&
- func_addr != (u64)arch_bpf_timed_may_goto)
+ if (func_addr != (u64)arch_bpf_timed_may_goto)
move_reg(ctx, regmap[BPF_REG_0], LOONGARCH_GPR_A0);
break;
--
2.42.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v1 2/4] LoongArch: BPF: Add support for instructions array
2026-11-23 4:39 [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow Tiezhu Yang
@ 2026-11-23 4:39 ` Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 3/4] LoongArch: BPF: Add support for indirect jumps Tiezhu Yang
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-11-23 4:39 UTC (permalink / raw)
To: Huacai Chen, Hengqi Chen; +Cc: loongarch, bpf, linux-kernel
Add support for the instructions array map type in the LoongArch
JIT by calling bpf_prog_update_insn_ptrs() with the offsets that
map xlated_offset to the jited_offset in the final image.
LoongArch JIT already has this offset array which was being used
for bpf_prog_fill_jited_linfo() and can be used directly for the
function bpf_prog_update_insn_ptrs().
This is similar to commit 84b1c40d5b4d ("bpf: arm64: Add support
for instructions array").
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/net/bpf_jit.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 873b2b146f02..415b945bf818 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -2401,6 +2401,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
for (i = 0; i <= prog->len; i++)
ctx.offset[i] *= LOONGARCH_INSN_SIZE;
bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
+ /*
+ * The bpf_prog_update_insn_ptrs function expects offsets to
+ * point to the first byte of the jitted instruction (unlike
+ * the bpf_prog_fill_jited_linfo above, which, for historical
+ * reasons, expects to point to the next instruction)
+ */
+ bpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);
out_offset:
kvfree(ctx.offset);
--
2.42.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v1 3/4] LoongArch: BPF: Add support for indirect jumps
2026-11-23 4:39 [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 2/4] LoongArch: BPF: Add support for instructions array Tiezhu Yang
@ 2026-11-23 4:39 ` Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 4/4] LoongArch: BPF: Add support for indirect calls Tiezhu Yang
2026-12-04 6:56 ` [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls George Guo
4 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-11-23 4:39 UTC (permalink / raw)
To: Huacai Chen, Hengqi Chen; +Cc: loongarch, bpf, linux-kernel
In order to implement JIT support for the indirect jump instruction
(BPF_JMP | BPF_JA | BPF_X), emit a jirl instruction to jump to the
target address held in dst without saving PC+4 into return address.
This is similar to commit f4a66cf1cb14 ("bpf: arm64: Add support for
indirect jumps").
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/net/bpf_jit.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 415b945bf818..52da39dce4f6 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1184,6 +1184,11 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
goto toofar;
break;
+ /* PC = dst */
+ case BPF_JMP | BPF_JA | BPF_X:
+ emit_insn(ctx, jirl, LOONGARCH_GPR_ZERO, dst, 0);
+ break;
+
/* PC += off */
case BPF_JMP | BPF_JA:
jmp_offset = bpf2la_offset(i, off, ctx);
--
2.42.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v1 4/4] LoongArch: BPF: Add support for indirect calls
2026-11-23 4:39 [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls Tiezhu Yang
` (2 preceding siblings ...)
2026-11-23 4:39 ` [PATCH v1 3/4] LoongArch: BPF: Add support for indirect jumps Tiezhu Yang
@ 2026-11-23 4:39 ` Tiezhu Yang
2026-12-04 6:56 ` [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls George Guo
4 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-11-23 4:39 UTC (permalink / raw)
To: Huacai Chen, Hengqi Chen; +Cc: loongarch, bpf, linux-kernel
In order to implement JIT support for the indirect call instruction
(BPF_JMP | BPF_CALL | BPF_X), load REG_TCC from the stack (just like
a direct bpf2bpf call) first, and emit a jirl instruction to jump to
the target subprog address held in dst while saving PC+4 into return
address.
This is similar to commit 1cec0b70fdc6 ("riscv, bpf: Support indirect
calls to bpf subprogs (callx)").
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
arch/loongarch/net/bpf_jit.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 52da39dce4f6..567729921d6d 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -1258,6 +1258,13 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
break;
+ /* indirect call to a bpf subprog via the address in dst */
+ case BPF_JMP | BPF_CALL | BPF_X:
+ tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(ctx->stack_size);
+ emit_insn(ctx, ldd, REG_TCC, LOONGARCH_GPR_SP, tcc_ptr_off);
+ emit_insn(ctx, jirl, LOONGARCH_GPR_RA, dst, 0);
+ break;
+
/* tail call */
case BPF_JMP | BPF_TAIL_CALL:
if (emit_bpf_tail_call(ctx, i) < 0)
@@ -2559,3 +2566,8 @@ bool bpf_jit_inlines_helper_call(s32 imm)
return false;
}
}
+
+bool bpf_jit_supports_callx(void)
+{
+ return true;
+}
--
2.42.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls
2026-11-23 4:39 [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls Tiezhu Yang
` (3 preceding siblings ...)
2026-11-23 4:39 ` [PATCH v1 4/4] LoongArch: BPF: Add support for indirect calls Tiezhu Yang
@ 2026-12-04 6:56 ` George Guo
2026-12-04 7:11 ` Tiezhu Yang
4 siblings, 1 reply; 7+ messages in thread
From: George Guo @ 2026-12-04 6:56 UTC (permalink / raw)
To: yangtiezhu
Cc: chenhuacai, hengqi.chen, loongarch, bpf, linux-kernel, George Guo
On 2026/11/23 12:39, Tiezhu Yang wrote:
> Tested with the series of BPF selftests, here are the test results:
>
> $ sudo ./test_progs -t bpf_insn_array | tail -1
> Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED
>
> $ sudo ./test_progs-cpuv4 -t gotox | tail -1
> Summary: 2/14 PASSED, 0 SKIPPED, 0/0 FAILED
>
> $ sudo ./test_progs -t callx | tail -1
> Summary: 6/89 PASSED, 6 SKIPPED, 0/0 FAILED
Tested the whole series on Loongson-3C6000. These tests are skipped on
LoongArch upstream, so I added LoongArch to the arch checks in
bpf_insn_array.c, callx_func_ptr_map.c, callx_rodata_lskel.c,
verifier_callx.c, verifier_callx_rodata.c and verifier_gotox.c locally:
$ sudo ./test_progs -t bpf_insn_array | tail -1
Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs-cpuv4 -t gotox | tail -1
Summary: 2/47 PASSED, 0 SKIPPED, 0/0 FAILED
$ sudo ./test_progs -t jit_insns,jit_insns_blinded | tail -1
Summary: 2/47 PASSED, 0 SKIPPED, 0/0 FAILED
With CONFIG_ARCH_STRICT_ALIGN=n:
$ sudo ./test_progs -t callx | tail -1
Summary: 4/77 PASSED, 0 SKIPPED, 0/0 FAILED
With CONFIG_ARCH_STRICT_ALIGN=y:
$ sudo ./test_progs -t callx | tail -1
Summary: 3/76 PASSED, 0 SKIPPED, 1/1 FAILED
The only failure with CONFIG_ARCH_STRICT_ALIGN=y is
verifier_callx_rodata/callx_rodata_misaligned_read, and it is not
related to this series. HAVE_EFFICIENT_UNALIGNED_ACCESS is not set
then, so the verifier checks alignment strictly. The program is still
rejected as expected, but by the alignment check first:
misaligned value access off 352+4 size 8
instead of the message the test looks for:
read of 8 bytes at offset
Reviewed-by: George Guo <guodongtai@kylinos.cn>
Tested-by: George Guo <guodongtai@kylinos.cn>
Thanks,
George
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls
2026-12-04 6:56 ` [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls George Guo
@ 2026-12-04 7:11 ` Tiezhu Yang
0 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-12-04 7:11 UTC (permalink / raw)
To: George Guo
Cc: chenhuacai, hengqi.chen, loongarch, bpf, linux-kernel, George Guo
On 12/4/26 2:56 PM, George Guo wrote:
> On 2026/11/23 12:39, Tiezhu Yang wrote:
>> Tested with the series of BPF selftests, here are the test results:
>>
>> $ sudo ./test_progs -t bpf_insn_array | tail -1
>> Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED
>>
>> $ sudo ./test_progs-cpuv4 -t gotox | tail -1
>> Summary: 2/14 PASSED, 0 SKIPPED, 0/0 FAILED
>>
>> $ sudo ./test_progs -t callx | tail -1
>> Summary: 6/89 PASSED, 6 SKIPPED, 0/0 FAILED
>
> Tested the whole series on Loongson-3C6000. These tests are skipped on
> LoongArch upstream, so I added LoongArch to the arch checks in
> bpf_insn_array.c, callx_func_ptr_map.c, callx_rodata_lskel.c,
> verifier_callx.c, verifier_callx_rodata.c and verifier_gotox.c locally:
Thanks for your testing.
Here is the related BPF selftests patch series, which needs to send v2
once jit patches are in bpf-next tree, maybe after the next rc1, but
can be used to test:
[PATCH bpf-next v1 0/3] selftests/bpf: Enable instructions array,
indirect calls tests for LoongArch
https://lore.kernel.org/loongarch/20261123045008.23691-1-yangtiezhu@loongson.cn/T/#u
> $ sudo ./test_progs -t bpf_insn_array | tail -1
> Summary: 1/20 PASSED, 0 SKIPPED, 0/0 FAILED
>
> $ sudo ./test_progs-cpuv4 -t gotox | tail -1
> Summary: 2/47 PASSED, 0 SKIPPED, 0/0 FAILED
>
> $ sudo ./test_progs -t jit_insns,jit_insns_blinded | tail -1
> Summary: 2/47 PASSED, 0 SKIPPED, 0/0 FAILED
>
> With CONFIG_ARCH_STRICT_ALIGN=n:
>
> $ sudo ./test_progs -t callx | tail -1
> Summary: 4/77 PASSED, 0 SKIPPED, 0/0 FAILED
>
> With CONFIG_ARCH_STRICT_ALIGN=y:
>
> $ sudo ./test_progs -t callx | tail -1
> Summary: 3/76 PASSED, 0 SKIPPED, 1/1 FAILED
>
> The only failure with CONFIG_ARCH_STRICT_ALIGN=y is
> verifier_callx_rodata/callx_rodata_misaligned_read, and it is not
> related to this series. HAVE_EFFICIENT_UNALIGNED_ACCESS is not set
> then, so the verifier checks alignment strictly. The program is still
> rejected as expected, but by the alignment check first:
Here is a related fix patch:
selftests/bpf: Allow unaligned access in callx_rodata_misaligned_read
https://lore.kernel.org/loongarch/20261123045008.23691-3-yangtiezhu@loongson.cn/
Thanks,
Tiezhu
^ permalink raw reply [flat|nested] 7+ messages in thread