selftests/livepatch: fix building and running outside the host kernel
Subsystems (2)
v2 cover selftests/livepatch: fix building and running outside the host kernel
The livepatch test modules are built against the running host kernel even when the selftests are built for a kernel in an output directory, and the scripts skip every test on a machine without a kernel build tree, even when the modules were built and installed with the selftests.
Patch 1 is unchanged from v1. It picks KDIR from O or KBUILD_OUTPUT, stops passing O and KBUILD_OUTPUT on to the module build, and skips the modules for kernels without CONFIG_LIVEPATCH.
Patch 2 is new. Sashiko pointed out on v1 that has_kdir() skips the tests on installed targets. That check predates this series, but it is the run time half of the same problem, so patch 2 checks for the test modules instead.
Tested on v7.4-rc1 with an x86_64 kernel with CONFIG_LIVEPATCH built in an output directory. The modules build against it with
make -C tools/testing/selftests O=... TARGETS=livepatch install
and the installed tree was run in a VM that has no build directory for that kernel. Without patch 2 every test skips on the missing KDIR. With it all 8 livepatch tests pass, and they skip cleanly when test_modules/ is missing.
Changes since v1:
- Add patch 2.
- Rebase on v7.4-rc1, no changes to patch 1.
v1: https://lore.kernel.org/all/[email protected]/
Sasha Levin (2):
selftests/livepatch: build test modules against the selected kernel selftests/livepatch: skip based on the test modules instead of KDIR
tools/testing/selftests/livepatch/Makefile | 27 +++++++++++++++++++ .../testing/selftests/livepatch/functions.sh | 17 +++++------- .../selftests/livepatch/test_modules/Makefile | 6 +++-- 3 files changed, 37 insertions(+), 13 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 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.
--- 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
@@ -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
@@ -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
@@ -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
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 1/2] selftests/livepatch: build test modules against the selected kernel
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/livepatch: clean fails to resolve LIVEPATCH_OBJTREE - [Medium] selftests/livepatch…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/livepatch: clean fails to resolve LIVEPATCH_OBJTREE
- [Medium] selftests/livepatch: relative command-line KDIR not resolved for sub-make --
commit 02a5cf889b06dba8630406e570f3de0adc2db5d6
Author: Sasha Levin <[email protected]>
selftests/livepatch: build test modules against the selected kernel
This commit updates the livepatch selftest Makefiles to select KDIR from O, KBUILD_OUTPUT, or the in-tree build before falling back to the running kernel's build directory. It also skips building test modules when CONFIG_LIVEPATCH is disabled in the target .config and stops forwarding O and KBUILD_OUTPUT to the external module build.
Quoted 7 lines
tools/testing/selftests/livepatch/Makefile +0
-0@@ -1,7 +1,25 @@ # SPDX-License-Identifier: GPL-20
tools/testing/selftests/livepatch/Makefile line 12
Quoted 10 lines
tools/testing/selftests/livepatch/Makefile +10
-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
[Severity: Medium]
Can this fail to locate the build directory during a clean when a relative O= or KBUILD_OUTPUT= path is used, or when the kernel build tree has already removed Module.symvers?
During all, run_tests, and install, tools/testing/selftests/Makefile resolves O and KBUILD_OUTPUT into abs_objtree and passes O=$(abs_objtree) to the target sub-makes, but the clean target does not pass O=$(abs_objtree):
tools/testing/selftests/Makefile:
clean:
@for TARGET in $(TARGETS) $(INSTALL_DEP_TARGETS); do \ BUILD_TARGET=$$BUILD/$$TARGET; \ $(MAKE) OUTPUT=$$BUILD_TARGET -C $$TARGET clean;\ done;
When running "make O=build kselftest-clean TARGETS=livepatch" or
"make -C tools/testing/selftests O=build TARGETS=livepatch clean", LIVEPATCH_OBJTREE in tools/testing/selftests/livepatch/Makefile receives the unanchored relative path "build" (via MAKEFLAGS or the KBUILD_OUTPUT environment variable) and checks for tools/testing/selftests/livepatch/build/Module.symvers instead of ../../../../build/Module.symvers.
Similarly, if the kernel build directory was cleaned before kselftest-clean, Module.symvers is already gone even though Kbuild external-module clean does not require it.
In both cases, KDIR falls back to /lib/modules/$(shell uname -r)/build. On a host or container where /lib/modules/$(uname -r)/build does not exist, tools/testing/selftests/livepatch/test_modules/Makefile sees an empty $(wildcard $(KDIR)) and skips cleaning test_modules/, leaving the built module artifacts behind.
tools/testing/selftests/livepatch/Makefile line 14
Quoted 2 lines
tools/testing/selftests/livepatch/Makefile +2
-0+override KDIR := $(abspath $(KDIR)) +export KDIR
[Severity: Medium]
Does passing a relative KDIR=... on the make command line still propagate the unresolved relative path to test_modules/Makefile?
In GNU Make, "override KDIR := $(abspath $(KDIR))" and "export KDIR" update KDIR in the current makefile and exported environment, but the original command-line assignment remains in MAKEOVERRIDES (forwarded via MAKEFLAGS).
When ../lib.mk (via gen_mods_dir or clean_mods_dir) or clean_livepatch_mods invokes $(MAKE) -C test_modules, the sub-make in tools/testing/selftests/livepatch/test_modules/Makefile only filters O=% and KBUILD_OUTPUT=% out of MAKEOVERRIDES:
tools/testing/selftests/livepatch/test_modules/Makefile:
modules clean: MAKEOVERRIDES := $(filter-out O=% KBUILD_OUTPUT=%,$(MAKEOVERRIDES))
As a result, the child make inherits the raw command-line KDIR=<relative_path> from MAKEFLAGS, which takes precedence over the exported environment variable and is evaluated one directory deeper inside test_modules/ (for example, "make -C tools/testing/selftests/livepatch KDIR=../../../.." resolves KDIR to tools/ inside test_modules/Makefile and fails the build, or silently skips building if the shifted path does not exist).
Should KDIR=% also be filtered out of MAKEOVERRIDES (or passed explicitly as KDIR=$(KDIR) to the test_modules sub-make)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/2 selftests/livepatch: skip based on the test modules instead of KDIR
In reply to Sasha Levinsetup_config() skips every test when there is no kernel build directory at $KDIR or /lib/modules/$(uname -r)/build. The test modules are built together with the selftests and the scripts load them from test_modules/, so nothing needs a build directory at run time. A machine running an installed kselftest tree usually has none, and the tests are skipped there although the modules were built and installed.
Check for the modules that __load_mod() loads instead, and remove has_kdir(), which has no other users.
--- tools/testing/selftests/livepatch/functions.sh | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
tools/testing/selftests/livepatch/functions.sh +5 -11
@@ -44,16 +44,10 @@ function is_root() { fi } -# Check if we can compile the modules before loading them -function has_kdir() { - if [ -z "$KDIR" ]; then - KDIR="/lib/modules/$(uname -r)/build" - fi - - if [ ! -d "$KDIR" ]; then - echo "skip all tests: KDIR ($KDIR) not available to compile modules." - exit $ksft_skip - fi +# Check that the test modules were built before loading them +function has_test_modules() { + compgen -G "test_modules/*.ko" > /dev/null || + skip "test modules are not available" } # die(msg) - game over, man
@@ -151,7 +145,7 @@ function cleanup() { function setup_config() { is_root [[ -d "$SYSFS_KLP_DIR" ]] || skip "livepatch is not available" - has_kdir + has_test_modules push_config set_dynamic_debug set_ftrace_enabled 1
Signature · 1 line
2.53.0