Skip to contentSkip to conversation

selftests/livepatch: fix building and running outside the host kernel

PATCH In review Sasha Levin 3 participants 6 messages Nov 3 – Nov 10

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

v3 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 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 makes the scripts check for the test modules instead of a kernel build tree.

Tested on v7.4-rc1 with an x86_64 kernel with CONFIG_LIVEPATCH built in an output directory. The modules build against it with relative and absolute O and KDIR, through tools/testing/selftests and through the kernel's kselftest targets, and clean removes them in all of those cases, also after Module.symvers is gone. The installed tree was run in a VM that has no build directory for that kernel: all 8 livepatch tests pass, and they skip cleanly when test_modules/ is missing.

Changes since v2:
- Patch 1: clean test_modules through the kernel source tree that contains them, so that clean no longer skips them for a relative O, a missing Module.symvers or a missing /lib/modules/$(uname -r)/build, and pass a KDIR given on the command line on as an absolute path. Both found by Sashiko.
- Patch 2 is unchanged.

v2: https://lore.kernel.org/all/[email protected]/
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    | 28 +++++++++++++++++++
 .../testing/selftests/livepatch/functions.sh  | 17 ++++-------
 .../selftests/livepatch/test_modules/Makefile | 11 ++++----
 3 files changed, 40 insertions(+), 16 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 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, also when it was given on the command line, 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, through the kernel source tree that contains them. Cleaning external modules needs neither a configuration nor Module.symvers, and unlike the build targets, the clean target of the selftests Makefile does not pass the absolute O on, so the kernel the modules were built against cannot always be found at that point.

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    | 28 +++++++++++++++++++
 .../testing/selftests/livepatch/functions.sh  |  1 +
 .../selftests/livepatch/test_modules/Makefile | 11 ++++----
 3 files changed, 35 insertions(+), 5 deletions(-)
tools/testing/selftests/livepatch/Makefile +28 -0
index a080eb54a215d..5c631812575dc 100644
@@ -1,7 +1,26 @@
 # 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
+MAKEOVERRIDES := $(filter-out KDIR=%,$(MAKEOVERRIDES))
+
+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 +35,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 +6 -5
index a13d398585dcb..62cb724c6b333 100644
@@ -16,14 +16,15 @@ 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
+# Any kernel tree can clean the test modules, so use the one containing them
 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
#2 lore raw

Re: [PATCH v3 1/2] selftests/livepatch: build test modules against the selected kernel

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

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

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

selftests/livepatch: build test modules against the selected kernel

This commit updates the livepatch selftests build and test scripts to select KDIR from O, KBUILD_OUTPUT, or the source tree before falling back to the running kernel's build directory, stops forwarding O and KBUILD_OUTPUT to the external module build, and skips building or running tests when CONFIG_LIVEPATCH is disabled.

Quoted 13 lines
tools/testing/selftests/livepatch/Makefile +6 -0
index a080eb54a215d..5c631812575dc 100644
@@ -1,7 +1,26 @@
 # 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 := ../../../..

tools/testing/selftests/livepatch/Makefile line 18

Quoted 10 lines
tools/testing/selftests/livepatch/Makefile +10 -0
+else
+KDIR := /lib/modules/$(shell uname -r)/build
+endif
+endif
+override KDIR := $(abspath $(KDIR))
+export KDIR
+MAKEOVERRIDES := $(filter-out KDIR=%,$(MAKEOVERRIDES))
+
+LIVEPATCH_CONFIG := $(shell if [ -r "$(KDIR)/.config" ]; then \
+	grep -qx 'CONFIG_LIVEPATCH=y' "$(KDIR)/.config" && echo y || echo n; fi)

[Severity: Medium]
When KDIR is not explicitly set, KDIR only selects $(LIVEPATCH_OBJTREE) or ../../../.. if Module.symvers exists in that directory, falling back to /lib/modules/$(shell uname -r)/build otherwise. Then LIVEPATCH_CONFIG checks $(KDIR)/.config for CONFIG_LIVEPATCH=y.

If a kernel is configured and built with CONFIG_MODULES=n (so CONFIG_LIVEPATCH is also disabled), the top-level Makefile clears KBUILD_MODULES and scripts/Makefile.modpost sets output-symdump to vmlinux.symvers instead of Module.symvers:

scripts/Makefile.modpost:
output-symdump := $(if $(KBUILD_MODULES), Module.symvers, vmlinux.symvers)

Because Module.symvers is never created in that build, or when running make kselftest-all or building only a kernel image target before modules, .config exists in $(LIVEPATCH_OBJTREE) or ../../../.. without Module.symvers.

In those cases, wouldn't KDIR ignore the target kernel directory and fall back to /lib/modules/$(uname -r)/build, causing LIVEPATCH_CONFIG to read the running host kernel's .config and attempt to build test_modules against the host kernel instead of skipping or using the target kernel tree? Conversely, if O= points to an unprepared kernel build tree configured with CONFIG_LIVEPATCH=y, wouldn't KDIR silently fall back to ../../../.. or /lib/modules/$(uname -r)/build instead of using O=?

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

Re: [PATCH v3 1/2] selftests/livepatch: build test modules against the selected kernel

In reply to Sasha Levin

Hi,

the commit message seems to be generated using LLM.
For me, it is a hard to read maze. But I'll try to go through it.

Let's start with Subject. What is "the selected kernel"? How can it be selected?

I would personally say that this this patch "allows to pre-built the test modules for the livepatch selftest into a separate directory".

My understanding is that there are several ways how to build the test modules, some work, and some does not. I would help me if the commit message better explains what works and what does not work.

Quoted 3 lines · Sasha Levin

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:

My understanding is that the Makefiles currently expects that "the kernel devel files, for the running kernel, are installed on the system " so that they could find them by /lib/modules/$(uname -r)/build.

The KDIR allows to find the devel files in another directory? But how do we set it?

Quoted 3 lines

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

What exactly is in the output directory?
What is build directory?
How they differ?

I see in Documentation/dev-tools/kselftest.rst:

<paste>
To build, save output files in a separate directory with O= ::

    $ make O=/tmp/kselftest TARGETS="size timers" kselftest
  </paste>

It would help if the commit message follows this naming scheme, aka "save output files in a separate directory with O=" and mentions an usable example, e.g.

  $ make O=/tmp/kselftest TARGETS="livepatch" kselftest

explain where it is supposed to search for the build files.

My understanding is that you want to pre-built the test modules when building a kernel. I mean to use a custom kernel source tree instead of using devel files for the currently running kernel.

Quoted 7 lines

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

This is another way how to build the modules:

make -C tools/testing/selftests TARGETS=livepatch

Again, it would be great to make it clear that it is for per-building the test modules from a custom kernel sources.

But it mentions also KBUILD_OUTPUT which seems to be another way, from Documentation/dev-tools/kselftest.rst:

<paste>
To build, save output files in a separate directory with KBUILD_OUTPUT ::

export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="size timers" kselftest
</paste>

It seems that "KBUILD_OUTPUT" has the same effect as "O". Why is it mentioned in a paragraph with
"make -C tools/testing/selftests TARGETS=livepatch" example?

Quoted 2 lines

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

This is a puzzle. If "the installed scripts would correctly skip" then what fails?

Quoted 16 lines

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, also when it was given on the command line, 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.

Quoted 5 lines

Always clean test_modules, through the kernel source tree that contains them. Cleaning external modules needs neither a configuration nor Module.symvers, and unlike the build targets, the clean target of the selftests Makefile does not pass the absolute O on, so the kernel the modules were built against cannot always be found at that point.

The above paragraphs might be "fine" as a "More details" section.

Quoted 2 lines

Have the scripts report a skip after the root check when the running kernel does not provide livepatch.

This patch should fix the build. This change does not fit here, see below.

tools/testing/selftests/livepatch/Makefile line 2

Quoted 5 lines
tools/testing/selftests/livepatch/Makefile +0 -0
@@ -1,7 +1,26 @@
 # SPDX-License-Identifier: GPL-2.0
 

The code below is a maze. It would be nice to add a comment here explaining how we try to find the kernel sources, ideally even include examples.

1. When using separate output directory, for example:
make O=/tmp/kselftest TARGETS="livepatch" kselftest
export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="livepatch" kselftest

2. When building directly from the tools/testing/selftests/livepatch source directory.

3. When using installed devel files for the running kernel.

But I might be wrong.

tools/testing/selftests/livepatch/Makefile line 4

Quoted 2 lines
tools/testing/selftests/livepatch/Makefile +2 -0
+ifndef KDIR
+LIVEPATCH_OBJTREE := $(firstword $(O) $(KBUILD_OUTPUT))

The above checks both $(O) $(KBUILD_OUTPUT)

tools/testing/selftests/livepatch/Makefile line 6

Quoted 2 lines
tools/testing/selftests/livepatch/Makefile +2 -0
+ifneq ($(and $(LIVEPATCH_OBJTREE),$(wildcard $(LIVEPATCH_OBJTREE)/Module.symvers)),)
+KDIR := $(LIVEPATCH_OBJTREE)

I am a bit confused. What is the KDIR when using, for example:

make O=/tmp/kselftest TARGETS="livepatch" kselftest

Is it in "/tmp" ?

tools/testing/selftests/livepatch/Makefile line 8

Quoted 2 lines
tools/testing/selftests/livepatch/Makefile +2 -0
+else ifneq ($(wildcard ../../../../Module.symvers),)
+KDIR := ../../../..

This seems to support building by calling:

   $ cd tools/tests/selftest/livepatch
   $ make

tools/testing/selftests/livepatch/Makefile line 10

Quoted 2 lines
tools/testing/selftests/livepatch/Makefile +2 -0
+else
+KDIR := /lib/modules/$(shell uname -r)/build

This supports the build with the devel files installed for the currently running kernel.

tools/testing/selftests/livepatch/Makefile line 18

Quoted 8 lines
tools/testing/selftests/livepatch/Makefile +8 -0
+endif
+endif
+override KDIR := $(abspath $(KDIR))
+export KDIR
+MAKEOVERRIDES := $(filter-out KDIR=%,$(MAKEOVERRIDES))
+
+LIVEPATCH_CONFIG := $(shell if [ -r "$(KDIR)/.config" ]; then \
+	grep -qx 'CONFIG_LIVEPATCH=y' "$(KDIR)/.config" && echo y || echo n; fi)

Why LIVEPATCH_CONFIG and not CONFIG_LIVEPATCH? IMHO, the reverse ordering adds more harm than good.

Quoted 9 lines
tools/testing/selftests/livepatch/Makefile +2 -0
 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 +35,12 @@ TEST_PROGS := \
 TEST_FILES := settings

tools/testing/selftests/livepatch/Makefile TEST_PROGS := \ · line 45

Quoted 10 lines
tools/testing/selftests/livepatch/Makefile +8 -0
 
 include ../lib.mk
+
+ifeq ($(LIVEPATCH_CONFIG),n)
+clean: clean_livepatch_mods
+
+clean_livepatch_mods:
+	$(Q)$(MAKE) -C test_modules clean
+
+.PHONY: clean_livepatch_mods

My understanding is that this will remove the test modules even LIVEPATCH_CONFIG became disabled in the meantime. I guess that it is related to the 3rd problem (puzzle) in the commit message.

It makes sense. I am surprised that we need an extra target for this.

Anyway, it would make sense to fix this in a separate patch.

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

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

This seems to be used only when _running_ the test. But this patch fixes _building_. It should be in a separate patch, maybe together with the check for CONFIG_LIVEPATCH.

Quoted 8 lines
tools/testing/selftests/livepatch/functions.sh +0 -0
 	has_kdir
 	push_config
 	set_dynamic_debug
tools/testing/selftests/livepatch/test_modules/Makefile +0 -0
index a13d398585dcb..62cb724c6b333 100644

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

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/Makefile +3 -1
@@ -16,14 +16,15 @@ 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)

Why is KBUILD_OUTPUT unset here?
It would deserve a comment.

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

Quoted 4 lines
tools/testing/selftests/livepatch/test_modules/Makefile +1 -1
 endif
 
-# Ensure that KDIR exists, otherwise skip the clean target
+# Any kernel tree can clean the test modules, so use the one containing them

Here is a comment. But what does "Any kernel tree" means? Could we be more specific, please?

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

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

Note that my workflow still works with this patch. I am testing the running kernel, have installed the devel files, and doing:

cd $KDIR/tools/tests/selftests/livepatch
make run_tests

But it would be great to better document which other scenarios are newly supported by this patch.

Best Regards,
Petr

Best Regards,
Petr

Quoted 1 line

LocalWords: cd

#4 lore raw

PATCH v3 2/2 selftests/livepatch: skip based on the test modules instead of KDIR

In reply to Sasha Levin

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

Fixes 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing")
---
 tools/testing/selftests/livepatch/functions.sh | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
tools/testing/selftests/livepatch/functions.sh +5 -11
index 8b26b70f92d8c..14fc9cade4de3 100644
@@ -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
maintainer
#5 lore raw

Re: [PATCH v3 2/2] selftests/livepatch: skip based on the test modules instead of KDIR

Quoted 9 lines · Sasha Levin

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

This is another a bit cryptic LLM generated commit message. IMHO, it will be much more strightforard to say that this patch allows to run livepatch selftests with pre-built test modules without a need to install kernel devel files.

Quoted 18 lines
Fixes 54ee3526796f ("selftests: livepatch: Avoid running the tests if kernel-devel is missing")
---
 tools/testing/selftests/livepatch/functions.sh | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
tools/testing/selftests/livepatch/functions.sh +0 -4
index 8b26b70f92d8c..14fc9cade4de3 100644
@@ -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"

tools/testing/selftests/livepatch/functions.sh function is_root() { · line 50

Quoted 10 lines
tools/testing/selftests/livepatch/functions.sh +4 -6
-	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"

I would prefer to keep also the original tests as a fallback and report:

echo "skip all tests: test modules are not available and KDIR ($KDIR) is not available to compile them."

Best Regards,
Petr