[PATCH v4 2/4] selftests/livepatch: clean the test modules without a kernel build directory

From: Sasha Levin

Date: Tue Nov 10 2026 - 11:20:38 EST


"make clean" removes the test modules only when KDIR exists, and KDIR
defaults to the build directory of the running kernel. Modules built
for another kernel are therefore left in the source tree by a plain
"make clean" on a machine without the running kernel's devel files,
for example after building them as KernelCI does:

make -C tools/testing/selftests O=<kernel> KDIR=<kernel> TARGETS=livepatch
make -C tools/testing/selftests TARGETS=livepatch clean

Removing external modules does not need a configured kernel. Clean
them through the kernel source tree that contains the selftests, and
do not pass on O= or KBUILD_OUTPUT, which the kernel Makefile would
take as its build directory.

Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
.../testing/selftests/livepatch/test_modules/Makefile | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile
index eddfb420db320..01eaf2e1755c9 100644
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile
@@ -32,8 +32,11 @@ ifdef BUILD_TEST_MODS
$(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
endif

-# Ensure that KDIR exists, otherwise skip the clean target
+# Cleaning external modules needs no configured kernel, so clean them
+# through the kernel source tree that contains this directory. The kernel
+# Makefile would take an O= or KBUILD_OUTPUT passed on from the selftests
+# build as its build directory, so drop them.
+clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))
clean:
-ifneq ("$(wildcard $(KDIR))", "")
- $(Q)$(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=$(TESTMODS_DIR)
-endif
+ $(Q)env -u KBUILD_OUTPUT $(MAKE) -C $(TESTMODS_DIR)/../../../../.. \
+ clean KBUILD_EXTMOD=$(TESTMODS_DIR)
--
2.53.0