Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf build: Stop using __weak bpf_map_create() to handle older libbpf versions

By adding a feature test for bpf_map_create() and providing a fallback if
it isn't present in older versions of libbpf.

This also fixes the build with torvalds/master at this point:

$ git log --oneline -5 torvalds/master
babf0bb978e3c9fc (torvalds/master) Merge tag 'xfs-5.19-for-linus' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
e375780b631a5fc2 Merge tag 'fsnotify_for_v5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
8b728edc5be16179 Merge tag 'fs_for_v5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
3f306ea2e18568f6 Merge tag 'dma-mapping-5.19-2022-05-25' of git://git.infradead.org/users/hch/dma-mapping
fbe86daca0ba878b Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
$

Coping with:

$ git log --oneline -2 d16495a982324f75
d16495a982324f75 libbpf: remove bpf_create_map*() APIs
e2371b1632b1c61c libbpf: start 1.0 development cycle
$

As the __weak function fails to build as it calls the now removed
bpf_create_map() API.

Testing:

$ rpm -q libbpf-devel
libbpf-devel-0.4.0-2.fc35.x86_64
$
$ make -C tools/perf BUILD_BPF_SKEL=1 LIBBPF_DYNAMIC=1 O=/tmp/build/perf install-bin
$ cat /tmp/build/perf/feature/test-libbpf-bpf_map_create.make.output
test-libbpf-bpf_map_create.c: In function ‘main’:
test-libbpf-bpf_map_create.c:6:16: error: implicit declaration of function ‘bpf_map_create’; did you mean ‘bpf_map_freeze’? [-Werror=implicit-function-declaration]
6 | return bpf_map_create(0 /* map_type */, NULL /* map_name */, 0, /* key_size */,
| ^~~~~~~~~~~~~~
| bpf_map_freeze
test-libbpf-bpf_map_create.c:6:87: error: expected expression before ‘,’ token
6 | return bpf_map_create(0 /* map_type */, NULL /* map_name */, 0, /* key_size */,
| ^
cc1: all warnings being treated as errors
$
$ objdump -dS /tmp/build/perf/perf | grep '<bpf_map_create>:' -A20
000000000058b290 <bpf_map_create>:
{
58b290: 55 push %rbp
58b291: 48 89 e5 mov %rsp,%rbp
58b294: 48 83 ec 10 sub $0x10,%rsp
58b298: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
58b29f: 00 00
58b2a1: 48 89 45 f8 mov %rax,-0x8(%rbp)
58b2a5: 31 c0 xor %eax,%eax
return bpf_create_map(map_type, key_size, value_size, max_entries, 0);
58b2a7: 48 8b 45 f8 mov -0x8(%rbp),%rax
58b2ab: 64 48 2b 04 25 28 00 sub %fs:0x28,%rax
58b2b2: 00 00
58b2b4: 75 10 jne 58b2c6 <bpf_map_create+0x36>
}
58b2b6: c9 leave
58b2b7: 89 d6 mov %edx,%esi
58b2b9: 89 ca mov %ecx,%edx
58b2bb: 44 89 c1 mov %r8d,%ecx
return bpf_create_map(map_type, key_size, value_size, max_entries, 0);
58b2be: 45 31 c0 xor %r8d,%r8d
$

Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Link: http://lore.kernel.org/linux-perf-users/Yo+XvQNKL4K5khl2@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+23 -1
+1
tools/build/Makefile.feature
··· 102 102 libbpf-bpf_prog_load \ 103 103 libbpf-bpf_object__next_program \ 104 104 libbpf-bpf_object__next_map \ 105 + libbpf-bpf_create_map \ 105 106 libpfm4 \ 106 107 libdebuginfod \ 107 108 clang-bpf-co-re
+4
tools/build/feature/Makefile
··· 59 59 test-libbpf.bin \ 60 60 test-libbpf-btf__load_from_kernel_by_id.bin \ 61 61 test-libbpf-bpf_prog_load.bin \ 62 + test-libbpf-bpf_map_create.bin \ 62 63 test-libbpf-bpf_object__next_program.bin \ 63 64 test-libbpf-bpf_object__next_map.bin \ 64 65 test-libbpf-btf__raw_data.bin \ ··· 297 296 $(BUILD) -lbpf 298 297 299 298 $(OUTPUT)test-libbpf-bpf_prog_load.bin: 299 + $(BUILD) -lbpf 300 + 301 + $(OUTPUT)test-libbpf-bpf_map_create.bin: 300 302 $(BUILD) -lbpf 301 303 302 304 $(OUTPUT)test-libbpf-bpf_object__next_program.bin:
+8
tools/build/feature/test-libbpf-bpf_map_create.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <bpf/bpf.h> 3 + 4 + int main(void) 5 + { 6 + return bpf_map_create(0 /* map_type */, NULL /* map_name */, 0, /* key_size */, 7 + 0 /* value_size */, 0 /* max_entries */, NULL /* opts */); 8 + }
+5
tools/perf/Makefile.config
··· 589 589 ifeq ($(feature-libbpf-btf__raw_data), 1) 590 590 CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA 591 591 endif 592 + $(call feature_check,libbpf-bpf_map_create) 593 + ifeq ($(feature-libbpf-bpf_map_create), 1) 594 + CFLAGS += -DHAVE_LIBBPF_BPF_MAP_CREATE 595 + endif 592 596 else 593 597 dummy := $(error Error: No libbpf devel library found, please install libbpf-devel); 594 598 endif ··· 602 598 CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM 603 599 CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_MAP 604 600 CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA 601 + CFLAGS += -DHAVE_LIBBPF_BPF_MAP_CREATE 605 602 endif 606 603 endif 607 604
+5 -1
tools/perf/util/bpf_counter.c
··· 312 312 (map_info.value_size == sizeof(struct perf_event_attr_map_entry)); 313 313 } 314 314 315 - int __weak 315 + #ifndef HAVE_LIBBPF_BPF_MAP_CREATE 316 + LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size, 317 + int value_size, int max_entries, __u32 map_flags); 318 + int 316 319 bpf_map_create(enum bpf_map_type map_type, 317 320 const char *map_name __maybe_unused, 318 321 __u32 key_size, ··· 328 325 return bpf_create_map(map_type, key_size, value_size, max_entries, 0); 329 326 #pragma GCC diagnostic pop 330 327 } 328 + #endif 331 329 332 330 static int bperf_lock_attr_map(struct target *target) 333 331 {