From: Vineet Gupta <vineet.gupta@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>, Eduard <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
Shuah Khan <shuah@kernel.org>, bpf <bpf@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 03/13] bpf: track low-32 scalar equality across zero-extending movs
Date: Wed, 16 Sep 2026 17:08:16 -0700 [thread overview]
Message-ID: <202c45e2-58ba-4ad5-a234-c90703031f91@linux.dev> (raw)
In-Reply-To: <CAADnVQ+N9tzBJ0Bbvp8_8QvR+QcCP4ej5CgV_kRHt=iw4BANSQ@mail.gmail.com>
On 9/15/26 9:23 PM, Alexei Starovoitov wrote:
> On Tue, Sep 15, 2026 at 1:31 PM Vineet Gupta <vineet.gupta@linux.dev> wrote:
>> On 9/12/26 11:59 AM, Alexei Starovoitov wrote:
>>> On Thu Sep 10, 2026 at 9:46 AM PDT, Vineet Gupta wrote:
>>>> Linked-scalar equality is full-64-bit only. A 32-bit mov from a source
>>>> with unknown high bits therefore has to drop the relationship, and a later
>>>> narrowing of the source never reaches the destination:
>>>>
>>>> r6 = ... /* full 64-bit unknown */
>>>> w7 = w6 /* 32-bit zero-extending mov */
>>>> if w6 != 0 goto ... /* not taken: r6's low 32 bits are 0 */
>>>> if w7 == 0 goto ... /* not deduced today */
>>>>
>>>> Record a low-32-only link instead: dst shares src's low 32 bits and its
>>>> high half is zero. On a later narrowing, sync_linked_regs() rebuilds such
>>>> a register from the base rather than copying it, by re-applying the same
>>>> zext_32_to_64() the mov used. The reverse direction is skipped: a ->subreg
>>>> base knows nothing about a full register's high half.
>>> at the first glance SUBREG_ZEXT is exactly the same as ADD_CONST32 delta == 0.
>>> no?
>>> Both are unidirectional:
>>> w6->id == 1
>>> w7->id == 1, add_const == 32, delta == 0
>>>
>>> will zero extend w7.
>>>
>>> This new SUBREG_ZEXT will do the same.
>>> What am I missing?
>> The 2 examples below both imply ADD_CONST32 + delta=0, with different
>> semantics and need special casing.
>>
>> w3 = w2; w3 += 0
>> w7 = w6; (r6 was wide)
>>
>> With code hacks it could in fact me made to work, but its less cleaner.
>>
>>
>> Current approach we have something like below in sync_linked_regs()
>>
>> /*
>> * A ->subreg register shares only the base's low 32
>> bits, so it
>> * is rebuilt rather than copied. Not modelled together
>> with a
>> * delta, so skip if either side has one (sound, less
>> precise).
>> */
>> if (reg->subreg) {
>> if (reg->add_const || known_reg->add_const)
>> continue;
>> if (reg->subreg == SUBREG_ZEXT)
>> reconstruct_zext32(reg, known_reg);
>> else
>> reconstruct_sext32(reg, known_reg);
>> if (e->is_reg)
>> mark_reg_scratched(env, e->regno);
>> else
>> mark_stack_slot_scratched(env, e->spi);
>> continue;
>> }
>> /*
>> * The reverse: known_reg knows only its low 32 bits,
>> which say
>> * nothing about reg's high half.
>> */
>> if (known_reg->subreg)
>> continue;
>>
>>
>> With the suggested approach it looks something like below.
>> ADD_CONST_32, delta == 0 has to be disambiguated from a real += 0 by
>> inspecting another register's bounds.
>>
>> /*
>> * A low-32 link shares only the base's low 32 bits, so it is
>> * rebuilt rather than copied.
>> *
>> * SIGN_EXTEND_32 says so outright. ADD_CONST_32 with
>> delta == 0
>> * does not: it is also what "w3 = w2; w3 += 0" records,
>> which is
>> * a plain equality. The two are separable only by the base's
>> * width
>> */
>> if (reg->add_const == SIGN_EXTEND_32 &&
>> !known_reg->add_const) {
>> reconstruct_sext32(reg, known_reg);
>> goto scratched;
>> }
>> if (reg->add_const == ADD_CONST_32 && reg->delta == 0 &&
>> !known_reg->add_const) {
>> reconstruct_zext32(reg, known_reg);
>> goto ...;
>> }
> I think that's a problem with reconstruct_zext32().
> It shouldn't be necessary.
> The existing code that adds a constant should
> already handle it.
> If not, we have a bug in add_const_32.
Not really a bug, but a deliberate design choice in the past.
if (alu32 && (dst_umax > U32_MAX))
goto clear_id;
So the core change is ADD_CONST_32 needs to be allowed on wider regs.
So treating it as
reg = (u32)(base + delta)
which simplifies to zero-extend for delta==0 and base can be either wide
or narrow.
> Instead of losing link at wx=wy time
> we can keep it with delta == 0
> and sync_linked_regs() shouldn't need any new code.
I toyed with a prototype which essentially lifts the restriction above
(and ensuing adjustments)
FWIW it *does* require a reverse guard in sync_linked_regs() - so some
additional code there (at least in my version).
It ended up with full testsuite run parity - after 4 incremental patches.
But the pattern of all those patches was adding some predicate /
special-casing to reg->add_const
hunk 1
- if (src_reg->add_const)
+ if (src_reg->add_const && src_reg->delta)
hunk 2
- if (dst_reg->add_const) {
+ if (dst_reg->add_const && !dst_reg->delta &&
+ dst_reg->add_const == (alu32 ? ADD_CONST_32 : ADD_CONST_64)) {
+ dst_reg->delta = off;
+ } else if (dst_reg->add_const) {
hunk 3
- if (subreg_link && reg->id)
- state->regs[dst_regno].subreg = is_ldsx ? SUBREG_SEXT : SUBREG_ZEXT;
+ if (subreg_link && reg->id) {
+ if (is_ldsx) {
+ state->regs[dst_regno].subreg_sext = true;
+ } else {
+ state->regs[dst_regno].add_const = ADD_CONST_32;
+ state->regs[dst_regno].delta = 0;
+ }
+ }
hunk 4
- if (known_reg->subreg)
+ if (known_reg->subreg_sext)
+ continue;
+ /*
+ * Same for a zero-extending link, now spelled ADD_CONST_32:
+ * reg == (u32)(base + delta) does not invert once the base is
+ * wider than 32 bits.
+ */
+ if (known_reg->add_const == ADD_CONST_32 &&
+ get_reg_width(reg) > 32)
continue;
Yes it does allow removal of reconstruct_zext32()
- if (reg->subreg) {
+ if (reg->subreg_sext) {
if (reg->add_const || known_reg->add_const)
continue;
- if (reg->subreg == SUBREG_ZEXT)
- reconstruct_zext32(reg, known_reg);
- else
- reconstruct_sext32(reg, known_reg);
+ reconstruct_sext32(reg, known_reg);
but personally speaking reading the code feels a bit more harder now.
I can send over the full patch which converts the v2 SUBREG_ZEXT into
ADD_CONST32+delta=0 to give a feel for all the special casings, were it
to be subsumed into the orig zext support work, but I think you
understand what I'm getting at ;-)
Happy to pursue whatever your maintainer hat tells you.
Thx,
-Vineet
next prev parent reply other threads:[~2026-09-17 0:08 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-10 16:46 [PATCH bpf-next v2 00/13] bpf: track scalar equality across the low 32 bits Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 01/13] bpf: move linked-scalar flags out of bpf_reg_state->id [NFC] Vineet Gupta
2026-09-10 17:52 ` bot+bpf-ci
2026-09-14 18:02 ` Vineet Gupta
2026-09-16 21:03 ` Alexei Starovoitov
2026-09-16 21:21 ` Vineet Gupta
2026-09-12 18:50 ` Alexei Starovoitov
2026-09-15 1:17 ` Vineet Gupta
2026-09-16 21:02 ` Alexei Starovoitov
2026-09-16 21:24 ` Vineet Gupta
2026-09-17 0:36 ` Alexei Starovoitov
2026-09-10 16:46 ` [PATCH bpf-next v2 02/13] bpf: compare linked-scalar kinds in regs_exact() Vineet Gupta
2026-09-12 18:51 ` Alexei Starovoitov
2026-09-15 1:11 ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 03/13] bpf: track low-32 scalar equality across zero-extending movs Vineet Gupta
2026-09-10 17:52 ` bot+bpf-ci
2026-09-11 9:29 ` Vineet Gupta
2026-09-12 18:59 ` Alexei Starovoitov
2026-09-15 20:31 ` Vineet Gupta
2026-09-16 4:23 ` Alexei Starovoitov
2026-09-17 0:08 ` Vineet Gupta [this message]
2026-09-17 0:30 ` Alexei Starovoitov
2026-09-10 16:46 ` [PATCH bpf-next v2 04/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 05/13] bpf: keep the range across a sign extension that cannot change it Vineet Gupta
2026-09-10 17:52 ` bot+bpf-ci
2026-09-11 10:37 ` Vineet Gupta
2026-09-12 19:02 ` Alexei Starovoitov
2026-09-15 21:18 ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 06/13] selftests/bpf: cover sign extensions that cannot change the range Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 07/13] bpf: track low-32 scalar equality across sign-extending movs Vineet Gupta
2026-09-10 17:52 ` bot+bpf-ci
2026-09-11 10:00 ` Vineet Gupta
2026-09-12 19:09 ` Alexei Starovoitov
2026-09-15 20:39 ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 08/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 17:52 ` bot+bpf-ci
2026-09-11 8:00 ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 09/13] bpf: track low-32 scalar equality across narrowing stack fills Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 10/13] selftests/bpf: cover the low-32 link for " Vineet Gupta
2026-09-10 17:31 ` bot+bpf-ci
2026-09-11 5:07 ` Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 11/13] bpf: record what a narrowing spill actually stores Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 12/13] bpf: track low-32 scalar equality across narrowing stack spills Vineet Gupta
2026-09-10 16:46 ` [PATCH bpf-next v2 13/13] 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=202c45e2-58ba-4ad5-a234-c90703031f91@linux.dev \
--to=vineet.gupta@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--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
all inboxes | Powered by JetHome®