Skip to contentSkip to conversation

libbpf: Fix is_pow_of_2

PATCH Superseded Ian Rogers 5 participants 7 messages Jun 3, 2022 – Jun 22, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 libbpf: Fix is_pow_of_2

From: Yuze Chi <[email protected]>

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

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index 3f4f18684bd3..fd0414ea00df 100644
@@ -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)
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#1 lore raw
Quoted 31 lines · Ian Rogers

From: Yuze Chi <[email protected]>

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

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 3f4f18684bd3..fd0414ea00df 100644

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4959

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -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).

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
 }
 
 static size_t adjust_ringbuf_sz(size_t sz)
--

2.36.1.255.ge46751e96f-goog

#2 lore raw
Quoted 34 lines · Zvi Effron
Quoted 32 lines · Ian Rogers

From: Yuze Chi <[email protected]>

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

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 3f4f18684bd3..fd0414ea00df 100644
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
 
Quoted 4 lines

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4959

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -1
 static bool is_pow_of_2(size_t x)
 {
-       return x && (x & (x - 1));
+       return x && !(x & (x - 1));

ugh... *facepalm*

Quoted 9 lines

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?

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
 }
 
 static size_t adjust_ringbuf_sz(size_t sz)
--

2.36.1.255.ge46751e96f-goog

#3 lore raw
Quoted 23 lines · Andrii Nakryiko
Quoted 21 lines · Zvi Effron
Quoted 19 lines · Ian Rogers

From: Yuze Chi <[email protected]>

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

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

also can you please add Fixes: tag?

Quoted 41 lines
Quoted 10 lines
Quoted 10 lines
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index 3f4f18684bd3..fd0414ea00df 100644
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
 
Quoted 4 lines

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4959

Quoted 4 lines
tools/lib/bpf/libbpf.c +1 -1
 static bool is_pow_of_2(size_t x)
 {
-       return x && (x & (x - 1));
+       return x && !(x & (x - 1));

ugh... *facepalm*

Quoted 9 lines

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?

Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
 }
 
 static size_t adjust_ringbuf_sz(size_t sz)
--

2.36.1.255.ge46751e96f-goog

#4 lore raw
In reply to Ian Rogers

Hi!

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4960

Quoted 10 lines

From: Yuze Chi <[email protected]>

+++ b/tools/lib/bpf/libbpf.c

tools/lib/bpf/libbpf.c +1 -1
@@ -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

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#5 lore raw
Quoted 8 lines · Pavel Machek

Hi!

Quoted 5 lines

From: Yuze Chi <[email protected]>

+++ b/tools/lib/bpf/libbpf.c

(quoted diff) +0 -0
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
 
Quoted 8 lines

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4960

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
 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.

Quoted 4 lines

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

#6 lore raw
Quoted 18 lines · Zvi Effron
Quoted 8 lines · Pavel Machek

Hi!

Quoted 5 lines

From: Yuze Chi <[email protected]>

+++ b/tools/lib/bpf/libbpf.c

(quoted diff) +0 -0
@@ -4956,7 +4956,7 @@ static void bpf_map__destroy(struct bpf_map *map);
 
Quoted 8 lines

tools/lib/bpf/libbpf.c static void bpf_map__destroy(struct bpf_map *map); · line 4960

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
 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?

Quoted 1 line

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))

Quoted 2 lines

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/[email protected]/

Thanks,
Tiezhu