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

perf bpf: Fix build with libbpf 0.7.0 by checking if bpf_program__set_insns() is available

During the transition to libbpf 1.0 some functions that perf used were
deprecated and finally removed from libbpf, so bpf_program__set_insns()
was introduced for perf to continue to use its bpf loader.

But when build with LIBBPF_DYNAMIC=1 we now need to check if that
function is available so that perf can build with older libbpf versions,
even if the end result is emitting a warning to the user that the use
of the perf BPF loader requires a newer libbpf, since bpf_program__set_insns()
touches libbpf objects internal state.

This affects only 'perf trace' when using bpf C code or pre-compiled
bytecode as an event.

Noticed on RHEL9, that has libbpf 0.7.0, where bpf_program__set_insns()
isn't available.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+36
+1
tools/build/Makefile.feature
··· 103 103 libbpf-bpf_prog_load \ 104 104 libbpf-bpf_object__next_program \ 105 105 libbpf-bpf_object__next_map \ 106 + libbpf-bpf_program__set_insns \ 106 107 libbpf-bpf_create_map \ 107 108 libpfm4 \ 108 109 libdebuginfod \
+4
tools/build/feature/Makefile
··· 63 63 test-libbpf-bpf_map_create.bin \ 64 64 test-libbpf-bpf_object__next_program.bin \ 65 65 test-libbpf-bpf_object__next_map.bin \ 66 + test-libbpf-bpf_program__set_insns.bin \ 66 67 test-libbpf-btf__raw_data.bin \ 67 68 test-get_cpuid.bin \ 68 69 test-sdt.bin \ ··· 315 314 $(BUILD) -lbpf 316 315 317 316 $(OUTPUT)test-libbpf-bpf_object__next_map.bin: 317 + $(BUILD) -lbpf 318 + 319 + $(OUTPUT)test-libbpf-bpf_program__set_insns.bin: 318 320 $(BUILD) -lbpf 319 321 320 322 $(OUTPUT)test-libbpf-btf__raw_data.bin:
+8
tools/build/feature/test-libbpf-bpf_program__set_insns.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <bpf/libbpf.h> 3 + 4 + int main(void) 5 + { 6 + bpf_program__set_insns(NULL /* prog */, NULL /* new_insns */, 0 /* new_insn_cnt */); 7 + return 0; 8 + }
+5
tools/perf/Makefile.config
··· 588 588 ifeq ($(feature-libbpf-bpf_object__next_map), 1) 589 589 CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_MAP 590 590 endif 591 + $(call feature_check,libbpf-bpf_program__set_insns) 592 + ifeq ($(feature-libbpf-bpf_program__set_insns), 1) 593 + CFLAGS += -DHAVE_LIBBPF_BPF_PROGRAM__SET_INSNS 594 + endif 591 595 $(call feature_check,libbpf-btf__raw_data) 592 596 ifeq ($(feature-libbpf-btf__raw_data), 1) 593 597 CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA ··· 608 604 CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD 609 605 CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM 610 606 CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_MAP 607 + CFLAGS += -DHAVE_LIBBPF_BPF_PROGRAM__SET_INSNS 611 608 CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA 612 609 CFLAGS += -DHAVE_LIBBPF_BPF_MAP_CREATE 613 610 endif
+18
tools/perf/util/bpf-loader.c
··· 36 36 37 37 #include <internal/xyarray.h> 38 38 39 + #ifndef HAVE_LIBBPF_BPF_PROGRAM__SET_INSNS 40 + int bpf_program__set_insns(struct bpf_program *prog __maybe_unused, 41 + struct bpf_insn *new_insns __maybe_unused, size_t new_insn_cnt __maybe_unused) 42 + { 43 + pr_err("%s: not support, update libbpf\n", __func__); 44 + return -ENOTSUP; 45 + } 46 + 47 + int libbpf_register_prog_handler(const char *sec __maybe_unused, 48 + enum bpf_prog_type prog_type __maybe_unused, 49 + enum bpf_attach_type exp_attach_type __maybe_unused, 50 + const struct libbpf_prog_handler_opts *opts __maybe_unused) 51 + { 52 + pr_err("%s: not support, update libbpf\n", __func__); 53 + return -ENOTSUP; 54 + } 55 + #endif 56 + 39 57 /* temporarily disable libbpf deprecation warnings */ 40 58 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 41 59