Skip to content

Commit e576718

Browse files
committed
Merge: selftests/bpf: Fix build of test_progs
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-10/-/merge_requests/31 There was a number of reasons why `test_progs` didn't compile in RHEL 10. This PR addresses all of them: - BPF selftests Makefiles did not correctly support passing options via env variables - this is addressed by the patch #1. - RPM build uses some compiler options unsupported by Clang (e.g. `-spec=...`). This is addressed by patch #2. - Not all binaries in BPF selftests were compiled as PIE which caused linker and feature detection issues. This is addressed by patches #3-#5. - BPF selftests do not build with source fortification so we disable it for selftests build in patch #6. - The `urandom_read` binary doesn't pass the `check-rpaths` step. The check is bypassed by patch #7. - BPF selftests need `libxml2-devel` as a dependency. This is added by patch #8. - One test prog skeleton cannot be built in Brew as it needs to read `/proc/kallsyms` which in Brew builders has permissions 000. Until that is changed, we remove the test prog in patch #9. Majority of the patches are either backports of upstream commits or are RHEL-only patches of the specfile. The only exception is the last patch which is a RHEL-only patch in kernel code (BPF selftests Makefile). Hopefully, we will be eventually able to drop it. JIRA: https://issues.redhat.com/browse/RHEL-48593 Signed-off-by: Viktor Malik <vmalik@redhat.com> Signed-off-by: Artem Savkov <asavkov@redhat.com> Approved-by: Derek Barbosa <debarbos@redhat.com> Approved-by: Jerome Marchand <jmarchan@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Jan Stancek <jstancek@redhat.com>
2 parents d138558 + f83db44 commit e576718

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

redhat/kernel.spec.template

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ BuildRequires: clang llvm-devel fuse-devel
791791
%ifarch x86_64 riscv64
792792
BuildRequires: lld
793793
%endif
794-
BuildRequires: libcap-devel libcap-ng-devel rsync libmnl-devel
794+
BuildRequires: libcap-devel libcap-ng-devel rsync libmnl-devel libxml2-devel
795795
BuildRequires: numactl-devel
796796
%endif
797797
BuildConflicts: rhbuildsys(DiskFree) < 500Mb
@@ -3202,7 +3202,7 @@ if [ ! -f include/generated/autoconf.h ]; then
32023202
fi
32033203

32043204
%{log_msg "build samples/bpf"}
3205-
%{make} %{?_smp_mflags} ARCH=$Arch V=1 M=samples/bpf/ VMLINUX_H="${RPM_VMLINUX_H}" || true
3205+
%{make} %{?_smp_mflags} EXTRA_CXXFLAGS="${RPM_OPT_FLAGS}" ARCH=$Arch V=1 M=samples/bpf/ VMLINUX_H="${RPM_VMLINUX_H}" || true
32063206

32073207
# Prevent bpf selftests to build bpftool repeatedly:
32083208
export BPFTOOL=$(pwd)/tools/bpf/bpftool/bpftool
@@ -3217,7 +3217,18 @@ pushd tools/testing/selftests
32173217
%endif
32183218

32193219
%{log_msg "main selftests compile"}
3220-
%{make} %{?_smp_mflags} ARCH=$Arch V=1 TARGETS="bpf cgroup mm net net/forwarding net/mptcp netfilter tc-testing memfd drivers/net/bonding iommu cachestat" SKIP_TARGETS="" $force_targets INSTALL_PATH=%{buildroot}%{_libexecdir}/kselftests VMLINUX_H="${RPM_VMLINUX_H}" install
3220+
3221+
# Some selftests (especially bpf) do not build with source fortification.
3222+
# Since selftests are not shipped, disable source fortification for them.
3223+
%global _fortify_level_bak %{_fortify_level}
3224+
%undefine _fortify_level
3225+
export CFLAGS="%{build_cflags}"
3226+
3227+
%{make} %{?_smp_mflags} EXTRA_CFLAGS="${RPM_OPT_FLAGS}" EXTRA_CXXFLAGS="${RPM_OPT_FLAGS}" EXTRA_LDFLAGS="%{__global_ldflags}" ARCH=$Arch V=1 TARGETS="bpf cgroup mm net net/forwarding net/mptcp netfilter tc-testing memfd drivers/net/bonding iommu cachestat" SKIP_TARGETS="" $force_targets INSTALL_PATH=%{buildroot}%{_libexecdir}/kselftests VMLINUX_H="${RPM_VMLINUX_H}" install
3228+
3229+
# Restore the original level of source fortification
3230+
%define _fortify_level %{_fortify_level_bak}
3231+
export CFLAGS="%{build_cflags}"
32213232

32223233
%ifarch %{klptestarches}
32233234
# kernel livepatching selftest test_modules will build against
@@ -3243,6 +3254,15 @@ for dir in bpf bpf/no_alu32 bpf/progs; do
32433254
done
32443255
%buildroot_save_unstripped "usr/libexec/kselftests/bpf/test_progs"
32453256
%buildroot_save_unstripped "usr/libexec/kselftests/bpf/test_progs-no_alu32"
3257+
3258+
# The urandom_read binary doesn't pass the check-rpaths check and upstream
3259+
# refuses to fix it. So, we save it to buildroot_unstripped and delete it so it
3260+
# will be hidden from check-rpaths and will automatically get restored later.
3261+
%buildroot_save_unstripped "usr/libexec/kselftests/bpf/urandom_read"
3262+
%buildroot_save_unstripped "usr/libexec/kselftests/bpf/no_alu32/urandom_read"
3263+
rm -f %{buildroot}/usr/libexec/kselftests/bpf/urandom_read
3264+
rm -f %{buildroot}/usr/libexec/kselftests/bpf/no_alu32/urandom_read
3265+
32463266
popd
32473267
export -n BPFTOOL
32483268
%{log_msg "end build selftests"}

tools/bpf/bpftool/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ ifeq ($(feature-llvm),1)
147147
# If LLVM is available, use it for JIT disassembly
148148
CFLAGS += -DHAVE_LLVM_SUPPORT
149149
LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
150-
CFLAGS += $(shell $(LLVM_CONFIG) --cflags)
150+
# llvm-config always adds -D_GNU_SOURCE, however, it may already be in CFLAGS
151+
# (e.g. when bpftool build is called from selftests build as selftests
152+
# Makefile includes lib.mk which sets -D_GNU_SOURCE) which would cause
153+
# compilation error due to redefinition. Let's filter it out here.
154+
CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
151155
LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS))
152156
ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static)
153157
LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))

tools/testing/selftests/bpf/Makefile

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c liburandom
274274
$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
275275
$(filter-out -static,$(CFLAGS) $(LDFLAGS)) \
276276
$(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
277+
-Wno-unused-command-line-argument \
277278
-fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
278279
-Wl,--version-script=liburandom_read.map \
279280
-fPIC -shared -o $@
@@ -282,6 +283,7 @@ $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_r
282283
$(call msg,BINARY,,$@)
283284
$(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
284285
$(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
286+
-Wno-unused-command-line-argument \
285287
-lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) \
286288
-fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
287289
-Wl,-rpath=. -o $@
@@ -295,25 +297,33 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
295297
$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
296298
$(call msg,MOD,,$@)
297299
$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
298-
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod
300+
$(Q)$(MAKE) $(submake_extras) -C bpf_testmod \
301+
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
302+
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
299303
$(Q)cp bpf_testmod/bpf_testmod.ko $@
300304

301305
$(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_no_cfi/Makefile bpf_test_no_cfi/*.[ch])
302306
$(call msg,MOD,,$@)
303307
$(Q)$(RM) bpf_test_no_cfi/bpf_test_no_cfi.ko # force re-compilation
304-
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_no_cfi
308+
$(Q)$(MAKE) $(submake_extras) -C bpf_test_no_cfi \
309+
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
310+
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
305311
$(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@
306312

307313
$(OUTPUT)/bpf_test_modorder_x.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_x/Makefile bpf_test_modorder_x/*.[ch])
308314
$(call msg,MOD,,$@)
309315
$(Q)$(RM) bpf_test_modorder_x/bpf_test_modorder_x.ko # force re-compilation
310-
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_modorder_x
316+
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_x \
317+
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
318+
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
311319
$(Q)cp bpf_test_modorder_x/bpf_test_modorder_x.ko $@
312320

313321
$(OUTPUT)/bpf_test_modorder_y.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_y/Makefile bpf_test_modorder_y/*.[ch])
314322
$(call msg,MOD,,$@)
315323
$(Q)$(RM) bpf_test_modorder_y/bpf_test_modorder_y.ko # force re-compilation
316-
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_modorder_y
324+
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_y \
325+
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
326+
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
317327
$(Q)cp bpf_test_modorder_y/bpf_test_modorder_y.ko $@
318328

319329

@@ -333,8 +343,8 @@ $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
333343
BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
334344
BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf/ \
335345
BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) \
336-
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS)' \
337-
EXTRA_LDFLAGS='$(SAN_LDFLAGS)' && \
346+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
347+
EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' && \
338348
cp $(RUNQSLOWER_OUTPUT)runqslower $@
339349

340350
TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL)
@@ -368,7 +378,8 @@ $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
368378
$(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
369379
$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
370380
ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" \
371-
EXTRA_CFLAGS='-g $(OPT_FLAGS)' \
381+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \
382+
EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \
372383
OUTPUT=$(HOST_BUILD_DIR)/bpftool/ \
373384
LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \
374385
LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/ \
@@ -379,7 +390,8 @@ $(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
379390
$(BPFOBJ) | $(BUILD_DIR)/bpftool
380391
$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
381392
ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) \
382-
EXTRA_CFLAGS='-g $(OPT_FLAGS)' \
393+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \
394+
EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \
383395
OUTPUT=$(BUILD_DIR)/bpftool/ \
384396
LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/ \
385397
LIBBPF_DESTDIR=$(SCRATCH_DIR)/ \
@@ -402,16 +414,18 @@ $(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
402414
$(APIDIR)/linux/bpf.h \
403415
| $(BUILD_DIR)/libbpf
404416
$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
405-
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS)' \
406-
EXTRA_LDFLAGS='$(SAN_LDFLAGS)' \
417+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(SAN_CFLAGS) $(EXTRA_CFLAGS)' \
418+
EXTRA_LDFLAGS='$(SAN_LDFLAGS) $(EXTRA_LDFLAGS)' \
407419
DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
408420

409421
ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
410422
$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
411423
$(APIDIR)/linux/bpf.h \
412424
| $(HOST_BUILD_DIR)/libbpf
413425
$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
414-
EXTRA_CFLAGS='-g $(OPT_FLAGS)' ARCH= CROSS_COMPILE= \
426+
ARCH= CROSS_COMPILE= \
427+
EXTRA_CFLAGS='-g $(OPT_FLAGS) $(EXTRA_CFLAGS)' \
428+
EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' \
415429
OUTPUT=$(HOST_BUILD_DIR)/libbpf/ \
416430
CC="$(HOSTCC)" LD="$(HOSTLD)" \
417431
DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
@@ -518,7 +532,7 @@ LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \
518532
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c
519533

520534
# Generate both light skeleton and libbpf skeleton for these
521-
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
535+
LSKELS_EXTRA := test_ksyms_module.c kfunc_call_test.c \
522536
kfunc_call_test_subprog.c
523537
SKEL_BLACKLIST += $$(LSKELS)
524538

tools/testing/selftests/bpf/prog_tests/ksyms_btf.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "test_ksyms_btf.skel.h"
88
#include "test_ksyms_btf_null_check.skel.h"
99
#include "test_ksyms_weak.skel.h"
10-
#include "test_ksyms_weak.lskel.h"
1110
#include "test_ksyms_btf_write_check.skel.h"
1211

1312
static int duration;
@@ -111,33 +110,6 @@ static void test_weak_syms(void)
111110
test_ksyms_weak__destroy(skel);
112111
}
113112

114-
static void test_weak_syms_lskel(void)
115-
{
116-
struct test_ksyms_weak_lskel *skel;
117-
struct test_ksyms_weak_lskel__data *data;
118-
int err;
119-
120-
skel = test_ksyms_weak_lskel__open_and_load();
121-
if (!ASSERT_OK_PTR(skel, "test_ksyms_weak_lskel__open_and_load"))
122-
return;
123-
124-
err = test_ksyms_weak_lskel__attach(skel);
125-
if (!ASSERT_OK(err, "test_ksyms_weak_lskel__attach"))
126-
goto cleanup;
127-
128-
/* trigger tracepoint */
129-
usleep(1);
130-
131-
data = skel->data;
132-
ASSERT_EQ(data->out__existing_typed, 0, "existing typed ksym");
133-
ASSERT_NEQ(data->out__existing_typeless, -1, "existing typeless ksym");
134-
ASSERT_EQ(data->out__non_existent_typeless, 0, "nonexistent typeless ksym");
135-
ASSERT_EQ(data->out__non_existent_typed, 0, "nonexistent typed ksym");
136-
137-
cleanup:
138-
test_ksyms_weak_lskel__destroy(skel);
139-
}
140-
141113
static void test_write_check(bool test_handler1)
142114
{
143115
struct test_ksyms_btf_write_check *skel;
@@ -180,9 +152,6 @@ void test_ksyms_btf(void)
180152
if (test__start_subtest("weak_ksyms"))
181153
test_weak_syms();
182154

183-
if (test__start_subtest("weak_ksyms_lskel"))
184-
test_weak_syms_lskel();
185-
186155
if (test__start_subtest("write_check1"))
187156
test_write_check(true);
188157

0 commit comments

Comments
 (0)