Skip to content

Commit cd39d9e

Browse files
Haoran Jiangchenhuacai
authored andcommitted
LoongArch: BPF: Fix jump offset calculation in tailcall
The extra pass of bpf_int_jit_compile() skips JIT context initialization which essentially skips offset calculation leaving out_offset = -1, so the jmp_offset in emit_bpf_tail_call is calculated by "#define jmp_offset (out_offset - (cur_offset))" is a negative number, which is wrong. The final generated assembly are as follow. 54: bgeu $a2, $t1, -8 # 0x0000004c 58: addi.d $a6, $s5, -1 5c: bltz $a6, -16 # 0x0000004c 60: alsl.d $t2, $a2, $a1, 0x3 64: ld.d $t2, $t2, 264 68: beq $t2, $zero, -28 # 0x0000004c Before apply this patch, the follow test case will reveal soft lock issues. cd tools/testing/selftests/bpf/ ./test_progs --allow=tailcalls/tailcall_bpf2bpf_1 dmesg: watchdog: BUG: soft lockup - CPU#2 stuck for 26s! [test_progs:25056] Cc: stable@vger.kernel.org Fixes: 5dc6155 ("LoongArch: Add BPF JIT support") Reviewed-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Haoran Jiang <jianghaoran@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 6abf17d commit cd39d9e

1 file changed

Lines changed: 5 additions & 16 deletions

File tree

arch/loongarch/net/bpf_jit.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,9 @@ bool bpf_jit_supports_far_kfunc_call(void)
222222
return true;
223223
}
224224

225-
/* initialized on the first pass of build_body() */
226-
static int out_offset = -1;
227-
static int emit_bpf_tail_call(struct jit_ctx *ctx)
225+
static int emit_bpf_tail_call(struct jit_ctx *ctx, int insn)
228226
{
229-
int off;
227+
int off, tc_ninsn = 0;
230228
u8 tcc = tail_call_reg(ctx);
231229
u8 a1 = LOONGARCH_GPR_A1;
232230
u8 a2 = LOONGARCH_GPR_A2;
@@ -236,7 +234,7 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
236234
const int idx0 = ctx->idx;
237235

238236
#define cur_offset (ctx->idx - idx0)
239-
#define jmp_offset (out_offset - (cur_offset))
237+
#define jmp_offset (tc_ninsn - (cur_offset))
240238

241239
/*
242240
* a0: &ctx
@@ -246,6 +244,7 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
246244
* if (index >= array->map.max_entries)
247245
* goto out;
248246
*/
247+
tc_ninsn = insn ? ctx->offset[insn+1] - ctx->offset[insn] : ctx->offset[0];
249248
off = offsetof(struct bpf_array, map.max_entries);
250249
emit_insn(ctx, ldwu, t1, a1, off);
251250
/* bgeu $a2, $t1, jmp_offset */
@@ -277,15 +276,6 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
277276
emit_insn(ctx, ldd, t3, t2, off);
278277
__build_epilogue(ctx, true);
279278

280-
/* out: */
281-
if (out_offset == -1)
282-
out_offset = cur_offset;
283-
if (cur_offset != out_offset) {
284-
pr_err_once("tail_call out_offset = %d, expected %d!\n",
285-
cur_offset, out_offset);
286-
return -1;
287-
}
288-
289279
return 0;
290280

291281
toofar:
@@ -930,7 +920,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
930920
/* tail call */
931921
case BPF_JMP | BPF_TAIL_CALL:
932922
mark_tail_call(ctx);
933-
if (emit_bpf_tail_call(ctx) < 0)
923+
if (emit_bpf_tail_call(ctx, i) < 0)
934924
return -EINVAL;
935925
break;
936926

@@ -1855,7 +1845,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
18551845
if (tmp_blinded)
18561846
bpf_jit_prog_release_other(prog, prog == orig_prog ? tmp : orig_prog);
18571847

1858-
out_offset = -1;
18591848

18601849
return prog;
18611850

0 commit comments

Comments
 (0)