Skip to content

Commit db49694

Browse files
4astborkmann
authored andcommitted
bpf: arm64: add JIT support for multi-function programs
similar to x64 add support for bpf-to-bpf calls. When program has calls to in-kernel helpers the target call offset is known at JIT time and arm64 architecture needs 2 passes. With bpf-to-bpf calls the dynamically allocated function start is unknown until all functions of the program are JITed. Therefore (just like x64) arm64 JIT needs one extra pass over the program to emit correct call offsets. Implementation detail: Avoid being too clever in 64-bit immediate moves and always use 4 instructions (instead of 3-4 depending on the address) to make sure only one extra pass is needed. If some future optimization would make it worth while to optimize 'call 64-bit imm' further, the JIT would need to do 4 passes over the program instead of 3 as in this patch. For typical bpf program address the mov needs 3 or 4 insns, so unconditional 4 insns to save extra pass is a worthy trade off at this state of JIT. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 1c2a088 commit db49694

1 file changed

Lines changed: 64 additions & 4 deletions

File tree

‎arch/arm64/net/bpf_jit_comp.c‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ static inline void emit_a64_mov_i64(const int reg, const u64 val,
9999
}
100100
}
101101

102+
static inline void emit_addr_mov_i64(const int reg, const u64 val,
103+
struct jit_ctx *ctx)
104+
{
105+
u64 tmp = val;
106+
int shift = 0;
107+
108+
emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
109+
for (;shift < 48;) {
110+
tmp >>= 16;
111+
shift += 16;
112+
emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
113+
}
114+
}
115+
102116
static inline void emit_a64_mov_i(const int is64, const int reg,
103117
const s32 val, struct jit_ctx *ctx)
104118
{
@@ -603,7 +617,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
603617
const u8 r0 = bpf2a64[BPF_REG_0];
604618
const u64 func = (u64)__bpf_call_base + imm;
605619

606-
emit_a64_mov_i64(tmp, func, ctx);
620+
if (ctx->prog->is_func)
621+
emit_addr_mov_i64(tmp, func, ctx);
622+
else
623+
emit_a64_mov_i64(tmp, func, ctx);
607624
emit(A64_BLR(tmp), ctx);
608625
emit(A64_MOV(1, r0, A64_R(0)), ctx);
609626
break;
@@ -835,11 +852,19 @@ static inline void bpf_flush_icache(void *start, void *end)
835852
flush_icache_range((unsigned long)start, (unsigned long)end);
836853
}
837854

855+
struct arm64_jit_data {
856+
struct bpf_binary_header *header;
857+
u8 *image;
858+
struct jit_ctx ctx;
859+
};
860+
838861
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
839862
{
840863
struct bpf_prog *tmp, *orig_prog = prog;
841864
struct bpf_binary_header *header;
865+
struct arm64_jit_data *jit_data;
842866
bool tmp_blinded = false;
867+
bool extra_pass = false;
843868
struct jit_ctx ctx;
844869
int image_size;
845870
u8 *image_ptr;
@@ -858,13 +883,29 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
858883
prog = tmp;
859884
}
860885

886+
jit_data = prog->aux->jit_data;
887+
if (!jit_data) {
888+
jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
889+
if (!jit_data) {
890+
prog = orig_prog;
891+
goto out;
892+
}
893+
prog->aux->jit_data = jit_data;
894+
}
895+
if (jit_data->ctx.offset) {
896+
ctx = jit_data->ctx;
897+
image_ptr = jit_data->image;
898+
header = jit_data->header;
899+
extra_pass = true;
900+
goto skip_init_ctx;
901+
}
861902
memset(&ctx, 0, sizeof(ctx));
862903
ctx.prog = prog;
863904

864905
ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
865906
if (ctx.offset == NULL) {
866907
prog = orig_prog;
867-
goto out;
908+
goto out_off;
868909
}
869910

870911
/* 1. Initial fake pass to compute ctx->idx. */
@@ -895,6 +936,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
895936
/* 2. Now, the actual pass. */
896937

897938
ctx.image = (__le32 *)image_ptr;
939+
skip_init_ctx:
898940
ctx.idx = 0;
899941

900942
build_prologue(&ctx);
@@ -920,13 +962,31 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
920962

921963
bpf_flush_icache(header, ctx.image + ctx.idx);
922964

923-
bpf_jit_binary_lock_ro(header);
965+
if (!prog->is_func || extra_pass) {
966+
if (extra_pass && ctx.idx != jit_data->ctx.idx) {
967+
pr_err_once("multi-func JIT bug %d != %d\n",
968+
ctx.idx, jit_data->ctx.idx);
969+
bpf_jit_binary_free(header);
970+
prog->bpf_func = NULL;
971+
prog->jited = 0;
972+
goto out_off;
973+
}
974+
bpf_jit_binary_lock_ro(header);
975+
} else {
976+
jit_data->ctx = ctx;
977+
jit_data->image = image_ptr;
978+
jit_data->header = header;
979+
}
924980
prog->bpf_func = (void *)ctx.image;
925981
prog->jited = 1;
926982
prog->jited_len = image_size;
927983

984+
if (!prog->is_func || extra_pass) {
928985
out_off:
929-
kfree(ctx.offset);
986+
kfree(ctx.offset);
987+
kfree(jit_data);
988+
prog->aux->jit_data = NULL;
989+
}
930990
out:
931991
if (tmp_blinded)
932992
bpf_jit_prog_release_other(prog, prog == orig_prog ?

0 commit comments

Comments
 (0)