Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Vineet Gupta <vineet.gupta@linux.dev>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	eddyz87@gmail.com, memxor@gmail.com
Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
	jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev,
	john.fastabend@gmail.com, shuah@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	Vineet Gupta <vineet.gupta@linux.dev>
Subject: [PATCH bpf-next v3 01/12] bpf: Keep the 32-bit range across a sign extension that cannot change it
Date: Tue, 20 Oct 2026 09:18:56 -0700	[thread overview]
Message-ID: <20261020161907.2115379-2-vineet.gupta@linux.dev> (raw)
In-Reply-To: <20261020161907.2115379-1-vineet.gupta@linux.dev>

This patch addresses the verifier deficiency where it would throw away
a good range whenever that range crossed zero.

| 1: (54) w0 &= 63      ; R0=scalar(smin32=0,smax32=63,...)
| 2: (14) w0 -= 63      ; R0=scalar(smin32=-63,smax32=0,...)
| 3: (bc) w0 = (s8)w0   ; R0=scalar(smin32=-128,smax32=127,...)
| 4: (c6) if w0 s< 0xffffffc1 goto pc+2
| ...
| 7: (34) w0 /= 0
| div by zero

[-63, 0] already fits an s8, so insn 3 cannot change any value in it, yet
the range comes out widened to [-128, 127]. The guard at insn 4 then
cannot be resolved and the div becomes reachable.

The issue is the following check in coerce_subreg_to_size_sx()

	top_smax_value = ((u32)reg_s32_max(reg) >> num_bits) << num_bits;
	top_smin_value = ((u32)reg_s32_min(reg) >> num_bits) << num_bits;

	if (top_smax_value != top_smin_value)
		goto out;

which doesn't fire when the range includes zero, since the top bits
differ. e.g. for [-63, 0]:

    smin32 = -63: high bits all ones
    smax32 =   0: high bits all zero

The cheap noop test above it does not catch this either -- it only looks
at reg_umax(), which a negative smin makes huge.

The 64-bit twin, coerce_reg_to_size_sx(), had the same gap and was given
an equivalent early return by commit 0892ef2a7ef3 ("bpf: Representation
for intervals with steps"). This patch does the same for the 32-bit path,
for the s8 and s16 widths it handles. It doesn't improve anything in
selftests or corpora of external objects -- the shape above only shows up
in the test the next patch adds -- but seems like the right thing to do
anyways.

Fixes: 8100928c8814 ("bpf: Support new sign-extension mov insns")
Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
---
v3: was v2 5/13.
 - moved to the front: it is unrelated to the low-32 tracking (Alexei)
 - narrowed to the 32-bit path. Commit 0892ef2a7ef3 ("bpf:
   Representation for intervals with steps") has since given
   coerce_reg_to_size_sx() an equivalent early return, so Fixes:
   1f9a1ea821ff goes with that half.
 - measured inert: 0 instruction and 0 state changes on both selftest
   corpora and on 441 external objects.

 kernel/bpf/verifier.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 62f8951126ba..e1b41975a442 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6182,6 +6182,7 @@ static void set_sext32_default_val(struct bpf_reg_state *reg, int size)
 static void coerce_subreg_to_size_sx(struct bpf_reg_state *reg, int size)
 {
 	s32 init_s32_max, init_s32_min, s32_max, s32_min, u32_val;
+	s32 field_smin, field_smax;
 	u32 top_smax_value, top_smin_value;
 	u32 num_bits = size * 8;
 
@@ -6192,6 +6193,27 @@ static void coerce_subreg_to_size_sx(struct bpf_reg_state *reg, int size)
 	if (reg_umax(reg) < (1ULL << (num_bits - 1)))
 		return;
 
+	if (size == 1) {
+		field_smin = S8_MIN;
+		field_smax = S8_MAX;
+	} else {
+		/* size == 2 */
+		field_smin = S16_MIN;
+		field_smax = S16_MAX;
+	}
+
+	/*
+	 * The range already fits the field, so (sN)v == v for every value the
+	 * register can hold and the sign extension changes nothing. The test
+	 * above misses this once the range straddles zero: it only looks at
+	 * reg_umax(), which a negative smin makes huge.
+	 *
+	 * This has to precede reg_step_reset() below: the extension is a noop,
+	 * so the base/step line still holds and there is nothing to discard.
+	 */
+	if (reg_s32_min(reg) >= field_smin && reg_s32_max(reg) <= field_smax)
+		return;
+
 	/* See comment at reg_step_check_unsigned(). */
 	reg_step_reset(reg);
 
-- 
2.53.0-Meta


  reply	other threads:[~2026-10-20 16:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20 16:18 [PATCH bpf-next v3 00/12] bpf: track scalar equality across the low 32 bits Vineet Gupta
2026-10-20 16:18 ` Vineet Gupta [this message]
2026-10-20 16:18 ` [PATCH bpf-next v3 02/12] selftests/bpf: Cover sign extensions that cannot change the range Vineet Gupta
2026-10-20 16:18 ` [PATCH bpf-next v3 03/12] bpf: Prepare linked scalars for relations narrower than 64 bits Vineet Gupta
2026-10-20 17:08   ` bot+bpf-ci
2026-10-20 16:18 ` [PATCH bpf-next v3 04/12] bpf: Track low-32 scalar equality across zero-extending movs Vineet Gupta
2026-10-20 17:09   ` bot+bpf-ci
2026-10-20 16:19 ` [PATCH bpf-next v3 05/12] selftests/bpf: Cover the low-32 link for " Vineet Gupta
2026-10-20 16:53   ` bot+bpf-ci
2026-10-20 16:19 ` [PATCH bpf-next v3 06/12] bpf: Track low-32 scalar equality across sign-extending movs Vineet Gupta
2026-10-20 16:19 ` [PATCH bpf-next v3 07/12] selftests/bpf: Cover the low-32 link for " Vineet Gupta
2026-10-20 16:19 ` [PATCH bpf-next v3 08/12] bpf: Recover the low 32 bits on the reverse link direction Vineet Gupta
2026-10-20 16:53   ` bot+bpf-ci
2026-10-20 16:19 ` [PATCH bpf-next v3 09/12] bpf: Track low-32 scalar equality across narrowing stack fills Vineet Gupta
2026-10-20 16:19 ` [PATCH bpf-next v3 10/12] selftests/bpf: Cover the low-32 link for " Vineet Gupta
2026-10-20 16:52   ` bot+bpf-ci
2026-10-20 16:19 ` [PATCH bpf-next v3 11/12] [do not merge] bpf: Track low-32 scalar equality across narrowing stack spills Vineet Gupta
2026-10-20 16:19 ` [PATCH bpf-next v3 12/12] [do not merge] selftests/bpf: Cover the low-32 link for " Vineet Gupta

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=20261020161907.2115379-2-vineet.gupta@linux.dev \
    --to=vineet.gupta@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@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