Skip to contentSkip to conversation

selftests/livepatch: build test modules against the selected kernel

PATCH In review Sasha Levin 2 participants 3 messages Oct 14 – Oct 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/livepatch: build test modules against the selected kernel

The livepatch test modules are built against /lib/modules/$(uname -r)/build unless KDIR is set, and they are built unconditionally. This has three problems:

- When the selftests are built for a kernel in an output directory (make O=...), the modules are still built against the running host kernel, or fail when the host has no build directory.

- The top-level selftests Makefile passes O to every suite, and the test_modules Makefile hands it on to the kernel build. Even an empty O= on the command line overrides the KBUILD_OUTPUT set by the Makefile of a kernel build directory, so the build fails against output directories and distribution headers such as Debian's:
"make -C tools/testing/selftests TARGETS=livepatch" builds no modules at all.

- A kernel configured without CONFIG_LIVEPATCH cannot build the modules, although the installed scripts would correctly skip.

Select KDIR from O or KBUILD_OUTPUT when it is a prepared kernel build directory containing Module.symvers, then from the source tree when the kernel was built in place, and otherwise from the running kernel. An explicit KDIR still takes precedence. Pass KDIR to test_modules as an absolute path and stop forwarding O and KBUILD_OUTPUT to the kernel build, including when the selftests are built through the kernel's kselftest targets, since KDIR already names the output directory.

Read CONFIG_LIVEPATCH from the selected .config and skip the modules only when the configuration is readable and the option is not enabled. A KDIR without a readable configuration still attempts the build, so that a directory that is not a prepared kernel tree fails the build instead of silently producing an installation without modules. A KDIR that does not exist keeps being skipped, as before.
Always clean test_modules, and have the scripts report a skip after the root check when the running kernel does not provide livepatch.

Fixes c4bbe83d27c2 ("livepatch: Move tests from lib/livepatch to selftests/livepatch")
---
 tools/testing/selftests/livepatch/Makefile    | 27 +++++++++++++++++++
 .../testing/selftests/livepatch/functions.sh  |  1 +
 .../selftests/livepatch/test_modules/Makefile |  6 +++--
 3 files changed, 32 insertions(+), 2 deletions(-)
tools/testing/selftests/livepatch/Makefile +27 -0
index a080eb54a215d..855bbad241235 100644
@@ -1,7 +1,25 @@
 # SPDX-License-Identifier: GPL-2.0
 
+ifndef KDIR
+LIVEPATCH_OBJTREE := $(firstword $(O) $(KBUILD_OUTPUT))
+ifneq ($(and $(LIVEPATCH_OBJTREE),$(wildcard $(LIVEPATCH_OBJTREE)/Module.symvers)),)
+KDIR := $(LIVEPATCH_OBJTREE)
+else ifneq ($(wildcard ../../../../Module.symvers),)
+KDIR := ../../../..
+else
+KDIR := /lib/modules/$(shell uname -r)/build
+endif
+endif
+override KDIR := $(abspath $(KDIR))
+export KDIR
+
+LIVEPATCH_CONFIG := $(shell if [ -r "$(KDIR)/.config" ]; then \
+	grep -qx 'CONFIG_LIVEPATCH=y' "$(KDIR)/.config" && echo y || echo n; fi)
+
 TEST_GEN_FILES := test_klp-call_getpid
+ifneq ($(LIVEPATCH_CONFIG),n)
 TEST_GEN_MODS_DIR := test_modules
+endif
 TEST_PROGS_EXTENDED := functions.sh
 TEST_PROGS := \
 	test-livepatch.sh \
@@ -16,3 +34,12 @@ TEST_PROGS := \
 TEST_FILES := settings
 
 include ../lib.mk
+
+ifeq ($(LIVEPATCH_CONFIG),n)
+clean: clean_livepatch_mods
+
+clean_livepatch_mods:
+	$(Q)$(MAKE) -C test_modules clean
+
+.PHONY: clean_livepatch_mods
+endif
tools/testing/selftests/livepatch/functions.sh +1 -0
index 429fce1f31431..8b26b70f92d8c 100644
@@ -150,6 +150,7 @@ function cleanup() {
 #		 the ftrace_enabled sysctl.
 function setup_config() {
 	is_root
+	[[ -d "$SYSFS_KLP_DIR" ]] || skip "livepatch is not available"
 	has_kdir
 	push_config
 	set_dynamic_debug
tools/testing/selftests/livepatch/test_modules/Makefile +4 -2
index a13d398585dcb..1fbeaac9c2b2c 100644
@@ -16,14 +16,16 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state3.o \
 	test_klp_syscall.o
 
+modules clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))
+
 # Ensure that KDIR exists, otherwise skip the compilation
 modules:
 ifneq ("$(wildcard $(KDIR))", "")
-	$(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
+	$(Q)env -u KBUILD_OUTPUT $(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
 endif
 
 # Ensure that KDIR exists, otherwise skip the clean target
 clean:
 ifneq ("$(wildcard $(KDIR))", "")
-	$(Q)$(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=$(TESTMODS_DIR)
+	$(Q)env -u KBUILD_OUTPUT $(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=$(TESTMODS_DIR)
 endif

base-commit: 4eb2586bd40db27d77cc5b724ca9386ed0c71d53

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] kbuild: target-specific MAKEOVERRIDES assignment is ignored by GNU Make - [Medium] selftests…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kbuild: target-specific MAKEOVERRIDES assignment is ignored by GNU Make
- [Medium] selftests: livepatch: tests falsely skip on target machines without KDIR --

commit 52a5776aaff15a8419816c34023de1b05a2d6d7f
Author: Sasha Levin <[email protected]>

selftests/livepatch: build test modules against the selected kernel

This commit attempts to fix building livepatch test modules against an output directory by selecting KDIR from O or KBUILD_OUTPUT. It also aims to stop forwarding O and KBUILD_OUTPUT to the kernel sub-make so that the build works correctly against distribution headers or output directories.

tools/testing/selftests/livepatch/functions.sh function cleanup() { · line 155

Quoted 11 lines
tools/testing/selftests/livepatch/functions.sh +1 -0
index 429fce1f31431..8b26b70f92d8c 100644
@@ -150,6 +150,7 @@ function cleanup() {
 #		 the ftrace_enabled sysctl.
 function setup_config() {
 	is_root
+	[[ -d "$SYSFS_KLP_DIR" ]] || skip "livepatch is not available"
 	has_kdir
 	push_config

[Severity: Medium]
Is the call to has_kdir inside setup_config still appropriate here?

When running tests via the kselftest_install workflow on a target system without a kernel build tree, has_kdir fails and unconditionally skips the tests. This causes the tests to falsely skip on installed target machines, even if the test modules were already successfully compiled and are available to load.

tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 19

Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/Makefile +1 -0
index a13d398585dcb..1fbeaac9c2b2c 100644
@@ -16,14 +16,16 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state3.o \
 	test_klp_syscall.o
 
+modules clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))

[Severity: Medium]
Will this target-specific assignment of MAKEOVERRIDES be ignored by GNU Make?

GNU Make does not support target-specific assignments for special variables like MAKEOVERRIDES and MAKEFLAGS. As a result, the global O= and KBUILD_OUTPUT= overrides will still be passed to the sub-make via MAKEFLAGS.

Could this leak the un-filtered variables to Kbuild and cause the build to fail against output directories when make O=... is used, defeating the purpose of the patch?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 8 lines · On Wed, Oct 14, 2026 at 08:12:26PM +0000, [email protected] wrote:
Quoted 1 line

+modules clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))

[Severity: Medium]
Will this target-specific assignment of MAKEOVERRIDES be ignored by GNU Make?

GNU Make does not support target-specific assignments for special variables like MAKEOVERRIDES and MAKEFLAGS. As a result, the global O= and KBUILD_OUTPUT= overrides will still be passed to the sub-make via MAKEFLAGS.

It is not ignored. MAKEFLAGS holds a reference to $(MAKEOVERRIDES), so the sub-make started from these recipes gets the filtered value.

I checked with make 4.4.1, running the livepatch Makefile with O= and KBUILD_OUTPUT= on the command line and a KDIR whose Makefile prints what it receives. Without this patch both arrive as command line variables. With it, neither is in MAKEFLAGS, KBUILD_OUTPUT is unset, and O is only in the environment, which the kernel Makefile ignores since it uses O only when its origin is "command line".

Quoted 5 lines

Is the call to has_kdir inside setup_config still appropriate here?

When running tests via the kselftest_install workflow on a target system without a kernel build tree, has_kdir fails and unconditionally skips the tests.

This patch does not change that. setup_config has called has_kdir since 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing"), and this patch only adds the livepatch availability check in front of it.

Signature · 2 lines
Thanks,
Sasha