Skip to content

Commit d6212d8

Browse files
tohojoanakryiko
authored andcommitted
selftests/bpf: Consolidate kernel modules into common directory
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(). Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Viktor Malik <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e10500b commit d6212d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+83
-161
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ TEST_PROGS_EXTENDED := with_addr.sh \
150150
with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
151151
test_xdp_vlan.sh test_bpftool.py
152152

153+
TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
154+
bpf_test_modorder_y.ko
155+
TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS))
156+
153157
# Compile but not part of 'make run_tests'
154158
TEST_GEN_PROGS_EXTENDED = \
155159
bench \
156-
bpf_testmod.ko \
157-
bpf_test_modorder_x.ko \
158-
bpf_test_modorder_y.ko \
159-
bpf_test_no_cfi.ko \
160160
flow_dissector_load \
161161
runqslower \
162162
test_cpp \
@@ -182,8 +182,9 @@ override define CLEAN
182182
$(Q)$(RM) -r $(TEST_GEN_PROGS)
183183
$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
184184
$(Q)$(RM) -r $(TEST_GEN_FILES)
185+
$(Q)$(RM) -r $(TEST_KMODS)
185186
$(Q)$(RM) -r $(EXTRA_CLEAN)
186-
$(Q)$(MAKE) -C bpf_testmod clean
187+
$(Q)$(MAKE) -C test_kmods clean
187188
$(Q)$(MAKE) docs-clean
188189
endef
189190

@@ -249,7 +250,7 @@ endif
249250
# to build individual tests.
250251
# NOTE: Semicolon at the end is critical to override lib.mk's default static
251252
# rule for binaries.
252-
$(notdir $(TEST_GEN_PROGS) \
253+
$(notdir $(TEST_GEN_PROGS) $(TEST_KMODS) \
253254
$(TEST_GEN_PROGS_EXTENDED)): %: $(OUTPUT)/% ;
254255

255256
# sort removes libbpf duplicates when not cross-building
@@ -303,37 +304,19 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
303304
$< -o $@ \
304305
$(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
305306

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

322-
$(OUTPUT)/bpf_test_modorder_x.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_x/Makefile bpf_test_modorder_x/*.[ch])
317+
$(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS))
323318
$(call msg,MOD,,$@)
324-
$(Q)$(RM) bpf_test_modorder_x/bpf_test_modorder_x.ko # force re-compilation
325-
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_x \
326-
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
327-
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
328-
$(Q)cp bpf_test_modorder_x/bpf_test_modorder_x.ko $@
329-
330-
$(OUTPUT)/bpf_test_modorder_y.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_modorder_y/Makefile bpf_test_modorder_y/*.[ch])
331-
$(call msg,MOD,,$@)
332-
$(Q)$(RM) bpf_test_modorder_y/bpf_test_modorder_y.ko # force re-compilation
333-
$(Q)$(MAKE) $(submake_extras) -C bpf_test_modorder_y \
334-
RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) \
335-
EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
336-
$(Q)cp bpf_test_modorder_y/bpf_test_modorder_y.ko $@
319+
$(Q)cp test_kmods/$(@F) $@
337320

338321

339322
DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
@@ -758,14 +741,12 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
758741
json_writer.c \
759742
flow_dissector_load.h \
760743
ip_check_defrag_frags.h
761-
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
762-
$(OUTPUT)/bpf_test_no_cfi.ko \
763-
$(OUTPUT)/bpf_test_modorder_x.ko \
764-
$(OUTPUT)/bpf_test_modorder_y.ko \
744+
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \
765745
$(OUTPUT)/liburandom_read.so \
766746
$(OUTPUT)/xdp_synproxy \
767747
$(OUTPUT)/sign-file \
768748
$(OUTPUT)/uprobe_multi \
749+
$(TEST_KMOD_TARGETS) \
769750
ima_setup.sh \
770751
verify_sig_setup.sh \
771752
$(wildcard progs/btf_dump_test_case_*.c) \
@@ -892,12 +873,9 @@ $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld
892873

893874
EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
894875
prog_tests/tests.h map_tests/tests.h verifier/tests.h \
895-
feature bpftool \
876+
feature bpftool $(TEST_KMOD_TARGETS) \
896877
$(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \
897-
no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \
898-
bpf_test_no_cfi.ko \
899-
bpf_test_modorder_x.ko \
900-
bpf_test_modorder_y.ko \
878+
no_alu32 cpuv4 bpf_gcc \
901879
liburandom_read.so) \
902880
$(OUTPUT)/FEATURE-DUMP.selftests
903881

tools/testing/selftests/bpf/bpf_test_modorder_x/Makefile

Lines changed: 0 additions & 19 deletions
This file was deleted.

tools/testing/selftests/bpf/bpf_test_modorder_y/Makefile

Lines changed: 0 additions & 19 deletions
This file was deleted.

tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile

Lines changed: 0 additions & 19 deletions
This file was deleted.

tools/testing/selftests/bpf/bpf_testmod/Makefile

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _GNU_SOURCE
33
#include <test_progs.h>
44
#include "progs/core_reloc_types.h"
5-
#include "bpf_testmod/bpf_testmod.h"
5+
#include "test_kmods/bpf_testmod.h"
66
#include <linux/limits.h>
77
#include <sys/mman.h>
88
#include <sys/syscall.h>

tools/testing/selftests/bpf/progs/bad_struct_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <vmlinux.h>
44
#include <bpf/bpf_helpers.h>
55
#include <bpf/bpf_tracing.h>
6-
#include "../bpf_testmod/bpf_testmod.h"
6+
#include "../test_kmods/bpf_testmod.h"
77

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

tools/testing/selftests/bpf/progs/cb_refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <vmlinux.h>
33
#include <bpf/bpf_tracing.h>
44
#include <bpf/bpf_helpers.h>
5-
#include "../bpf_testmod/bpf_testmod_kfunc.h"
5+
#include "../test_kmods/bpf_testmod_kfunc.h"
66

77
struct map_value {
88
struct prog_test_ref_kfunc __kptr *ptr;

tools/testing/selftests/bpf/progs/epilogue_exit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <vmlinux.h>
55
#include <bpf/bpf_tracing.h>
66
#include "bpf_misc.h"
7-
#include "../bpf_testmod/bpf_testmod.h"
8-
#include "../bpf_testmod/bpf_testmod_kfunc.h"
7+
#include "../test_kmods/bpf_testmod.h"
8+
#include "../test_kmods/bpf_testmod_kfunc.h"
99

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

tools/testing/selftests/bpf/progs/epilogue_tailcall.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <vmlinux.h>
55
#include <bpf/bpf_tracing.h>
66
#include "bpf_misc.h"
7-
#include "../bpf_testmod/bpf_testmod.h"
8-
#include "../bpf_testmod/bpf_testmod_kfunc.h"
7+
#include "../test_kmods/bpf_testmod.h"
8+
#include "../test_kmods/bpf_testmod_kfunc.h"
99

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

0 commit comments

Comments
 (0)