Skip to contentSkip to conversation

selftests/livepatch: build and run the tests outside the running kernel

PATCH In review Sasha Levin 3 participants 14 messages Nov 10 – Nov 23

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

v4 cover selftests/livepatch: build and run the tests outside the running kernel

The livepatch selftests build their test modules against the running kernel and skip every test without its devel files, so they do not work when built for one kernel and run on another, as CI systems do. In KernelCI, 36 of 69 kselftest builds of mainline fail to compile the modules because the kernel lacks CONFIG_LIVEPATCH, and where the modules do build, the installed tests skip themselves.

- Patch 1 skips the modules and tests when livepatch is not enabled.
- Patch 2 lets "make clean" remove the modules without a kernel build directory.
- Patch 3 builds the modules against the kernel in O= or KBUILD_OUTPUT.
- Patch 4 runs the tests with the installed modules, without the devel files.

Tested on x86_64 against kernels built with O= and in place, a kernel without livepatch and the Debian headers. The modules build and clean in every case, and the installed tests pass 8/8 in a VM without devel files, where they were all skipped before.

Changes since v3:
- Split into four patches, rewrote the commit messages and added comments. (Petr)
- Moved the CONFIG_LIVEPATCH check into test_modules/Makefile, which drops the extra clean target (Petr), and read it from include/config/auto.conf.
- Patch 4 still skips when neither the modules nor KDIR are available. (Petr)

v3: https://lore.kernel.org/all/[email protected]/

Sasha Levin (4):
selftests/livepatch: skip the tests on kernels without livepatch selftests/livepatch: clean the test modules without a kernel build
directory
selftests/livepatch: build the test modules against the kernel in O= selftests/livepatch: run the tests with prebuilt test modules

 tools/testing/selftests/livepatch/Makefile    | 26 +++++++++++++++++
 .../testing/selftests/livepatch/functions.sh  | 13 ++++++---
 .../selftests/livepatch/test_modules/Makefile | 29 ++++++++++++++-----
 3 files changed, 57 insertions(+), 11 deletions(-)

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/4 selftests/livepatch: skip the tests on kernels without livepatch

The test modules can only be built against a kernel configured with CONFIG_LIVEPATCH, but the selftests try to build them for any kernel.

KernelCI builds the selftests for many kernel configurations, most of which do not enable livepatch. On mainline from November 8, 36 of its 69 kselftest builds fail to compile the test modules, for example:

test_klp_callbacks_demo2.c:23:16: error: invalid use of undefined type 'struct klp_object'

These are arm and riscv kernels, which do not support livepatch, and x86, arm64, powerpc and loongarch configurations that do not enable it.

Do not build the test modules when the kernel in KDIR was configured without CONFIG_LIVEPATCH, and skip the tests when the running kernel has no /sys/kernel/livepatch.

Fixes c4bbe83d27c2 ("livepatch: Move tests from lib/livepatch to selftests/livepatch")
---
 tools/testing/selftests/livepatch/functions.sh     |  1 +
 .../selftests/livepatch/test_modules/Makefile      | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
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 +12 -2
index a13d398585dcb..eddfb420db320 100644
@@ -16,9 +16,19 @@ obj-m += test_klp_atomic_replace.o \
 	test_klp_state3.o \
 	test_klp_syscall.o
 
-# Ensure that KDIR exists, otherwise skip the compilation
-modules:
+# 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
+# directory fails the build instead of being skipped.
+KDIR_CONFIG := $(KDIR)/include/config/auto.conf
 ifneq ("$(wildcard $(KDIR))", "")
+ifneq ($(shell [ -r "$(KDIR_CONFIG)" ] && ! grep -qx 'CONFIG_LIVEPATCH=y' "$(KDIR_CONFIG)" && echo n),n)
+BUILD_TEST_MODS := y
+endif
+endif
+
+modules:
+ifdef BUILD_TEST_MODS
 	$(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
 endif
 
Signature · 1 line
2.53.0
maintainer Reviewed-by Tested-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] selftests/livepatch: skip the tests on kernels without livepatch

Quoted 12 lines · Sasha Levin

The test modules can only be built against a kernel configured with CONFIG_LIVEPATCH, but the selftests try to build them for any kernel.

KernelCI builds the selftests for many kernel configurations, most of which do not enable livepatch. On mainline from November 8, 36 of its 69 kselftest builds fail to compile the test modules, for example:

test_klp_callbacks_demo2.c:23:16: error: invalid use of undefined type 'struct klp_object'

These are arm and riscv kernels, which do not support livepatch, and x86, arm64, powerpc and loongarch configurations that do not enable it.

Quoted 6 lines

Do not build the test modules when the kernel in KDIR was configured without CONFIG_LIVEPATCH, and skip the tests when the running kernel has no /sys/kernel/livepatch.

Fixes c4bbe83d27c2 ("livepatch: Move tests from lib/livepatch to selftests/livepatch")

Looks reasonable and seems to work:

Best Regards,
Petr

#3 lore raw

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

In reply to Sasha Levin

"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.

---
 .../testing/selftests/livepatch/test_modules/Makefile | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/livepatch/test_modules/Makefile +7 -4
index eddfb420db320..01eaf2e1755c9 100644
@@ -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)
Signature · 1 line
2.53.0
#4 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/livepatch: missing Fixes: trailer in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/livepatch: missing Fixes: trailer in commit message --

commit d55e0dca037d90570993238825fe598e3b4846aa
Author: Sasha Levin <[email protected]>

Quoted 11 lines

selftests/livepatch: clean the test modules without a kernel build directory

"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

Quoted 6 lines

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.

[Severity: Low]
This fixes the KDIR check on the clean target. Should it include:

Fixes 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

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

In reply to Sasha Levin
Quoted 7 lines · Sasha Levin

"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

The two parameters "O=<kernel> KDIR=<kernel>" do not make much sense to me. The command starts compiling other selftests. I get:

$> make -C tools/testing/selftests O=<kernel> KDIR=<kernel> TARGETS=livepatch
Makefile:4: *** Package alsa not found, please install alsa development package or add directory containing alsa.pc in PKG_CONFIG_PATH. Stop. test_execve.c:4:10: fatal error: cap-ng.h: No such file or directory

    4 | #include <cap-ng.h>
      |          ^~~~~~~~~~

compilation terminated.
make[1]: *** [../lib.mk:226: /prace/ai/kernel/linux-livepatching/tools/testing/selftests/capabilities/test_execve] Error 1 /usr/bin/ld.bfd: cannot find -lcap: No such file or directory
collect2: error: ld returned 1 exit status

It works well when I omit the two parameters:

$> make -C tools/testing/selftests TARGETS=livepatch
make: Entering directory '/prace/ai/kernel/linux-livepatching/tools/testing/selftests'
CC test_klp-call_getpid
env -u KBUILD_OUTPUT make -C /prace/ai/kernel/linux-livepatching modules KBUILD_EXTMOD=/prace/ai/kernel/linux-livepatching/tools/testing/selftests/livepatch/test_modules
CC [M] test_klp_atomic_replace.o
CC [M] test_klp_callbacks_busy.o

Are these parameters really used by the KernelCI?
Could you please provide link to a build log?

Quoted 15 lines

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.

tools/testing/selftests/livepatch/test_modules/Makefile +1 -1
@@ -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

tools/testing/selftests/livepatch/test_modules/Makefile ifdef BUILD_TEST_MODS · line 42

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/Makefile +6 -3
+# 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)

Otherwise, the change seems to do the job.
So, maybe just the sample command in the commit message is wrong.

Best Regards,
Petr

#6 lore raw

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

Quoted 5 lines · Petr Mladek

The two parameters "O=<kernel> KDIR=<kernel>" do not make much sense to me. The command starts compiling other selftests. I get:

$> make -C tools/testing/selftests O=<kernel> KDIR=<kernel> TARGETS=livepatch
Makefile:4: *** Package alsa not found, please install alsa development package or add directory containing alsa.pc in PKG_CONFIG_PATH. Stop.

Sorry, <kernel> was a placeholder for the kernel build directory. Pasted as is, the shell takes "<kernel>" as redirections, so make gets an empty O= and no TARGETS at all, and its output goes to a file named "TARGETS=livepatch". If that's what happened, that file and an empty "KDIR=" are now at the top of your tree.

Quoted 2 lines

Are these parameters really used by the KernelCI?
Could you please provide link to a build log?

Yes. This is from Saturday's x86_64 build of mainline:

  make --silent --keep-going --jobs=8 O=/tmp/kci/kernel_build \
       INSTALL_PATH=/tmp/kci/kernel_build/kselftest_install \
       KDIR=/tmp/kci/kernel_build ARCH=x86_64 SRCARCH=x86 \
       CROSS_COMPILE=x86_64-linux-gnu- -C tools/testing/selftests all

https://files.kernelci.org/kbuild-gcc-14-x86-kselftest-6b01150f99c74e1a99a2a261/build.log.gz

Quoted 1 line

So, maybe just the sample command in the commit message is wrong.

Yes, it should have used a real path:

make -C tools/testing/selftests O=/tmp/build KDIR=/tmp/build TARGETS=livepatch

Please keep O= and KDIR= in it, though. The paragraph is about modules built for a kernel other than the running one, and that's how they get built without this series.

--
Thanks,
Sasha

#7 lore raw

PATCH v4 3/4 selftests/livepatch: build the test modules against the kernel in O=

In reply to Sasha Levin

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")
---
 tools/testing/selftests/livepatch/Makefile    | 26 +++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile | 12 +++++----
 2 files changed, 33 insertions(+), 5 deletions(-)
tools/testing/selftests/livepatch/Makefile +26 -0
index a080eb54a215d..97478ef90945c 100644
@@ -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
tools/testing/selftests/livepatch/test_modules/Makefile +7 -5
index 01eaf2e1755c9..82f0119f6cf0d 100644
@@ -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)
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v4 3/4] selftests/livepatch: build the test modules against the kernel in O=

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/livepatch: KDIR fallback ignores builds without Module.symvers --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/livepatch: KDIR fallback ignores builds without Module.symvers
--

commit 62f70537bbca33262ec62f1273becbdf534f58fc
Author: Sasha Levin <[email protected]>

selftests/livepatch: build the test modules against the kernel in O=

This commit selects KDIR from O= or KBUILD_OUTPUT (or the kernel source tree) before falling back to the running kernel's build directory, and strips O= and KBUILD_OUTPUT when invoking the kernel Makefile for the livepatch test modules.

Quoted 16 lines
tools/testing/selftests/livepatch/Makefile +9 -0
index a080eb54a215d..97478ef90945c 100644
@@ -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.

tools/testing/selftests/livepatch/Makefile line 21

Quoted 10 lines
tools/testing/selftests/livepatch/Makefile +10 -0
+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

[Severity: Medium]
Does checking only for Module.symvers cause KDIR to fall back to the host's /lib/modules/$(uname -r)/build when the target kernel in O= or the source tree was built with CONFIG_MODULES=n or an image-only target (which produces vmlinux.symvers instead of Module.symvers)?

When that happens and the host has kernel headers with CONFIG_LIVEPATCH=y, test_modules/Makefile reads the host's include/config/auto.conf and builds the test modules against the running host kernel instead of skipping them for the target kernel, which breaks cross-compiled builds or builds where the in-tree test modules use newer kernel APIs than the host.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#9 lore raw

Re: [PATCH v4 3/4] selftests/livepatch: build the test modules against the kernel in O=

Quoted 49 lines · On Tue 2026-11-10 16:53:01, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/livepatch: KDIR fallback ignores builds without Module.symvers
--

commit 62f70537bbca33262ec62f1273becbdf534f58fc
Author: Sasha Levin <[email protected]>

selftests/livepatch: build the test modules against the kernel in O=

This commit selects KDIR from O= or KBUILD_OUTPUT (or the kernel source tree) before falling back to the running kernel's build directory, and strips O= and KBUILD_OUTPUT when invoking the kernel Makefile for the livepatch test modules.

Quoted 16 lines
tools/testing/selftests/livepatch/Makefile +9 -0
index a080eb54a215d..97478ef90945c 100644
@@ -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.

tools/testing/selftests/livepatch/Makefile line 21

Quoted 10 lines
tools/testing/selftests/livepatch/Makefile +10 -0
+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

[Severity: Medium]
Does checking only for Module.symvers cause KDIR to fall back to the host's /lib/modules/$(uname -r)/build when the target kernel in O= or the source tree was built with CONFIG_MODULES=n or an image-only target (which produces vmlinux.symvers instead of Module.symvers)?

Quoted 5 lines

When that happens and the host has kernel headers with CONFIG_LIVEPATCH=y, test_modules/Makefile reads the host's include/config/auto.conf and builds the test modules against the running host kernel instead of skipping them for the target kernel, which breaks cross-compiled builds or builds where the in-tree test modules use newer kernel APIs than the host.

The problem looks real. But I would consider it a corner case. IMHO, there are only few users using CONFIG_MODULES=n. They might use SKIP_TARGETS="livepatch".

By other words, we could fix hit later when there is a demand. You would not block this patch because of this. It fixes some scenarios and goes in the right direction...

Best Regards,
Petr

maintainer Reviewed-by Tested-by on v4 3/4
#10 lore raw

Re: [PATCH v4 3/4] selftests/livepatch: build the test modules against the kernel in O=

In reply to Sasha Levin
Quoted 25 lines · Sasha Levin

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:

Quoted 7 lines

/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")

The patch looks and seems to work reasonably well.

Best Regards,
Petr

#11 lore raw

PATCH v4 4/4 selftests/livepatch: run the tests with prebuilt test modules

In reply to Sasha Levin

The tests are skipped unless the machine running them has a kernel build directory, in $KDIR or /lib/modules/$(uname -r)/build. They do not need one when the test modules were built with the selftests, because the scripts load the modules from test_modules/.

As a result the tests are skipped on a machine running an installed selftests tree without the kernel devel files, for example one installed with

make O=/tmp/kselftest TARGETS=livepatch kselftest-install

although the modules are there.

Run the tests when the test modules are available, so that they can run without the kernel devel files. Without the modules, keep skipping the tests when there is no kernel build directory to build them with.

Fixes 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing")
---
 tools/testing/selftests/livepatch/functions.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/livepatch/functions.sh +8 -4
index 8b26b70f92d8c..eebd36b0e0eaf 100644
@@ -44,14 +44,18 @@ function is_root() {
 	fi
 }
 
-# Check if we can compile the modules before loading them
-function has_kdir() {
+# Check that the test modules were built, or that we can compile them
+function has_test_modules() {
+	if compgen -G "test_modules/*.ko" > /dev/null; then
+		return
+	fi
+
 	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."
+		echo "skip all tests: test modules are not available and KDIR ($KDIR) is not available to compile them."
 		exit $ksft_skip
 	fi
 }
@@ -151,7 +155,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
maintainer Reviewed-by Tested-by on v4 4/4
#12 lore raw

Re: [PATCH v4 4/4] selftests/livepatch: run the tests with prebuilt test modules

Quoted 11 lines · Sasha Levin

The tests are skipped unless the machine running them has a kernel build directory, in $KDIR or /lib/modules/$(uname -r)/build. They do not need one when the test modules were built with the selftests, because the scripts load the modules from test_modules/.

As a result the tests are skipped on a machine running an installed selftests tree without the kernel devel files, for example one installed with

make O=/tmp/kselftest TARGETS=livepatch kselftest-install

Quoted 8 lines

although the modules are there.

Run the tests when the test modules are available, so that they can run without the kernel devel files. Without the modules, keep skipping the tests when there is no kernel build directory to build them with.

Fixes 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing")

Looks good and seems to work:

Best Regards,
Petr

maintainer
#13 lore raw
In reply to Sasha Levin
Quoted 14 lines · Sasha Levin

The livepatch selftests build their test modules against the running kernel and skip every test without its devel files, so they do not work when built for one kernel and run on another, as CI systems do. In KernelCI, 36 of 69 kselftest builds of mainline fail to compile the modules because the kernel lacks CONFIG_LIVEPATCH, and where the modules do build, the installed tests skip themselves.

- Patch 1 skips the modules and tests when livepatch is not enabled.
- Patch 2 lets "make clean" remove the modules without a kernel build directory.
- Patch 3 builds the modules against the kernel in O= or KBUILD_OUTPUT.
- Patch 4 runs the tests with the installed modules, without the devel files.

Quoted 4 lines

Tested on x86_64 against kernels built with O= and in place, a kernel without livepatch and the Debian headers. The modules build and clean in every case, and the installed tests pass 8/8 in a VM without devel files, where they were all skipped before.

My only open problem is the "O=<kernel> KDIR=<kernel>" parameters mentions in the commit message in the 2nd patch. They do not work for me.

Otherwise, this patchset looks ready for linux-next.

I am going to push it (with removing the problematic parameters from the commit message) by the end of this week unless anyone complains in the meantime.

Best Regards,
Petr