Skip to content

Commit 8c11ea5

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, arm64: fix getting subprog addr from aux for calls
The arm64 JIT has the same issue as ppc64 JIT in that the relative BPF to BPF call offset can be too far away from core kernel in that relative encoding into imm is not sufficient and could potentially be truncated, see also fd045f6 ("arm64: add support for module PLTs") which adds spill-over space for module_alloc() and therefore bpf_jit_binary_alloc(). Therefore, use the recently added bpf_jit_get_func_addr() helper for properly fetching the address through prog->aux->func[off]->bpf_func instead. This also has the benefit to optimize normal helper calls since their address can use the optimized emission. Tested on Cavium ThunderX CN8890. Fixes: db49694 ("bpf: arm64: add JIT support for multi-function programs") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e2c95a6 commit 8c11ea5

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

‎arch/arm64/net/bpf_jit_comp.c‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ static void build_epilogue(struct jit_ctx *ctx)
351351
* >0 - successfully JITed a 16-byte eBPF instruction.
352352
* <0 - failed to JIT.
353353
*/
354-
static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
354+
static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
355+
bool extra_pass)
355356
{
356357
const u8 code = insn->code;
357358
const u8 dst = bpf2a64[insn->dst_reg];
@@ -625,12 +626,19 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
625626
case BPF_JMP | BPF_CALL:
626627
{
627628
const u8 r0 = bpf2a64[BPF_REG_0];
628-
const u64 func = (u64)__bpf_call_base + imm;
629+
bool func_addr_fixed;
630+
u64 func_addr;
631+
int ret;
629632

630-
if (ctx->prog->is_func)
631-
emit_addr_mov_i64(tmp, func, ctx);
633+
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
634+
&func_addr, &func_addr_fixed);
635+
if (ret < 0)
636+
return ret;
637+
if (func_addr_fixed)
638+
/* We can use optimized emission here. */
639+
emit_a64_mov_i64(tmp, func_addr, ctx);
632640
else
633-
emit_a64_mov_i64(tmp, func, ctx);
641+
emit_addr_mov_i64(tmp, func_addr, ctx);
634642
emit(A64_BLR(tmp), ctx);
635643
emit(A64_MOV(1, r0, A64_R(0)), ctx);
636644
break;
@@ -753,7 +761,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
753761
return 0;
754762
}
755763

756-
static int build_body(struct jit_ctx *ctx)
764+
static int build_body(struct jit_ctx *ctx, bool extra_pass)
757765
{
758766
const struct bpf_prog *prog = ctx->prog;
759767
int i;
@@ -762,7 +770,7 @@ static int build_body(struct jit_ctx *ctx)
762770
const struct bpf_insn *insn = &prog->insnsi[i];
763771
int ret;
764772

765-
ret = build_insn(insn, ctx);
773+
ret = build_insn(insn, ctx, extra_pass);
766774
if (ret > 0) {
767775
i++;
768776
if (ctx->image == NULL)
@@ -858,7 +866,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
858866
/* 1. Initial fake pass to compute ctx->idx. */
859867

860868
/* Fake pass to fill in ctx->offset. */
861-
if (build_body(&ctx)) {
869+
if (build_body(&ctx, extra_pass)) {
862870
prog = orig_prog;
863871
goto out_off;
864872
}
@@ -888,7 +896,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
888896

889897
build_prologue(&ctx, was_classic);
890898

891-
if (build_body(&ctx)) {
899+
if (build_body(&ctx, extra_pass)) {
892900
bpf_jit_binary_free(header);
893901
prog = orig_prog;
894902
goto out_off;

0 commit comments

Comments
 (0)