From: Mike Bazov The next patch lets a second task into a tracing or raw tracepoint program while another task is preempted inside it on the same CPU. Some programs rely on that not happening: - A per-CPU map value that a program looked up stays its own until the run ends: programs use these maps as scratch space. - A private stack is per CPU. After the previous patch, struct_ops programs that request one are the only programs with this check that still have it. Mark them at load time, so that the next patch can keep their runs apart on a CPU. Set prog->percpu_state when the verifier sees: - a helper called with a per-CPU map, also through an inner map, - the address of a per-CPU array value loaded directly, - bpf_this_cpu_ptr() or bpf_per_cpu_ptr(), - bpf_get_smp_processor_id(): the CPU id often indexes per-CPU slots that the program keeps itself, e.g. in a regular array, - a tail call: the target runs in the caller's run and may use a per-CPU map, - a private stack, in a struct_ops program that requests one. Only verified code counts: a per-CPU map referenced in a branch that a constant in .rodata disables doesn't mark the program. Timed may_goto is not marked. Its budget is per-CPU memory that the first insns of a run reset, so a run that preempts another one can renew the other's budget. Marking would avoid that only by running the whole loop with preemption disabled, for up to 250 ms. lsm, cgroup, struct_ops and uprobe progs, which have no per-CPU recursion counter, already share the budget this way today. It is left to a separate fix of may_goto that covers all prog types. No functional change: nothing reads the flag yet. Trade-off: every program that calls bpf_get_smp_processor_id() is marked, also one that only records the CPU id. Signed-off-by: Mike Bazov --- include/linux/bpf.h | 3 ++- kernel/bpf/verifier.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index fa7e1bca4edd..95e0d1bcc1d5 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2075,7 +2075,8 @@ struct bpf_prog { call_get_func_ip:1, /* Do we call get_func_ip() */ call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ - sleepable:1; /* BPF program is sleepable */ + sleepable:1, /* BPF program is sleepable */ + percpu_state:1; /* Runs must not overlap on a CPU */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 3dee5dce18cb..accded7c9280 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6158,6 +6158,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env) if (round_up_stack_depth(env, si[s].stack_depth) >= BPF_PRIV_STACK_MIN_SIZE) { si[s].priv_stack_mode = PRIV_STACK_ADAPTIVE; env->prog->aux->jits_use_priv_stack = true; + env->prog->percpu_state = true; } } @@ -13106,6 +13107,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn if (err) return err; + /* A per-CPU value stays the run's own only while no other run starts on the CPU */ + if (meta.map.ptr && bpf_map_is_percpu_map(meta.map.ptr->map_type)) + env->prog->percpu_state = true; + err = record_func_map(env, &meta, func_id, insn_idx); if (err) return err; @@ -13132,10 +13137,16 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn switch (func_id) { case BPF_FUNC_tail_call: + /* The target runs without a recursion check of its own */ + env->prog->percpu_state = true; err = check_resource_leak(env, false, true, "tail_call"); if (err) return err; break; + case BPF_FUNC_get_smp_processor_id: + /* The CPU id often indexes per-CPU slots that the program keeps itself */ + env->prog->percpu_state = true; + break; case BPF_FUNC_get_local_storage: /* check that flags argument in get_local_storage(map, flags) is 0, * this is required because get_local_storage() can't return an error. @@ -13247,6 +13258,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn struct bpf_reg_state *reg = ®s[BPF_REG_1]; const struct btf_type *type; + env->prog->percpu_state = true; + if (reg->type & MEM_RCU) { type = btf_type_by_id(reg->btf, reg->btf_id); if (!type || !btf_type_is_struct(type)) { @@ -19638,6 +19651,8 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) __mark_reg_known(dst_reg, aux->map_off); dst_reg->type = PTR_TO_MAP_VALUE; dst_reg->map_ptr = map; + if (bpf_map_is_percpu_map(map->map_type)) + env->prog->percpu_state = true; WARN_ON_ONCE(map->map_type != BPF_MAP_TYPE_INSN_ARRAY && map->max_entries != 1); /* We want reg->id to be same (0) as map_value is not distinct */ -- 2.43.0