From: Alexei Starovoitov may_goto keeps the count of iterations and the start time in per-cpu memory of the prog. Each invocation resets them at entry, also when it preempts another one: task A task B reset count and time while (can_loop) ... preempted -> reset count and time while (can_loop) ... runs again <- preempted A goes on with the count and time of B. It gets more time than it should, or it is cancelled because B ran out of time. If A is preempted often enough it is never cancelled. Keep the count, the time and the 'time is up' bit in one u64 in a cpu register. The kernel preserves it over preemption and interrupts. BPF functions pass the register to each other. Helpers don't pass it to callbacks, so spill it to current before the call and fill it in the callback. Otherwise each call of the callback gets a new time slice. Do the same for subprogs of progs with tail calls, since the target of a tail call doesn't preserve the register. An extension doesn't either, so refuse freplace of a subprog when the prog has may_goto. The time slice becomes 256 ms instead of 250, since the time is in units of 4 ms. No JIT supports the new form of may_goto yet. Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 4 + include/linux/bpf_verifier.h | 9 ++ include/linux/filter.h | 59 +++++++++ include/linux/sched.h | 2 + kernel/bpf/arena.c | 1 + kernel/bpf/core.c | 29 +++++ kernel/bpf/disasm.c | 15 +++ kernel/bpf/fixups.c | 223 ++++++++++++++++++++++++++++++++++- kernel/bpf/helpers.c | 3 + kernel/bpf/rqspinlock.c | 2 + kernel/bpf/stream.c | 1 + kernel/bpf/verifier.c | 19 +++ 12 files changed, 363 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index a1bd30e6511b..40e42e8caa92 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2002,6 +2002,10 @@ struct bpf_prog_aux { struct bpf_prog_aux *main_prog_aux; /* main prog: there when the prog has timed may_goto */ struct bpf_timed_may_goto __percpu *timed_may_goto; + /* main prog: the JIT gets may_goto as an insn */ + bool has_may_goto; + /* the function keeps the state of may_goto in the cpu register */ + bool may_goto_in_reg; void *jit_data; /* JIT specific data. arch dependent */ struct bpf_jit_poke_descriptor *poke_tab; struct bpf_kfunc_desc_tab *kfunc_tab; diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 39a0868c66be..afb6fb2ba7ef 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1374,6 +1374,10 @@ struct bpf_insn_aux_data { * accepts callback function as a parameter. */ u32 calls_callback:1; + /* the helper or kfunc calls its callback before it returns */ + u32 calls_sync_cb:1; + /* the callee can call bpf_may_goto_clear_cnt() */ + u32 may_dump_stack:1; /* true if some jump or call instruction targets this instruction */ u32 jump_target:1; /* it can read or write the frame of a caller, in a call with a known callee */ @@ -1574,6 +1578,10 @@ struct bpf_subprog_info { bool is_loop_cb: 1; /* another helper or a kfunc calls it */ bool is_other_cb: 1; + /* a helper or a kfunc calls it, not counting a bpf_loop() that was inlined */ + bool called_by_kernel: 1; + /* it keeps the state of may_goto in the cpu register */ + bool may_goto_in_reg: 1; /* a call insn or callx calls it */ bool is_called: 1; bool args_cached: 1; @@ -3089,6 +3097,7 @@ struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem); int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off); bool bpf_insn_is_cond_jump(u8 code); bool bpf_is_may_goto_insn(struct bpf_insn *insn); +bool bpf_kfunc_may_dump_stack(const struct bpf_insn *insn); void bpf_verbose_insn(struct bpf_verifier_env *env, struct bpf_insn *insn); bool bpf_get_call_summary(struct bpf_verifier_env *env, struct bpf_insn *call, diff --git a/include/linux/filter.h b/include/linux/filter.h index 275c89ed7574..6fe38197cbbc 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -311,6 +311,62 @@ static inline bool insn_is_mov_percpu_addr(const struct bpf_insn *insn) static_assert(BPF_CANCEL_GOTO > BPF_UNWIND_GOTO); static_assert(BPF_CANCEL_EXIT > BPF_UNWIND_EXIT); +/* + * An invocation is one call from the kernel into the main prog or an async callback, with the + * subprogs and sync callbacks it reaches. All its may_goto share 64 bits of state: a 16-bit count + * of trips at bit BPF_MAY_GOTO_CNT_SHIFT, below it the time when the clock was read first, and + * BPF_MAY_GOTO_EXPIRED. The bits above the count are 0, so the state is never negative. + * + * A JIT that has bpf_jit_supports_may_goto_insn() keeps the state in a cpu register, which calls + * of BPF functions pass and return. + * + * may_goto: BPF_JMP32 | BPF_JCOND, the target in imm. + * if the subtraction of BPF_MAY_GOTO_CNT_ONE from the state borrows: + * old = state; state = bpf_may_goto_slowpath(state); + * if (old & BPF_MAY_GOTO_EXPIRED) goto target; + * + * Moves of the state. BPF_REG_AX as dst_reg names the cpu register. They may clobber AX. + * READ dst_reg = the cpu register + * INIT dst_reg = BPF_MAY_GOTO_INIT_STATE + * FILL dst_reg = current->bpf_may_goto_state + * SPILL current->bpf_may_goto_state = dst_reg + * SPILL_CALL SPILL, and FILL after the call that follows and at its landing pads. + * The JIT emits the FILL, since unwind_goto has to follow the call. + * SAVE *(u64 *)(r10 + imm) = current->bpf_may_goto_state + * RESTORE current->bpf_may_goto_state = *(u64 *)(r10 + imm) + */ +#define BPF_MAY_GOTO_CNT_SHIFT 23 +#define BPF_MAY_GOTO_CNT_ONE BIT_ULL(BPF_MAY_GOTO_CNT_SHIFT) +#define BPF_MAY_GOTO_EXPIRED 1 +#define BPF_MAY_GOTO_INIT_STATE ((BPF_MAX_TIMED_LOOPS - 1) * BPF_MAY_GOTO_CNT_ONE) + +enum { + BPF_MAY_GOTO_READ = -8, + BPF_MAY_GOTO_INIT, + BPF_MAY_GOTO_FILL, + BPF_MAY_GOTO_SPILL, + BPF_MAY_GOTO_SPILL_CALL, + BPF_MAY_GOTO_SAVE, + BPF_MAY_GOTO_RESTORE, +}; + +#define __BPF_MAY_GOTO_MOV(OP, REG, IMM) \ + BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X, REG, REG, OP, IMM) +#define BPF_MAY_GOTO_MOV(OP, REG) __BPF_MAY_GOTO_MOV(BPF_MAY_GOTO_##OP, REG, 0) +#define BPF_MAY_GOTO_SAVE_INSN(OFF) __BPF_MAY_GOTO_MOV(BPF_MAY_GOTO_SAVE, BPF_REG_FP, OFF) +#define BPF_MAY_GOTO_RESTORE_INSN(OFF) __BPF_MAY_GOTO_MOV(BPF_MAY_GOTO_RESTORE, BPF_REG_FP, OFF) + +static inline bool insn_is_may_goto_mov(const struct bpf_insn *insn) +{ + return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && + insn->off >= BPF_MAY_GOTO_READ && insn->off <= BPF_MAY_GOTO_RESTORE; +} + +static inline bool insn_is_jit_may_goto(const struct bpf_insn *insn) +{ + return insn->code == (BPF_JMP32 | BPF_JCOND) && insn->src_reg == BPF_MAY_GOTO; +} + /* gotol, cancel_goto and an unwind_goto that the verifier adds have a 32 bit offset, in imm */ static inline bool bpf_jmp_offset_is_imm(const struct bpf_insn *insn) { @@ -1444,6 +1500,7 @@ bool bpf_jit_supports_arena_stack(void); bool bpf_jit_supports_local_atomics(void); bool bpf_jit_supports_large_stack(void); bool bpf_jit_supports_timed_may_goto(void); +bool bpf_jit_supports_may_goto_insn(void); bool bpf_jit_supports_fsession(void); /* @@ -1494,6 +1551,8 @@ u64 bpf_arch_uaddress_limit(void); void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie); u64 arch_bpf_timed_may_goto(void); u64 bpf_check_timed_may_goto(struct bpf_timed_may_goto *); +u64 bpf_may_goto_slowpath(u64 state); +void bpf_may_goto_clear_cnt(const struct bpf_prog *prog); void bpf_timed_may_goto_check_time(const struct bpf_prog *prog); void bpf_timed_may_goto_save(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved); void bpf_timed_may_goto_restore(const struct bpf_prog *prog, struct bpf_timed_may_goto *saved); diff --git a/include/linux/sched.h b/include/linux/sched.h index e4f6323d7855..691a1e322985 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1633,6 +1633,8 @@ struct task_struct { /* Used for BPF run context */ struct bpf_run_ctx *bpf_ctx; #endif + /* The state of BPF may_goto when it is not in a cpu register */ + u64 bpf_may_goto_state; /* Used by BPF for per-TASK xdp storage */ struct bpf_net_context *bpf_net_context; diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index c0c431124a13..8d6e067aee89 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -1221,6 +1221,7 @@ u64 bpf_arena_stack_get(struct bpf_prog_aux *aux, u64 busy) bpf_stream_stage(ss, aux->prog, BPF_STDERR, ({ bpf_stream_printk(ss, "ERROR: No memory for the stack in arena. The program didn't run\n"); })); + bpf_may_goto_clear_cnt(aux->prog); return 0; } diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 143fa134fea5..933c19556725 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -4117,6 +4117,11 @@ bool __weak bpf_jit_supports_timed_may_goto(void) return false; } +bool __weak bpf_jit_supports_may_goto_insn(void) +{ + return false; +} + u64 __weak arch_bpf_timed_may_goto(void) { return 0; @@ -4260,6 +4265,30 @@ void bpf_timed_may_goto_check_time(const struct bpf_prog *prog) this_cpu_write(tmg->count, 1); } +/* The count in @state ran out. Bit 1 of the state is 2^22 ns. The time slice is 256 ms. */ +u64 bpf_may_goto_slowpath(u64 state) +{ + u32 mask = BPF_MAY_GOTO_CNT_ONE - 1, start = state & mask, now; + + if (state & BPF_MAY_GOTO_EXPIRED) + return BPF_MAY_GOTO_EXPIRED; + now = (ktime_get_mono_fast_ns() >> 22 << 1) & mask; + if (!start) + return BPF_MAY_GOTO_INIT_STATE | now; + /* Both times are rounded down. The clock of another cpu can be one step behind. */ + if (((now - start + 2) & mask) < (((NSEC_PER_SEC / 4) >> 22) + 3) << 1) + return BPF_MAY_GOTO_INIT_STATE | start; + bpf_prog_report_may_goto_violation(); + return BPF_MAY_GOTO_EXPIRED; +} + +/* A report is too slow for BPF_MAX_TIMED_LOOPS trips. The next may_goto reads the clock. */ +void bpf_may_goto_clear_cnt(const struct bpf_prog *prog) +{ + if (prog->aux->main_prog_aux->has_may_goto) + current->bpf_may_goto_state &= BPF_MAY_GOTO_CNT_ONE - 1; +} + /* for configs without MMU or 32-bit */ __weak const struct bpf_map_ops arena_map_ops; __weak u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena) diff --git a/kernel/bpf/disasm.c b/kernel/bpf/disasm.c index 8426c6c04610..ffde0a46841d 100644 --- a/kernel/bpf/disasm.c +++ b/kernel/bpf/disasm.c @@ -12,6 +12,8 @@ #define BPF_ATOMIC_LOCAL 0x1000 #define BPF_CANCEL_GOTO 2 #define BPF_CANCEL_EXIT 2 +#define BPF_MAY_GOTO_READ (-8) +#define BPF_MAY_GOTO_RESTORE (-2) #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x) static const char * const func_id_str[] = { @@ -230,6 +232,15 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, } else if (is_mov_percpu_addr(insn)) { verbose(cbs->private_data, "(%02x) r%d = &(void __percpu *)(r%d)", insn->code, insn->dst_reg, insn->src_reg); + } else if (insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) && + insn->off >= BPF_MAY_GOTO_READ && insn->off <= BPF_MAY_GOTO_RESTORE) { + static const char * const op[] = { + "read", "init", "fill", "spill", "spill_call", "save", "restore" + }; + + verbose(cbs->private_data, "(%02x) may_goto_%s r%d%+d", + insn->code, op[insn->off - BPF_MAY_GOTO_READ], insn->dst_reg, + insn->imm); } else if (BPF_SRC(insn->code) == BPF_X) { verbose(cbs->private_data, "(%02x) %c%d %s %s%c%d", insn->code, class == BPF_ALU ? 'w' : 'r', @@ -402,6 +413,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, insn->src_reg == BPF_MAY_GOTO) { verbose(cbs->private_data, "(%02x) may_goto pc%+d", insn->code, insn->off); + } else if (insn->code == (BPF_JMP32 | BPF_JCOND) && + insn->src_reg == BPF_MAY_GOTO) { + verbose(cbs->private_data, "(%02x) may_goto pc%+d", + insn->code, insn->imm); } else if (insn->code == (BPF_JMP | BPF_JCOND) && insn->src_reg == BPF_UNWIND_GOTO) { verbose(cbs->private_data, "(%02x) unwind_goto pc%+d", diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index f8c70dd90bfb..f60a300c6a9b 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -2309,6 +2309,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) func[i]->aux->name[0] = 'F'; func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; + func[i]->aux->may_goto_in_reg = env->subprog_info[i].may_goto_in_reg; if (env->subprog_info[i].priv_stack_mode == PRIV_STACK_ADAPTIVE) func[i]->aux->jits_use_priv_stack = true; @@ -2725,6 +2726,203 @@ static int may_goto_expand_timed(struct bpf_insn *insn_buf, int *jmp, return cnt; } +/* Only bpf_loop() or bpf_for_each_map_elem() call it. Their loop ends when it returns 1. */ +static bool is_loop_cb_only(const struct bpf_subprog_info *sub) +{ + return !sub->main && sub->is_loop_cb && !sub->is_called && !sub->is_other_cb && + !sub->is_async_cb; +} + +/* Each bpf_loop() that calls it was inlined, so the calls are direct */ +static bool is_inlined_loop_cb(const struct bpf_subprog_info *sub) +{ + return sub->is_loop_cb && !sub->called_by_kernel && !sub->is_async_cb; +} + +/* callx or a helper can call it. A call of an async callback is an invocation of its own. */ +static bool is_indirect_callee(const struct bpf_subprog_info *sub) +{ + return sub->addr_taken && (!is_inlined_loop_cb(sub) || sub->via_callx) && + (!sub->is_async_cb || sub->via_callx || sub->is_loop_cb || sub->is_other_cb); +} + +/* Marks of the call graph of bpf_insert_may_goto_spills() */ +enum { + MAY_GOTO_MOD = BIT(0), /* it or a callee writes the state */ + MAY_GOTO_REF = BIT(1), /* it or a callee tests BPF_MAY_GOTO_EXPIRED at entry */ + MAY_GOTO_MOD_REF = MAY_GOTO_MOD | MAY_GOTO_REF, + MAY_GOTO_SPILLS = BIT(2), /* it or a callee spills the state to the task */ + MAY_GOTO_ARG_IN_TASK = BIT(3), /* the state is passed and returned in the task */ +}; + +/* What the callee of insn @i does to the state in current->bpf_may_goto_state */ +static u16 task_slot_mod_ref(struct bpf_verifier_env *env, struct bpf_call_graph *cg, int i) +{ + const struct bpf_insn_aux_data *aux = &env->insn_aux_data[i]; + struct bpf_insn *insn = &env->prog->insnsi[i]; + u16 callee = 0; + + if (aux->may_dump_stack) + return MAY_GOTO_MOD; + if (bpf_pseudo_call(insn)) + callee = cg->marks[insn_callee(env, i)]; + else if (bpf_is_callx(insn) || aux->calls_sync_cb) + callee = cg->marks[env->subprog_cnt]; + return callee & MAY_GOTO_ARG_IN_TASK ? callee & MAY_GOTO_MOD_REF : 0; +} + +/* + * Build the call graph and find the functions that modify or read the state. The last node stands + * for indirect calls: callx and helpers call it, it calls each subprog whose address is taken. + */ +static int mark_may_goto_mod_ref(struct bpf_verifier_env *env, struct bpf_call_graph *cg) +{ + struct bpf_subprog_info *si = env->subprog_info; + int i, s, indirect = env->subprog_cnt, err = 0; + + for (s = 0; s < env->subprog_cnt; s++) { + cg->marks[s] = is_loop_cb_only(&si[s]) ? MAY_GOTO_REF : 0; + /* a direct call passes the state in the register: MOD keeps it there */ + if (cg->marks[s] && is_inlined_loop_cb(&si[s])) + cg->marks[s] |= MAY_GOTO_MOD; + for (i = si[s].start; i < si[s + 1].start; i++) { + const struct bpf_insn_aux_data *aux = &env->insn_aux_data[i]; + struct bpf_insn *insn = &env->prog->insnsi[i]; + + if (verifier_bug_if((aux->calls_sync_cb || aux->may_dump_stack) && + !is_call(insn), env, "insn %d is no call", i)) + return -EFAULT; + if (bpf_pseudo_call(insn)) + err = bpf_call_graph_add(cg, s, insn_callee(env, i)); + else if (bpf_is_callx(insn) || aux->calls_sync_cb) + err = bpf_call_graph_add(cg, s, indirect); + if (err) + return err; + /* A report clears the count. So does a fault in arena that is skipped. */ + if (insn_is_jit_may_goto(insn) || aux->may_dump_stack || + (is_arena_access(insn) && !aux->fault_unwinds && + !bpf_is_unwind_goto(insn + 1))) + cg->marks[s] |= MAY_GOTO_MOD; + } + } + for (s = 0; s < env->subprog_cnt && !err; s++) + if (is_indirect_callee(&si[s])) + err = bpf_call_graph_add(cg, indirect, s); + if (err) + return err; + bpf_call_graph_sort(cg, 0); + bpf_call_graph_mark_callers(cg, MAY_GOTO_MOD_REF, 0); + return 0; +} + +/* Pick the calling convention of each function, and find the callers that have to spill for it */ +static void mark_may_goto_call_conv(struct bpf_verifier_env *env, struct bpf_call_graph *cg) +{ + struct bpf_subprog_info *si = env->subprog_info; + int i, s, indirect = env->subprog_cnt; + bool tail_call_reachable = false; + + /* The target of a tail call need not preserve the register */ + for (s = 0; s < env->subprog_cnt; s++) + tail_call_reachable |= si[s].tail_call_reachable; + /* All targets of indirect calls have one convention */ + for (s = 0; s < env->subprog_cnt; s++) + if (tail_call_reachable || + (is_indirect_callee(&si[s]) && si[s].is_cb && !is_inlined_loop_cb(&si[s]) && + cg->marks[s] & MAY_GOTO_MOD_REF)) + cg->marks[indirect] |= MAY_GOTO_ARG_IN_TASK; + for (s = 0; s < env->subprog_cnt; s++) + if (tail_call_reachable || (si[s].is_cb && !is_inlined_loop_cb(&si[s])) || + (is_indirect_callee(&si[s]) && cg->marks[indirect] & MAY_GOTO_ARG_IN_TASK)) + cg->marks[s] |= MAY_GOTO_ARG_IN_TASK; + for (s = 0; s < env->subprog_cnt; s++) + for (i = si[s].start; i < si[s + 1].start; i++) + if (task_slot_mod_ref(env, cg, i)) + cg->marks[s] |= MAY_GOTO_SPILLS; + bpf_call_graph_mark_callers(cg, MAY_GOTO_SPILLS, 0); +} + +static int insert_subprog_may_goto_spills(struct bpf_verifier_env *env, + struct bpf_call_graph *cg, int s) +{ + struct bpf_subprog_info *sub = &env->subprog_info[s]; + struct bpf_insn prologue[6], epilogue = {}, patch[2]; + u16 marks = cg->marks[s], callee; + int i, cnt = 0, err; + + /* a callback that only tests BPF_MAY_GOTO_EXPIRED reads it from the task */ + sub->may_goto_in_reg = sub->main || marks & (MAY_GOTO_MOD | MAY_GOTO_SPILLS); + + if (sub->main && marks & MAY_GOTO_SPILLS) { + sub->stack_depth = round_up(sub->stack_depth, 8) + 8; + prologue[cnt++] = BPF_MAY_GOTO_SAVE_INSN(-sub->stack_depth); + epilogue = BPF_MAY_GOTO_RESTORE_INSN(-sub->stack_depth); + } + if (sub->main) { + prologue[cnt++] = BPF_MAY_GOTO_MOV(INIT, BPF_REG_AX); + } else if (sub->may_goto_in_reg && marks & MAY_GOTO_ARG_IN_TASK) { + prologue[cnt++] = BPF_MAY_GOTO_MOV(FILL, BPF_REG_AX); + epilogue = BPF_MAY_GOTO_MOV(SPILL, BPF_REG_AX); + } + /* The time is up: return 1, see the same check in bpf_do_misc_fixups() */ + if (is_loop_cb_only(sub)) { + prologue[cnt++] = sub->may_goto_in_reg ? BPF_MAY_GOTO_MOV(READ, BPF_REG_0) : + BPF_MAY_GOTO_MOV(FILL, BPF_REG_0); + prologue[cnt++] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, BPF_MAY_GOTO_EXPIRED); + prologue[cnt++] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1); + prologue[cnt++] = BPF_EXIT_INSN(); + } + /* jumps to the first insn of the subprog skip the prologue */ + err = cnt ? bpf_patch_list_insert(env, sub->start, prologue, cnt) : 0; + + for (i = sub->start; i < sub[1].start && !err; i++) { + patch[1] = env->prog->insnsi[i]; + callee = sub->may_goto_in_reg ? task_slot_mod_ref(env, cg, i) : 0; + if (callee & MAY_GOTO_MOD) + patch[0] = BPF_MAY_GOTO_MOV(SPILL_CALL, BPF_REG_AX); + else if (callee) + patch[0] = BPF_MAY_GOTO_MOV(SPILL, BPF_REG_AX); + else if (epilogue.code && (patch[1].code == (BPF_JMP | BPF_EXIT) || + patch[1].code == (BPF_JMP | BPF_TAIL_CALL))) + patch[0] = epilogue; + else + continue; + err = PTR_ERR_OR_ZERO(bpf_patch_list_add(env, i, patch, 2)); + } + return err; +} + +/* + * The state of may_goto is a hidden argument and return value of BPF functions, in a cpu register. + * Helpers and kfuncs don't pass it to their callbacks, so it goes through a slot in the task: + * + * main: save; init cb: fill + * spill may_goto + * call bpf_for_each_map_elem(cb) spill + * fill exit + * restore; exit + * + * The main prog saves and restores the slot, so invocations nest. A call gets no spill when the + * callee doesn't touch the state, and no fill when the callee doesn't modify it. + */ +static int bpf_insert_may_goto_spills(struct bpf_verifier_env *env) +{ + struct bpf_call_graph cg; + int s, err; + + err = bpf_call_graph_init(&cg, env->subprog_cnt + 1) ?: mark_may_goto_mod_ref(env, &cg); + if (err) + goto out; + mark_may_goto_call_conv(env, &cg); + for (s = 0; s < env->subprog_cnt && !err; s++) + if (cg.marks[s] & MAY_GOTO_MOD_REF) + err = insert_subprog_may_goto_spills(env, &cg, s); + env->prog->aux->may_goto_in_reg = env->subprog_info[0].may_goto_in_reg; +out: + bpf_call_graph_free(&cg); + return err; +} + #ifdef CONFIG_64BIT struct bpf_div_magic { u64 m; /* multiplier, sign extended for signed division */ @@ -2931,9 +3129,13 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) u16 stack_depth_extra = 0; /* arch_bpf_timed_may_goto() takes and returns AX in a cpu register: 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; for (i = 0; i < insn_cnt;) { + env->insn_aux_data[i].calls_sync_cb = bpf_is_sync_callback_calling_insn(insn); + env->insn_aux_data[i].may_dump_stack = bpf_kfunc_may_dump_stack(insn); + if (is_addr_space_cast32(env->prog, insn)) { /* convert to 32-bit mov that clears upper 32-bit */ insn->code = BPF_ALU | BPF_MOV | BPF_X; @@ -3164,7 +3366,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) goto next_insn; } - if (bpf_is_may_goto_insn(insn) && timed_may_goto) { + if (bpf_is_may_goto_insn(insn) && may_goto_insn) { + *insn = BPF_RAW_INSN(BPF_JMP32 | BPF_JCOND, 0, BPF_MAY_GOTO, 0, insn->off); + prog->jit_required = 1; + prog->aux->has_may_goto = true; + goto next_insn; + } else if (bpf_is_may_goto_insn(insn) && timed_may_goto) { u16 live = env->insn_aux_data[i].live_regs_before; int reg, jmp, save_off = 0, to = i + insn->off + 1; @@ -3247,6 +3454,9 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) goto next_insn; /* imm is not the id of a helper */ if (!insn->src_reg && insn->off == BPF_KERNEL_CALL) { + /* a main prog exits when it gets no stack, a callback returns to its loop */ + env->insn_aux_data[i].may_dump_stack = !subprogs[cur_subprog].main && + insn->imm == BPF_CALL_IMM(bpf_arena_stack_get); insn->off = 0; goto next_insn; } @@ -3738,14 +3948,16 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env) return ret; prog = env->prog; + ret = prog->aux->has_may_goto ? bpf_insert_may_goto_spills(env) : 0; + if (ret) + return ret; + env->prog->aux->stack_depth = subprogs[0].stack_depth; for (i = 0; i < env->subprog_cnt; i++) { int stack_slots = subprogs[i].stack_extra / 8; /* an invocation begins, see bpf_timed_may_goto_save() for async callbacks */ bool main_prog = tmg && subprogs[i].main; - /* only a helper calls it, in a loop that ends when it returns 1 */ - bool loop_cb = tmg && i && subprogs[i].is_loop_cb && !subprogs[i].is_called && - !subprogs[i].is_other_cb && !subprogs[i].is_async_cb; + bool loop_cb = tmg && is_loop_cb_only(&subprogs[i]); if (timed_may_goto ? !main_prog && !loop_cb : !stack_slots) continue; @@ -4003,6 +4215,9 @@ int bpf_optimize_bpf_loop(struct bpf_verifier_env *env) inline_state->callback_subprogno); if (err) return err; + } else if (is_bpf_loop_call(insn)) { + /* push_callback_call() skips a call while it is fit for inlining */ + subprogs[inline_state->callback_subprogno].called_by_kernel = true; } if (subprogs[cur_subprog + 1].start == i + 1) { diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 5054fbf25e6b..dc99c35a3eb4 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1241,10 +1241,13 @@ static void bpf_run_async_cb(struct bpf_prog *prog, bpf_callback_t callback_fn, { struct bpf_run_ctx run_ctx, *old_ctx; struct bpf_timed_may_goto saved; + u64 old_state = READ_ONCE(current->bpf_may_goto_state); old_ctx = bpf_set_run_ctx_prog(&run_ctx, BPF_PROG_ASYNC_CB); bpf_timed_may_goto_save(prog, &saved); + WRITE_ONCE(current->bpf_may_goto_state, BPF_MAY_GOTO_INIT_STATE); callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); + WRITE_ONCE(current->bpf_may_goto_state, old_state); bpf_timed_may_goto_restore(prog, &saved); bpf_reset_run_ctx(old_ctx); } diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c index 14d379833589..4f23f0ded7b4 100644 --- a/kernel/bpf/rqspinlock.c +++ b/kernel/bpf/rqspinlock.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -680,6 +681,7 @@ static void bpf_prog_report_rqspinlock_violation(const char *str, void *lock, bo bpf_stream_printk(ss, "Held lock[%2d] = 0x%px\n", i, rqh->locks[i]); bpf_stream_dump_stack(ss); })); + bpf_may_goto_clear_cnt(prog); } #define REPORT_STR(ret) ({ (ret) == -ETIMEDOUT ? "Timeout detected" : "AA or ABBA deadlock detected"; }) diff --git a/kernel/bpf/stream.c b/kernel/bpf/stream.c index c726e54a63c0..c312e74ebd57 100644 --- a/kernel/bpf/stream.c +++ b/kernel/bpf/stream.c @@ -444,6 +444,7 @@ __bpf_kfunc int bpf_stream_print_stack(int stream_id, struct bpf_prog_aux *aux) bpf_stream_stage(ss, prog, stream_id, ({ bpf_stream_dump_stack(ss); })); + bpf_may_goto_clear_cnt(prog); return 0; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 832f2efcbce4..e49382e654e0 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12068,6 +12068,9 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins * callbacks */ env->subprog_info[subprog].is_cb = true; + if (bpf_pseudo_kfunc_call(insn) || insn->imm != BPF_FUNC_loop || + !env->insn_aux_data[insn_idx].loop_inline_state.fit_for_inline) + env->subprog_info[subprog].called_by_kernel = true; enter_empty_handed(env, subprog); if (bpf_pseudo_kfunc_call(insn) && !is_callback_calling_kfunc(insn->imm)) { @@ -14890,6 +14893,7 @@ enum special_kfunc_type { KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, KF_bpf_session_is_return, + KF_bpf_stream_print_stack, }; BTF_ID_LIST(special_kfunc_list) @@ -14991,6 +14995,16 @@ BTF_ID(func, bpf_session_is_return) #else BTF_ID_UNUSED #endif +BTF_ID(func, bpf_stream_print_stack) + +/* The kfunc can call bpf_may_goto_clear_cnt() */ +bool bpf_kfunc_may_dump_stack(const struct bpf_insn *insn) +{ + return bpf_pseudo_kfunc_call(insn) && !insn->off && + (insn->imm == special_kfunc_list[KF_bpf_stream_print_stack] || + insn->imm == special_kfunc_list[KF_bpf_res_spin_lock] || + insn->imm == special_kfunc_list[KF_bpf_res_spin_lock_irqsave]); +} static bool is_bpf_cast_to_kern_ctx_kfunc(const struct bpf_call_arg_meta *meta) { @@ -26243,6 +26257,11 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, BPF_REG_ARENA_SP); return -EOPNOTSUPP; } + /* x86: the extension doesn't preserve the register with the state of may_goto */ + if (subprog && aux->has_may_goto && prog_extension) { + bpf_log(log, "Cannot replace a subprog of a program with may_goto\n"); + return -EOPNOTSUPP; + } if (aux->entry_cnt && (!subprog || aux->func[subprog]->aux->is_entry)) { bpf_log(log, "%s is a program of its own\n", tname); return -EINVAL; -- 2.55.0