Skip to content

Commit 0ae065a

Browse files
committed
perf build: Fix check for btf__load_from_kernel_by_id() in libbpf
Avi Kivity reported a problem where the __weak btf__load_from_kernel_by_id() in tools/perf/util/bpf-event.c was being used and it called btf__get_from_id() in tools/lib/bpf/btf.c that in turn called back to btf__load_from_kernel_by_id(), resulting in an endless loop. Fix this by adding a feature test to check if btf__load_from_kernel_by_id() is available when building perf with LIBBPF_DYNAMIC=1, and if not then provide the fallback to the old btf__get_from_id(), that doesn't call back to btf__load_from_kernel_by_id() since at that time it didn't exist at all. Tested on Fedora 35 where we have libbpf-devel 0.4.0 with LIBBPF_DYNAMIC where we don't have btf__load_from_kernel_by_id() and thus its feature test fail, not defining HAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID: $ cat /tmp/build/perf-urgent/feature/test-libbpf-btf__load_from_kernel_by_id.make.output test-libbpf-btf__load_from_kernel_by_id.c: In function ‘main’: test-libbpf-btf__load_from_kernel_by_id.c:6:16: error: implicit declaration of function ‘btf__load_from_kernel_by_id’ [-Werror=implicit-function-declaration] 6 | return btf__load_from_kernel_by_id(20151128, NULL); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors $ $ nm /tmp/build/perf-urgent/perf | grep btf__load_from_kernel_by_id 00000000005ba180 T btf__load_from_kernel_by_id $ $ objdump --disassemble=btf__load_from_kernel_by_id -S /tmp/build/perf-urgent/perf /tmp/build/perf-urgent/perf: file format elf64-x86-64 <SNIP> 00000000005ba180 <btf__load_from_kernel_by_id>: #include "record.h" #include "util/synthetic-events.h" #ifndef HAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID struct btf *btf__load_from_kernel_by_id(__u32 id) { 5ba180: 55 push %rbp 5ba181: 48 89 e5 mov %rsp,%rbp 5ba184: 48 83 ec 10 sub $0x10,%rsp 5ba188: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 5ba18f: 00 00 5ba191: 48 89 45 f8 mov %rax,-0x8(%rbp) 5ba195: 31 c0 xor %eax,%eax struct btf *btf; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" int err = btf__get_from_id(id, &btf); 5ba197: 48 8d 75 f0 lea -0x10(%rbp),%rsi 5ba19b: e8 a0 57 e5 ff call 40f940 <btf__get_from_id@plt> 5ba1a0: 89 c2 mov %eax,%edx #pragma GCC diagnostic pop return err ? ERR_PTR(err) : btf; 5ba1a2: 48 98 cltq 5ba1a4: 85 d2 test %edx,%edx 5ba1a6: 48 0f 44 45 f0 cmove -0x10(%rbp),%rax } <SNIP> Fixes: 218e7b7 ("perf bpf: Provide a weak btf__load_from_kernel_by_id() for older libbpf versions") Reported-by: Avi Kivity <[email protected]> Link: https://lore.kernel.org/linux-perf-users/[email protected] Cc: Adrian Hunter <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/linux-perf-users/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 3d7285a commit 0ae065a

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

tools/build/Makefile.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ FEATURE_TESTS_EXTRA := \
9898
llvm-version \
9999
clang \
100100
libbpf \
101+
libbpf-btf__load_from_kernel_by_id \
101102
libpfm4 \
102103
libdebuginfod \
103104
clang-bpf-co-re

tools/build/feature/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ FILES= \
5757
test-lzma.bin \
5858
test-bpf.bin \
5959
test-libbpf.bin \
60+
test-libbpf-btf__load_from_kernel_by_id.bin \
6061
test-get_cpuid.bin \
6162
test-sdt.bin \
6263
test-cxx.bin \
@@ -287,6 +288,9 @@ $(OUTPUT)test-bpf.bin:
287288
$(OUTPUT)test-libbpf.bin:
288289
$(BUILD) -lbpf
289290

291+
$(OUTPUT)test-libbpf-btf__load_from_kernel_by_id.bin:
292+
$(BUILD) -lbpf
293+
290294
$(OUTPUT)test-sdt.bin:
291295
$(BUILD)
292296

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <bpf/libbpf.h>
3+
4+
int main(void)
5+
{
6+
return btf__load_from_kernel_by_id(20151128, NULL);
7+
}

tools/perf/Makefile.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,16 @@ ifndef NO_LIBELF
553553
ifeq ($(feature-libbpf), 1)
554554
EXTLIBS += -lbpf
555555
$(call detected,CONFIG_LIBBPF_DYNAMIC)
556+
557+
$(call feature_check,libbpf-btf__load_from_kernel_by_id)
558+
ifeq ($(feature-libbpf-btf__load_from_kernel_by_id), 1)
559+
CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID
560+
endif
556561
else
557562
dummy := $(error Error: No libbpf devel library found, please install libbpf-devel);
558563
endif
564+
else
565+
CFLAGS += -DHAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID
559566
endif
560567
endif
561568

tools/perf/util/bpf-event.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#include "record.h"
2323
#include "util/synthetic-events.h"
2424

25-
struct btf * __weak btf__load_from_kernel_by_id(__u32 id)
25+
#ifndef HAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID
26+
struct btf *btf__load_from_kernel_by_id(__u32 id)
2627
{
2728
struct btf *btf;
2829
#pragma GCC diagnostic push
@@ -32,6 +33,7 @@ struct btf * __weak btf__load_from_kernel_by_id(__u32 id)
3233

3334
return err ? ERR_PTR(err) : btf;
3435
}
36+
#endif
3537

3638
int __weak bpf_prog_load(enum bpf_prog_type prog_type,
3739
const char *prog_name __maybe_unused,

0 commit comments

Comments
 (0)