[PATCH v3 02/11] selftests/bpf: Route test_kmods through the kselftest module flow

From: Ricardo B. Marlière

Date: Mon Apr 06 2026 - 12:11:53 EST


Now that test_kmods/Makefile correctly handles O=, KBUILD_OUTPUT, and
missing kernel directories, wire it into lib.mk's module build
infrastructure via TEST_GEN_MODS_DIR.

Replace the bespoke pattern rule with a gen_mods_dir target that adds
VMLINUX_BTF and RESOLVE_BTFIDS as prerequisites, letting lib.mk drive
the actual build and clean lifecycle. Make TEST_KMOD_TARGETS order-only
dependents of gen_mods_dir and make the .ko copy step resilient so
that a missing module results in a SKIP rather than an error.

Signed-off-by: Ricardo B. Marlière <rbm@xxxxxxxx>
---
tools/testing/selftests/bpf/Makefile | 22 +++++++++-------------
1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index f75c4f52c028..715f282193c8 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -123,6 +123,7 @@ TEST_PROGS_EXTENDED := \
TEST_KMODS := bpf_testmod.ko bpf_test_no_cfi.ko bpf_test_modorder_x.ko \
bpf_test_modorder_y.ko bpf_test_rqspinlock.ko
TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS))
+TEST_GEN_MODS_DIR := test_kmods

# Compile but not part of 'make run_tests'
TEST_GEN_PROGS_EXTENDED = \
@@ -152,7 +153,6 @@ override define CLEAN
$(Q)$(RM) -r $(TEST_GEN_FILES)
$(Q)$(RM) -r $(TEST_KMODS)
$(Q)$(RM) -r $(EXTRA_CLEAN)
- $(Q)$(MAKE) -C test_kmods clean
$(Q)$(MAKE) docs-clean
endef

@@ -277,19 +277,15 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
$< -o $@ \
$(shell $(PKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)

-# 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=''
-
-$(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS))
+gen_mods_dir: $(VMLINUX_BTF) $(RESOLVE_BTFIDS)
+
+$(TEST_KMOD_TARGETS): | gen_mods_dir
$(call msg,MOD,,$@)
- $(Q)cp test_kmods/$(@F) $@
+ $(Q)if [ -f test_kmods/$(@F) ]; then \
+ cp test_kmods/$(@F) $@; \
+ else \
+ printf ' %-8s %s\n' SKIP $(@F) 1>&2; \
+ fi


DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool

--
2.53.0