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_prog_load() to handle older libbpf versions

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

Committer testing:

$ rpm -q libbpf-devel
libbpf-devel-0.4.0-2.fc35.x86_64
$ make -C tools/perf LIBBPF_DYNAMIC=1 O=/tmp/build/perf install-bin
$ cat /tmp/build/perf/feature/test-libbpf-bpf_prog_load.make.output
test-libbpf-bpf_prog_load.c: In function ‘main’:
test-libbpf-bpf_prog_load.c:6:16: error: implicit declaration of function ‘bpf_prog_load’ [-Werror=implicit-function-declaration]
6 | return bpf_prog_load(0 /* prog_type */, NULL /* prog_name */,
| ^~~~~~~~~~~~~
cc1: all warnings being treated as errors
$

$ objdump -dS /tmp/build/perf/perf | grep '<bpf_prog_load>:' -A20
00000000005b2d70 <bpf_prog_load>:
{
5b2d70: 55 push %rbp
5b2d71: 48 89 ce mov %rcx,%rsi
5b2d74: 4c 89 c8 mov %r9,%rax
5b2d77: 49 89 d2 mov %rdx,%r10
5b2d7a: 4c 89 c2 mov %r8,%rdx
5b2d7d: 48 89 e5 mov %rsp,%rbp
5b2d80: 48 83 ec 18 sub $0x18,%rsp
5b2d84: 64 48 8b 0c 25 28 00 mov %fs:0x28,%rcx
5b2d8b: 00 00
5b2d8d: 48 89 4d f8 mov %rcx,-0x8(%rbp)
5b2d91: 31 c9 xor %ecx,%ecx
return bpf_load_program(prog_type, insns, insn_cnt, license,
5b2d93: 41 8b 49 5c mov 0x5c(%r9),%ecx
5b2d97: 51 push %rcx
5b2d98: 4d 8b 49 60 mov 0x60(%r9),%r9
5b2d9c: 4c 89 d1 mov %r10,%rcx
5b2d9f: 44 8b 40 1c mov 0x1c(%rax),%r8d
5b2da3: e8 f8 aa e5 ff call 40d8a0 <bpf_load_program@plt>
}
$

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ilya Leoshkevich <iii@linux.ibm.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/YozLKby7ITEtchC9@krava
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
5c83eff3 73534617

+26 -5
+1
tools/build/Makefile.feature
··· 99 99 clang \ 100 100 libbpf \ 101 101 libbpf-btf__load_from_kernel_by_id \ 102 + libbpf-bpf_prog_load \ 102 103 libpfm4 \ 103 104 libdebuginfod \ 104 105 clang-bpf-co-re
+4
tools/build/feature/Makefile
··· 58 58 test-bpf.bin \ 59 59 test-libbpf.bin \ 60 60 test-libbpf-btf__load_from_kernel_by_id.bin \ 61 + test-libbpf-bpf_prog_load.bin \ 61 62 test-get_cpuid.bin \ 62 63 test-sdt.bin \ 63 64 test-cxx.bin \ ··· 290 289 $(BUILD) -lbpf 291 290 292 291 $(OUTPUT)test-libbpf-btf__load_from_kernel_by_id.bin: 292 + $(BUILD) -lbpf 293 + 294 + $(OUTPUT)test-libbpf-bpf_prog_load.bin: 293 295 $(BUILD) -lbpf 294 296 295 297 $(OUTPUT)test-sdt.bin:
+9
tools/build/feature/test-libbpf-bpf_prog_load.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <bpf/bpf.h> 3 + 4 + int main(void) 5 + { 6 + return bpf_prog_load(0 /* prog_type */, NULL /* prog_name */, 7 + NULL /* license */, NULL /* insns */, 8 + 0 /* insn_cnt */, NULL /* opts */); 9 + }
+5
tools/perf/Makefile.config
··· 573 573 ifeq ($(feature-libbpf-btf__load_from_kernel_by_id), 1) 574 574 CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID 575 575 endif 576 + $(call feature_check,libbpf-bpf_prog_load) 577 + ifeq ($(feature-libbpf-bpf_prog_load), 1) 578 + CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD 579 + endif 576 580 else 577 581 dummy := $(error Error: No libbpf devel library found, please install libbpf-devel); 578 582 endif 579 583 else 580 584 CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID 585 + CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD 581 586 endif 582 587 endif 583 588
+7 -5
tools/perf/util/bpf-event.c
··· 35 35 } 36 36 #endif 37 37 38 - int __weak bpf_prog_load(enum bpf_prog_type prog_type, 39 - const char *prog_name __maybe_unused, 40 - const char *license, 41 - const struct bpf_insn *insns, size_t insn_cnt, 42 - const struct bpf_prog_load_opts *opts) 38 + #ifndef HAVE_LIBBPF_BPF_PROG_LOAD 39 + int bpf_prog_load(enum bpf_prog_type prog_type, 40 + const char *prog_name __maybe_unused, 41 + const char *license, 42 + const struct bpf_insn *insns, size_t insn_cnt, 43 + const struct bpf_prog_load_opts *opts) 43 44 { 44 45 #pragma GCC diagnostic push 45 46 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" ··· 48 47 opts->kern_version, opts->log_buf, opts->log_size); 49 48 #pragma GCC diagnostic pop 50 49 } 50 + #endif 51 51 52 52 struct bpf_program * __weak 53 53 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)