Skip to content

Commit f1bdebb

Browse files
committed
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 <[email protected]>
1 parent 409fb6b commit f1bdebb

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

tools/build/Makefile.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ FEATURE_TESTS_EXTRA := \
103103
libbpf-bpf_prog_load \
104104
libbpf-bpf_object__next_program \
105105
libbpf-bpf_object__next_map \
106+
libbpf-bpf_program__set_insns \
106107
libbpf-bpf_create_map \
107108
libpfm4 \
108109
libdebuginfod \

tools/build/feature/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ FILES= \
6363
test-libbpf-bpf_map_create.bin \
6464
test-libbpf-bpf_object__next_program.bin \
6565
test-libbpf-bpf_object__next_map.bin \
66+
test-libbpf-bpf_program__set_insns.bin \
6667
test-libbpf-btf__raw_data.bin \
6768
test-get_cpuid.bin \
6869
test-sdt.bin \
@@ -316,6 +317,9 @@ $(OUTPUT)test-libbpf-bpf_object__next_program.bin:
316317
$(OUTPUT)test-libbpf-bpf_object__next_map.bin:
317318
$(BUILD) -lbpf
318319

320+
$(OUTPUT)test-libbpf-bpf_program__set_insns.bin:
321+
$(BUILD) -lbpf
322+
319323
$(OUTPUT)test-libbpf-btf__raw_data.bin:
320324
$(BUILD) -lbpf
321325

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

tools/perf/Makefile.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ ifndef NO_LIBELF
588588
ifeq ($(feature-libbpf-bpf_object__next_map), 1)
589589
CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_MAP
590590
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
591595
$(call feature_check,libbpf-btf__raw_data)
592596
ifeq ($(feature-libbpf-btf__raw_data), 1)
593597
CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA
@@ -604,6 +608,7 @@ ifndef NO_LIBELF
604608
CFLAGS += -DHAVE_LIBBPF_BPF_PROG_LOAD
605609
CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM
606610
CFLAGS += -DHAVE_LIBBPF_BPF_OBJECT__NEXT_MAP
611+
CFLAGS += -DHAVE_LIBBPF_BPF_PROGRAM__SET_INSNS
607612
CFLAGS += -DHAVE_LIBBPF_BTF__RAW_DATA
608613
CFLAGS += -DHAVE_LIBBPF_BPF_MAP_CREATE
609614
endif

tools/perf/util/bpf-loader.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@
3636

3737
#include <internal/xyarray.h>
3838

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+
3957
/* temporarily disable libbpf deprecation warnings */
4058
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4159

0 commit comments

Comments
 (0)