[Devel] [PATCH vz10 v3 2/2] selftests: build test modules against the kernel tree
Konstantin Khorenko
khorenko at virtuozzo.com
Thu Aug 13 20:58:06 MSK 2026
From: Eva Kurchatova <eva.kurchatova at virtuozzo.com>
Default KDIR to the kernel tree being packaged everywhere, or to its O=
/ KBUILD_OUTPUT build directory, when it is configured and built, that
is when include/config/auto.conf and Module.symvers are in place.
Otherwise keep the old default /lib/modules/$(uname -r)/build, so a bare
source checkout still builds against the running kernel.
This avoids building kselftests kernel modules against running kernel
during keernel packaging.
An O= build directory carries the generated configuration but no source
headers, so mm/Makefile, which decides whether to build page_frag_test.ko
by looking for include/linux/page_frag_cache.h below KDIR, has to look
through the "source" symlink of the build directory as well. Otherwise an
O= build skips the module and blames a too old kernel for it.
https://virtuozzo.atlassian.net/browse/VSTOR-139647
Feature: fix selftests
Signed-off-by: Eva Kurchatova <eva.kurchatova at virtuozzo.com>
---
.../testing/selftests/bpf/test_kmods/Makefile | 20 +++++++++++++-
.../selftests/cgroup/test_modules/Makefile | 18 +++++++++++++
.../selftests/livepatch/test_modules/Makefile | 18 +++++++++++++
tools/testing/selftests/mm/Makefile | 27 ++++++++++++++++++-
tools/testing/selftests/mm/page_frag/Makefile | 18 +++++++++++++
5 files changed, 99 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_kmods/Makefile b/tools/testing/selftests/bpf/test_kmods/Makefile
index d4e50c4509c93..65547f8570577 100644
--- a/tools/testing/selftests/bpf/test_kmods/Makefile
+++ b/tools/testing/selftests/bpf/test_kmods/Makefile
@@ -1,5 +1,23 @@
TEST_KMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
-KDIR ?= $(abspath $(TEST_KMOD_DIR)/../../../../..)
+
+# Build the modules against the kernel tree these tests are part of, or
+# its O= build directory, when that tree is configured and built; fall
+# back to the running kernel's build tree.
+KSRC_TREE := $(abspath $(TEST_KMOD_DIR)/../../../../..)
+KOUT := $(or $(O),$(KBUILD_OUTPUT))
+ifeq ($(KOUT),)
+KDIR_TREE := $(KSRC_TREE)
+else ifneq (,$(filter /%,$(KOUT)))
+KDIR_TREE := $(abspath $(KOUT))
+else
+KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
+endif
+ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
+ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
+KDIR ?= $(KDIR_TREE)
+endif
+endif
+KDIR ?= /lib/modules/$(shell uname -r)/build
ifeq ($(V),1)
Q =
diff --git a/tools/testing/selftests/cgroup/test_modules/Makefile b/tools/testing/selftests/cgroup/test_modules/Makefile
index 38121d783d921..6d6ce08335737 100644
--- a/tools/testing/selftests/cgroup/test_modules/Makefile
+++ b/tools/testing/selftests/cgroup/test_modules/Makefile
@@ -1,4 +1,22 @@
TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+
+# Build the modules against the kernel tree these tests are part of, or
+# its O= build directory, when that tree is configured and built; fall
+# back to the running kernel's build tree.
+KSRC_TREE := $(abspath $(TESTMODS_DIR)/../../../../..)
+KOUT := $(or $(O),$(KBUILD_OUTPUT))
+ifeq ($(KOUT),)
+KDIR_TREE := $(KSRC_TREE)
+else ifneq (,$(filter /%,$(KOUT)))
+KDIR_TREE := $(abspath $(KOUT))
+else
+KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
+endif
+ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
+ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
+KDIR ?= $(KDIR_TREE)
+endif
+endif
KDIR ?= /lib/modules/$(shell uname -r)/build
obj-m += cg_freezer_hang.o \
diff --git a/tools/testing/selftests/livepatch/test_modules/Makefile b/tools/testing/selftests/livepatch/test_modules/Makefile
index 34e2f76fb4cec..2820d4e444c61 100644
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile
@@ -1,4 +1,22 @@
TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+
+# Build the modules against the kernel tree these tests are part of, or
+# its O= build directory, when that tree is configured and built; fall
+# back to the running kernel's build tree.
+KSRC_TREE := $(abspath $(TESTMODS_DIR)/../../../../..)
+KOUT := $(or $(O),$(KBUILD_OUTPUT))
+ifeq ($(KOUT),)
+KDIR_TREE := $(KSRC_TREE)
+else ifneq (,$(filter /%,$(KOUT)))
+KDIR_TREE := $(abspath $(KOUT))
+else
+KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
+endif
+ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
+ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
+KDIR ?= $(KDIR_TREE)
+endif
+endif
KDIR ?= /lib/modules/$(shell uname -r)/build
obj-m += test_klp_atomic_replace.o \
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 3de23ea4663f7..c837b0549c892 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -36,9 +36,34 @@ MAKEFLAGS += --no-builtin-rules
CFLAGS = -Wall -I $(top_srcdir) $(EXTRA_CFLAGS) $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
LDLIBS = -lrt -lpthread -lm
+# Build the modules against the kernel tree these tests are part of, or
+# its O= build directory, when that tree is configured and built; fall
+# back to the running kernel's build tree.
+# Keep in sync with page_frag/Makefile, which lib.mk invokes separately.
+KSRC_TREE := $(abspath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../../../..)
+KOUT := $(or $(O),$(KBUILD_OUTPUT))
+ifeq ($(KOUT),)
+KDIR_TREE := $(KSRC_TREE)
+else ifneq (,$(filter /%,$(KOUT)))
+KDIR_TREE := $(abspath $(KOUT))
+else
+KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
+endif
+ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
+ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
+KDIR ?= $(KDIR_TREE)
+endif
+endif
KDIR ?= /lib/modules/$(shell uname -r)/build
+
+# In an O= build the headers stay in the source tree, which
+# outputmakefile links as $(objtree)/source.
+PAGE_FRAG_HDR := $(firstword $(wildcard \
+ $(KDIR)/include/linux/page_frag_cache.h \
+ $(KDIR)/source/include/linux/page_frag_cache.h))
+
ifneq (,$(wildcard $(KDIR)/Module.symvers))
-ifneq (,$(wildcard $(KDIR)/include/linux/page_frag_cache.h))
+ifneq (,$(PAGE_FRAG_HDR))
TEST_GEN_MODS_DIR := page_frag
else
PAGE_FRAG_WARNING = "missing page_frag_cache.h, please use a newer kernel"
diff --git a/tools/testing/selftests/mm/page_frag/Makefile b/tools/testing/selftests/mm/page_frag/Makefile
index 8c8bb39ffa282..43a2189a2c9f9 100644
--- a/tools/testing/selftests/mm/page_frag/Makefile
+++ b/tools/testing/selftests/mm/page_frag/Makefile
@@ -1,4 +1,22 @@
PAGE_FRAG_TEST_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+
+# Build the modules against the kernel tree these tests are part of, or
+# its O= build directory, when that tree is configured and built; fall
+# back to the running kernel's build tree.
+KSRC_TREE := $(abspath $(PAGE_FRAG_TEST_DIR)/../../../../..)
+KOUT := $(or $(O),$(KBUILD_OUTPUT))
+ifeq ($(KOUT),)
+KDIR_TREE := $(KSRC_TREE)
+else ifneq (,$(filter /%,$(KOUT)))
+KDIR_TREE := $(abspath $(KOUT))
+else
+KDIR_TREE := $(abspath $(KSRC_TREE)/$(KOUT))
+endif
+ifneq (,$(wildcard $(KDIR_TREE)/include/config/auto.conf))
+ifneq (,$(wildcard $(KDIR_TREE)/Module.symvers))
+KDIR ?= $(KDIR_TREE)
+endif
+endif
KDIR ?= /lib/modules/$(shell uname -r)/build
ifeq ($(V),1)
--
2.43.0
More information about the Devel
mailing list