BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Viktor Malik" <vmalik@redhat.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/2] bpf: Track CPUs that have executed a BPF program
Date: Fri, 04 Dec 2026 14:03:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-249924@kernel.org> (raw)
In-Reply-To: <5cf89312e3fea4409dc1fc9da6aa37b1e7a7f3b5.1796391323.git.vmalik@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] bpf: link failure under CONFIG_BPF_JIT=n in bpf_prog_mark_ran()
- [Medium] bpf: missing bpf_prog_mark_ran() in bpf_run_async_cb()
- [Low] bpf: untracked bpf_prog_pack trampoline execution bypassing
  __bpf_prog_exit*()
--

commit a794f32a47f5dda2a5c528bf869f1bd4c8182a6e
Author: Viktor Malik <vmalik@redhat.com>

bpf: Track CPUs that have executed a BPF program

This commit introduces a per-CPU bpf_prog_ran flag, gated behind the
bpf_pred_flush_enabled static key, and sets it in __bpf_prog_run() and
the trampoline exit helpers so arch code can restrict indirect branch
predictor flushes to CPUs that have executed BPF code.

> diff --git a/include/linux/filter.h b/include/linux/filter.h
> index 275c89ed7574a..cb408ddbca2f0 100644
> --- a/include/linux/filter.h
> +++ b/include/linux/filter.h
> @@ -873,6 +873,31 @@ typedef unsigned int (*bpf_dispatcher_fn)(const void *ctx,
>  					  unsigned int (*bpf_func)(const void *,
>  								   const struct bpf_insn *));
>  
> +DECLARE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
> +DECLARE_PER_CPU(bool, bpf_prog_ran);
> +
> +/*
> + * When BPF JIT-spraying mitigation is in place, record, on the current CPU,
> + * that a JITed BPF program has just executed.
> + *
> + * The mitigation flushes the indirect branch predictors (IBPB on x86) when cBPF
> + * JIT memory is reused, to drop stale predictions from the code that previously
> + * occupied that memory. That flush is only ever needed on CPUs that have
> + * actually executed BPF since the last flush, so arch code consults this flag
> + * to target those CPUs.
> + */
> +static __always_inline void bpf_prog_mark_ran(void)
> +{
> +	bool *ran;
> +
> +	if (!static_branch_unlikely(&bpf_pred_flush_enabled))
> +		return;
> +
> +	ran = raw_cpu_ptr(&bpf_prog_ran);
> +	if (!READ_ONCE(*ran))
> +		WRITE_ONCE(*ran, true);
> +}

[Severity: High]
Does this break linking when CONFIG_BPF=y and CONFIG_BPF_JIT=n?

bpf_pred_flush_enabled and bpf_prog_ran are declared and referenced
unconditionally here, but both definitions and exports in kernel/bpf/core.c
sit inside #ifdef CONFIG_BPF_JIT.  Any interpreter-only build with callers
of __bpf_prog_run(), such as seccomp_run_filters(), will fail vmlinux link
and module modpost with undefined references to both symbols.

Guarding the declarations and bpf_prog_mark_ran() with #ifdef CONFIG_BPF_JIT
and providing an empty stub otherwise fixes the build and avoids emitting a
dead static branch when JIT is disabled.

[ ... ]

> @@ -898,6 +923,7 @@ static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog,
>  	} else {
>  		ret = dfunc(ctx, prog->insnsi, prog->bpf_func);
>  	}
> +	bpf_prog_mark_ran();
>  	return ret;
>  }

[Severity: Medium]
Should bpf_run_async_cb() in kernel/bpf/helpers.c also call
bpf_prog_mark_ran()?

Async BPF callbacks (such as bpf_timer_cb() and bpf_wq_work()) invoke JITed
subprograms allocated from bpf_prog_pack directly via callback_fn(),
bypassing __bpf_prog_run() and the trampoline exit helpers.

A CPU that runs one while bpf_prog_ran is false is skipped by a later
targeted flush when those bpf_prog_pack chunks are reused by cBPF, leaving
stale indirect branch predictions live.

All callers of bpf_run_async_cb() already run with migration disabled, so
calling bpf_prog_mark_ran() after callback_fn() there closes this gap.

[ ... ]

> diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> index 619297eae8c15..43c1b2fb88399 100644
> --- a/kernel/bpf/trampoline.c
> +++ b/kernel/bpf/trampoline.c
> @@ -1382,6 +1382,7 @@ static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,
>  					  struct bpf_tramp_run_ctx *run_ctx)
>  	__releases(RCU)
>  {
> +	bpf_prog_mark_ran();
>  	/* Before the exit, which can enable preemption: time off the CPU is not run time */
>  	update_prog_stats(prog, start);
>  	bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx);

[Severity: Low]
Can a CPU execute JITed trampoline code from bpf_prog_pack without setting
bpf_prog_ran?

On x86-64, arch_alloc_bpf_trampoline() allocates trampolines from
bpf_prog_pack, and under BPF_TRAMP_F_ORIG_STACK the trampoline runs an
indirect call *rbx there with migration enabled.

In an fexit-only trampoline, a task can run call *rbx on CPU0, sleep in the
original function, and migrate to CPU1 where __bpf_prog_exit*() runs,
leaving bpf_prog_ran false on CPU0.  The same happens through
__bpf_tramp_exit() or during skip patching in __bpf_trampoline_unlink_prog().

If the trampoline image is later freed and reused by a cBPF allocation, a
targeted predictor flush would skip that CPU.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796391323.git.vmalik@redhat.com?part=1

  reply	other threads:[~2026-12-04 14:03 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:44 [PATCH bpf-next 0/2] bpf, x86: Restrict BPF JIT IBPB flush to CPUs that ran BPF Viktor Malik
2026-12-04 13:44 ` [PATCH bpf-next 1/2] bpf: Track CPUs that have executed a BPF program Viktor Malik
2026-12-04 14:03   ` sashiko-bot [this message]
2026-12-04 15:22     ` Viktor Malik
2026-12-04 14:51   ` Valentin Schneider
2026-12-04 16:27   ` Dave Hansen
2026-12-04 16:45     ` Viktor Malik
2026-12-04 13:44 ` [PATCH bpf-next 2/2] x86/bugs: Restrict BPF JIT IBPB flush to CPUs that ran BPF Viktor Malik
2026-12-04 14:17   ` sashiko-bot
2026-12-04 15:33     ` Viktor Malik
2026-12-04 14:51   ` Valentin Schneider
2026-12-04 15:01 ` [PATCH bpf-next 0/2] bpf, x86: " Valentin Schneider
2026-12-04 16:37   ` Dave Hansen
2026-12-04 17:46     ` Valentin Schneider
2026-12-04 19:42       ` Alexei Starovoitov

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-249924@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vmalik@redhat.com \
    /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