Skip to contentSkip to conversation

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

PATCH In review Arnaldo Carvalho de Melo 2 participants 3 messages Mar 21, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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.")
Cc Alexei Starovoitov <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Ian Rogers <[email protected]>
---
 tools/lib/bpf/libbpf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +2 -1
index efab29b8935bd9f7..af0b0e24978558f8 100644
@@ -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);
Signature · 1 line
2.44.0
#1 lore raw
Quoted 29 lines · Arnaldo Carvalho de Melo

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.")
Cc Alexei Starovoitov <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Ian Rogers <[email protected]>
Quoted 4 lines
---
 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/[email protected]/

Quoted 23 lines
tools/lib/bpf/libbpf.c +2 -2
index efab29b8935bd9f7..af0b0e24978558f8 100644
@@ -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

#2 lore raw
Quoted 9 lines · Andrii Nakryiko

On Thu, Mar 21, 2024 at 8:16 AM Arnaldo Carvalho de Melo

Quoted 7 lines

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.

Quoted 6 lines
Quoted 2 lines
Fixes 79ff13e99169ddb0 ("libbpf: Add support for bpf_arena.")

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

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

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

- Arnaldo