BPF List
 help / color / mirror / Atom feed
From: Shung-Hsi Yu <shung-hsi.yu@suse.com>
To: Vinicius Sampaio <vldsampaio@pm.me>
Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	 john.fastabend@gmail.com, andrii@kernel.org,
	martin.lau@linux.dev, eddyz87@gmail.com,  song@kernel.org,
	yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me,
	 haoluo@google.com, jolsa@kernel.org, tangyazhou518@outlook.com,
	 shenghaoyuan0928@163.com
Subject: Re: [PATCH bpf-next 1/2] bpf: Simplify cnum contains() and normalize() implementation
Date: Wed, 29 Jul 2026 14:29:53 +0800	[thread overview]
Message-ID: <ammWxQ-PiJ-Z0z67@u94a> (raw)
In-Reply-To: <20260728015601.1567098-2-vldsampaio@pm.me>

On Tue, Jul 28, 2026 at 01:56:58AM +0000, Vinicius Sampaio wrote:
> This patch introduces two optimizations one for the normalize() function
> and other for the contains() function.

Nit: is there a reason these two optimizations need to land as a single
commit?

> In normalize(), there's no need to compare cnum.base with ST_MAX,
> because any cnum with size == UT_MAX represents the full unsigned domain
> independently of base, so normalize() can canonicalize all such values
> to base 0.
> 
> In contains(), bearing in mind that a non-empty cnum represents a
> inclusive circular range in the corresponding unsigned integer domain,
> membership could be tested by checking whether the distance from the
> range base to the queried value is within the range size.
> 
> This makes the explicit wrapping and non-wrapping cases in contains()
> unnecessary, since:
> 
>         v - cnum.base <= cnum.size
> 
> is equivalent for both ordinary ranges and ranges that cross the
> unsigned wrap boundary.
[...]
> @@ -181,7 +181,7 @@ void FN(intersect_with_srange)(struct cnum_t *dst, st min, st max)
>  
>  static inline struct cnum_t FN(normalize)(struct cnum_t cnum)
>  {
> -	if (cnum.size == UT_MAX && cnum.base != 0 && cnum.base != (ut)ST_MAX)
> +	if (cnum.size == UT_MAX && cnum.base != 0)
>  		cnum.base = 0;

Seems right, or at least I could see what semantic `base == ST_MAX` is
encoding. And normalize does not deal with the empty semantic anyway[1].
Could we go even further and also drop the `cnum.base != 0`, just
relying on the `cnum.size == UT_MAX` guard solely?

>  	return cnum;
>  }
> @@ -210,12 +210,7 @@ bool FN(is_empty)(struct cnum_t cnum)
>  
>  bool FN(contains)(struct cnum_t cnum, ut v)
>  {
> -	if (FN(is_empty)(cnum))
> -		return false;
> -	if (FN(urange_overflow)(cnum))
> -		return v >= cnum.base || v <= (ut)cnum.base + cnum.size;
> -	else
> -		return v >= cnum.base && v <= (ut)cnum.base + cnum.size;
> +	return !FN(is_empty)(cnum) && v - cnum.base <= cnum.size;
>  }

Make sense, with `v - cnum.base` we are pivoting to a cnum where

  { .base = 0, .size = cnum.size }

If `v - cnum.base` is within the pivoted cnum, then `v` should be in the
original cnum as well.

Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>

1: https://lore.kernel.org/bpf/7a5c9c3e8ef2cb86b7ae38fb4593e7337e9962c0.camel@gmail.com/

  reply	other threads:[~2026-07-29  6:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28  1:56 [PATCH bpf-next 0/2] Simplify min()/max(), contains() and fix normalize() cnum implementation Vinicius Sampaio
2026-07-28  1:56 ` [PATCH bpf-next 1/2] bpf: Simplify cnum contains() and normalize() implementation Vinicius Sampaio
2026-07-29  6:29   ` Shung-Hsi Yu [this message]
2026-07-29 19:15     ` Vinicius Sampaio
2026-07-28  1:57 ` [PATCH bpf-next 2/2] bpf: Avoid redundant min()/max() in cnum signed bounds Vinicius Sampaio
2026-07-29  7:59   ` Shung-Hsi Yu
2026-07-29 19:21     ` Vinicius Sampaio

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=ammWxQ-PiJ-Z0z67@u94a \
    --to=shung-hsi.yu@suse.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=shenghaoyuan0928@163.com \
    --cc=song@kernel.org \
    --cc=tangyazhou518@outlook.com \
    --cc=vldsampaio@pm.me \
    --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