[PATCH v4 3/4] selftests/livepatch: build the test modules against the kernel in O=
From: Sasha Levin
Date: Tue Nov 10 2026 - 12:50:52 EST
Documentation/dev-tools/kselftest.rst describes building the selftests
for a kernel built in a separate output directory:
make O=/tmp/kselftest TARGETS=livepatch kselftest
The livepatch test modules are not built against the kernel in
/tmp/kselftest, but against the running kernel, using its devel files
in /lib/modules/$(uname -r)/build. When those are not installed, the
modules are silently left out. Only an explicit KDIR=/tmp/kselftest
builds them for the kernel in /tmp/kselftest.
When KDIR is not set, use the output directory given with O= or
KBUILD_OUTPUT if a kernel was built there, then the kernel source tree
if the kernel was built in it, and only then the running kernel.
The selftests also pass their O= on to the module build, and the kernel
Makefile takes it as the kernel build directory instead of KDIR. The
top-level selftests Makefile passes O= even when it is empty, so on a
Debian machine with the devel files of the running kernel installed,
make -C tools/testing/selftests TARGETS=livepatch
uses the source part of the devel files as the build directory and
fails:
/usr/src/linux-headers-6.19.13+deb14-common/Makefile:798: /usr/src/linux-headers-6.19.13+deb14-common/include/config/auto.conf: No such file or directory
Do not pass O= and KBUILD_OUTPUT on to the module build, since KDIR
already names the kernel build directory.
Fixes: c4bbe83d27c2 ("livepatch: Move tests from lib/livepatch to selftests/livepatch")
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
tools/testing/selftests/livepatch/Makefile | 26 +++++++++++++++++++
.../selftests/livepatch/test_modules/Makefile | 12 +++++----
2 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/livepatch/Makefile b/tools/testing/selftests/livepatch/Makefile
index a080eb54a215d..97478ef90945c 100644
--- a/tools/testing/selftests/livepatch/Makefile
+++ b/tools/testing/selftests/livepatch/Makefile
@@ -1,5 +1,31 @@
# SPDX-License-Identifier: GPL-2.0
+# The test modules are built against the kernel in KDIR. When KDIR is not
+# set, use the output directory given with O= or KBUILD_OUTPUT if a kernel
+# was built there, as with
+# make O=/tmp/kselftest TARGETS=livepatch kselftest
+# otherwise the kernel source tree if the kernel was built in it, as with
+# cd tools/testing/selftests/livepatch; make
+# and otherwise the build directory of the running kernel, which its
+# devel files provide. A kernel was built in a directory if it has a
+# Module.symvers.
+ifndef KDIR
+KBUILD_DIR := $(firstword $(O) $(KBUILD_OUTPUT))
+ifneq ($(and $(KBUILD_DIR),$(wildcard $(KBUILD_DIR)/Module.symvers)),)
+KDIR := $(KBUILD_DIR)
+else ifneq ($(wildcard ../../../../Module.symvers),)
+KDIR := ../../../..
+else
+KDIR := /lib/modules/$(shell uname -r)/build
+endif
+endif
+
+# test_modules is built from its own directory, so pass KDIR on as an
+# absolute path, also when it was given on the command line.
+override KDIR := $(abspath $(KDIR))
+export KDIR
+MAKEOVERRIDES := $(filter-out KDIR=%,$(MAKEOVERRIDES))
+
TEST_GEN_FILES := test_klp-call_getpid
TEST_GEN_MODS_DIR := test_modules
TEST_PROGS_EXTENDED := functions.sh
diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile
index 01eaf2e1755c9..82f0119f6cf0d 100644
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile
@@ -16,6 +16,11 @@ obj-m += test_klp_atomic_replace.o \
test_klp_state3.o \
test_klp_syscall.o
+# The kernel Makefile would take an O= or KBUILD_OUTPUT passed on from the
+# selftests build as its build directory, instead of KDIR or, for clean,
+# the source tree. Drop them.
+modules clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))
+
# Build the modules only if KDIR exists and its kernel was configured with
# CONFIG_LIVEPATCH, as recorded in include/config/auto.conf. If that cannot
# be read, still try the build, so that a KDIR that is not a kernel build
@@ -29,14 +34,11 @@ endif
modules:
ifdef BUILD_TEST_MODS
- $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
+ $(Q)env -u KBUILD_OUTPUT $(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
endif
# 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))
+# through the kernel source tree that contains this directory.
clean:
$(Q)env -u KBUILD_OUTPUT $(MAKE) -C $(TESTMODS_DIR)/../../../../.. \
clean KBUILD_EXTMOD=$(TESTMODS_DIR)
--
2.53.0