Skip to content

Commit f6606a4

Browse files
Xu KuohaiAlexei Starovoitov
authored andcommitted
bpf, arm64: Emit BTI for indirect jump target
On CPUs that support BTI, the indirect jump selftest triggers a kernel panic because there is no BTI instructions at the indirect jump targets. Fix it by emitting a BTI instruction for each indirect jump target. For reference, below is a sample panic log. Internal error: Oops - BTI: 0000000036000003 [#1] SMP ... Call trace: bpf_prog_2e5f1c71c13ac3e0_big_jump_table+0x54/0xf8 (P) bpf_prog_run_pin_on_cpu+0x140/0x468 bpf_prog_test_run_syscall+0x280/0x3b8 bpf_prog_test_run+0x22c/0x2c0 Fixes: f4a66cf ("bpf: arm64: Add support for indirect jumps") Reviewed-by: Anton Protopopov <a.s.protopopov@gmail.com> # v8 Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com> # v12 Acked-by: Leon Hwang <leon.hwang@linux.dev> Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Link: https://lore.kernel.org/r/20260416064341.151802-6-xukuohai@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 9a0e89d commit f6606a4

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

arch/arm64/net/bpf_jit_comp.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,8 @@ static int add_exception_handler(const struct bpf_insn *insn,
11971197
* >0 - successfully JITed a 16-byte eBPF instruction.
11981198
* <0 - failed to JIT.
11991199
*/
1200-
static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
1201-
bool extra_pass)
1200+
static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn *insn,
1201+
struct jit_ctx *ctx, bool extra_pass)
12021202
{
12031203
const u8 code = insn->code;
12041204
u8 dst = bpf2a64[insn->dst_reg];
@@ -1223,6 +1223,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
12231223
int ret;
12241224
bool sign_extend;
12251225

1226+
if (bpf_insn_is_indirect_target(env, ctx->prog, i))
1227+
emit_bti(A64_BTI_J, ctx);
1228+
12261229
switch (code) {
12271230
/* dst = src */
12281231
case BPF_ALU | BPF_MOV | BPF_X:
@@ -1898,7 +1901,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
18981901
return 0;
18991902
}
19001903

1901-
static int build_body(struct jit_ctx *ctx, bool extra_pass)
1904+
static int build_body(struct bpf_verifier_env *env, struct jit_ctx *ctx, bool extra_pass)
19021905
{
19031906
const struct bpf_prog *prog = ctx->prog;
19041907
int i;
@@ -1917,7 +1920,7 @@ static int build_body(struct jit_ctx *ctx, bool extra_pass)
19171920
int ret;
19181921

19191922
ctx->offset[i] = ctx->idx;
1920-
ret = build_insn(insn, ctx, extra_pass);
1923+
ret = build_insn(env, insn, ctx, extra_pass);
19211924
if (ret > 0) {
19221925
i++;
19231926
ctx->offset[i] = ctx->idx;
@@ -2073,7 +2076,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
20732076
if (build_prologue(&ctx, was_classic))
20742077
goto out_off;
20752078

2076-
if (build_body(&ctx, extra_pass))
2079+
if (build_body(env, &ctx, extra_pass))
20772080
goto out_off;
20782081

20792082
ctx.epilogue_offset = ctx.idx;
@@ -2121,7 +2124,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
21212124
/* Dont write body instructions to memory for now */
21222125
ctx.write = false;
21232126

2124-
if (build_body(&ctx, extra_pass))
2127+
if (build_body(env, &ctx, extra_pass))
21252128
goto out_free_hdr;
21262129

21272130
ctx.epilogue_offset = ctx.idx;
@@ -2130,7 +2133,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
21302133
ctx.write = true;
21312134

21322135
/* Pass 3: Adjust jump offset and write final image */
2133-
if (build_body(&ctx, extra_pass) ||
2136+
if (build_body(env, &ctx, extra_pass) ||
21342137
WARN_ON_ONCE(ctx.idx != ctx.epilogue_offset))
21352138
goto out_free_hdr;
21362139

0 commit comments

Comments
 (0)