libbpf: Fix is_pow_of_2
Subsystems (4)
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
@@ -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
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
0with the not this is:
x && !(x & (x - 1))
4096 && !(4096 & (4096 - 1))
4096 && !(4096 & 4095)
4096 && !0
4096 && 1
1Reported-by Yuze Chi <[email protected]>Signed-off-by Yuze Chi <[email protected]>Signed-off-by Ian Rogers <[email protected]>--- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/libbpf.c +0
-0
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
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
0with the not this is:
x && !(x & (x - 1))
4096 && !(4096 & (4096 - 1))
4096 && !(4096 & 4095)
4096 && !0
4096 && 1
1Reported-by Yuze Chi <[email protected]>Signed-off-by Yuze Chi <[email protected]>Signed-off-by Ian Rogers <[email protected]>--- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/libbpf.c +0
-0@@ -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
-1static 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
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
0with 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
Reported-by Yuze Chi <[email protected]>Signed-off-by Yuze Chi <[email protected]>Signed-off-by Ian Rogers <[email protected]>
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@@ -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
-1static 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 fromlinker.cintolibbpf_internal.has 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
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.ctools/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
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
-1static 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
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
-1static 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_2inlog2.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