From: Alexei Starovoitov may_goto loads and stores the count on each iteration, and each load waits for the previous store. Keep the state in x24, which the JIT doesn't use: subs x24, x24, #0x800, lsl #12 b.lo slow x24 is callee saved, so helpers and kfuncs preserve it. Neoverse V2: ns per iteration before after empty loop 2.15 0.48 load and store in arena 2.15 1.21 call of bpf_get_numa_node_id() 2.15 0.95 may_goto in callback of bpf_loop() 2.74 2.17 call of a subprog with may_goto 4.30 2.15 the same in a prog with a tail call 4.30 4.40 The last row is slower, since such progs pass the state through current. Signed-off-by: Alexei Starovoitov --- arch/arm64/net/bpf_jit_comp.c | 203 +++++++++++++++--- arch/arm64/net/bpf_timed_may_goto.S | 20 +- .../bpf/progs/verifier_arena_stack.c | 1 - .../bpf/progs/verifier_bpf_fastcall.c | 6 +- .../selftests/bpf/progs/verifier_may_goto_1.c | 2 +- .../selftests/bpf/progs/verifier_patch_list.c | 3 + 6 files changed, 187 insertions(+), 48 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index c3547c7e4c55..3ac287580d22 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -33,6 +33,7 @@ #define TMP_REG_3 (MAX_BPF_JIT_REG + 3) #define PRIVATE_SP (MAX_BPF_JIT_REG + 4) #define ARENA_VM_START (MAX_BPF_JIT_REG + 5) +#define MAY_GOTO_STATE (MAX_BPF_JIT_REG + 6) #define check_imm(bits, imm) do { \ if ((((imm) > 0) && ((imm) >> ((bits) - 1))) || \ @@ -76,6 +77,8 @@ static const int bpf2a64[] = { [PRIVATE_SP] = A64_R(27), /* callee saved register for kern_vm_start address */ [ARENA_VM_START] = A64_R(28), + /* callee saved register for the state of may_goto */ + [MAY_GOTO_STATE] = A64_R(24), }; struct jit_ctx { @@ -87,7 +90,7 @@ struct jit_ctx { int *landing_ex; /* the extable entry of each landing, see build_landings() */ int landing_idx; int nr_used_callee_reg; - u8 used_callee_reg[8]; /* r6~r9, fp, arena_vm_start */ + u8 used_callee_reg[9]; /* see find_used_callee_regs() */ __le32 *image; __le32 *ro_image; u32 stack_size; @@ -825,6 +828,11 @@ static void find_used_callee_regs(struct jit_ctx *ctx) ctx->fp_used = true; reg_used |= 16; } + + /* other functions take the state from the caller and return it */ + if (insn_is_may_goto_mov(insn) && insn->dst_reg == BPF_REG_AX && + (insn->off == BPF_MAY_GOTO_INIT || insn->off == BPF_MAY_GOTO_FILL)) + reg_used |= 32; } i = 0; @@ -846,6 +854,9 @@ static void find_used_callee_regs(struct jit_ctx *ctx) ctx->used_callee_reg[i++] = bpf2a64[PRIVATE_SP]; } + if (reg_used & 32) + ctx->used_callee_reg[i++] = bpf2a64[MAY_GOTO_STATE]; + if (ctx->arena_vm_start) ctx->used_callee_reg[i++] = bpf2a64[ARENA_VM_START]; @@ -1506,11 +1517,11 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic, bool unwind) * * Bit layout of `fixup` (32-bit): * - * +-----------+--------+---------+-------------+-----------+-----------+----------+ - * | 31-27 | 26-24 | 23 | 22 | 21 | 20-16 | 15-0 | - * | | | | | | | | - * | FIXUP_REG | Unused | UNWINDS | ARENA_WRITE | ARENA_ACC | ARENA_REG | OFFSET | - * +-----------+--------+---------+-------------+-----------+-----------+----------+ + * +-----------+--------+----------+---------+-------------+-----------+-----------+----------+ + * | 31-27 | 26-25 | 24 | 23 | 22 | 21 | 20-16 | 15-0 | + * | | | | | | | | | + * | FIXUP_REG | Unused | MAY_GOTO | UNWINDS | ARENA_WRITE | ARENA_ACC | ARENA_REG | OFFSET | + * +-----------+--------+----------+---------+-------------+-----------+-----------+----------+ * * - OFFSET (16 bits): Offset used to compute address for Load/Store instruction. * - ARENA_REG (5 bits): Register that is used to calculate the address for load/store when @@ -1522,6 +1533,7 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic, bool unwind) * - UNWINDS (1 bit): The frame holds nothing at the insn, and no unwind_goto follows it. The * handler goes to prog->aux->fault_pad, the landing pad that all such * accesses of the function share. + * - MAY_GOTO (1 bit): The state of may_goto is in its register. * - FIXUP_REG (5 bits): Destination register for the load instruction (cleared on fault) or set to * DONT_CLEAR if the instruction does not read into a register. */ @@ -1531,6 +1543,7 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic, bool unwind) #define BPF_ARENA_ACCESS BIT(21) #define BPF_ARENA_WRITE BIT(22) #define BPF_ARENA_UNWINDS BIT(23) +#define BPF_ARENA_MAY_GOTO BIT(24) #define BPF_FIXUP_REG_MASK GENMASK(31, 27) #define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */ @@ -1549,6 +1562,9 @@ bool ex_handler_bpf(const struct exception_table_entry *ex, addr = regs->regs[arena_reg] + off; pad = bpf_arena_fault(is_write, addr, regs->pc, end, ex->fixup & BPF_ARENA_UNWINDS); + /* The access is skipped. The next may_goto reads the clock. */ + if (pad == end && ex->fixup & BPF_ARENA_MAY_GOTO) + regs->regs[bpf2a64[MAY_GOTO_STATE]] &= BPF_MAY_GOTO_CNT_ONE - 1; } /* @@ -1633,6 +1649,8 @@ static int add_exception_handler(const struct bpf_verifier_env *env, ex->fixup |= BPF_ARENA_WRITE; if (bpf_insn_fault_unwinds(env, ctx->prog, insn - ctx->prog->insnsi)) ex->fixup |= BPF_ARENA_UNWINDS; + if (ctx->prog->aux->may_goto_in_reg) + ex->fixup |= BPF_ARENA_MAY_GOTO; /* * insn->src_reg/dst_reg holds the address in the arena region with upper 32-bits * being zero because of a preceding addr_space_cast(r, 0x0, 0x1) instruction. @@ -1656,15 +1674,125 @@ static int add_exception_handler(const struct bpf_verifier_env *env, return 0; } -/* Whether @insn, unwind_goto or cancel_goto, follows the call of a helper or a kfunc */ -static bool goto_after_helper(const struct bpf_insn *insn) +/* BPF_MAY_GOTO_MOV() */ +static void emit_may_goto_mov(int op, const struct bpf_insn *insn, struct jit_ctx *ctx) +{ + const int task_off = offsetof(struct task_struct, bpf_may_goto_state); + const u8 fp = bpf2a64[BPF_REG_FP], state = bpf2a64[MAY_GOTO_STATE]; + const u8 task = bpf2a64[TMP_REG_1], tmp = bpf2a64[TMP_REG_2]; + const u8 reg = insn->dst_reg == BPF_REG_AX ? state : bpf2a64[insn->dst_reg]; + + BUILD_BUG_ON(task_off % 8 || task_off > 0xfff * 8); + + if (op != BPF_MAY_GOTO_READ && op != BPF_MAY_GOTO_INIT) + emit(A64_MRS_SP_EL0(task), ctx); + switch (op) { + case BPF_MAY_GOTO_READ: + emit(A64_MOV(1, reg, state), ctx); + break; + case BPF_MAY_GOTO_INIT: + emit_a64_mov_i64(reg, BPF_MAY_GOTO_INIT_STATE, ctx); + break; + case BPF_MAY_GOTO_FILL: + emit(A64_LDR64I(reg, task, task_off), ctx); + break; + case BPF_MAY_GOTO_SPILL: + case BPF_MAY_GOTO_SPILL_CALL: + emit(A64_STR64I(reg, task, task_off), ctx); + break; + case BPF_MAY_GOTO_SAVE: + emit(A64_LDR64I(tmp, task, task_off), ctx); + emit_ldst(BPF_STX | BPF_MEM | BPF_DW, tmp, fp, insn->imm, bpf2a64[TMP_REG_3], ctx); + break; + case BPF_MAY_GOTO_RESTORE: + emit_ldst(BPF_LDX | BPF_MEM | BPF_DW, tmp, fp, insn->imm, bpf2a64[TMP_REG_3], ctx); + emit(A64_STR64I(tmp, task, task_off), ctx); + break; + } +} + +static bool call_needs_may_goto_fill(const struct bpf_prog *prog, const struct bpf_insn *call) { - const struct bpf_insn *call = insn - 1; + return call > prog->insnsi && insn_is_may_goto_mov(call - 1) && + call[-1].off == BPF_MAY_GOTO_SPILL_CALL; +} +/* The code at the return address of @call */ +static void emit_after_call(const struct bpf_insn *call, struct jit_ctx *ctx) +{ + emit(A64_MOV(1, bpf2a64[BPF_REG_0], A64_R(0)), ctx); + if (call_needs_may_goto_fill(ctx->prog, call)) + emit_may_goto_mov(BPF_MAY_GOTO_FILL, call - 1, ctx); +} + +static int after_call_len(const struct bpf_prog *prog, const struct bpf_insn *call) +{ + return call_needs_may_goto_fill(prog, call) ? 3 : 1; +} + +/* + * The slow path of may_goto, insn @i. It is among the landings. + * + * near: far: + * subs x24, x24, CNT_ONE subs x24, x24, CNT_ONE + * b.lo slow b.hs end + * bl slow + * end: + * ... + * slow: tbz x24, 0, 1f + * mov x24, EXPIRED + * b target + * 1: adr lr, end <- near form only + * b arch_bpf_timed_may_goto, which returns to end + * + * Pass 1 takes the far form, no later layout is longer, see find_tb(). ctx->tb marks the near form. + */ +static int build_may_goto_slowpath(int i, struct jit_ctx *ctx) +{ + const u8 state = bpf2a64[MAY_GOTO_STATE]; + long func_addr = (long)arch_bpf_timed_may_goto; + int end = ctx->offset[i + 1], jmp_offset = ctx->idx - (end - 1); + bool near; + + if (!ctx->image && jmp_offset + 3 < SZ_256K) + __set_bit(i, ctx->tb); + near = test_bit(i, ctx->tb); + check_imm26(jmp_offset); + if (ctx->image) + ctx->image[end - 1] = cpu_to_le32(near ? A64_B_(A64_COND_CC, jmp_offset) : + A64_BRANCH(A64_BR_OFF(jmp_offset), LINK)); + + emit(A64_TBZ(state, 0, 3), ctx); + emit(A64_MOVZ(1, state, BPF_MAY_GOTO_EXPIRED, 0), ctx); + jmp_offset = ctx->offset[i + 1 + ctx->prog->insnsi[i].imm] - ctx->idx; + check_imm26(jmp_offset); + emit(A64_B(jmp_offset), ctx); + if (near) + emit(A64_ADR(A64_LR, A64_BR_OFF(end - ctx->idx)), ctx); + if (should_emit_indirect_call(func_addr, ctx)) { + /* BTI C at the target takes BR of x16 */ + emit_addr_mov_i64(A64_R(16), func_addr, ctx); + emit(A64_BR(A64_R(16)), ctx); + } else { + emit(A64_BRANCH(func_addr - (long)&ctx->ro_image[ctx->idx], NOLINK), ctx); + } + return 0; +} + +/* The insn that @insn, unwind_goto or cancel_goto, belongs to */ +static const struct bpf_insn *pad_goto_call(const struct bpf_insn *insn) +{ /* 'call; unwind_goto; cancel_goto' */ - if (bpf_is_cancel_goto(insn) && bpf_is_unwind_goto(call)) - call--; - return call->code == (BPF_JMP | BPF_CALL) && call->src_reg != BPF_PSEUDO_CALL; + return insn - 1 - (bpf_is_cancel_goto(insn) && bpf_is_unwind_goto(insn - 1)); +} + +/* Whether @insn, unwind_goto or cancel_goto, needs the code after its call */ +static bool goto_has_landing(const struct bpf_prog *prog, const struct bpf_insn *insn) +{ + const struct bpf_insn *call = pad_goto_call(insn); + + return (call->code == (BPF_JMP | BPF_CALL) && call->src_reg != BPF_PSEUDO_CALL) || + call_needs_may_goto_fill(prog, call); } /* @@ -1725,7 +1853,7 @@ static int add_goto_exentry(struct jit_ctx *ctx, const struct bpf_insn *insn, in int i = insn - ctx->prog->insnsi; int target = ctx->offset[i + 1 + bpf_jmp_offset(insn)]; - if (goto_after_helper(insn)) { + if (goto_has_landing(ctx->prog, insn)) { *landing = ctx->exentry_idx; target = ret_addr; } @@ -1747,21 +1875,23 @@ static int add_goto_exentry(struct jit_ctx *ctx, const struct bpf_insn *insn, in * mov x8, x0; b cancel_pad <- cancel_exit returns here, key ret_addr - 2 * * build_insn() has added the entries. Each landing fills in the fixup of its - * entry. + * entry. The fill of the state of may_goto after a call is repeated like the mov. */ static int build_landings(struct jit_ctx *ctx) { const struct bpf_prog *prog = ctx->prog; int i, k = 0; - for (i = 1; i < prog->len; i++) { + for (i = 0; i < prog->len; i++) { const struct bpf_insn *insn = &prog->insnsi[i]; int ret_addr, jmp_offset; - if (!bpf_is_pad_goto(insn) || !goto_after_helper(insn)) + if (insn_is_jit_may_goto(insn) && build_may_goto_slowpath(i, ctx)) + return -EINVAL; + if (!bpf_is_pad_goto(insn) || !goto_has_landing(prog, insn)) continue; /* unwind_goto emits no code, so this also holds for the cancel_goto after it */ - ret_addr = ctx->offset[i] - 1; + ret_addr = ctx->offset[i] - after_call_len(prog, pad_goto_call(insn)); if (ctx->image) { struct exception_table_entry *ex; @@ -1770,7 +1900,7 @@ static int build_landings(struct jit_ctx *ctx) ex->fixup = (ctx->idx - ret_addr) * AARCH64_INSN_SIZE; } k++; - emit(A64_MOV(1, bpf2a64[BPF_REG_0], A64_R(0)), ctx); + emit_after_call(pad_goto_call(insn), ctx); jmp_offset = ctx->offset[i + 1 + bpf_jmp_offset(insn)] - ctx->idx; check_imm26(jmp_offset); emit(A64_B(jmp_offset), ctx); @@ -2078,6 +2208,9 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn emit(A64_MOVK(1, dst, upper_high, 48), ctx); } break; + } else if (insn_is_may_goto_mov(insn)) { + emit_may_goto_mov(off, insn, ctx); + break; } else if (insn_is_mov_percpu_addr(insn)) { if (dst != src) emit(A64_MOV(1, dst, src), ctx); @@ -2485,13 +2618,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn emit_kfunc_arg_moves(ctx, fm); } emit_call(func_addr, ctx); - /* - * Call to arch_bpf_timed_may_goto() is emitted by the - * verifier and called with custom calling convention with - * first argument and return value in BPF_REG_AX (x9). - */ - if (func_addr != (u64)arch_bpf_timed_may_goto) - emit(A64_MOV(1, r0, A64_R(0)), ctx); + emit_after_call(insn, ctx); break; } /* indirect call of a bpf subprog, dst holds its address */ @@ -2503,14 +2630,14 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn * counter and the private stack are callee saved. */ emit(A64_BLR(dst), ctx); - emit(A64_MOV(1, bpf2a64[BPF_REG_0], A64_R(0)), ctx); + emit_after_call(insn, ctx); break; /* tail call */ case BPF_JMP | BPF_TAIL_CALL: if (emit_bpf_tail_call(ctx)) return -EFAULT; break; - /* unwind_goto, cancel_goto. The verifier expanded may_goto. */ + /* may_goto, unwind_goto, cancel_goto */ case BPF_JMP | BPF_JCOND: case BPF_JMP32 | BPF_JCOND: { @@ -2518,9 +2645,18 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn /* 'call; unwind_goto; cancel_goto', and this is the second one */ bool after_unwind = cancel_goto && bpf_is_unwind_goto(insn - 1); int landing[2] = { -1, -1 }; - int ret_addr = ctx->idx - 1; - int j; - + int ret_addr, j; + + if (insn_is_jit_may_goto(insn)) { + emit(A64_SUBS_I(1, bpf2a64[MAY_GOTO_STATE], bpf2a64[MAY_GOTO_STATE], + BPF_MAY_GOTO_CNT_ONE), ctx); + if (!test_bit(i, ctx->tb)) + emit(A64_B_(A64_COND_CS, 2), ctx); + /* build_may_goto_slowpath() writes the branch */ + emit(A64_NOP, ctx); + break; + } + ret_addr = ctx->idx - after_call_len(ctx->prog, pad_goto_call(insn)); /* * The extable is sorted by address, and the entry of cancel_goto has * the lower one. So unwind_goto adds no entry when cancel_goto follows @@ -2916,7 +3052,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr for (i = 1; i < prog->len; i++) if (bpf_is_pad_goto(&prog->insnsi[i]) && - goto_after_helper(&prog->insnsi[i])) + goto_has_landing(prog, &prog->insnsi[i])) nr_landings++; if (nr_landings) { ctx.landing_ex = kvzalloc_objs(int, nr_landings); @@ -4145,6 +4281,11 @@ bool bpf_jit_supports_timed_may_goto(void) return true; } +bool bpf_jit_supports_may_goto_insn(void) +{ + return true; +} + bool bpf_jit_inlines_helper_call(s32 imm) { switch (imm) { diff --git a/arch/arm64/net/bpf_timed_may_goto.S b/arch/arm64/net/bpf_timed_may_goto.S index f8732b13bb1e..15f8452c4973 100644 --- a/arch/arm64/net/bpf_timed_may_goto.S +++ b/arch/arm64/net/bpf_timed_may_goto.S @@ -12,26 +12,22 @@ SYM_FUNC_START(arch_bpf_timed_may_goto) stp x8, x0, [sp, #16] stp x1, x2, [sp, #32] stp x3, x4, [sp, #48] - /* Save stack arguments 6 - 8 (x5-x7) */ + /* Save stack arguments 6 - 8 (x5-x7) and BPF_REG_AX (x9) */ stp x5, x6, [sp, #64] - str x7, [sp, #80] + stp x7, x9, [sp, #80] - /* - * The pointer to count and timestamp was passed in BPF_REG_AX (x9), - * pass it as the first argument in x0. - */ - mov x0, x9 - bl bpf_check_timed_may_goto - /* BPF_REG_AX(x9) will be stored into count, so move return value to it. */ - mov x9, x0 + /* The state is in x24, LR is the address after may_goto */ + mov x0, x24 + bl bpf_may_goto_slowpath + mov x24, x0 /* Restore BPF registers R0 - R5 (x8, x0-x4) */ ldp x8, x0, [sp, #16] ldp x1, x2, [sp, #32] ldp x3, x4, [sp, #48] - /* Restore stack arguments 6 - 8 (x5-x7) */ + /* Restore stack arguments 6 - 8 (x5-x7) and BPF_REG_AX (x9) */ ldp x5, x6, [sp, #64] - ldr x7, [sp, #80] + ldp x7, x9, [sp, #80] /* Restore FP and LR */ ldp x29, x30, [sp], #96 diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c index afe6dc7958b8..ce140f0cc8a2 100644 --- a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c +++ b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c @@ -1069,7 +1069,6 @@ static __naked __noinline __used long spin_cb(void) */ SEC("syscall") __description("arena stack: bpf_loop() ends when the time of may_goto is up") -__arch_x86_64 __success __retval(121) /* a call of the callback when the time is up returns 1 before it takes a stack */ __xlated("r0 &= 1") diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c index 5b3d760bd160..bdd462f2f039 100644 --- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c @@ -935,8 +935,9 @@ __naked void unconditional_jump_to_fill(void) SEC("raw_tp") __arch_x86_64 +__arch_arm64 __log_level(4) -__msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 8") +__msg("subprog 0 (may_goto_interaction_insn) main {{.*}} stack 8") __xlated("0: may_goto_init") __xlated("1: r1 = 1") __xlated("...") @@ -948,7 +949,7 @@ __xlated("exit") __xlated("r0 = 0") __xlated("exit") __success -__naked void may_goto_interaction_x86_64(void) +__naked void may_goto_interaction_insn(void) { asm volatile ( "r1 = 1;" @@ -966,7 +967,6 @@ __naked void may_goto_interaction_x86_64(void) } SEC("raw_tp") -__arch_arm64 __arch_riscv64 __arch_loongarch __log_level(4) diff --git a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c index aeff39968a4d..53dd54ad2762 100644 --- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c @@ -84,7 +84,6 @@ SEC("raw_tp") __description("may_goto batch with offsets 2/0") __load_if_JITed() __arch_s390x -__arch_arm64 __arch_riscv64 __arch_loongarch /* the main prog sets count, timestamp, owner */ @@ -128,6 +127,7 @@ SEC("raw_tp") __description("may_goto batch with offsets 2/0, JIT of may_goto") __load_if_JITed() __arch_x86_64 +__arch_arm64 __xlated("0: may_goto_init") /* when the time is up may_goto goes to the landing pad of the prog */ __xlated("1: may_goto pc+3") diff --git a/tools/testing/selftests/bpf/progs/verifier_patch_list.c b/tools/testing/selftests/bpf/progs/verifier_patch_list.c index 2a18f182c0db..75da37f72721 100644 --- a/tools/testing/selftests/bpf/progs/verifier_patch_list.c +++ b/tools/testing/selftests/bpf/progs/verifier_patch_list.c @@ -91,6 +91,7 @@ __naked void patch_list_backward(void) SEC("raw_tp") __description("patch list: may_goto between patched insns") __arch_x86_64 +__arch_arm64 __success __xlated("0: may_goto_init") __xlated("1: call") @@ -314,6 +315,7 @@ int patch_list_indirect(void) SEC("raw_tp") __description("patch list: far may_goto") __arch_x86_64 +__arch_arm64 __success __xlated("4: may_goto pc+32770") __xlated("...") @@ -400,6 +402,7 @@ static unsigned long patch_list_loop_at_start(void) SEC("raw_tp") __description("patch list: call of and jump to an insn with insns inserted in front") __arch_x86_64 +__arch_arm64 __success __xlated("22: call pc+10") __xlated("23: cancel_goto pc-15") -- 2.55.0