test_kmods/Makefile always pointed KDIR at the kernel source tree root,
ignoring O= and KBUILD_OUTPUT. On distro kernels where the source tree has
not been built, there was no fallback and the build would fail
unconditionally.

When O= or KBUILD_OUTPUT points at a prepared kernel build directory (one
containing Module.symvers), pass it through so kbuild can locate the
correct build infrastructure. Note that module artifacts still land in the
M= directory (test_kmods/); O= only controls where kbuild finds its build
infrastructure. Fall back to /lib/modules/$(uname -r)/build when neither an
explicit valid build directory nor an in-tree Module.symvers is present.

A selftests-only O= value (one that does not contain Module.symvers) is
intentionally not treated as a kernel build directory to avoid passing an
unrelated directory to kbuild. The parent bpf/Makefile resolves O= and
KBUILD_OUTPUT to absolute paths before invoking the test_kmods sub-make so
relative paths anchor to the user's invocation directory rather than the
sub-make's CWD.

Guard the clean target against a missing KDIR unconditionally. In
permissive mode, also guard the all target and make the parent Makefile's
cp step conditional so that a missing or unbuilt module does not abort the
build.

Signed-off-by: Ricardo B. Marlière <[email protected]>
---
 tools/testing/selftests/bpf/Makefile            | 10 +++++----
 tools/testing/selftests/bpf/test_kmods/Makefile | 30 ++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile 
b/tools/testing/selftests/bpf/Makefile
index 6094fe99b5f6..cc6ee7a2df93 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -296,13 +296,15 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
 # 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)        \
+       $(Q)$(MAKE) $(submake_extras) -C test_kmods                             
\
+               $(if $(O),O=$(abspath $(O)))                                    
\
+               $(if $(KBUILD_OUTPUT),KBUILD_OUTPUT=$(abspath 
$(KBUILD_OUTPUT)))\
+               RESOLVE_BTFIDS=$(RESOLVE_BTFIDS)                                
\
                EXTRA_CFLAGS='' EXTRA_LDFLAGS=''
 
 $(TEST_KMOD_TARGETS): $(addprefix test_kmods/,$(TEST_KMODS))
        $(call msg,MOD,,$@)
-       $(Q)cp test_kmods/$(@F) $@
+       $(Q)$(if $(PERMISSIVE),if [ -f test_kmods/$(@F) ]; then )cp 
test_kmods/$(@F) $@$(if $(PERMISSIVE),; fi)
 
 
 DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
@@ -718,7 +720,7 @@ $(TRUNNER_LIB_OBJS): 
$(TRUNNER_OUTPUT)/%.o:$(TOOLSDIR)/lib/%.c
 $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
 ifneq ($2:$(OUTPUT),:$(shell pwd))
        $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
-       $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
+       $(Q)rsync -aq $(if $(PERMISSIVE),--ignore-missing-args) $$^ 
$(TRUNNER_OUTPUT)/
 endif
 
 # some X.test.o files have runtime dependencies on Y.bpf.o files
diff --git a/tools/testing/selftests/bpf/test_kmods/Makefile 
b/tools/testing/selftests/bpf/test_kmods/Makefile
index 63c4d3f6a12f..dcba595b4b99 100644
--- a/tools/testing/selftests/bpf/test_kmods/Makefile
+++ b/tools/testing/selftests/bpf/test_kmods/Makefile
@@ -1,5 +1,16 @@
 TEST_KMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
-KDIR ?= $(abspath $(TEST_KMOD_DIR)/../../../../..)
+SRCTREE_KDIR := $(abspath $(TEST_KMOD_DIR)/../../../../..)
+# Honor O=/KBUILD_OUTPUT only if they point at a prepared kernel build
+# directory (one containing Module.symvers); otherwise treat the value as a
+# selftests-only output directory and fall back to in-tree or distro headers.
+# The parent bpf/Makefile resolves O=/KBUILD_OUTPUT to absolute paths before
+# invoking this sub-make so relative paths still anchor to the user's
+# invocation directory.
+KMOD_O := $(or $(O),$(KBUILD_OUTPUT))
+KMOD_O_VALID := $(if $(KMOD_O),$(if $(wildcard 
$(KMOD_O)/Module.symvers),$(KMOD_O)))
+KDIR ?= $(if $(KMOD_O_VALID),$(SRCTREE_KDIR), \
+           $(if $(wildcard $(SRCTREE_KDIR)/Module.symvers),$(SRCTREE_KDIR), \
+               /lib/modules/$(shell uname -r)/build))
 
 ifeq ($(V),1)
 Q =
@@ -14,8 +25,21 @@ $(foreach m,$(MODULES),$(eval obj-m += $(m:.ko=.o)))
 
 CFLAGS_bpf_testmod.o = -I$(src)
 
+# When BPF_STRICT_BUILD != 0, a missing KDIR is fatal (the default).
+# When permissive, skip silently.
+PERMISSIVE := $(filter 0,$(BPF_STRICT_BUILD))
+
 all:
-       $(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) modules
+ifeq ($(PERMISSIVE),)
+       $(Q)$(MAKE) -C $(KDIR) $(if 
$(KMOD_O_VALID),O=$(KMOD_O_VALID),KBUILD_OUTPUT=) \
+               M=$(TEST_KMOD_DIR) modules
+else ifneq ("$(wildcard $(KDIR))", "")
+       $(Q)$(MAKE) -C $(KDIR) $(if 
$(KMOD_O_VALID),O=$(KMOD_O_VALID),KBUILD_OUTPUT=) \
+               M=$(TEST_KMOD_DIR) modules
+endif
 
 clean:
-       $(Q)$(MAKE) -C $(KDIR) M=$(TEST_KMOD_DIR) clean
+ifneq ("$(wildcard $(KDIR))", "")
+       $(Q)$(MAKE) -C $(KDIR) $(if 
$(KMOD_O_VALID),O=$(KMOD_O_VALID),KBUILD_OUTPUT=) \
+               M=$(TEST_KMOD_DIR) clean
+endif

-- 
2.54.0


Reply via email to