BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mike Bazov" <mikeba@sentinelone.com>, bpf@vger.kernel.org
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx
Date: Mon, 30 Nov 2026 12:26:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-241049@kernel.org> (raw)
In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-4-bd28389dfd64@sentinelone.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] bpf: preempt_enable_notrace() in bpf_prog_exit_run_ctx() runs
  before ctx reset
- [Medium] bpf: softirq async callbacks bypass percpu_state in
  bpf_prog_enter_run_ctx()
- [Medium] bpf: missing prog->sleepable check in
  bpf_prog_recur_per_task()
--

commit 32586a3ce8e3d947d639f3b9940d0d310222d699
Author: Mike Bazov <mikeba@sentinelone.com>

bpf: Check program recursion per task via bpf_ctx

This commit replaces the per-CPU recursion counter for tracing, raw
tracepoint, and syscall tracepoint programs with a per-task recursion check
walking the current->bpf_ctx chain, while disabling preemption for
non-sleepable programs with per-CPU state.

> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 95e0d1bcc1d52..b24f26a48b771 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
[ ... ]
> @@ -2818,27 +2827,81 @@ struct bpf_tramp_run_ctx {
[ ... ]
> +/*
> + * Check whether @prog may run in the frame that the caller has just linked on
> + * top of @old_ctx. It must not if a frame from @old_ctx outward runs @prog
> + * already: that would be recursion. Interrupts and NMIs run on top of the chain
> + * of the task they interrupt, so recursion through them is caught too.
> + *
> + * A program with per-CPU state (prog->percpu_state) runs with preemption
> + * disabled, so that no other run of it starts on this CPU before it exits. A
> + * sleepable one has to use bpf_preempt_disable() itself.
> + *
> + * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way.
> + * Always inline: the callers are notrace.
> + */
> +static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog,
> +						   struct bpf_run_ctx *old_ctx)
> +{
> +	struct bpf_run_ctx *ctx;
> +
> +	if (prog->percpu_state && !prog->sleepable)
> +		preempt_disable_notrace();
> +	for (ctx = old_ctx; ctx; ctx = READ_ONCE(ctx->saved_run_ctx)) {
> +		if (READ_ONCE(ctx->prog) == prog)
> +			return false;
> +	}
> +	return true;
> +}

[Severity: Medium]
Can a softirq async callback re-enter a prog->percpu_state program on the
same CPU before the interrupted run exits?

preempt_disable_notrace() does not disable softirqs, so if a hardware IRQ
interrupts a percpu_state program and runs HRTIMER_SOFTIRQ or RCU_SOFTIRQ on
IRQ exit, bpf_run_async_cb() replaces current->bpf_ctx with
&bpf_async_run_ctx (whose saved_run_ctx is NULL):

kernel/bpf/helpers.c:bpf_run_async_cb() {
	struct bpf_run_ctx *old_ctx = READ_ONCE(current->bpf_ctx);
	struct bpf_timed_may_goto saved;

	/* All CPUs share bpf_async_run_ctx, so link it without writing to it */
	WRITE_ONCE(current->bpf_ctx, &bpf_async_run_ctx);
	bpf_timed_may_goto_save(prog, &saved);
	callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
	...
}

That leads to overlapping execution on the same CPU:

__bpf_prog_enter_recur(prog)
  bpf_set_run_ctx_prog(&run_ctx->run_ctx, prog)
  bpf_prog_enter_run_ctx(prog, old_run_ctx)
    preempt_disable_notrace()
  prog()
    <hardirq>
      __irq_exit_rcu()
        handle_softirqs()
          bpf_timer_cb()
            bpf_run_async_cb()
              WRITE_ONCE(current->bpf_ctx, &bpf_async_run_ctx)
              callback_fn()
                ...
                  __bpf_prog_enter_recur(prog)
                    bpf_prog_enter_run_ctx(prog, &bpf_async_run_ctx)
                      --> returns true, re-entering prog on same CPU

This corrupts per-CPU map state or overwrites the active per-CPU private
stack frame of a private-stack struct_ops program.

> +
> +static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog,
> +						  struct bpf_run_ctx *run_ctx)
> +{
> +	if (prog->percpu_state && !prog->sleepable)
> +		preempt_enable_notrace();
> +}

[Severity: Medium]
Does calling bpf_prog_exit_run_ctx() before bpf_reset_run_ctx() allow the
task to be preempted while its frame is still linked in current->bpf_ctx?

In __bpf_prog_exit_recur(), __bpf_trace_run(),
__bpf_prog_test_run_raw_tp(), and bpf_prog_run_array_sleepable(),
bpf_prog_exit_run_ctx() runs before current->bpf_ctx is restored (or before
run_ctx.run_ctx.prog is cleared).  Under CONFIG_PREEMPT_RCU,
rcu_read_lock_dont_migrate() only disables migration, so
preempt_enable_notrace() drops preempt_count to 0 and can call
__preempt_schedule_notrace() immediately:

__bpf_prog_exit_recur(prog)
  bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx)
    preempt_enable_notrace()
      __preempt_schedule_notrace()
        __schedule()
          trace_sched_switch()
            __bpf_trace_run(prog)
              bpf_prog_enter_run_ctx(prog, old_run_ctx)
                --> sees run_ctx->run_ctx.prog == prog, false recursion miss
  bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx)
  update_prog_stats(prog, start)

Besides falsely dropping the program if it fires during schedule() while
current is still the outgoing task, __bpf_prog_exit_recur() charges the
entire off-CPU preemption duration to update_prog_stats() and preempts
before rcu_read_unlock_migrate().  Also, the run_ctx parameter of
bpf_prog_exit_run_ctx() is unused.

> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index 50b3f8ab9720a..787e019cd8075 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h
> @@ -2039,6 +2039,29 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
>  	}
>  }
>  
> +/* Is @prog run through bpf_prog_enter_run_ctx()? */
> +static inline bool bpf_prog_enters_run_ctx(const struct bpf_prog *prog)
> +{
> +	switch (resolve_prog_type(prog)) {
> +	case BPF_PROG_TYPE_RAW_TRACEPOINT:
> +	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
> +	/* It may be attached to a syscall tracepoint */
> +	case BPF_PROG_TYPE_TRACEPOINT:
> +		return true;
> +	case BPF_PROG_TYPE_TRACING:
> +	case BPF_PROG_TYPE_STRUCT_OPS:
> +		return bpf_prog_check_recur(prog);
> +	default:
> +		return false;
> +	}
> +}
> +
> +/* Can two tasks run @prog at once on a CPU: it is checked per task and runs preemptibly? */
> +static inline bool bpf_prog_recur_per_task(const struct bpf_prog *prog)
> +{
> +	return !prog->percpu_state && bpf_prog_enters_run_ctx(prog);
> +}

[Severity: Medium]
Should bpf_prog_recur_per_task() return true when prog->sleepable is set,
even if prog->percpu_state is also true?

bpf_prog_enter_run_ctx() only disables preemption when
prog->percpu_state && !prog->sleepable, so a sleepable target program with
percpu_state set still runs preemptibly.  Because func_has_rcu_read_lock()
allows a non-sleepable BPF_PROG_TYPE_EXT program to replace a global
subprogram of a sleepable target when called under bpf_rcu_read_lock(),
bpf_tracing_prog_attach() will permit attaching a percpu_state extension to a
sleepable target that also has percpu_state set, allowing two tasks to
overlap inside the extension on the same CPU.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com?part=4

  reply	other threads:[~2026-11-30 12:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:54 [PATCH bpf-next v4 0/6] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 1/6] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-30 12:07   ` sashiko-bot
2026-11-30 11:54 ` [PATCH bpf-next v4 2/6] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-30 12:26   ` sashiko-bot [this message]
2026-11-30 11:54 ` [PATCH bpf-next v4 5/6] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-241049@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mikeba@sentinelone.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox