From: Luis Gerhorst <luis.gerhorst@fau.de>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org,
eddyz87@gmail.com, memxor@gmail.com
Subject: Re: [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack
Date: Thu, 26 Nov 2026 08:59:46 +0100 [thread overview]
Message-ID: <87fqwo3tvx.fsf@fau.de> (raw)
In-Reply-To: <20261126033816.4124847-5-alexei.starovoitov@gmail.com>
Alexei Starovoitov <alexei.starovoitov@gmail.com> writes:
> From: Alexei Starovoitov <ast@kernel.org>
>
> check_stack_write_fixed_off() sets nospec_result for BPF_ATOMIC insns
> and for calls of helpers and kfuncs too. do_check() ends a speculative
> path there, but bpf_convert_ctx_accesses() emits the nospec only after
> BPF_MEM stores.
>
> Later passes rewrite a call and need its aux data. Put the nospec in
> front of the next insn.
>
> Fixes: d6f1c85f2253 ("bpf: Fall back to nospec for Spectre v1")
> Fixes: 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")
> Suggested-by: Luis Gerhorst <luis.gerhorst@fau.de>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
> kernel/bpf/fixups.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index 9d5f2774bf8f..8c3a17a05a4e 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -911,6 +911,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
> const int insn_cnt = env->prog->len;
> struct bpf_insn *epilogue_buf = env->epilogue_buf;
> struct bpf_insn *insn_buf = env->insn_buf;
> + bool nospec, nospec_next = false;
> struct bpf_insn *insn;
> u32 target_size, size_default, off;
> struct bpf_prog *new_prog;
> @@ -994,8 +995,13 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
> enum bpf_reg_type ptr_type;
> u8 mode;
>
> - if (env->insn_aux_data[i + delta].nospec) {
> - WARN_ON_ONCE(env->insn_aux_data[i + delta].alu_state);
> + /* A call or an atomic gets its nospec in front of the next insn */
> + nospec = env->insn_aux_data[i + delta].nospec;
> + WARN_ON_ONCE(nospec && env->insn_aux_data[i + delta].alu_state);
> + nospec |= nospec_next;
> + nospec_next = env->insn_aux_data[i + delta].nospec_result;
> +
> + if (nospec) {
> struct bpf_insn *patch = insn_buf;
>
> *patch++ = BPF_ST_NOSPEC();
> @@ -1102,6 +1108,7 @@ int bpf_convert_ctx_accesses(struct bpf_verifier_env *env)
> delta += cnt - 1;
> env->prog = new_prog;
> insn = new_prog->insnsi + i + delta;
> + nospec_next = false;
> continue;
> }
This looks much better than my proposal, thank you!
The bot appears to not have realized that nospec_result is mostly a
rename of sanitize_stack_spill.
Acked-by: Luis Gerhorst <luis.gerhorst@fau.de>
next prev parent reply other threads:[~2026-11-26 7:59 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 3:38 [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
2026-11-26 3:38 ` [PATCH bpf v2 1/4] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
2026-11-26 3:38 ` [PATCH bpf v2 2/4] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
2026-11-26 3:38 ` [PATCH bpf v2 3/4] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
2026-11-26 3:38 ` [PATCH bpf v2 4/4] bpf: Emit nospec after calls and atomics that write to the stack Alexei Starovoitov
2026-11-26 4:27 ` bot+bpf-ci
2026-11-26 7:59 ` Luis Gerhorst [this message]
2026-11-26 20:10 ` [PATCH bpf v2 0/4] bpf: Fixes for speculation barriers and constant reads patchwork-bot+netdevbpf
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=87fqwo3tvx.fsf@fau.de \
--to=luis.gerhorst@fau.de \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=memxor@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.