BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Luis Gerhorst" <luis.gerhorst@fau.de>
Cc: <bpf@vger.kernel.org>, <daniel@iogearbox.net>,
	<andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>
Subject: Re: [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack
Date: Thu, 26 Nov 2026 03:40:17 +0000	[thread overview]
Message-ID: <DN4TWT5BB3DK.2AHODHZP7ADY6@gmail.com> (raw)
In-Reply-To: <20261125-nospec-result-reply-d60fdc6da8ef@fau.de>

On Wed, Nov 25, 2026 at 09:12 AM Luis Gerhorst <luis.gerhorst@fau.de> wrote:
> Would it not be possible to avoid the atomic/call-specific handling of
> nospec_result? We can patch in the nospec after the call (next
> patch) and avoid the speculative path analysis there. Then the design
> would treat all instructions that do safety-critical stores the same.

Yes. That is better. Patch 5 also rejects a bounded loop with such
a call when there is no CAP_PERFMON.

> In response to this you find a series that implements this idea. Main
> downside is that this requires a new instruction buffer in the
> environment, not sure if this is desired.

bpf_insert_insns() and the new buffer are not needed.
Here is what I did in new patch 4:

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;
 		}
 

  parent reply	other threads:[~2026-11-26  3:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  4:08 [PATCH bpf 0/5] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 1/5] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 2/5] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 3/5] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack Alexei Starovoitov
2026-11-25 17:12   ` Luis Gerhorst
2026-11-25 17:12     ` [PATCH bpf 0/4] bpf: Emit nospec after every insn that " Luis Gerhorst
2026-11-25 17:12       ` [PATCH bpf 1/4] bpf: Add bpf_insert_insns() to insert insns next to an insn Luis Gerhorst
2026-11-25 17:51         ` sashiko-bot
2026-11-25 18:17         ` bot+bpf-ci
2026-11-25 17:12       ` [PATCH bpf 2/4] selftests/bpf: Test the insns that bpf_insert_insns() inserts Luis Gerhorst
2026-11-25 17:39         ` sashiko-bot
2026-11-25 17:12       ` [PATCH bpf 3/4] bpf: Emit nospec after every insn with nospec_result Luis Gerhorst
2026-11-25 18:17         ` bot+bpf-ci
2026-11-25 17:12       ` [PATCH bpf 4/4] selftests/bpf: Test nospec after stack writes of calls and atomics Luis Gerhorst
2026-11-25 17:45         ` sashiko-bot
2026-11-26  3:40     ` Alexei Starovoitov [this message]
2026-11-25  4:08 ` [PATCH bpf 5/5] bpf: Do not end a speculative path at a call Alexei Starovoitov
2026-11-25  5:00   ` bot+bpf-ci

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=DN4TWT5BB3DK.2AHODHZP7ADY6@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=luis.gerhorst@fau.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox