netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libbpf: Fix is_pow_of_2
@ 2022-06-03  4:17 Ian Rogers
  2022-06-03  4:31 ` Zvi Effron
  2022-06-19 17:12 ` Pavel Machek
  0 siblings, 2 replies; 7+ messages in thread
From: Ian Rogers @ 2022-06-03  4:17 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel
  Cc: Yuze Chi, Ian Rogers

From: Yuze Chi <chiyuze@google.com>

There is a missing not. Consider a power of 2 number like 4096:

x && (x & (x - 1))
4096 && (4096 & (4096 - 1))
4096 && (4096 & 4095)
4096 && 0
0

with the not this is:
x && !(x & (x - 1))
4096 && !(4096 & (4096 - 1))
4096 && !(4096 & 4095)
4096 && !0
4096 && 1
1

Reported-by: Yuze Chi <chiyuze@google.com>
Signed-off-by: Yuze Chi <chiyuze@google.com>
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3f4f18684bd3..fd0414ea00df 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
 
 static bool is_pow_of_2(size_t x)
 {
-	return x && (x & (x - 1));
+	return x && !(x & (x - 1));
 }
 
 static size_t adjust_ringbuf_sz(size_t sz)
-- 
2.36.1.255.ge46751e96f-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-03  4:17 [PATCH] libbpf: Fix is_pow_of_2 Ian Rogers
@ 2022-06-03  4:31 ` Zvi Effron
  2022-06-03  5:33   ` Andrii Nakryiko
  2022-06-19 17:12 ` Pavel Machek
  1 sibling, 1 reply; 7+ messages in thread
From: Zvi Effron @ 2022-06-03  4:31 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel, Yuze Chi

On Thu, Jun 2, 2022 at 9:17 PM Ian Rogers <irogers@google.com> wrote:
>
> From: Yuze Chi <chiyuze@google.com>
>
> There is a missing not. Consider a power of 2 number like 4096:
>
> x && (x & (x - 1))
> 4096 && (4096 & (4096 - 1))
> 4096 && (4096 & 4095)
> 4096 && 0
> 0
>
> with the not this is:
> x && !(x & (x - 1))
> 4096 && !(4096 & (4096 - 1))
> 4096 && !(4096 & 4095)
> 4096 && !0
> 4096 && 1
> 1
>
> Reported-by: Yuze Chi <chiyuze@google.com>
> Signed-off-by: Yuze Chi <chiyuze@google.com>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/lib/bpf/libbpf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 3f4f18684bd3..fd0414ea00df 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
>
>  static bool is_pow_of_2(size_t x)
>  {
> -       return x && (x & (x - 1));
> +       return x && !(x & (x - 1));

No idea if anyone cares about the consistency, but in linker.c (same directory)
the same static function is defined using == 0 at the end instead of using the
not operator.

Aside from the consistency issue, personally I find the == 0 version a little
bit easier to read and understand because it's a bit less dense (and a "!" next
to a "(" is an easy character to overlook).

>  }
>
>  static size_t adjust_ringbuf_sz(size_t sz)
> --
> 2.36.1.255.ge46751e96f-goog
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-03  4:31 ` Zvi Effron
@ 2022-06-03  5:33   ` Andrii Nakryiko
  2022-06-03  5:35     ` Andrii Nakryiko
  0 siblings, 1 reply; 7+ messages in thread
From: Andrii Nakryiko @ 2022-06-03  5:33 UTC (permalink / raw)
  To: Zvi Effron
  Cc: Ian Rogers, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Networking, bpf, open list, Yuze Chi

On Thu, Jun 2, 2022 at 9:31 PM Zvi Effron <zeffron@riotgames.com> wrote:
>
> On Thu, Jun 2, 2022 at 9:17 PM Ian Rogers <irogers@google.com> wrote:
> >
> > From: Yuze Chi <chiyuze@google.com>
> >
> > There is a missing not. Consider a power of 2 number like 4096:
> >
> > x && (x & (x - 1))
> > 4096 && (4096 & (4096 - 1))
> > 4096 && (4096 & 4095)
> > 4096 && 0
> > 0
> >
> > with the not this is:
> > x && !(x & (x - 1))
> > 4096 && !(4096 & (4096 - 1))
> > 4096 && !(4096 & 4095)
> > 4096 && !0
> > 4096 && 1
> > 1
> >
> > Reported-by: Yuze Chi <chiyuze@google.com>
> > Signed-off-by: Yuze Chi <chiyuze@google.com>
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/lib/bpf/libbpf.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 3f4f18684bd3..fd0414ea00df 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
> >
> >  static bool is_pow_of_2(size_t x)
> >  {
> > -       return x && (x & (x - 1));
> > +       return x && !(x & (x - 1));

ugh... *facepalm*

>
> No idea if anyone cares about the consistency, but in linker.c (same directory)
> the same static function is defined using == 0 at the end instead of using the
> not operator.
>
> Aside from the consistency issue, personally I find the == 0 version a little
> bit easier to read and understand because it's a bit less dense (and a "!" next
> to a "(" is an easy character to overlook).
>

I agree, even more so, logical not used with arbitrary integer (not a
pointer or bool) is a mental stumbling block for me, so much so that I
avoid doing !strcmp(), for example.

But in this case, I'm not sure why I copy/pasted is_pow_of_2() instead
of moving the one from linker.c into libbpf_internal.h as static
inline. Let's do that instead?

> >  }
> >
> >  static size_t adjust_ringbuf_sz(size_t sz)
> > --
> > 2.36.1.255.ge46751e96f-goog
> >

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-03  5:33   ` Andrii Nakryiko
@ 2022-06-03  5:35     ` Andrii Nakryiko
  0 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2022-06-03  5:35 UTC (permalink / raw)
  To: Zvi Effron
  Cc: Ian Rogers, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Networking, bpf, open list, Yuze Chi

On Thu, Jun 2, 2022 at 10:33 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Jun 2, 2022 at 9:31 PM Zvi Effron <zeffron@riotgames.com> wrote:
> >
> > On Thu, Jun 2, 2022 at 9:17 PM Ian Rogers <irogers@google.com> wrote:
> > >
> > > From: Yuze Chi <chiyuze@google.com>
> > >
> > > There is a missing not. Consider a power of 2 number like 4096:
> > >
> > > x && (x & (x - 1))
> > > 4096 && (4096 & (4096 - 1))
> > > 4096 && (4096 & 4095)
> > > 4096 && 0
> > > 0
> > >
> > > with the not this is:
> > > x && !(x & (x - 1))
> > > 4096 && !(4096 & (4096 - 1))
> > > 4096 && !(4096 & 4095)
> > > 4096 && !0
> > > 4096 && 1
> > > 1
> > >
> > > Reported-by: Yuze Chi <chiyuze@google.com>
> > > Signed-off-by: Yuze Chi <chiyuze@google.com>
> > > Signed-off-by: Ian Rogers <irogers@google.com>

also can you please add Fixes: tag?

> > > ---
> > >  tools/lib/bpf/libbpf.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > > index 3f4f18684bd3..fd0414ea00df 100644
> > > --- a/tools/lib/bpf/libbpf.c
> > > +++ b/tools/lib/bpf/libbpf.c
> > > @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
> > >
> > >  static bool is_pow_of_2(size_t x)
> > >  {
> > > -       return x && (x & (x - 1));
> > > +       return x && !(x & (x - 1));
>
> ugh... *facepalm*
>
> >
> > No idea if anyone cares about the consistency, but in linker.c (same directory)
> > the same static function is defined using == 0 at the end instead of using the
> > not operator.
> >
> > Aside from the consistency issue, personally I find the == 0 version a little
> > bit easier to read and understand because it's a bit less dense (and a "!" next
> > to a "(" is an easy character to overlook).
> >
>
> I agree, even more so, logical not used with arbitrary integer (not a
> pointer or bool) is a mental stumbling block for me, so much so that I
> avoid doing !strcmp(), for example.
>
> But in this case, I'm not sure why I copy/pasted is_pow_of_2() instead
> of moving the one from linker.c into libbpf_internal.h as static
> inline. Let's do that instead?
>
> > >  }
> > >
> > >  static size_t adjust_ringbuf_sz(size_t sz)
> > > --
> > > 2.36.1.255.ge46751e96f-goog
> > >

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-03  4:17 [PATCH] libbpf: Fix is_pow_of_2 Ian Rogers
  2022-06-03  4:31 ` Zvi Effron
@ 2022-06-19 17:12 ` Pavel Machek
  2022-06-21 23:03   ` Zvi Effron
  1 sibling, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2022-06-19 17:12 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel, Yuze Chi

Hi!

> From: Yuze Chi <chiyuze@google.com>
> 
> +++ b/tools/lib/bpf/libbpf.c
> @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
>  
>  static bool is_pow_of_2(size_t x)
>  {
> -	return x && (x & (x - 1));
> +	return x && !(x & (x - 1));
>  }

I'm pretty sure we have this test in macro in includes somewhere... should we use
that instead?
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-19 17:12 ` Pavel Machek
@ 2022-06-21 23:03   ` Zvi Effron
  2022-06-22  0:20     ` Tiezhu Yang
  0 siblings, 1 reply; 7+ messages in thread
From: Zvi Effron @ 2022-06-21 23:03 UTC (permalink / raw)
  To: Pavel Machek
  Cc: Ian Rogers, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel, Yuze Chi

On Sun, Jun 19, 2022 at 12:13 PM Pavel Machek <pavel@ucw.cz> wrote:
>
> Hi!
>
> > From: Yuze Chi <chiyuze@google.com>
> >
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
> >
> > static bool is_pow_of_2(size_t x)
> > {
> > - return x && (x & (x - 1));
> > + return x && !(x & (x - 1));
> > }
>
> I'm pretty sure we have this test in macro in includes somewhere... should we use
> that instead?

I went looking for a macro that provided this check and could not find one. I
did find the inlined static function is_power_of_2 in log2.h, though, that we
could use.

> Pavel
> --
> (english) http://www.livejournal.com/~pavelmachek
> (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] libbpf: Fix is_pow_of_2
  2022-06-21 23:03   ` Zvi Effron
@ 2022-06-22  0:20     ` Tiezhu Yang
  0 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-06-22  0:20 UTC (permalink / raw)
  To: Zvi Effron, Pavel Machek
  Cc: Ian Rogers, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel, Yuze Chi



On 06/22/2022 07:03 AM, Zvi Effron wrote:
> On Sun, Jun 19, 2022 at 12:13 PM Pavel Machek <pavel@ucw.cz> wrote:
>>
>> Hi!
>>
>>> From: Yuze Chi <chiyuze@google.com>
>>>
>>> +++ b/tools/lib/bpf/libbpf.c
>>> @@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
>>>
>>> static bool is_pow_of_2(size_t x)
>>> {
>>> - return x && (x & (x - 1));
>>> + return x && !(x & (x - 1));
>>> }
>>
>> I'm pretty sure we have this test in macro in includes somewhere... should we use
>> that instead?
>
> I went looking for a macro that provided this check and could not find one. I

arch/microblaze/mm/pgtable.c

#define is_power_of_2(x)	((x) != 0 && (((x) & ((x) - 1)) == 0))

> did find the inlined static function is_power_of_2 in log2.h, though, that we
> could use.

Here is a patch, but it seems that this is not worth the extra pain.

https://lore.kernel.org/bpf/8e5291b7-bd89-6fea-bfb7-954cacdb8523@iogearbox.net/

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-06-22  0:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03  4:17 [PATCH] libbpf: Fix is_pow_of_2 Ian Rogers
2022-06-03  4:31 ` Zvi Effron
2022-06-03  5:33   ` Andrii Nakryiko
2022-06-03  5:35     ` Andrii Nakryiko
2022-06-19 17:12 ` Pavel Machek
2022-06-21 23:03   ` Zvi Effron
2022-06-22  0:20     ` Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).