public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Helen Koike <koike@igalia.com>,
	andrii@kernel.org, shung-hsi.yu@suse.com,
		yonghong.song@linux.dev, ast@kernel.org, bpf@vger.kernel.org,
		linux-kernel@vger.kernel.org, kernel-dev@igalia.com
Subject: Re: [PATCH] bpf: fix umin/umax when lower bits fall outside u32 range
Date: Fri, 27 Mar 2026 13:46:02 -0700	[thread overview]
Message-ID: <74e65b463d1f992bc0e4846085394a440796f563.camel@gmail.com> (raw)
In-Reply-To: <20260327194849.855397-1-koike@igalia.com>

On Fri, 2026-03-27 at 16:48 -0300, Helen Koike wrote:

[...]

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index a965b2c45bbe..ddac09c8a9e5 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -2702,9 +2702,29 @@ static void __reg_deduce_mixed_bounds(struct bpf_reg_state *reg)
>  	__u64 new_umin, new_umax;
>  	__s64 new_smin, new_smax;
>  
> -	/* u32 -> u64 tightening, it's always well-formed */
> -	new_umin = (reg->umin_value & ~0xffffffffULL) | reg->u32_min_value;
> -	new_umax = (reg->umax_value & ~0xffffffffULL) | reg->u32_max_value;
> +	/*
> +	 * If (u32)umin > u32_max, no value in the current upper-32-bit block
> +	 * satisfies [u32_min, u32_max] while being >= umin; advance umin to
> +	 * the next block. Otherwise apply standard u32->u64 tightening.
> +	 */
> +	if ((u32)reg->umin_value > reg->u32_max_value)
> +		new_umin = (reg->umin_value & ~0xffffffffULL) + (1ULL << 32) |
> +			   reg->u32_min_value;
> +	else
> +		new_umin = (reg->umin_value & ~0xffffffffULL) |
> +			   reg->u32_min_value;

What would happen if there is no next or previous 32-bit block?
E.g. if (reg->umin_value & ~0xffffffffULL) + (1ULL << 32) wraps around.
I guess the argument is that if this happens, there is an invariant
violation already, will the final register still bin in invariant
violation state?

Useful picture:

 N*2^32                   (N+1)*2^32                (N+2)*2^32                (N+3)*2^32
 ||----|=====|--|----------||----|=====|-------------||--|-|=====|-------------||
       |< b >|  |                |< b >|                 | |< b >|
                |                |     |                 |
                |<---------------+- a -+---------------->|
                                 |     |
                                 |< t >| refined r0 range

Also, as this is based solely on unsigned ranges, the following case
is not covered, right?

 N*2^32                   (N+1)*2^32                (N+2)*2^32                (N+3)*2^32
 ||===|---------|------|===||===|----------------|===||===|---------|------|===||
  |b >|         |      |< b||b >|                |< b||b >|         |      |< b|
                |      |                                  |         |
                |<-----+----------------- a --------------+-------->|
                       |                                  |
                       |<---------------- t ------------->| refined r0 range

Would it be hard to implementing something in the same line as [2] to cover it?

> +
> +	/*
> +	 * Symmetrically, if (u32)umax < u32_min, retreat umax to the
> +	 * previous block. Otherwise apply standard u32->u64 tightening.
> +	 */
> +	if ((u32)reg->umax_value < reg->u32_min_value)
> +		new_umax = (reg->umax_value & ~0xffffffffULL) - (1ULL << 32) |
> +			   reg->u32_max_value;
> +	else
> +		new_umax = (reg->umax_value & ~0xffffffffULL) |
> +			   reg->u32_max_value;
> +
>  	reg->umin_value = max_t(u64, reg->umin_value, new_umin);
>  	reg->umax_value = min_t(u64, reg->umax_value, new_umax);
>  	/* u32 -> s64 tightening, u32 range embedded into s64 preserves range validity */

I think we can move forward with this, as the fate of my RFC is
uncertain. Please add some selftests, e.g. from [1].

[1] https://lore.kernel.org/bpf/20260318-cnum-sync-bounds-v1-4-1f2e455ea711@gmail.com/
[2] https://github.com/eddyz87/cnum-verif/blob/master/cnum.c#L242

  parent reply	other threads:[~2026-03-27 20:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-27 19:48 [PATCH] bpf: fix umin/umax when lower bits fall outside u32 range Helen Koike
2026-03-27 20:18 ` Eduard Zingerman
2026-03-27 20:46 ` Eduard Zingerman [this message]
2026-03-30 12:09   ` Helen Koike
2026-03-30 16:24 ` kernel test robot
2026-03-30 17:53 ` kernel test robot

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=74e65b463d1f992bc0e4846085394a440796f563.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-dev@igalia.com \
    --cc=koike@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=shung-hsi.yu@suse.com \
    --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