* [PATCH v1 0/4] LoongArch: BPF: Support instructions array, indirect jumps and calls
@ 2026-11-23 4:39 Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow Tiezhu Yang
` (3 more replies)
0 siblings, 4 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
This series was created on top of the following unmerged patches,
though there is no actual dependency and git am applies cleanly
without them:
LoongArch: BPF: Fix the offset of stptr.w and stptr.d stores
https://lore.kernel.org/loongarch/20261118071444.10258-1-dongtai.guo@linux.dev/
LoongArch: BPF: Optimize doubleword loads with ld.d and ldptr.d
https://lore.kernel.org/loongarch/20261118081201.10498-1-yangtiezhu@loongson.cn/
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
Tiezhu Yang (4):
LoongArch: BPF: Optimize BPF_CALL control flow
LoongArch: BPF: Add support for instructions array
LoongArch: BPF: Add support for indirect jumps
LoongArch: BPF: Add support for indirect calls
arch/loongarch/net/bpf_jit.c | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
--
2.42.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [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:42 ` sashiko-bot
2026-11-23 4:39 ` [PATCH v1 2/4] LoongArch: BPF: Add support for instructions array Tiezhu Yang
` (2 subsequent siblings)
3 siblings, 1 reply; 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 related [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
2026-11-23 4:39 ` [PATCH v1 4/4] LoongArch: BPF: Add support for indirect calls Tiezhu Yang
3 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 related [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
3 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 related [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
3 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 related [flat|nested] 7+ messages in thread
* Re: [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow
2026-11-23 4:39 ` [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow Tiezhu Yang
@ 2026-11-23 4:42 ` sashiko-bot
2026-11-23 5:00 ` Tiezhu Yang
0 siblings, 1 reply; 7+ messages in thread
From: sashiko-bot @ 2026-11-23 4:42 UTC (permalink / raw)
To: Tiezhu Yang; +Cc: bpf
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] LoongArch: BPF: unshifted and unaligned offset for
stptrw/stptrd in build_insn() [1]
--
commit 0dc0cb0459b2a4e50da067a04e5643e5c6bf634c
Author: Tiezhu Yang <yangtiezhu@loongson.cn>
LoongArch: BPF: Optimize BPF_CALL control flow
This commit separates BPF_PSEUDO_CALL handling in build_insn() so that it
emits the jump and breaks early, avoiding a redundant check at the end of
the BPF_CALL case.
> diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> index 15af1abb5cb69..b26b75f62fb31 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;
[Severity: High]
This problem wasn't introduced by this patch, but can emit_insn() for
stptrw and stptrd in build_insn() corrupt memory outside the target buffer
[1]?
Later in build_insn(), the BPF_ST and BPF_STX cases emit stptrw and stptrd
when off is outside the 12-bit signed immediate range [-2048, 2047] but
within the 14-bit signed range [-8192, 8191]:
arch/loongarch/net/bpf_jit.c:build_insn() {
...
case BPF_W:
if (is_signed_imm12(off)) {
emit_insn(ctx, stw, src, dst, off);
} else if (is_signed_imm14(off)) {
emit_insn(ctx, stptrw, src, dst, off);
} else {
...
}
In LoongArch, stptr.w and stptr.d require a 4-byte-aligned offset encoded
as a word-scaled 14-bit signed immediate (si14 << 2), whereas
emit_stptrw() and emit_stptrd() write their immediate argument directly
into insn->reg2i14_format.immediate without shifting. Passing the byte
offset off directly causes the store to write to dst + (off << 2) instead
of dst + off, corrupting kernel memory up to 24 KB beyond a verifier-
checked map value or arena buffer.
[1] https://sashiko.dev/#/bug/linux-9c06f20c-c232-47f5-ae96-11fb3889acdc
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123043907.20225-1-yangtiezhu@loongson.cn?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v1 1/4] LoongArch: BPF: Optimize BPF_CALL control flow
2026-11-23 4:42 ` sashiko-bot
@ 2026-11-23 5:00 ` Tiezhu Yang
0 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2026-11-23 5:00 UTC (permalink / raw)
To: sashiko-reviews; +Cc: bpf
On 11/23/26 12:42 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found no new issues
> in this patch, but noticed 1 related pre-existing issue it does not
> introduce:
This pre-existing issue has already been fixed by:
LoongArch: BPF: Fix the offset of stptr.w and stptr.d stores
https://lore.kernel.org/loongarch/20261118071444.10258-1-dongtai.guo@linux.dev/
Thanks,
Tiezhu
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-23 5:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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:42 ` sashiko-bot
2026-11-23 5:00 ` 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 ` [PATCH v1 3/4] LoongArch: BPF: Add support for indirect jumps Tiezhu Yang
2026-11-23 4:39 ` [PATCH v1 4/4] LoongArch: BPF: Add support for indirect calls Tiezhu Yang
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.