diff mbox series

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

Message ID 1655713404-7133-1-git-send-email-yangtiezhu@loongson.cn (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next,v2] libbpf: Include linux/log2.h to use is_power_of_2() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 5 maintainers not CCed: songliubraving@fb.com yhs@fb.com john.fastabend@gmail.com kafai@fb.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Tiezhu Yang June 20, 2022, 8:23 a.m. UTC
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@ucw.cz>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 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(-)

Comments

Daniel Borkmann June 20, 2022, 12:39 p.m. UTC | #1
On 6/20/22 10:23 AM, Tiezhu Yang wrote:
> 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@ucw.cz>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>   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;
> -}

I don't think this is worth the extra pain and I would just leave above as-is, because then
we would also need a `LGPL-2.1 OR BSD-2-Clause` compatible reimplementation of linux/log2.h
header inside the standalone repo https://github.com/libbpf/libbpf/tree/master/include/linux
in order to build libbpf there once this get imported to it.

>   #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;
>
diff mbox series

Patch

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;