From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796041588; b=mKHIB6kRiR8pWyjdgZkv/JTWZF6e8En5KXmwtPH14ChcBfDWWfWde0xeSNuMyqs9Ud8G 5OHZloq8BYxEnkUlUKU9gyXhvh93U2lk3sxqKTPTl1Sl1sMNtkuOhukSa/Myo+b0aw774 hyF/ppNqgQ1Mkf7qO36fUiFpr8jJkTeJB+xSeB6v7TDefT5aBjvO6D0GfR8ycxLMWFPoq wiO/ifhDC6i8H2aCVcd1Yt3R0GiYJf38DbCcTWSvkwMILEHQQVwjcWPCjh/31PluSv4sH xePbCvcT7oTd2KyeyJKJwEhRGGKrAOUTHvqAxyFjueJK5eC7XbP/7BXj2b2OPR/aGpA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796041588; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=SsKQ7F4EjPq1sjTmSGsXjsV2U3Y2KR5YH41F7xr4CsE=; b=UYiNUt6/hjeUZ0WD0iN6AJE1Zl0NEUK8sORvnh1vRy3psDi1EzilGM5sznfVfqplARuq 3eC7hs/Khe5QLJtsuZqCl+vhqDsYgPgep5ydjqXMnJFOCME0RK7oKKgog1YUXFtA+6BfG CbOVNeawCZ9Z9ZBzD1RBYCli9QJO4sKtJM+09gB4jBTiqJ4soCM2GdGOBhXgdZqWl/TqB JUOK26wj61S7cNN/6Cdy5sv3Ui+umrRPkjuNXwlEpv5+bAHc9fpoZj+RJYMNVD9jLF18F BJAm7DDHSs0JCO5UUeLUtOwjddqYx9iAwUEckBhJI0ztnRR/9GgTGZXDvjbX8ayxG3A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X5ntIDW3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCB9E606A320; Mon, 30 Nov 2026 12:26:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X5ntIDW3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DC4E1F00897; Mon, 30 Nov 2026 12:26:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796041588; bh=SsKQ7F4EjPq1sjTmSGsXjsV2U3Y2KR5YH41F7xr4CsE=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=X5ntIDW3oyAbjhXAYAv/1S4MxygKHt1BqBgYh/p83Nz6KDtZOKJNIiZ1ATHQarJb9 FSdHHhmpbrMmfAic8KJCNylKH6kZbMgECDC4S/h4GYuMS73dkdkKxchZo4WUpymzhd JhDnRAUotRZ/2gc1y7PA/qGSVimt4SRWy7hheRS1FWQATYIqNBQjDqu8/hxTrEuTHv Fjc4EreULj7hrGQ4E+juzSZACZBSUtu3FjgHsD43JqTs6wILh0QThb3kaHSCYwEYX0 oRFubKf2wwQyubngXnHJQDGqWiVcNqDThdNiW8QDlDNR73iTsM/l7yHUsFu3LD+ed0 uDkxG0GYHO8Hw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx Reply-To: sashiko-reviews@lists.linux.dev To: "Mike Bazov" , bpf@vger.kernel.org Cc: linux-trace-kernel@vger.kernel.org In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-4-bd28389dfd64@sentinelone.com> References: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com> <20261130-bpf-preempt-v2-r2-v4-4-bd28389dfd64@sentinelone.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 12:26:28 +0000 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 lin= ked on > + * top of @old_ctx. It must not if a frame from @old_ctx outward runs @p= rog > + * already: that would be recursion. Interrupts and NMIs run on top of t= he 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 exi= ts. A > + * sleepable one has to use bpf_preempt_disable() itself. > + * > + * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() eit= her 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 =3D old_ctx; ctx; ctx =3D READ_ONCE(ctx->saved_run_ctx)) { > + if (READ_ONCE(ctx->prog) =3D=3D 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 =3D 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() __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 =3D=3D 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 stru= ct bpf_prog *prog) > } > } > =20 > +/* 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130-bpf-preemp= t-v2-r2-v4-0-bd28389dfd64@sentinelone.com?part=3D4