On Wed, Sep 2, 2026 at 9:45 PM Petr Mladek [off-list ref] wrote:
On Tue 2026-08-25 19:46:38, Yafang Shao wrote:
quoted
The legacy "replace" field in struct klp_patch was replaced by the
provides/obsoletes mechanism. As a result, the atomic replace
selftests fail to build against kernels that only support
provides/obsoletes.
Adapt the selftests so that they build and run on both old and new
kernels:
- test_modules/Makefile detects whether the kernel header still
declares the "replace" field. If so, it passes KLP_HAS_REPLACE to
the modules via ccflags-y.
- test_klp_atomic_replace.c and other source files using .replace guard
their replace module parameter and .replace assignment with
#ifdef KLP_HAS_REPLACE. The #else branches are left as TODO
placeholders for the upcoming provides/obsoletes based tests.
- functions.sh adds detect_replace_attr(), which loads a temporary
livepatch and checks for the /sys/kernel/livepatch/<patch>/replace
attribute, setting HAS_REPLACE_ATTR accordingly.
- test-livepatch.sh and test-callbacks.sh run the tests that depend
on multiple livepatches coexisting or on the atomic replace
behavior ("multiple livepatches" and "atomic replace") only when
HAS_REPLACE_ATTR is set. These scenarios do not apply to the new
kernel, where provides=0 replaces any other livepatch by default.
The provides/obsoletes based selftests will be added later.
Assisted-by: Comagic:DeepSeek-V4-Flash
Signed-off-by: Yafang Shao <redacted>
---
tools/testing/selftests/livepatch/functions.sh | 15 +++++++++++++++
.../selftests/livepatch/test-callbacks.sh | 6 ++++++
.../selftests/livepatch/test-livepatch.sh | 6 ++++++
.../selftests/livepatch/test_modules/Makefile | 14 ++++++++++++++
.../test_modules/test_klp_atomic_replace.c | 16 ++++++++++++++++
.../test_modules/test_klp_callbacks_demo2.c | 16 ++++++++++++++++
.../livepatch/test_modules/test_klp_state.c | 7 +++++++
.../livepatch/test_modules/test_klp_state2.c | 7 +++++++
8 files changed, 87 insertions(+)
diff --git a/tools/testing/selftests/livepatch/functions.sh b/tools/testing/selftests/livepatch/functions.sh
index 30dc677b2f45..17810521d069 100644
--- a/tools/testing/selftests/livepatch/functions.sh
+++ b/tools/testing/selftests/livepatch/functions.sh
@@ -351,6 +351,21 @@ function does_sysfs_exist() {
[[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]]
}
+# detect_replace_attr() - detect whether the running kernel still exposes
+# the legacy "replace" sysfs attribute and set HAS_REPLACE_ATTR
+# accordingly. The atomic replace tests are only run when the attribute
+# is present. The provides/obsoletes based tests will be added later.
+function detect_replace_attr() {
+ HAS_REPLACE_ATTR=0
+
+ load_lp test_klp_livepatch
+ if does_sysfs_exist test_klp_livepatch "replace"; then
+ HAS_REPLACE_ATTR=1
+ fi
+ disable_lp test_klp_livepatch
+ unload_lp test_klp_livepatch
+}
+
# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs
# path permissions
# modname - livepatch module creating the sysfs interface
--- a/tools/testing/selftests/livepatch/test_modules/Makefile
+++ b/tools/testing/selftests/livepatch/test_modules/Makefile@@ -16,6 +16,20 @@ obj-m += test_klp_atomic_replace.o \
test_klp_state3.o \
test_klp_syscall.o
+# The legacy "replace" field was replaced by provides/obsoletes. When the
+# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the
+# modules so that they can #ifdef the legacy "replace" code. Otherwise
+# the modules are built with the provides/obsoletes based code (to be
+# added later).
+KLP_SRC := $(if $(wildcard $(srctree)/include/linux/livepatch.h), \
+ $(srctree), $(KDIR))
+KLP_HAS_REPLACE := $(shell grep -q 'bool replace' \
+ $(KLP_SRC)/include/linux/livepatch.h 2>/dev/null \
+ && echo y)
+ifeq ($(KLP_HAS_REPLACE),y)
+ccflags-y += -DKLP_HAS_REPLACE
+endif
+
# Ensure that KDIR exists, otherwise skip the compilation
modules:
ifneq ("$(wildcard $(KDIR))", "")
These two ways to detect the feature are dirty hacks. It might be
acceptable in selftests. But I wonder if we could do better.
Alternative solution might be to define:
HAVE_KLP_PROVIDES as true in kernel/livepatch/Kconfig
We could then use in the Makefile:
ifeq ($(HAVE_KLP_PROVIDES),y)
ccflags-y += -DHAVE_KLP_PROVIDES
endif
And we could zgrep /proc/config.gz in the shell scripts.
For example, see kconfig_has() in
tools/testing/selftests/firmware/fw_lib.sh
Note 1: It will work only when we change the state of HAVE_KLP_PROVIDES
in the patch when the provides/obsoletes are substituted.
Note 2: We should update the selftests before we change the behavior.
Otherwise, we would break bisection.
Good point.
I will update it.
quoted
diff --git a/tools/testing/selftests/livepatch/test-callbacks.sh b/tools/testing/selftests/livepatch/test-callbacks.sh
index 2a03deb26a12..e9b2dc6ea099 100755
--- a/tools/testing/selftests/livepatch/test-callbacks.sh
+++ b/tools/testing/selftests/livepatch/test-callbacks.sh
@@ -451,6 +453,7 @@ $MOD_TARGET_BUSY: busymod_work_func exit
$MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit"
+if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then
# Test loading multiple livepatches. This test-case is mainly for comparing
# with the next test-case.
#
Nit: Marcos made several tests conditional and he updated indentation
after adding the if/fi.
I could live with both approaches. It just would be nice to be
consistent.
I will update it
And I like the idea to explicitly mention that a test has been
skipped because a feature is not available.
Best Regards,
Petr
--
Regards
Yafang