[PATCH bpf-next v2] libbpf: Include linux/log2.h to use is_power_of_2()

From: Tiezhu Yang
Date: Mon Jun 20 2022 - 04:23:45 EST


is_power_of_2() is already defined in tools/include/linux/log2.h [1],
so no need to define it again in tools/lib/bpf/libbpf_internal.h, so
just include linux/log2.h directly.

[1] https://lore.kernel.org/bpf/20220619171248.GC3362@bug/

Suggested-by: Pavel Machek <pavel@xxxxxx>
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 2 +-
tools/lib/bpf/libbpf_internal.h | 6 +-----
tools/lib/bpf/linker.c | 2 +-
3 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 49e359c..5252e51 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5131,7 +5131,7 @@ static size_t adjust_ringbuf_sz(size_t sz)
* a power-of-2 multiple of kernel's page size. If user diligently
* satisified these conditions, pass the size through.
*/
- if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
+ if ((sz % page_sz) == 0 && is_power_of_2(sz / page_sz))
return sz;

/* Otherwise find closest (page_sz * power_of_2) product bigger than
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index a1ad145..021946a 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -13,6 +13,7 @@
#include <limits.h>
#include <errno.h>
#include <linux/err.h>
+#include <linux/log2.h>
#include <fcntl.h>
#include <unistd.h>
#include "libbpf_legacy.h"
@@ -582,9 +583,4 @@ struct bpf_link * usdt_manager_attach_usdt(struct usdt_manager *man,
const char *usdt_provider, const char *usdt_name,
__u64 usdt_cookie);

-static inline bool is_pow_of_2(size_t x)
-{
- return x && (x & (x - 1)) == 0;
-}
-
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 4ac02c2..b2edb5f 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -719,7 +719,7 @@ static int linker_sanity_check_elf(struct src_obj *obj)
return -EINVAL;
}

- if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
+ if (sec->shdr->sh_addralign && !is_power_of_2(sec->shdr->sh_addralign))
return -EINVAL;
if (sec->shdr->sh_addralign != sec->data->d_align)
return -EINVAL;
--
2.1.0