From: Alexei Starovoitov may_goto takes 10 insns with a load and a store of the count. Turn it into: sub r11, 0x800000 jl slow There is no free callee saved register, so use %r11. Spill it around helpers, kfuncs and the few insns that need it, like division by a constant. Other users of %r11 switch to %r10 in progs with may_goto. Trampolines preserve %r11 when they are attached to BPF functions. Trampolines on kernel functions and progs without may_goto don't change. Ice Lake: ns per iteration before after empty loop 1.81 0.29 load and store in arena 1.44 1.15 call of bpf_get_numa_node_id() 2.01 1.81 may_goto in callback of bpf_loop() 2.61 2.02 call of a subprog with may_goto 3.49 1.99 the same in a prog with a tail call 2.99 4.28 The last row is slower, since such progs pass the state through current. Signed-off-by: Alexei Starovoitov --- arch/x86/net/bpf_jit_comp.c | 362 +++++++++++++++--- arch/x86/net/bpf_timed_may_goto.S | 17 +- kernel/bpf/fixups.c | 2 +- .../bpf/progs/verifier_arena_stack.c | 7 +- .../bpf/progs/verifier_bpf_fastcall.c | 29 +- .../selftests/bpf/progs/verifier_may_goto_1.c | 29 +- .../selftests/bpf/progs/verifier_patch_list.c | 96 ++--- 7 files changed, 394 insertions(+), 148 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index c3edda0392fd..34d6fe93d78d 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -134,7 +134,7 @@ static int bpf_size_to_x86_bytes(int bpf_size) #define X86_JG 0x7F /* Pick a register outside of BPF range for JIT internal work */ -#define AUX_REG (MAX_BPF_JIT_REG + 1) +#define X86_REG_R11 (MAX_BPF_JIT_REG + 1) #define X86_REG_R9 (MAX_BPF_JIT_REG + 2) #define X86_REG_R12 (MAX_BPF_JIT_REG + 3) @@ -163,7 +163,7 @@ static const int reg2hex[] = { [BPF_REG_FP] = 5, /* RBP readonly */ [BPF_REG_ARENA_SP] = 1, /* R9 */ [BPF_REG_AX] = 2, /* R10 temp register */ - [AUX_REG] = 3, /* R11 temp register */ + [X86_REG_R11] = 3, /* R11 state of may_goto, or AUX_REG */ [X86_REG_R9] = 1, /* R9 register, 6th function argument */ [X86_REG_R12] = 4, /* R12 callee saved */ }; @@ -198,7 +198,7 @@ static bool is_atomic_fetch_op(const struct bpf_insn *insn) static bool is_ereg(u32 reg) { return (1 << reg) & (BIT(BPF_REG_5) | - BIT(AUX_REG) | + BIT(X86_REG_R11) | BIT(BPF_REG_7) | BIT(BPF_REG_8) | BIT(BPF_REG_9) | @@ -292,9 +292,10 @@ int bpf_arch_text_invalidate(void *dst, size_t len) /* * The landing of an unwind_goto or cancel_goto, see BPF_JCOND in do_jit(): the * @len bytes of @code that follow the native call, and a jump to insn @to. + * Also the slow path of may_goto. */ struct jit_landing { - u8 code[16]; + u8 code[64]; u8 len; int to; }; @@ -1347,7 +1348,7 @@ static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog, /* * The one-operand x86 mul/imul forms use RAX as the implicit first * operand and store the 128-bit product in RDX:RAX. - * The JIT uses AUX_REG as the explicit second operand. + * The JIT uses R11 as the explicit second operand. */ static void emit_hmul_aux(u8 **pprog, u32 dst_reg, bool is_signed) { @@ -1363,8 +1364,8 @@ static void emit_hmul_aux(u8 **pprog, u32 dst_reg, bool is_signed) if (save_rax) emit_mov_reg(&prog, true, BPF_REG_0, dst_reg); - maybe_emit_1mod(&prog, AUX_REG, true); - EMIT2(0xF7, add_1reg(mul_modrm_base, AUX_REG)); + maybe_emit_1mod(&prog, X86_REG_R11, true); + EMIT2(0xF7, add_1reg(mul_modrm_base, X86_REG_R11)); if (save_rdx) emit_mov_reg(&prog, true, dst_reg, BPF_REG_3); @@ -1733,19 +1734,19 @@ static int emit_atomic_rmw_index(u8 **pprog, u32 atomic_op, u32 size, } /* - * AUX_REG = dst_reg, with the low bits of dst_reg + off in the top bits. r12 + AUX_REG + off + * tmp = dst_reg, with the low bits of dst_reg + off in the top bits. r12 + tmp + off * is not canonical when it's not aligned: the access faults, no split lock is taken. */ -static void emit_arena_align_check(u8 **pprog, const struct bpf_insn *insn, u32 dst_reg) +static void emit_arena_align_check(u8 **pprog, const struct bpf_insn *insn, u32 dst_reg, u32 tmp) { int size = BPF_LDST_BYTES(insn); u8 *prog = *pprog; - /* lea r11, [dst_reg + off % size] */ - EMIT2(add_2mod(0x48, dst_reg, AUX_REG), 0x8D); - emit_insn_suffix(&prog, dst_reg, AUX_REG, insn->off & (size - 1)); - emit_shift_imm(&prog, BPF_LSH, AUX_REG, 64 - ilog2(size), true); - emit_alu_reg(&prog, BPF_OR, AUX_REG, dst_reg, true); + /* lea tmp, [dst_reg + off % size] */ + EMIT2(add_2mod(0x48, dst_reg, tmp), 0x8D); + emit_insn_suffix(&prog, dst_reg, tmp, insn->off & (size - 1)); + emit_shift_imm(&prog, BPF_LSH, tmp, 64 - ilog2(size), true); + emit_alu_reg(&prog, BPF_OR, tmp, dst_reg, true); *pprog = prog; } @@ -1822,7 +1823,7 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size, * rest of the sequence and restores R0 itself, see FIXUP_ARENA_R0_IN_AX. */ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, - u32 src_reg, int index_reg, u8 **fault) + u32 src_reg, u32 aux, int index_reg, u8 **fault) { bool is64 = BPF_SIZE(insn->code) == BPF_DW; u8 *branch_target, *prog = *pprog; @@ -1832,33 +1833,33 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, /* Load old value */ if (index_reg >= 0) - emit_arena_align_check(&prog, insn, dst_reg); + emit_arena_align_check(&prog, insn, dst_reg, aux); if (fault) fault[0] = prog; if (index_reg < 0) emit_ldx(&prog, BPF_SIZE(insn->code), BPF_REG_0, dst_reg, insn->off); else - emit_ldx_index(&prog, BPF_SIZE(insn->code), BPF_REG_0, AUX_REG, + emit_ldx_index(&prog, BPF_SIZE(insn->code), BPF_REG_0, aux, index_reg, insn->off); /* * Perform the (commutative) operation locally, put the result in - * the AUX_REG. + * @aux. */ - emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0); - maybe_emit_mod(&prog, AUX_REG, src_reg, is64); + emit_mov_reg(&prog, is64, aux, BPF_REG_0); + maybe_emit_mod(&prog, aux, src_reg, is64); EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)], - add_2reg(0xC0, AUX_REG, src_reg)); + add_2reg(0xC0, aux, src_reg)); /* Attempt to swap in new value */ if (fault) fault[1] = prog; if (index_reg < 0) - err = emit_atomic_rmw(&prog, BPF_CMPXCHG, dst_reg, AUX_REG, + err = emit_atomic_rmw(&prog, BPF_CMPXCHG, dst_reg, aux, insn->off, BPF_SIZE(insn->code)); else err = emit_atomic_rmw_index(&prog, BPF_CMPXCHG, BPF_SIZE(insn->code), - dst_reg, AUX_REG, index_reg, insn->off); + dst_reg, aux, index_reg, insn->off); if (WARN_ON(err)) return err; @@ -1881,11 +1882,13 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, * * Bit layout of `fixup` (32-bit): * - * +-----------+-------------+----------+---------+--------+-----------+---------+----------+ - * | 31 | 30 | 29 | 28 | 27-24 | 23-16 | 15-8 | 7-0 | - * | | | | | | | | | - * | ARENA_ACC | ARENA_WRITE | R0_IN_AX | UNWINDS | Unused | ARENA_REG | DST_REG | INSN_LEN | - * +-----------+-------------+----------+---------+--------+-----------+---------+----------+ + * +-----------+-------------+----------+---------+------------+-----------+---------+----------+ + * | 31 | 30 | 29 | 28 | 27 | 23-16 | 15-8 | 7-0 | + * | | | | | | | | | + * | ARENA_ACC | ARENA_WRITE | R0_IN_AX | UNWINDS | R11_PUSHED | ARENA_REG | DST_REG | INSN_LEN | + * +-----------+-------------+----------+---------+------------+-----------+---------+----------+ + * + * Bits 26-24 are unused. * * - INSN_LEN (8 bits): How far past the faulting insn the BPF insn ends. That is its own length * for a single-insn access, but the distance to the end of the whole @@ -1907,6 +1910,7 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, * - 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. + * - R11_PUSHED (1 bit): The CMPXCHG loop pushed R11, the state of may_goto. The handler pops it. * * Bit layout of `data` (32-bit): * @@ -1924,12 +1928,24 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, #define FIXUP_INSN_LEN_MASK GENMASK(7, 0) #define FIXUP_REG_MASK GENMASK(15, 8) #define FIXUP_ARENA_REG_MASK GENMASK(23, 16) +#define FIXUP_ARENA_R11_PUSHED BIT(27) #define FIXUP_ARENA_UNWINDS BIT(28) #define FIXUP_ARENA_R0_IN_AX BIT(29) #define FIXUP_ARENA_WRITE BIT(30) #define FIXUP_ARENA_ACCESS BIT(31) #define DATA_ARENA_OFFSET_MASK GENMASK(31, 16) +/* The access was skipped. The next may_goto reads the clock. */ +static void may_goto_clear_cnt(struct pt_regs *regs) +{ + struct bpf_prog *prog; + + guard(rcu)(); + prog = bpf_prog_ksym_find(regs->ip); + if (prog && prog->aux->may_goto_in_reg) + regs->r11 &= BPF_MAY_GOTO_CNT_ONE - 1; +} + bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) { u32 reg = FIELD_GET(FIXUP_REG_MASK, x->fixup); @@ -1947,6 +1963,12 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) addr = *(unsigned long *)((void *)regs + arena_reg) + off; pad = bpf_arena_fault(is_write, addr, regs->ip, end, x->fixup & FIXUP_ARENA_UNWINDS); + if (x->fixup & FIXUP_ARENA_R11_PUSHED) { + regs->r11 = *(unsigned long *)regs->sp; + regs->sp += sizeof(long); + } + if (pad == end) + may_goto_clear_cnt(regs); } /* @@ -2233,9 +2255,9 @@ static int emit_kfunc_arg_moves(const struct btf_func_model *fm, s32 stack_base, * BPF_REG_AX only to pass one stack slot to another. */ if (src == BPF_JIT_ARG_TMP) { - reg = AUX_REG; + reg = X86_REG_R11; } else if (src_mem) { - reg = dst == BPF_JIT_ARG_TMP ? AUX_REG : + reg = dst == BPF_JIT_ARG_TMP ? X86_REG_R11 : dst_mem ? BPF_REG_AX : x86_arg_reg[dst]; emit_ldx(&prog, BPF_DW, reg, BPF_REG_FP, stack_base + (src - nreg) * 8); @@ -2245,7 +2267,7 @@ static int emit_kfunc_arg_moves(const struct btf_func_model *fm, s32 stack_base, /* And leave it where the argument belongs. */ if (dst == BPF_JIT_ARG_TMP) - emit_mov_reg(&prog, true, AUX_REG, reg); + emit_mov_reg(&prog, true, X86_REG_R11, reg); else if (dst_mem) emit_stx(&prog, BPF_DW, BPF_REG_FP, reg, stack_base + (dst - nreg) * 8); @@ -2503,19 +2525,25 @@ static bool call_has_cancel_goto(const struct bpf_insn *call, int cnt) * unwind_exit, cancel_exit: replace the return address of this frame with what * @target returns for it, bpf_unwind_target() or bpf_cancel_target(). The * epilogue then returns there. r0 and r2 are returned, so they are saved - * around the call. So is BPF_REG_ARENA_SP, when it's used. + * around the call. So are BPF_REG_ARENA_SP and the state of may_goto, when they're used. */ -static int emit_unwind_ret_addr(u8 **pprog, u8 *ip, bool arena_sp, void *target) +static int emit_unwind_ret_addr(u8 **pprog, u8 *ip, const struct bpf_prog_aux *aux, void *target) { + bool has_may_goto = aux->main_prog_aux->has_may_goto; + bool arena_sp = aux->arena_stack_size; u8 *prog = *pprog, *start = prog; EMIT2(0x50, 0x56); /* push rax; push rsi */ if (arena_sp) push_r9(&prog); + if (has_may_goto) + EMIT2(0x41, 0x53); /* push r11 */ EMIT4(0x48, 0x8B, 0x7D, 0x08); /* mov rdi, [rbp + 8] */ if (emit_rsb_call(&prog, target, ip + (prog - start))) return -EINVAL; EMIT4(0x48, 0x89, 0x45, 0x08); /* mov [rbp + 8], rax */ + if (has_may_goto) + EMIT2(0x41, 0x5B); /* pop r11 */ if (arena_sp) pop_r9(&prog); EMIT2(0x5E, 0x58); /* pop rsi; pop rax */ @@ -2523,6 +2551,133 @@ static int emit_unwind_ret_addr(u8 **pprog, u8 *ip, bool arena_sp, void *target) return 0; } +/* BPF_MAY_GOTO_MOV(). @reg is dst_reg of @insn, with FP mapped to R9 on a private stack. */ +static void emit_may_goto_mov(u8 **pprog, int op, const struct bpf_insn *insn, u32 reg) +{ + int task_off = offsetof(struct task_struct, bpf_may_goto_state); + u8 *prog = *pprog; + + if (reg == BPF_REG_AX) + reg = X86_REG_R11; + if (op != BPF_MAY_GOTO_READ && op != BPF_MAY_GOTO_INIT) { + /* mov r10, gs:[current_task] */ + if (IS_ENABLED(CONFIG_SMP)) + EMIT1(0x65); + EMIT4_off32(0x4C, 0x8B, 0x14, 0x25, (u32)(unsigned long)¤t_task); + } + switch (op) { + case BPF_MAY_GOTO_READ: + emit_mov_reg(&prog, true, reg, X86_REG_R11); + break; + case BPF_MAY_GOTO_INIT: + emit_mov_imm64(&prog, reg, BPF_MAY_GOTO_INIT_STATE >> 32, + (u32)BPF_MAY_GOTO_INIT_STATE); + break; + case BPF_MAY_GOTO_FILL: + emit_ldx(&prog, BPF_DW, reg, BPF_REG_AX, task_off); + break; + case BPF_MAY_GOTO_SPILL: + case BPF_MAY_GOTO_SPILL_CALL: + emit_stx(&prog, BPF_DW, BPF_REG_AX, reg, task_off); + break; + case BPF_MAY_GOTO_SAVE: + emit_ldx(&prog, BPF_DW, BPF_REG_AX, BPF_REG_AX, task_off); + emit_stx(&prog, BPF_DW, reg, BPF_REG_AX, insn->imm); + break; + case BPF_MAY_GOTO_RESTORE: + /* push qword ptr [reg + imm]; pop qword ptr [r10 + task_off] */ + EMIT2(add_1mod(0x40, reg), 0xFF); + emit_insn_suffix(&prog, reg, BPF_REG_2 /* FF /6 is push */, insn->imm); + EMIT3_off32(0x41, 0x8F, 0x82, task_off); + break; + } + *pprog = prog; +} + +static bool call_needs_may_goto_fill(const struct bpf_prog *prog, const struct bpf_insn *call) +{ + return call > prog->insnsi && insn_is_may_goto_mov(call - 1) && + call[-1].off == BPF_MAY_GOTO_SPILL_CALL; +} + +/* After a call the state is in the task, in the frame at @off, or the callee returned it in R11 */ +static void emit_fill_after_call(u8 **pprog, const struct bpf_prog *bpf_prog, + const struct bpf_insn *call, int off) +{ + if (call_needs_may_goto_fill(bpf_prog, call)) + emit_may_goto_mov(pprog, BPF_MAY_GOTO_FILL, call - 1, BPF_REG_AX); + else if (off) + emit_ldx(pprog, BPF_DW, X86_REG_R11, BPF_REG_FP, off); +} + +/* In a prog with may_goto the code of @insn leaves R11 alone, or restores it itself */ +static bool insn_preserves_r11(const struct bpf_prog *prog, const struct bpf_insn *insn) +{ + u8 mode = BPF_MODE(insn->code); + + switch (BPF_CLASS(insn->code)) { + case BPF_ALU: + case BPF_ALU64: + switch (BPF_OP(insn->code)) { + case BPF_ADD: + case BPF_SUB: + case BPF_AND: + case BPF_OR: + case BPF_XOR: + case BPF_NEG: + case BPF_MOV: + case BPF_END: + return true; + case BPF_MUL: + return !bpf_insn_is_hmul(insn); + case BPF_DIV: + case BPF_MOD: + return BPF_SRC(insn->code) == BPF_X && + insn->src_reg != BPF_REG_0 && insn->src_reg != BPF_REG_3; + case BPF_LSH: + case BPF_RSH: + case BPF_ARSH: + return BPF_SRC(insn->code) == BPF_K || boot_cpu_has(X86_FEATURE_BMI2); + } + return false; + case BPF_LD: + return mode == BPF_IMM; + case BPF_LDX: + return mode == BPF_MEM || mode == BPF_PROBE_MEM || mode == BPF_PROBE_MEM32 || + mode == BPF_MEMSX || mode == BPF_PROBE_MEMSX || mode == BPF_PROBE_MEM32SX; + case BPF_ST: + return mode == BPF_MEM || mode == BPF_PROBE_MEM32 || mode == BPF_NOSPEC; + case BPF_STX: + return mode == BPF_MEM || mode == BPF_PROBE_MEM32 || mode == BPF_PROBE_ATOMIC || + (mode == BPF_ATOMIC && !is_atomic_fetch_op(insn)); + } + if (insn->code != (BPF_JMP | BPF_CALL) || call_needs_may_goto_fill(prog, insn)) + return true; + /* The target of a tail call returns for the subprog. It is a prog of its own. */ + return insn->src_reg == BPF_PSEUDO_CALL && !prog->aux->tail_call_reachable; +} + +/* + * Emit the slow path of may_goto into @code, which runs at @ip. @next_ip is the insn after + * may_goto. The caller adds the jump to the target of may_goto. + */ +static int emit_may_goto_slowpath(u8 *code, u8 *ip, u8 *next_ip) +{ + u8 *prog = code; + + EMIT3(0x4C, 0x8D, 0x15); /* lea r10, [next_ip] */ + EMIT(next_ip - (ip + (prog - code) + 4), 4); + if (emit_rsb_call(&prog, arch_bpf_timed_may_goto, ip + (prog - code))) + return -EINVAL; + EMIT4(0x41, 0xF6, 0xC2, BPF_MAY_GOTO_EXPIRED); /* test r10b, 1 */ + EMIT2(0x0F, X86_JE + 0x10); /* je next_ip */ + EMIT(next_ip - (ip + (prog - code) + 4), 4); + return prog - code; +} + +/* R11 as a temporary. The JIT fails if insn_preserves_r11() is true for the insn. */ +#define AUX_REG ({ r11_used = true; X86_REG_R11; }) + static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image, int oldproglen, struct jit_context *ctx, bool jmp_padding) { @@ -2540,6 +2695,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * int ilen, moved, proglen = 0; u8 *ip, *prog = temp; u32 stack_depth; + /* R11 has the state of may_goto, so R10 is the temporary where one is enough */ + bool has_may_goto = bpf_prog->aux->main_prog_aux->has_may_goto, r11_used; + u32 tmp = has_may_goto ? BPF_REG_AX : X86_REG_R11; + /* the spill slot of R11. spill_off is 0 when the insn needs no spill. */ + int r11_off = 0, spill_off; int callee_saved_size; s32 outgoing_arg_base; /* the program uses BPF_REG_ARENA_SP, which is R9 */ @@ -2548,7 +2708,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * bool arena_sp_save = arena_sp && calls_clobber_arena_sp(bpf_prog); s32 arena_sp_off = 0; /* the bytes emitted after the native call of the last call insn */ - u8 after_call[16], *ret_addr; + u8 after_call[sizeof_field(struct jit_landing, code)], *ret_addr; /* how many of them the landing of unwind_goto repeats, and that of cancel_goto */ int after_call_len = 0, after_call_cancel_len = 0; /* the number of landings in this pass, and their size in bytes so far */ @@ -2563,6 +2723,19 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * stack_depth = 0; } + /* + * A function with the state in R11 spills it around the insns that clobber it. + * Other subprogs save it once, for their callers. A main prog need not preserve it. + */ + if (bpf_prog->aux->may_goto_in_reg || (has_may_goto && bpf_is_subprog(bpf_prog))) { + for (i = 0; i < insn_cnt && insn_preserves_r11(bpf_prog, insn + i); i++) + ; + if (i < insn_cnt) { + stack_depth = round_up(stack_depth, 8) + 8; + r11_off = -stack_depth; + } + } + /* * Follow x86-64 calling convention for both BPF-to-BPF and * kfunc calls: @@ -2656,6 +2829,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * if (priv_frame_ptr) emit_priv_frame_ptr(&prog, priv_frame_ptr); + if (r11_off && !bpf_prog->aux->may_goto_in_reg) + emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R11, r11_off); + ilen = prog - temp; if (rw_image) memcpy(rw_image + proglen, temp, ilen); @@ -2678,7 +2854,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * u8 *func; int nops; u32 jmp_op = BPF_OP(insn->code); - u32 fault_unwinds; + u32 fixup_flags; s32 cmp_imm; if (priv_frame_ptr) { @@ -2692,13 +2868,20 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1)) EMIT_ENDBR(); + r11_used = false; + spill_off = 0; + if (bpf_prog->aux->may_goto_in_reg && !insn_preserves_r11(bpf_prog, insn)) + spill_off = r11_off; + if (spill_off) + emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R11, spill_off); + ip = image + addrs[i - 1] + (prog - temp); insn_idx = i - 1 + bpf_prog->aux->subprog_start; accesses_stack_only = env ? !env->insn_aux_data[insn_idx].non_stack_access : false; - fault_unwinds = bpf_insn_fault_unwinds(env, bpf_prog, i - 1) ? - FIXUP_ARENA_UNWINDS : 0; + fixup_flags = bpf_insn_fault_unwinds(env, bpf_prog, i - 1) ? + FIXUP_ARENA_UNWINDS : 0; switch (insn->code) { /* ALU */ @@ -2720,16 +2903,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * if (insn_is_cast_user(insn)) { /* mov dst_reg32, src_reg32 to zero extend the low half */ emit_mov_reg(&prog, false, dst_reg, src_reg); - /* mov r11, the high half of user_vm_start; or r11, dst_reg */ - emit_mov_imm64(&prog, AUX_REG, user_vm_start >> 32, 0); - emit_alu_reg(&prog, BPF_OR, AUX_REG, dst_reg, true); + /* mov tmp, the high half of user_vm_start; or tmp, dst_reg */ + emit_mov_imm64(&prog, tmp, user_vm_start >> 32, 0); + emit_alu_reg(&prog, BPF_OR, tmp, dst_reg, true); /* test dst_reg32, dst_reg32; a NULL pointer stays NULL */ maybe_emit_mod(&prog, dst_reg, dst_reg, false); EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg)); - /* cmovne dst_reg, r11 */ + /* cmovne dst_reg, tmp */ /* WARNING: Intel swapped src/dst register encoding in CMOVcc !!! */ - maybe_emit_mod(&prog, AUX_REG, dst_reg, true); - EMIT3(0x0F, 0x45, add_2reg(0xC0, AUX_REG, dst_reg)); + maybe_emit_mod(&prog, tmp, dst_reg, true); + EMIT3(0x0F, 0x45, add_2reg(0xC0, tmp, dst_reg)); break; } else if (insn_is_mov_percpu_addr(insn)) { /* mov , (if necessary) */ @@ -2741,6 +2924,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * EMIT((u32)(unsigned long)&this_cpu_off, 4); #endif break; + } else if (insn_is_may_goto_mov(insn)) { + emit_may_goto_mov(&prog, insn->off, insn, dst_reg); + break; } fallthrough; case BPF_ALU | BPF_MOV | BPF_X: @@ -3194,7 +3380,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * image + proglen + (start_of_ldx - temp), prog - start_of_ldx, fixup_reg, arena_reg, (is_write ? FIXUP_ARENA_WRITE : 0) | - fault_unwinds, insn->off); + fixup_flags, insn->off); if (err) return err; } @@ -3250,18 +3436,18 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * u8 *end_of_jmp; /* - * lea r11, [src_reg + insn->off - VSYSCALL_ADDR] + * lea tmp, [src_reg + insn->off - VSYSCALL_ADDR] * * VSYSCALL_ADDR is a small negative number, so the * whole offset fits into the 32-bit displacement. */ BUILD_BUG_ON((long)(s32)VSYSCALL_ADDR != (long)VSYSCALL_ADDR); - EMIT2(add_2mod(0x48, src_reg, AUX_REG), 0x8D); - emit_insn_suffix(&prog, src_reg, AUX_REG, + EMIT2(add_2mod(0x48, src_reg, tmp), 0x8D); + emit_insn_suffix(&prog, src_reg, tmp, insn->off - (s32)VSYSCALL_ADDR); - /* shr r11, fls64(limit) */ - emit_shift_imm(&prog, BPF_RSH, AUX_REG, fls64(limit), true); + /* shr tmp, fls64(limit) */ + emit_shift_imm(&prog, BPF_RSH, tmp, fls64(limit), true); /* if not zero, goto load */ EMIT2(X86_JNE, 0); @@ -3371,7 +3557,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * } if (is_atomic_fetch) { err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg, - real_src_reg, -1, NULL); + real_src_reg, AUX_REG, -1, NULL); if (err) return err; break; @@ -3409,10 +3595,17 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * if (dst_reg == BPF_REG_0) real_dst_reg = BPF_REG_AX; + if (has_may_goto) { + EMIT2(0x41, 0x53); /* push r11 */ + fixup_flags |= FIXUP_ARENA_R11_PUSHED; + } err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg, - real_src_reg, X86_REG_R12, fault); + real_src_reg, X86_REG_R11, + X86_REG_R12, fault); if (err) return err; + if (has_may_goto) + EMIT2(0x41, 0x5B); /* pop r11 */ /* * The page may be unmapped between the load and the @@ -3428,7 +3621,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * prog - fault[j], reg2pt_regs[real_src_reg], reg2pt_regs[real_dst_reg], - FIXUP_ARENA_WRITE | fault_unwinds | + FIXUP_ARENA_WRITE | fixup_flags | FIXUP_ARENA_R0_IN_AX, insn->off); if (err) return err; @@ -3436,7 +3629,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * break; } if (!bpf_atomic_is_load_store(insn)) - emit_arena_align_check(&prog, insn, dst_reg); + emit_arena_align_check(&prog, insn, dst_reg, tmp); start_of_ldx = prog; if (bpf_atomic_is_load_store(insn)) @@ -3445,7 +3638,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * src_reg, X86_REG_R12, insn->off); else err = emit_atomic_rmw_index(&prog, insn->imm, BPF_SIZE(insn->code), - AUX_REG, src_reg, X86_REG_R12, + tmp, src_reg, X86_REG_R12, insn->off); if (err) return err; @@ -3522,6 +3715,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * emit_sub_rsp(&prog, 8); if (arena_sp && call_clobbers_arena_sp(insn)) emit_ldx(&prog, BPF_DW, BPF_REG_ARENA_SP, BPF_REG_FP, arena_sp_off); + emit_fill_after_call(&prog, bpf_prog, insn, spill_off); /* * The landing of cancel_goto ends here: after cancel_exit RDX * is not the second half of what the kfunc returned. @@ -3560,11 +3754,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * ret_addr = prog; if (priv_frame_ptr) pop_r9(&prog); + emit_fill_after_call(&prog, bpf_prog, insn, spill_off); after_call_len = after_call_cancel_len = prog - ret_addr; memcpy(after_call, ret_addr, after_call_len); 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: { bool cancel_goto = bpf_is_cancel_goto(insn); @@ -3577,6 +3772,25 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * int len = cancel_goto ? after_call_cancel_len : after_call_len; struct jit_landing *landing; + if (insn_is_jit_may_goto(insn)) { + if (nr_landings >= ctx->max_landings) + return -EFAULT; + /* sub r11, BPF_MAY_GOTO_CNT_ONE; jl slow. No state is negative. */ + EMIT3_off32(0x49, 0x81, 0xEB, BPF_MAY_GOTO_CNT_ONE); + target = addrs[insn_cnt] + tail; + EMIT2_off32(0x0F, X86_JL + 0x10, target - addrs[i]); + landing = &ctx->landings[nr_landings++]; + /* target is an estimate until there is an image */ + len = emit_may_goto_slowpath(landing->code, + image ? image + target : NULL, + image + addrs[i]); + if (len < 0) + return len; + landing->len = len; + landing->to = to; + tail += len + 5; + break; + } if (BPF_CLASS(insn[-1 - after_unwind].code) != BPF_JMP) { if (cancel_goto) { pr_err("bpf_jit: cancel_goto follows no call\n"); @@ -3897,10 +4111,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * case BPF_JMP | BPF_EXIT: if (src_reg == BPF_UNWIND_EXIT && bpf_prog_can_unwind(bpf_prog) && - emit_unwind_ret_addr(&prog, ip, arena_sp, bpf_unwind_target)) + emit_unwind_ret_addr(&prog, ip, bpf_prog->aux, bpf_unwind_target)) return -EINVAL; if (src_reg == BPF_CANCEL_EXIT && bpf_prog_can_unwind(bpf_prog) && - emit_unwind_ret_addr(&prog, ip, arena_sp, bpf_cancel_target)) + emit_unwind_ret_addr(&prog, ip, bpf_prog->aux, bpf_cancel_target)) return -EINVAL; if (seen_exit) { jmp_offset = ctx->cleanup_addr - addrs[i] + moved; @@ -3914,6 +4128,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * if (emit_spectre_bhb_barrier(&prog, ip, bpf_prog)) return -EINVAL; } + if (r11_off && !bpf_prog->aux->may_goto_in_reg) + emit_ldx(&prog, BPF_DW, X86_REG_R11, BPF_REG_FP, r11_off); /* Deallocate outgoing args 7+ area and the slot of BPF_REG_ARENA_SP. */ emit_add_rsp(&prog, outgoing_rsp + (arena_sp_save ? 8 : 0)); pop_callee_regs(&prog, callee_regs_used); @@ -3936,6 +4152,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * return -EINVAL; } + if (WARN_ON_ONCE(has_may_goto && r11_used && + insn_preserves_r11(bpf_prog, insn))) + return -EFAULT; + if (spill_off && BPF_CLASS(insn->code) != BPF_JMP) + emit_ldx(&prog, BPF_DW, X86_REG_R11, BPF_REG_FP, spill_off); + ilen = prog - temp; if (ilen > BPF_MAX_INSN_SIZE) { pr_err("bpf_jit: fatal insn size error\n"); @@ -4488,7 +4710,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im int i, ret, nr_regs, stack_size = 0; u8 pos_of_slot[MAX_BPF_FUNC_ARG_SLOTS]; int regs_off, func_meta_off, ip_off, run_ctx_off, arg_stack_off, rbx_off; - int tcc_off = 0; + int tcc_off = 0, r11_off = 0; struct bpf_tramp_nodes *fentry = &tnodes[BPF_TRAMP_FENTRY]; struct bpf_tramp_nodes *fexit = &tnodes[BPF_TRAMP_FEXIT]; struct bpf_tramp_nodes *fmod_ret = &tnodes[BPF_TRAMP_MODIFY_RETURN]; @@ -4541,6 +4763,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im * * RBP - tcc_off [ tail_call_cnt_ptr ] BPF_TRAMP_F_TAIL_CALL_CTX * + * RBP - r11_off [ r11 value ] the target is a BPF function + * * [ stack slotN ] BPF_TRAMP_F_CALL_ORIG, * [ ... ] the incoming stack * [ stack slot2 ] arguments, alignment @@ -4580,6 +4804,12 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im tcc_off = stack_size; } + /* BPF functions take and return the state of may_goto in R11 */ + if (is_bpf_text_address((long)func_addr)) { + stack_size += 8; + r11_off = stack_size; + } + if (nr_regs > 6 && (flags & BPF_TRAMP_F_CALL_ORIG)) { /* the space that used to pass arguments on-stack */ stack_size += get_nr_stack_slots(pos_of_slot, nr_regs) * 8; @@ -4633,6 +4863,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im if (flags & BPF_TRAMP_F_TAIL_CALL_CTX) /* mov QWORD PTR [rbp - tcc_off], rax */ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -tcc_off); + if (r11_off) + emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R11, -r11_off); /* mov QWORD PTR [rbp - rbx_off], rbx */ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_6, -rbx_off); @@ -4695,6 +4927,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im */ LOAD_TRAMP_TAIL_CALL_CNT_PTR(tcc_off); } + if (r11_off) + emit_ldx(&prog, BPF_DW, X86_REG_R11, BPF_REG_FP, -r11_off); if (flags & BPF_TRAMP_F_ORIG_STACK) { emit_ldx(&prog, BPF_DW, BPF_REG_6, BPF_REG_FP, 8); @@ -4708,6 +4942,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im } /* remember return value in a stack for bpf prog to access */ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8); + if (r11_off) + emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R11, -r11_off); } if (fmod_ret->nr_nodes) { @@ -4765,6 +5001,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8); emit_ldx(&prog, BPF_DW, BPF_REG_6, BPF_REG_FP, -rbx_off); + if (r11_off) + emit_ldx(&prog, BPF_DW, X86_REG_R11, BPF_REG_FP, -r11_off); EMIT1(0xC9); /* leave */ im->ksym.fp_end = prog - (u8 *)rw_image; @@ -5051,7 +5289,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr if (!addrs) goto out_addrs; for (i = 0; i < prog->len; i++) - ctx.max_landings += bpf_is_pad_goto(&prog->insnsi[i]); + ctx.max_landings += bpf_is_pad_goto(&prog->insnsi[i]) || + insn_is_jit_may_goto(&prog->insnsi[i]); if (ctx.max_landings) { ctx.landings = kvmalloc_objs(*ctx.landings, ctx.max_landings); if (!ctx.landings) @@ -5417,6 +5656,11 @@ bool bpf_jit_supports_timed_may_goto(void) return true; } +bool bpf_jit_supports_may_goto_insn(void) +{ + return true; +} + bool bpf_jit_supports_fsession(void) { return true; diff --git a/arch/x86/net/bpf_timed_may_goto.S b/arch/x86/net/bpf_timed_may_goto.S index da42de7cca98..a4e254314857 100644 --- a/arch/x86/net/bpf_timed_may_goto.S +++ b/arch/x86/net/bpf_timed_may_goto.S @@ -15,6 +15,10 @@ SYM_FUNC_START(arch_bpf_timed_may_goto) pushq %rbp movq %rsp, %rbp + /* r10 is the address after may_goto. A dump of the stack takes it as the return address. */ + pushq 8(%rbp) + movq %r10, 8(%rbp) + /* Save r0-r5. */ pushq %rax pushq %rdi @@ -25,15 +29,17 @@ SYM_FUNC_START(arch_bpf_timed_may_goto) /* Save stack argument 6. */ pushq %r9 - /* r10 has the pointer to count and timestamp. Pass it as first argument. */ - movq %r10, %rdi + /* r11 has the state. Pass it as first argument. */ + pushq %r11 + movq %r11, %rdi /* Emit call depth accounting for call below. */ CALL_DEPTH_ACCOUNT - call bpf_check_timed_may_goto + call bpf_may_goto_slowpath - /* BPF_REG_AX=r10 will be stored into count, so move return value to it. */ - movq %rax, %r10 + /* The old state goes back in r10. */ + popq %r10 + movq %rax, %r11 popq %r9 /* Restore r5-r0. */ @@ -43,6 +49,7 @@ SYM_FUNC_START(arch_bpf_timed_may_goto) popq %rsi popq %rdi popq %rax + popq 8(%rbp) leave RET diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index f60a300c6a9b..9d4f89269711 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -3127,7 +3127,7 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) struct bpf_subprog_info *subprogs = env->subprog_info; u16 stack_depth = subprogs[cur_subprog].stack_depth; u16 stack_depth_extra = 0; - /* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: for JITs only */ + /* arch_bpf_timed_may_goto() has a calling convention of its own: for JITs only */ bool timed_may_goto = prog->jit_requested && bpf_jit_supports_timed_may_goto(); bool may_goto_insn = timed_may_goto && bpf_jit_supports_may_goto_insn(); struct bpf_timed_may_goto __percpu *tmg = NULL; diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c index 2a3210a50678..afe6dc7958b8 100644 --- a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c +++ b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c @@ -1002,7 +1002,7 @@ __naked void callback_of_drain(void) : __clobber_all); } -/* 200000 iterations: may_goto calls arch_bpf_timed_may_goto() three times */ +/* 200000 iterations: may_goto takes its slow path three times */ SEC("syscall") __description("arena stack: may_goto doesn't change r12") __success __retval(42) @@ -1069,10 +1069,11 @@ 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("if r0 != 0x0 goto pc+2") -__xlated("r0 = 1") +__xlated("r0 &= 1") +__xlated("if r0 == 0x0 goto pc+1") __xlated("exit") __xlated("r6 = ") __xlated("r6 = &(void __percpu *)(r6)") diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c index 8a69d6b7874f..5b3d760bd160 100644 --- a/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +++ b/tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c @@ -937,31 +937,16 @@ SEC("raw_tp") __arch_x86_64 __log_level(4) __msg("subprog 0 (may_goto_interaction_x86_64) main {{.*}} stack 8") -/* may_goto counter and timestamp */ -__xlated("0: r0 = ") -__xlated("2: r0 = &(void __percpu *)(r0)") +__xlated("0: may_goto_init") +__xlated("1: r1 = 1") __xlated("...") -__xlated("6: r1 = 1") -__xlated("...") -__xlated("8: r0 = &(void __percpu *)(r0)") -__xlated("...") -/* may_goto expansion starts, r0 and r1 are live */ -__xlated("10: r2 = ") -__xlated("12: r2 = &(void __percpu *)(r2)") -__xlated("13: r13 = *(u64 *)(r2 +0)") /* when the time is up may_goto goes to the landing pad of the prog */ -__xlated("14: if r13 == 0x0 goto pc+7") -__xlated("15: r13 -= 1") -__xlated("16: if r13 != 0x0 goto pc+2") -__xlated("17: r13 = r2") -__xlated("18: call unknown") -__xlated("19: *(u64 *)(r2 +0) = r13") -/* may_goto expansion ends */ -__xlated("20: *(u64 *)(r10 -8) = r1") -__xlated("21: exit") +__xlated("may_goto pc+2") +__xlated("*(u64 *)(r10 -8) = r1") +__xlated("exit") /* the landing pad */ -__xlated("22: r0 = 0") -__xlated("23: exit") +__xlated("r0 = 0") +__xlated("exit") __success __naked void may_goto_interaction_x86_64(void) { 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 f8861ad046ce..aeff39968a4d 100644 --- a/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +++ b/tools/testing/selftests/bpf/progs/verifier_may_goto_1.c @@ -83,7 +83,6 @@ __naked void may_goto_batch_1(void) SEC("raw_tp") __description("may_goto batch with offsets 2/0") __load_if_JITed() -__arch_x86_64 __arch_s390x __arch_arm64 __arch_riscv64 @@ -125,6 +124,34 @@ __naked void may_goto_batch_2(void) : __clobber_all); } +SEC("raw_tp") +__description("may_goto batch with offsets 2/0, JIT of may_goto") +__load_if_JITed() +__arch_x86_64 +__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") +__xlated("2: r0 = 1") +__xlated("3: r0 = 2") +__xlated("4: exit") +/* the landing pad */ +__xlated("5: r0 = 0") +__xlated("6: exit") +__success +__naked void may_goto_batch_2_insn(void) +{ + asm volatile ( + ".8byte %[may_goto1];" + ".8byte %[may_goto3];" + "r0 = 1;" + "r0 = 2;" + "exit;" + : + : __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 2 /* offset */, 0)), + __imm_insn(may_goto3, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0)) + : __clobber_all); +} + /* * Use bpf_get_prandom_u32() to prevent DCE from removing the checks. * retval: 0=all ok, 1-6=R0-R5 clobbered. diff --git a/tools/testing/selftests/bpf/progs/verifier_patch_list.c b/tools/testing/selftests/bpf/progs/verifier_patch_list.c index da5542bcb6e7..2a18f182c0db 100644 --- a/tools/testing/selftests/bpf/progs/verifier_patch_list.c +++ b/tools/testing/selftests/bpf/progs/verifier_patch_list.c @@ -86,36 +86,27 @@ __naked void patch_list_backward(void) } /* - * The jump of may_goto leaves its own patch and crosses another one, on the way to the landing pad - * that follows the prog. + * The jump of may_goto crosses a patch on the way to the landing pad that follows the prog. */ SEC("raw_tp") __description("patch list: may_goto between patched insns") __arch_x86_64 -__arch_arm64 __success -__xlated("0: r0 = ") -__xlated("2: r0 = &(void __percpu *)(r0)") -__xlated("3: *(u64 *)(r0 +0) = 65535") -__xlated("...") -__xlated("6: call") -__xlated("7: r1 = r0") +__xlated("0: may_goto_init") +__xlated("1: call") +__xlated("2: r1 = r0") +__xlated("3: if r1 != 0x0 goto pc+2") +__xlated("4: w0 ^= w0") +__xlated("5: goto pc+1") +__xlated("6: r0 /= r1") +__xlated("7: may_goto pc+5") __xlated("8: if r1 != 0x0 goto pc+2") __xlated("9: w0 ^= w0") __xlated("10: goto pc+1") __xlated("11: r0 /= r1") -__xlated("12: r2 = ") -__xlated("14: r2 = &(void __percpu *)(r2)") -__xlated("15: r13 = *(u64 *)(r2 +0)") -__xlated("16: if r13 == 0x0 goto pc+10") -__xlated("...") -__xlated("22: if r1 != 0x0 goto pc+2") -__xlated("23: w0 ^= w0") -__xlated("24: goto pc+1") -__xlated("25: r0 /= r1") -__xlated("26: exit") -__xlated("27: r0 = 0") -__xlated("28: exit") +__xlated("12: exit") +__xlated("13: r0 = 0") +__xlated("14: exit") __naked void patch_list_may_goto(void) { asm volatile ( @@ -316,38 +307,34 @@ int patch_list_indirect(void) #endif /* - * When the time is up may_goto goes to the landing pad, which follows the prog: over 8191 - * divisions, 32764 insns once each of them is guarded by a patch of 4 insns. That does not fit - * the 16 bits of the jump that may_goto expands to, which has to become gotol. + * When the time is up may_goto goes to the landing pad, which follows the prog: over 8192 + * divisions, 32768 insns once each of them is guarded by a patch of 4 insns. That does not fit + * 16 bits. */ SEC("raw_tp") -__description("patch list: far may_goto expanded to gotol") +__description("patch list: far may_goto") __arch_x86_64 -__arch_arm64 __success -__xlated("12: r13 = *(u64 *)(r2 +0)") -__xlated("13: if r13 == 0x0 goto pc+1") -__xlated("14: goto pc+1") -__xlated("15: gotol pc+32771") +__xlated("4: may_goto pc+32770") __xlated("...") -__xlated("32786: exit") -__xlated("32787: r0 = 0") -__xlated("32788: exit") -__naked void patch_list_may_goto_gotol(void) +__xlated("32774: exit") +__xlated("32775: r0 = 0") +__xlated("32776: exit") +__naked void patch_list_may_goto_far(void) { asm volatile (" \ call %[bpf_get_prandom_u32]; \ r1 = r0; \ r6 = 0; \ .8byte %[may_goto]; \ - .rept 8191; \ + .rept 8192; \ r0 /= r1; \ .endr; \ r0 = 0; \ exit; \ " : : __imm(bpf_get_prandom_u32), - __imm_insn(may_goto, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 8191 /* offset */, 0)) + __imm_insn(may_goto, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 8192 /* offset */, 0)) : __clobber_all); } @@ -403,7 +390,7 @@ static unsigned long patch_list_loop_at_start(void) : __clobber_all) /* - * The check of the may_goto count goes in front of the callback of bpf_loop(). + * The check of the time of may_goto goes in front of the callback of bpf_loop(). * The call of the inlined bpf_loop() lands on the check, the jump to the first * insn of the callback behind it. * This is the code for the JITs that support the timed may_goto and unwind programs: the callback @@ -413,28 +400,23 @@ 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("27: call pc+10") -__xlated("28: cancel_goto pc-15") -__xlated("...") -__xlated("37: exit") -__xlated("38: r0 = ") -__xlated("40: r0 = &(void __percpu *)(r0)") -__xlated("41: r0 = *(u64 *)(r0 +0)") -__xlated("42: if r0 != 0x0 goto pc+2") -__xlated("43: r0 = 1") -__xlated("44: exit") -__xlated("45: r1 += -1") -__xlated("...") -__xlated("50: if r13 == 0x0 goto pc+8") +__xlated("22: call pc+10") +__xlated("23: cancel_goto pc-15") __xlated("...") -__xlated("56: if r1 != 0x0 goto pc-12") -__xlated("57: r0 = 0") -__xlated("58: exit") -__xlated("59: r0 = 0") -__xlated("60: r2 = 0") -__xlated("61: cancel_exit") +__xlated("32: exit") +__xlated("33: may_goto_read r0") +__xlated("34: r0 &= 1") +__xlated("35: if r0 == 0x0 goto pc+1") +__xlated("36: exit") +__xlated("37: r1 += -1") +__xlated("38: may_goto pc+3") +__xlated("39: if r1 != 0x0 goto pc-3") +__xlated("40: r0 = 0") +__xlated("41: exit") +__xlated("42: r0 = 0") +__xlated("43: r2 = 0") +__xlated("44: cancel_exit") __naked void patch_list_insert_in_front(void) { INSERT_IN_FRONT; -- 2.55.0