mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vineet Gupta <vineet.gupta@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	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
Subject: Re: [PATCH bpf-next v2 07/13] bpf: track low-32 scalar equality across sign-extending movs
Date: Tue, 15 Sep 2026 13:39:28 -0700	[thread overview]
Message-ID: <d5d78882-2c68-4fec-b061-7642f3f2871f@linux.dev> (raw)
In-Reply-To: <DLDKPDWY833W.1QJX9MTD6SZG5@gmail.com>



On 9/12/26 12:09 PM, Alexei Starovoitov wrote:
> On Thu Sep 10, 2026 at 9:46 AM PDT, Vineet Gupta wrote:
>> The zero-extending mov records that dst shares src's low 32 bits. A 32-bit
>> sign extension shares them too -- it keeps the low half and fills the high
>> half from bit 31 -- so the same link applies, with a different rule for
>> rebuilding the high bits:
>>
>>    r6 = ...              /* full 64-bit unknown */
>>    r7 = (s32)r6          /* 32-bit sign-extending mov */
>>    if w6 == -1 goto ...  /* taken: r6's low 32 bits are all ones */
>>    ...                   /* r7 is -1, not deduced today */
>>
>> Add SUBREG_SEXT alongside SUBREG_ZEXT, and sext_32_to_64() alongside
>> zext_32_to_64() to drive the reconstruction. Both work from the base's
>> 32-bit range, which is what a 32-bit compare narrows.
>> coerce_reg_to_size_sx() cannot serve here: it reads smin/smax, which
>> straddle after such a compare and collapse to the full field range.
>>
>> tnum_sext() is the counterpart to tnum_cast(). Unlike a tnum_range() over
>> the new bounds it keeps the known low bits.
>>
>> The enum has room for the third value, so bpf_reg_state stays 80 bytes.
>>
>> Unlike the zero-extending arm, a self-mov can form a link here, but only
>> when src is already linked: r0 = (s32)r0 is how a sign-extended int return
>> lands. On an unlinked register there is nothing to link to, and minting an
>> id would leave the register describing itself.
>>
>> Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
>> ---
>> v2: was RFC 5/6.
>>   - no forward declaration (Eduard)
>>   - src renamed known_reg (Eduard)
>>   - sext_32_to_64() and tnum_sext() rather than reusing
>>     coerce_reg_to_size_sx(); the sync path needs the base's 32-bit range,
>>     see the cover letter
>>   - tnum_sext() keeps the known low bits a tnum_range() would drop (Eduard)
>>   - a self-mov links only when src already has an id, narrower than the RFC
>>
>>   include/linux/bpf_verifier.h |  1 +
>>   include/linux/tnum.h         |  3 +++
>>   kernel/bpf/log.c             |  2 ++
>>   kernel/bpf/tnum.c            | 15 ++++++++++++
>>   kernel/bpf/verifier.c        | 47 +++++++++++++++++++++++++++++++++---
>>   5 files changed, 65 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
>> index f1b01059c5da..920c9490ecc8 100644
>> --- a/include/linux/bpf_verifier.h
>> +++ b/include/linux/bpf_verifier.h
>> @@ -53,6 +53,7 @@ enum bpf_add_const {
>>   enum bpf_subreg {
>>   	SUBREG_NONE = 0,
>>   	SUBREG_ZEXT,		/* high bits are zero (32-bit zero-extending mov) */
>> +	SUBREG_SEXT,		/* high bits repeat bit 31 (32-bit sign-extending mov) */
> If my earlier suggestion to model SUBREG_ZEXT as add_const32 with delta == 0
> works, then sign extension could be:
>
> enum bpf_add_const {
>         ADD_CONST_NONE = 0,
>         ADD_CONST_32,    /* delta was added with a 32-bit ALU op */ /* works as ZERO_EXTEND when delta == 0 */
>         ADD_CONST_64,    /* ... with a 64-bit ALU op */
>         SIGN_EXTEND_32, 	/* delta has to be == 0 */
> };

If we were to go that route sure, but no point if we are not doing that.

Also keeping them together makes add_const and sext mutually exclusive. 
Today they are but what if in future we were to implement the following.

w7 = w6      /* r7 gets SUBREG_ZEXT */
w7 += 4      /* r7 also gets ADD_CONST_32, delta 4 */

> and if we really want to we can track movsx 8 and 16 too.
> Which might be an overkill.

Indeed.
Eduard and I discussed this in the off line review before RFC. gcc 
currently doesn't seem to generate these a lot so maybe leave this for 
future.

Thx,
-Vineet

  reply	other threads:[~2026-09-15 20:39 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
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 [this message]
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=d5d78882-2c68-4fec-b061-7642f3f2871f@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®