Re: [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx
From: Alexei Starovoitov
Date: Mon Nov 30 2026 - 22:53:43 EST
On Mon, Nov 30, 2026 at 03:54 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@xxxxxxxxxx> wrote:
> +static inline struct bpf_run_ctx *bpf_set_run_ctx_prog(struct bpf_run_ctx *new_ctx,
> + const struct bpf_prog *prog)
Pls use __always_inline to avoid future headaches.
> + case BPF_PROG_TYPE_TRACING:
> + case BPF_PROG_TYPE_STRUCT_OPS:
> + return bpf_prog_check_recur(prog);
Dead code. bpf_check_attach_target() rejects these targets.
Drop both helpers and check three prog types in
bpf_tracing_prog_attach().
> + if (prog->type == BPF_PROG_TYPE_EXT && prog->percpu_state &&
> + bpf_prog_recur_per_task(tgt_prog)) {
> + err = -EINVAL;
A sleepable target is preemptible with or without the mark:
raw_tp.s target runs attach of a marked extension
--------------- ------------ ----------------------------
not marked preemptibly -EINVAL
marked preemptibly ok
Reject both. Also add bpf_log().
> - bpf_reset_run_ctx(run_ctx->saved_run_ctx);
> + bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);
> + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx);
The order is wrong. Marked progs still lose events.
The deferred preemption fires while the frame is linked.
P is one raw_tp prog on sys_enter and sched_switch:
bpf_prog_exit_run_ctx(P)
preempt_enable_notrace()
__schedule()
trace_sched_switch()
bpf_prog_enter_run_ctx(P, old_ctx)
old_ctx->prog == P <- dropped, P has exited already
Same for an fentry.multi prog that covers the scheduler.
Unlink the frame first in all runners, like in the diff in v3 thread.
In the trampoline enable preemption after update_prog_stats().
Otherwise off-cpu time goes into run_time_ns.
Nothing reads the run_ctx argument. Drop it.
pw-bot: cr