diff mbox series

[1/1] libbpf: Use (uintptr_t) for a u64 cast to a void pointer to fix build on 32-bit arches

Message ID ZfxKeSF5e6mXpnrs@x1 (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [1/1] libbpf: Use (uintptr_t) for a u64 cast to a void pointer to fix build on 32-bit arches | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Arnaldo Carvalho de Melo March 21, 2024, 3:16 p.m. UTC
Noticed on several perf container builds:

  perfbuilder@number:~$ grep -B1 -A2 "cast to pointer"  dm.log/*\:*
  dm.log/ubuntu:18.04-x-arm-libbpf.c: In function 'bpf_object__create_maps':
  dm.log/ubuntu:18.04-x-arm:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  dm.log/ubuntu:18.04-x-arm-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
  dm.log/ubuntu:18.04-x-arm-                        ^
  --
  dm.log/ubuntu:18.04-x-powerpc-libbpf.c: In function 'bpf_object__create_maps':
  dm.log/ubuntu:18.04-x-powerpc:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  dm.log/ubuntu:18.04-x-powerpc-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
  dm.log/ubuntu:18.04-x-powerpc-                        ^
  --
  dm.log/ubuntu:18.04-x-sh4-libbpf.c: In function 'bpf_object__create_maps':
  dm.log/ubuntu:18.04-x-sh4:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  dm.log/ubuntu:18.04-x-sh4-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
  dm.log/ubuntu:18.04-x-sh4-                        ^
  perfbuilder@number:~$

I think Namhyung mentioned as well on a chat conversation about noticing
this on a 32-bit build on Debian.

Fixes: 79ff13e99169ddb0 ("libbpf: Add support for bpf_arena.")
Reported-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/lib/bpf/libbpf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Andrii Nakryiko March 21, 2024, 3:59 p.m. UTC | #1
On Thu, Mar 21, 2024 at 8:16 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Noticed on several perf container builds:
>
>   perfbuilder@number:~$ grep -B1 -A2 "cast to pointer"  dm.log/*\:*
>   dm.log/ubuntu:18.04-x-arm-libbpf.c: In function 'bpf_object__create_maps':
>   dm.log/ubuntu:18.04-x-arm:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
>   dm.log/ubuntu:18.04-x-arm-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
>   dm.log/ubuntu:18.04-x-arm-                        ^
>   --
>   dm.log/ubuntu:18.04-x-powerpc-libbpf.c: In function 'bpf_object__create_maps':
>   dm.log/ubuntu:18.04-x-powerpc:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
>   dm.log/ubuntu:18.04-x-powerpc-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
>   dm.log/ubuntu:18.04-x-powerpc-                        ^
>   --
>   dm.log/ubuntu:18.04-x-sh4-libbpf.c: In function 'bpf_object__create_maps':
>   dm.log/ubuntu:18.04-x-sh4:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
>   dm.log/ubuntu:18.04-x-sh4-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
>   dm.log/ubuntu:18.04-x-sh4-                        ^
>   perfbuilder@number:~$
>
> I think Namhyung mentioned as well on a chat conversation about noticing
> this on a 32-bit build on Debian.
>
> Fixes: 79ff13e99169ddb0 ("libbpf: Add support for bpf_arena.")
> Reported-by: Namhyung Kim <namhyung@kernel.org>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Cc: Ian Rogers <irogers@google.com>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/lib/bpf/libbpf.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>

We've already landed a similar fix in the bpf tree, see [0].

  [0] https://patchwork.kernel.org/project/netdevbpf/patch/20240319215143.1279312-1-andrii@kernel.org/


> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index efab29b8935bd9f7..af0b0e24978558f8 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -14,6 +14,7 @@
>  #define _GNU_SOURCE
>  #endif
>  #include <stdlib.h>
> +#include <stdint.h>
>  #include <stdio.h>
>  #include <stdarg.h>
>  #include <libgen.h>
> @@ -5352,7 +5353,7 @@ bpf_object__create_maps(struct bpf_object *obj)
>                                         goto err_out;
>                         }
>                         if (map->def.type == BPF_MAP_TYPE_ARENA) {
> -                               map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
> +                               map->mmaped = mmap((void *)(uintptr_t)map->map_extra, bpf_map_mmap_sz(map),
>                                                    PROT_READ | PROT_WRITE,
>                                                    map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
>                                                    map->fd, 0);
> --
> 2.44.0
Arnaldo Carvalho de Melo March 21, 2024, 4:06 p.m. UTC | #2
On Thu, Mar 21, 2024 at 08:59:57AM -0700, Andrii Nakryiko wrote:
> On Thu, Mar 21, 2024 at 8:16 AM Arnaldo Carvalho de Melo
> >   dm.log/ubuntu:18.04-x-sh4:libbpf.c:5355:24: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
> >   dm.log/ubuntu:18.04-x-sh4-     map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
> >   dm.log/ubuntu:18.04-x-sh4-                        ^
> >   perfbuilder@number:~$

> > I think Namhyung mentioned as well on a chat conversation about noticing
> > this on a 32-bit build on Debian.

> > Fixes: 79ff13e99169ddb0 ("libbpf: Add support for bpf_arena.")
> > Reported-by: Namhyung Kim <namhyung@kernel.org>
 
> We've already landed a similar fix in the bpf tree, see [0].
> 
>   [0] https://patchwork.kernel.org/project/netdevbpf/patch/20240319215143.1279312-1-andrii@kernel.org/

Great, uintptr_t looks cleaner tho, but fits the bill.

- Arnaldo
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index efab29b8935bd9f7..af0b0e24978558f8 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -14,6 +14,7 @@ 
 #define _GNU_SOURCE
 #endif
 #include <stdlib.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <libgen.h>
@@ -5352,7 +5353,7 @@  bpf_object__create_maps(struct bpf_object *obj)
 					goto err_out;
 			}
 			if (map->def.type == BPF_MAP_TYPE_ARENA) {
-				map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
+				map->mmaped = mmap((void *)(uintptr_t)map->map_extra, bpf_map_mmap_sz(map),
 						   PROT_READ | PROT_WRITE,
 						   map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
 						   map->fd, 0);