Skip to content

Commit

Permalink
selftests/bpf: Consolidate kernel modules into common directory
Browse files Browse the repository at this point in the history
The selftests build four kernel modules which use copy-pasted Makefile
targets. This is a bit messy, and doesn't scale so well when we add more
modules, so let's consolidate these rules into a single rule generated
for each module name, and move the module sources into a single
directory.

To avoid parallel builds of the different modules stepping on each
other's toes during the 'modpost' phase of the Kbuild 'make modules',
the module files should really be a grouped target. However, make only
added explicit support for grouped targets in version 4.3, which is
newer than the minimum version supported by the kernel. However, make
implicitly treats pattern matching rules with multiple targets as a
grouped target, so we can work around this by turning the rule into a
pattern matching target. We do this by replacing '.ko' with '%ko' in the
targets with subst().

Acked-by: Viktor Malik <[email protected]>
Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo authored and Kernel Patches Daemon committed Dec 6, 2024
1 parent 9cf821f commit b3e7185
Show file tree
Hide file tree
Showing 51 changed files with 83 additions and 161 deletions.
64 changes: 21 additions & 43 deletions tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ TEST_PROGS_EXTENDED := with_addr.sh \
with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
test_xdp_vlan.sh test_bpftool.py

TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
bpf_test_modorder_y.ko
TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS))

# Compile but not part of 'make run_tests'
TEST_GEN_PROGS_EXTENDED = \
bench \
bpf_testmod.ko \
bpf_test_modorder_x.ko \
bpf_test_modorder_y.ko \
bpf_test_no_cfi.ko \
flow_dissector_load \
runqslower \
test_cpp \
Expand All @@ -184,8 +184,9 @@ override define CLEAN
$(Q)$(RM) -r $(TEST_GEN_PROGS)
$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
$(Q)$(RM) -r $(TEST_GEN_FILES)
$(Q)$(RM) -r $(TEST_KMODS)
$(Q)$(RM) -r $(EXTRA_CLEAN)
$(Q)$(MAKE) -C bpf_testmod clean
$(Q)$(MAKE) -C test_kmods clean
$(Q)$(MAKE) docs-clean
endef

Expand Down Expand Up @@ -251,7 +252,7 @@ endif
# to build individual tests.
# NOTE: Semicolon at the end is critical to override lib.mk's default static
# rule for binaries.
$(notdir $(TEST_GEN_PROGS) \
$(notdir $(TEST_GEN_PROGS) $(TEST_KMODS) \
$(TEST_GEN_PROGS_EXTENDED)): %: $(OUTPUT)/% ;

# sort removes libbpf duplicates when not cross-building
Expand Down Expand Up @@ -305,37 +306,19 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
$< -o $@ \
$(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)

$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
$(call msg,MOD,,$@)
$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
$(Q)$(MAKE) $(submake_extras) -C bpf_testmod \
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
$(Q)cp bpf_testmod/bpf_testmod.ko $@

$(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_no_cfi/Makefile bpf_test_no_cfi/*.[ch])
$(call msg,MOD,,$@)
$(Q)$(RM) bpf_test_no_cfi/bpf_test_no_cfi.ko # force re-compilation
$(Q)$(MAKE) $(submake_extras) -C bpf_test_no_cfi \
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
# This should really be a grouped target, but make versions before 4.3 don't
# support that for regular rules. However, pattern matching rules are implicitly
# treated as grouped even with older versions of make, so as a workaround, the
# subst() turns the rule into a pattern matching rule
$(addprefix test_kmods/,$(subst .ko,%ko,$(TEST_KMODS))): $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard test_kmods/Makefile test_kmods/*.[ch])
$(Q)$(RM) test_kmods/*.ko test_kmods/*.mod.o # force re-compilation
$(Q)$(MAKE) $(submake_extras) -C test_kmods \
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
$(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@

$(OUTPUT)/bpf_test_modorder_x.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_x/Makefile bpf_test_modorder_x/*.[ch])
$(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS))
$(call msg,MOD,,$@)
$(Q)$(RM) bpf_test_modorder_x/bpf_test_modorder_x.ko # force re-compilation
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_x \
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
$(Q)cp bpf_test_modorder_x/bpf_test_modorder_x.ko $@

$(OUTPUT)/bpf_test_modorder_y.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_y/Makefile bpf_test_modorder_y/*.[ch])
$(call msg,MOD,,$@)
$(Q)$(RM) bpf_test_modorder_y/bpf_test_modorder_y.ko # force re-compilation
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_y \
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
$(Q)cp bpf_test_modorder_y/bpf_test_modorder_y.ko $@
$(Q)cp test_kmods/$(@F) $@


DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
Expand Down Expand Up @@ -760,14 +743,12 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
json_writer.c \
flow_dissector_load.h \
ip_check_defrag_frags.h
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
$(OUTPUT)/bpf_test_no_cfi.ko \
$(OUTPUT)/bpf_test_modorder_x.ko \
$(OUTPUT)/bpf_test_modorder_y.ko \
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
$(OUTPUT)/liburandom_read.so \
$(OUTPUT)/xdp_synproxy \
$(OUTPUT)/sign-file \
$(OUTPUT)/uprobe_multi \
$(TEST_KMOD_TARGETS) \
ima_setup.sh \
verify_sig_setup.sh \
$(wildcard progs/btf_dump_test_case_*.c) \
Expand Down Expand Up @@ -894,12 +875,9 @@ $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld

EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
prog_tests/tests.h map_tests/tests.h verifier/tests.h \
feature bpftool \
feature bpftool $(TEST_KMOD_TARGETS) \
$(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \
no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \
bpf_test_no_cfi.ko \
bpf_test_modorder_x.ko \
bpf_test_modorder_y.ko \
no_alu32 cpuv4 bpf_gcc \
liburandom_read.so) \
$(OUTPUT)/FEATURE-DUMP.selftests

Expand Down
19 changes: 0 additions & 19 deletions tools/testing/selftests/bpf/bpf_test_modorder_x/Makefile

This file was deleted.

19 changes: 0 additions & 19 deletions tools/testing/selftests/bpf/bpf_test_modorder_y/Makefile

This file was deleted.

19 changes: 0 additions & 19 deletions tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile

This file was deleted.

20 changes: 0 additions & 20 deletions tools/testing/selftests/bpf/bpf_testmod/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/core_reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _GNU_SOURCE
#include <test_progs.h>
#include "progs/core_reloc_types.h"
#include "bpf_testmod/bpf_testmod.h"
#include "test_kmods/bpf_testmod.h"
#include <linux/limits.h>
#include <sys/mman.h>
#include <sys/syscall.h>
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/bad_struct_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "../bpf_testmod/bpf_testmod.h"
#include "../test_kmods/bpf_testmod.h"

char _license[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/cb_refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

struct map_value {
struct prog_test_ref_kfunc __kptr *ptr;
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/epilogue_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/epilogue_tailcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/iters_testmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "bpf_experimental.h"
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/jit_probe_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

static struct prog_test_ref_kfunc __kptr *v;
long total_sum = -1;
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/kfunc_call_destructive.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

SEC("tc")
int kfunc_destructive_test(void)
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/kfunc_call_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Copyright (c) 2021 Facebook */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

struct syscall_test_args {
__u8 data[16];
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/kfunc_call_race.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

SEC("tc")
int kfunc_call_fail(struct __sk_buff *ctx)
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/kfunc_call_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Copyright (c) 2021 Facebook */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

SEC("tc")
int kfunc_call_test4(struct __sk_buff *skb)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

extern const int bpf_prog_active __ksym;
int active_res = -1;
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/local_kptr_stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "../bpf_experimental.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

struct plain_local;

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/map_kptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

struct map_value {
struct prog_test_ref_kfunc __kptr_untrusted *unref_ptr;
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/map_kptr_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

struct map_value {
char buf[8];
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/missed_kprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/nested_acquire.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/pro_epilogue.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/pro_epilogue_goto_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "../bpf_testmod/bpf_testmod.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

char _license[] SEC("license") = "GPL";

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/sock_addr_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* Copyright (c) 2024 Google LLC */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "../bpf_testmod/bpf_testmod_kfunc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

SEC("syscall")
int init_sock(struct init_sock_args *args)
Expand Down
Loading

0 comments on commit b3e7185

Please sign in to comment.