Skip to contentSkip to conversation

livepatch: Add support for scoped atomic replace

PATCH Superseded Yafang Shao 3 participants 23 messages Oct 16 – Oct 25

Subsystems (3)

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v12 cover livepatch: Add support for scoped atomic replace

Atomic replacement is currently all-or-nothing: a livepatch with "replace" set either atomically replaces all previously installed livepatches, or - with "replace" disabled - it replaces none. There is no way to atomically replace only a selected set of livepatches while keeping the rest running untouched.

We previously proposed a BPF+livepatch method to enable rapid experimentation with new kernel features without interrupting production workloads:

https://lore.kernel.org/live-patching/[email protected]/

In the resulting discussion, Song and Petr suggested that it should be possible to selectively replace or skip individual livepatches.

This patchset introduces a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the replacement scope of the livepatch. Livepatches that share the same provides id replace each other, so at most one livepatch of each provides id can be enabled at a time. By default (provides=0), every livepatch that does not declare an explicit provides id belongs to the same scope:
loading a new provides=0 livepatch atomically replaces the previously enabled one.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that satisfies either of the following conditions:

1. it has the same provides id as the new patch,
2. its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

Previously, setting 'replace' to 0 was the only way to keep certain livepatches persistent on the system, forcing developers to disable atomic replacement entirely. With the introduction of provides and obsoletes, developers now have a selective option to keep specific livepatches persistent while maintaining atomic replacement capabilities elsewhere.

IMPORTANT:
- this design deprecates the traditional non-atomic-replace model.
- the behavior of replacing all non-atomic-replace livepatches with a single atomic-replace livepatch is also deprecated.

The new "provides" and "obsoletes" attributes are exposed through sysfs and the kselftests cover the replacement and coexistence semantics described above.

At present, KLP state, shadow variables, and callbacks are not integrated with the new provides/obsoletes mechanism in this patchset. Support for these features is deferred until Petr's klp-state-transfer infrastructure is completed and merged:

https://github.com/pmladek/linux/tree/klp-state-transfer-v1-iter12

It is based on livepatching tree's for-next branch.

Future work
===========

For backward compatibility with the old non-atomic-replace model, we might consider adding a new "noreplace" flag. A livepatch with this flag set would not replace any other livepatch, but it could still be replaced by an atomic-replace livepatch. This would preserve the behavior of the original non-atomic-replace model.

We can revisit this once a real use case for the non-atomic-replace model emerges.

Changes
=======

v11->v12:
Comments from Petr after the LPC discussion:
- document the rollback pitfalls in #5
- warn about installing more livepatches in parallel in #5

v11: https://lore.kernel.org/live-patching/[email protected]/

v10->v11:
- check $HAS_PROVIDES_ATTR positively in #2 (Petr)
- document the conflict semantics in #5 (Petr)
- per the LPC discussion, drop the symmetric obsoletes semantics

v10: https://lore.kernel.org/live-patching/[email protected]/

v9->v10:
Various comments from Miroslav:
- rename skip() to skip_exit() and skip_test() to skip() in #2
- define CONFIG_KLP_HAS_PROVIDES in #2
- add a comment about the CONFIG_IKCONFIG_PROC fallback in #2
- adjust the order around CONFIG_KLP_HAS_PROVIDES
- update the comment above klp_add_nops() and klp_unpatch_replaced_patches()
- add verification step for provides 0 coexists with provides 1 in #7

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

v8->v9:
- extract the replace-related test scenarios into functions in patch #2 (Song)
- make the state module parameters read-only to avoid a potential state leak if tampered with after load (sashiko-bot)
- verify that /proc/meminfo is no longer patched after the atomic replacement (sashiko-bot)
- unload the coexisting state livepatches in LIFO order so that the console_loglevel is properly restored (sashiko-bot)

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

v7->v8:
- fix the commit log and documentation regarding --obsoletes (sashiko-bot)
- remove the --obsoletes validation requirement from klp-build (Josh)
- explicitly log the skip info for the deprecated replace attribute on the new kernel
- implement symmetric obsoletes (Petr)
- rename test module files for clarity (Petr)
- add CONFIG_KLP_HAS_PROVIDES (Petr)
- avoid duplicate test module source files (Petr)
- documentation and commit log improvement (Petr)
- add more selftests for provides/obsoletes (Petr)
- other code cleanups (Petr, Josh)

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

v6->v7:
- rebase it to livepatching's for-next branch
- rename klp_patch_replaceable() to klp_patch_replaces() (Song)
- remove "[]" around --obsoletes (Song)

v6: https://lore.kernel.org/live-patching/[email protected]/

v5->v6:
- Check --provides argument in `klp-build (sashiko)
- Fix the 'replace' feature detection for OOT kernel builds (sashiko)
- Fix race condition in sysfs polling (sashiko)

v5: https://lore.kernel.org/live-patching/[email protected]

v4(RFC)->v5:
- Add selftests and Remove the RFC
- Fmprove klp_has_function_conflict() (Song)
- Fix a pre-exisiting bug
- Fix bugs reported by sashiko

v4 (RFC): https://lore.kernel.org/live-patching/[email protected]/

v3->v4(RFC):
- Allow a livepatch to replace livepatches with different provides IDs. Replace the single replace_set field with two separate fields, provides and obsoletes, for more flexible replacement semantics. (Petr, Joe)

v3: https://lore.kernel.org/live-patching/[email protected]/

v2->v3:
- Address the feedback from Sachiko AI
- Fix the pre-existing NULL pointer dereference issue
- Move klp_find_func into core.h
- Don't deprecate stack_order completely

v2: https://lore.kernel.org/live-patching/[email protected]/

v1->v2:
- Incorporate feedback from Petr:
- Initialize replace_set to 0 by default
- Improve documentation
- Enforce that livepatches in different replace_sets cannot use the same

    state->id.

- Enforce that livepatches in different replace_sets cannot modify the same function.
- Ensure consistent capitalization and naming usage of KLP_REPLACE_SET.
- Incorporate feedback from Sachiko AI:
- Skip the klp_transition patch during klp_force_transition().

v1 (RFC): https://lore.kernel.org/live-patching/[email protected]/

Yafang Shao (9):
selftests/livepatch: Clarify test module file names selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
livepatch: Make klp_find_func() non static
livepatch: Call klp_init_patch_early() earlier
livepatch: Implement provides and obsoletes for scoped atomic replace
livepatch: Deprecate stack_order
selftests/livepatch: Add provides/obsoletes test scenarios selftests/livepatch: Add state test for provides/obsoletes selftests/livepatch: Add function test for provides/obsoletes

 .../ABI/removed/sysfs-kernel-livepatch        |  16 +
 .../ABI/testing/sysfs-kernel-livepatch        |  27 +-
 .../livepatch/cumulative-patches.rst          | 127 +++-
 Documentation/livepatch/livepatch.rst         |  29 +-
 include/linux/livepatch.h                     |   8 +-
 kernel/livepatch/Kconfig                      |  14 +
 kernel/livepatch/core.c                       | 124 ++--
 kernel/livepatch/core.h                       |   2 +
 kernel/livepatch/state.c                      |  57 +-
 kernel/livepatch/transition.c                 |  15 +-
 scripts/livepatch/init.c                      |  19 +-
 scripts/livepatch/klp-build                   |  23 +-
 tools/testing/selftests/livepatch/Makefile    |   3 +-
 .../testing/selftests/livepatch/functions.sh  |  42 +-
 .../selftests/livepatch/test-callbacks.sh     |  43 +-
 .../selftests/livepatch/test-ftrace.sh        |   2 +-
 .../selftests/livepatch/test-kprobe.sh        |  14 +-
 .../selftests/livepatch/test-livepatch.sh     | 108 +--
 .../livepatch/test-provides-obsoletes.sh      | 656 ++++++++++++++++++
 .../selftests/livepatch/test-syscall.sh       |   2 +-
 .../testing/selftests/livepatch/test-sysfs.sh |   6 +-
 .../selftests/livepatch/test_modules/Makefile |   9 +-
 .../test_modules/test_klp_callbacks_demo2.c   |  12 +
 ...est_klp_kprobe.c => test_klp_cmdline_kp.c} |  10 +-
 ..._klp_livepatch.c => test_klp_cmdline_lp.c} |  21 +-
 ...atomic_replace.c => test_klp_meminfo_lp.c} |  32 +-
 .../test_modules/test_klp_meminfo_lp2.c       |   2 +
 .../livepatch/test_modules/test_klp_state.c   |  40 +-
 .../livepatch/test_modules/test_klp_state2.c  |  48 +-
 ...lp_syscall.c => test_klp_syscall_getpid.c} |   4 +-
 30 files changed, 1270 insertions(+), 245 deletions(-)
 create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
 create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_kprobe.c => test_klp_cmdline_kp.c} (78%)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_livepatch.c => test_klp_cmdline_lp.c} (66%)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_atomic_replace.c => test_klp_meminfo_lp.c} (57%)
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_syscall.c => test_klp_syscall_getpid.c} (95%)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v12 1/9 selftests/livepatch: Clarify test module file names

Rename the test module files to be more descriptive of what they actually test, as suggested by Petr.

---
 tools/testing/selftests/livepatch/test-ftrace.sh     |  2 +-
 tools/testing/selftests/livepatch/test-kprobe.sh     | 12 ++++++------
 tools/testing/selftests/livepatch/test-livepatch.sh  |  6 +++---
 tools/testing/selftests/livepatch/test-syscall.sh    |  2 +-
 tools/testing/selftests/livepatch/test-sysfs.sh      |  6 +++---
 .../selftests/livepatch/test_modules/Makefile        |  8 ++++----
 .../{test_klp_kprobe.c => test_klp_cmdline_kp.c}     | 10 +++++-----
 .../{test_klp_livepatch.c => test_klp_cmdline_lp.c}  | 10 +++++-----
 ...st_klp_atomic_replace.c => test_klp_meminfo_lp.c} | 10 +++++-----
 ...{test_klp_syscall.c => test_klp_syscall_getpid.c} |  4 ++--
 10 files changed, 35 insertions(+), 35 deletions(-)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_kprobe.c => test_klp_cmdline_kp.c} (78%)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_livepatch.c => test_klp_cmdline_lp.c} (79%)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_atomic_replace.c => test_klp_meminfo_lp.c} (82%)
 rename tools/testing/selftests/livepatch/test_modules/{test_klp_syscall.c => test_klp_syscall_getpid.c} (95%)
tools/testing/selftests/livepatch/test-ftrace.sh +1 -1
index d2c3dea63104..90e8fcc4d065 100755
@@ -4,7 +4,7 @@
 
 . $(dirname $0)/functions.sh
 
-MOD_LIVEPATCH=test_klp_livepatch
+MOD_LIVEPATCH=test_klp_cmdline_lp
 
 setup_config
 
tools/testing/selftests/livepatch/test-kprobe.sh +6 -6
index 7ced4082cff3..81ab6d4760aa 100755
@@ -7,8 +7,8 @@
 
 grep -q kprobe_ftrace_ops /proc/kallsyms || skip "test-kprobe requires CONFIG_KPROBES_ON_FTRACE"
 
-MOD_LIVEPATCH=test_klp_livepatch
-MOD_KPROBE=test_klp_kprobe
+MOD_LIVEPATCH=test_klp_cmdline_lp
+MOD_KPROBE=test_klp_cmdline_kp
 
 setup_config
 
@@ -24,7 +24,7 @@ load_mod $MOD_KPROBE has_post_handler=y
 load_failing_mod $MOD_LIVEPATCH
 unload_mod $MOD_KPROBE
 
-check_result "% insmod test_modules/test_klp_kprobe.ko has_post_handler=y
+check_result "% insmod test_modules/$MOD_KPROBE.ko has_post_handler=y
 % insmod test_modules/$MOD_LIVEPATCH.ko
 livepatch: enabling patch '$MOD_LIVEPATCH'
 livepatch: '$MOD_LIVEPATCH': initializing patching transition
@@ -35,7 +35,7 @@ livepatch: '$MOD_LIVEPATCH': canceling patching transition, going to unpatch
 livepatch: '$MOD_LIVEPATCH': completing unpatching transition
 livepatch: '$MOD_LIVEPATCH': unpatching complete
 insmod: ERROR: could not insert module test_modules/$MOD_LIVEPATCH.ko: Device or resource busy
-% rmmod test_klp_kprobe"
+% rmmod $MOD_KPROBE"
 
 start_test "livepatch interaction with kprobed function without post_handler"
 
@@ -46,14 +46,14 @@ unload_mod $MOD_KPROBE
 disable_lp $MOD_LIVEPATCH
 unload_lp $MOD_LIVEPATCH
 
-check_result "% insmod test_modules/test_klp_kprobe.ko has_post_handler=n
+check_result "% insmod test_modules/$MOD_KPROBE.ko has_post_handler=n
 % insmod test_modules/$MOD_LIVEPATCH.ko
 livepatch: enabling patch '$MOD_LIVEPATCH'
 livepatch: '$MOD_LIVEPATCH': initializing patching transition
 livepatch: '$MOD_LIVEPATCH': starting patching transition
 livepatch: '$MOD_LIVEPATCH': completing patching transition
 livepatch: '$MOD_LIVEPATCH': patching complete
-% rmmod test_klp_kprobe
+% rmmod $MOD_KPROBE
 % echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
 livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
 livepatch: '$MOD_LIVEPATCH': starting unpatching transition
tools/testing/selftests/livepatch/test-livepatch.sh +3 -3
index c44c5341a2f1..5380b5f174d4 100755
@@ -4,10 +4,10 @@
 
 . $(dirname $0)/functions.sh
 
-MOD_LIVEPATCH1=test_klp_livepatch
-MOD_LIVEPATCH2=test_klp_syscall
+MOD_LIVEPATCH1=test_klp_cmdline_lp
+MOD_LIVEPATCH2=test_klp_syscall_getpid
 MOD_LIVEPATCH3=test_klp_callbacks_demo
-MOD_REPLACE=test_klp_atomic_replace
+MOD_REPLACE=test_klp_meminfo_lp
 MOD_TARGET=test_klp_mod_target
 MOD_TARGET_PATCH=test_klp_mod_patch
 
tools/testing/selftests/livepatch/test-syscall.sh +1 -1
index 5f9344277b62..488207f25c50 100755
@@ -5,7 +5,7 @@
 
 . $(dirname $0)/functions.sh
 
-MOD_SYSCALL=test_klp_syscall
+MOD_SYSCALL=test_klp_syscall_getpid
 
 setup_config
 
tools/testing/selftests/livepatch/test-sysfs.sh +3 -3
index 3b16285c6e67..32d3af0097ac 100755
@@ -4,9 +4,9 @@
 
 . $(dirname $0)/functions.sh
 
-MOD_LIVEPATCH=test_klp_livepatch
+MOD_LIVEPATCH=test_klp_cmdline_lp
 MOD_LIVEPATCH2=test_klp_callbacks_demo
-MOD_LIVEPATCH3=test_klp_syscall
+MOD_LIVEPATCH3=test_klp_syscall_getpid
 
 HAS_PATCH_ATTR=0
 HAS_REPLACE_ATTR=0
@@ -108,7 +108,7 @@ fi
 if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then
 	start_test "sysfs test replace enabled"
 
-	MOD_ATOMIC_REPLACE=test_klp_atomic_replace
+	MOD_ATOMIC_REPLACE=test_klp_meminfo_lp
 	load_lp $MOD_ATOMIC_REPLACE replace=1
 
 	check_sysfs_rights "$MOD_ATOMIC_REPLACE" "replace" "-r--r--r--"
tools/testing/selftests/livepatch/test_modules/Makefile +4 -4
index a13d398585dc..732e0003930b 100644
@@ -1,20 +1,20 @@
 TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
 KDIR ?= /lib/modules/$(shell uname -r)/build
 
-obj-m += test_klp_atomic_replace.o \
+obj-m += test_klp_meminfo_lp.o \
 	test_klp_callbacks_busy.o \
 	test_klp_callbacks_demo.o \
 	test_klp_callbacks_demo2.o \
 	test_klp_callbacks_mod.o \
-	test_klp_kprobe.o \
-	test_klp_livepatch.o \
+	test_klp_cmdline_kp.o \
+	test_klp_cmdline_lp.o \
 	test_klp_mod_patch.o \
 	test_klp_mod_target.o \
 	test_klp_shadow_vars.o \
 	test_klp_state.o \
 	test_klp_state2.o \
 	test_klp_state3.o \
-	test_klp_syscall.o
+	test_klp_syscall_getpid.o
 
 # Ensure that KDIR exists, otherwise skip the compilation
 modules:
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_kp.c renamed from tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c +5 -5
similarity index 78%
rename from tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c
rename to tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_kp.c
index 67a8d29012f6..28ef7ea33fc7 100644
@@ -18,7 +18,7 @@ static struct kprobe kp = {
 	.symbol_name = "cmdline_proc_show",
 };
 
-static int __init kprobe_init(void)
+static int __init cmdline_kp_init(void)
 {
 	if (has_post_handler)
 		kp.post_handler = post_handler;
@@ -26,13 +26,13 @@ static int __init kprobe_init(void)
 	return register_kprobe(&kp);
 }
 
-static void __exit kprobe_exit(void)
+static void __exit cmdline_kp_exit(void)
 {
 	unregister_kprobe(&kp);
 }
 
-module_init(kprobe_init)
-module_exit(kprobe_exit)
+module_init(cmdline_kp_init)
+module_exit(cmdline_kp_exit)
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Michael Vetter <[email protected]>");
-MODULE_DESCRIPTION("Livepatch test: kprobe function");
+MODULE_DESCRIPTION("Livepatch test: cmdline kprobe");
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c renamed from tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c +5 -5
similarity index 79%
rename from tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c
rename to tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c
index aff08199de71..88c6aed925fb 100644
@@ -34,18 +34,18 @@ static struct klp_patch patch = {
 	.objs = objs,
 };
 
-static int test_klp_livepatch_init(void)
+static int test_klp_cmdline_lp_init(void)
 {
 	return klp_enable_patch(&patch);
 }
 
-static void test_klp_livepatch_exit(void)
+static void test_klp_cmdline_lp_exit(void)
 {
 }
 
-module_init(test_klp_livepatch_init);
-module_exit(test_klp_livepatch_exit);
+module_init(test_klp_cmdline_lp_init);
+module_exit(test_klp_cmdline_lp_exit);
 MODULE_LICENSE("GPL");
 MODULE_INFO(livepatch, "Y");
 MODULE_AUTHOR("Seth Jennings <[email protected]>");
-MODULE_DESCRIPTION("Livepatch test: livepatch module");
+MODULE_DESCRIPTION("Livepatch test: cmdline livepatch");
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c renamed from tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c +5 -5
similarity index 82%
rename from tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c
rename to tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c
index 5af7093ca00c..f2477b217aab 100644
@@ -39,19 +39,19 @@ static struct klp_patch patch = {
 	/* set .replace in the init function below for demo purposes */
 };
 
-static int test_klp_atomic_replace_init(void)
+static int test_klp_meminfo_lp_init(void)
 {
 	patch.replace = replace;
 	return klp_enable_patch(&patch);
 }
 
-static void test_klp_atomic_replace_exit(void)
+static void test_klp_meminfo_lp_exit(void)
 {
 }
 
-module_init(test_klp_atomic_replace_init);
-module_exit(test_klp_atomic_replace_exit);
+module_init(test_klp_meminfo_lp_init);
+module_exit(test_klp_meminfo_lp_exit);
 MODULE_LICENSE("GPL");
 MODULE_INFO(livepatch, "Y");
 MODULE_AUTHOR("Joe Lawrence <[email protected]>");
-MODULE_DESCRIPTION("Livepatch test: atomic replace");
+MODULE_DESCRIPTION("Livepatch test: meminfo livepatch");
tools/testing/selftests/livepatch/test_modules/test_klp_syscall_getpid.c renamed from tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c +2 -2
similarity index 95%
rename from tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c
rename to tools/testing/selftests/livepatch/test_modules/test_klp_syscall_getpid.c
index 08aacc0e14de..5da2bc7920cc 100644
@@ -93,7 +93,7 @@ static int livepatch_init(void)
 {
 	int ret;
 
-	klp_kobj = kobject_create_and_add("test_klp_syscall", kernel_kobj);
+	klp_kobj = kobject_create_and_add("test_klp_syscall_getpid", kernel_kobj);
 	if (!klp_kobj)
 		return -ENOMEM;
 
@@ -128,4 +128,4 @@ MODULE_INFO(livepatch, "Y");
 MODULE_AUTHOR("Libor Pechacek <[email protected]>");
 MODULE_AUTHOR("Nicolai Stange <[email protected]>");
 MODULE_AUTHOR("Marcos Paulo de Souza <[email protected]>");
-MODULE_DESCRIPTION("Livepatch test: syscall transition");
+MODULE_DESCRIPTION("Livepatch test: syscall getpid transition");
Signature · 1 line
2.52.0
#2 lore raw

PATCH v12 2/9 selftests/livepatch: Adapt atomic replace tests to provides/obsoletes

In reply to Yafang Shao

The legacy "replace" field in struct klp_patch will be 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. On kernels without the legacy "replace" support, the replace-related test cases are skipped with a SKIP message instead of being run.

Also introduce the CONFIG_KLP_HAS_PROVIDES compile-time marker in kernel/livepatch/Kconfig. It defaults to n and will be set to y once the provides/obsoletes support is implemented later in this series.

The provides/obsoletes-based selftests will be added later in this series, after the legacy "replace" field has been substituted by the new mechanism.

---
 kernel/livepatch/Kconfig                      |  14 +++
 .../testing/selftests/livepatch/functions.sh  |  42 +++++++-
 .../selftests/livepatch/test-callbacks.sh     |  43 +++++---
 .../selftests/livepatch/test-kprobe.sh        |   2 +-
 .../selftests/livepatch/test-livepatch.sh     | 102 ++++++++++--------
 .../test_modules/test_klp_callbacks_demo2.c   |  16 +++
 .../test_modules/test_klp_meminfo_lp.c        |  16 +++
 .../livepatch/test_modules/test_klp_state.c   |   7 ++
 .../livepatch/test_modules/test_klp_state2.c  |   7 ++
 9 files changed, 186 insertions(+), 63 deletions(-)
kernel/livepatch/Kconfig +14 -0
index 4c0a9c18d0b2..d1eb971abb1a 100644
@@ -30,3 +30,17 @@ config KLP_BUILD
 	select OBJTOOL
 	help
 	  Enable klp-build support
+
+config KLP_HAS_PROVIDES
+	def_bool n
+	help
+	  Compile-time marker for the livepatch provides/obsoletes support.
+	  It is always set when the kernel is built with the scoped atomic
+	  replace model, i.e. struct klp_patch provides the "provides" and
+	  "obsoletes" fields instead of the legacy "replace" flag.
+
+	  The option itself does not enable or add any functionality; it
+	  only serves as a compile-time marker so that out-of-tree modules
+	  and tools (e.g. the livepatch kselftests) can detect kernels that
+	  speak the provides/obsoletes interface and adapt to it with
+	  #ifdef CONFIG_KLP_HAS_PROVIDES.
tools/testing/selftests/livepatch/functions.sh +39 -3

66 lines not on this page · raw message

tools/testing/selftests/livepatch/test-callbacks.sh +28 -15

77 lines not on this page · raw message

tools/testing/selftests/livepatch/test-kprobe.sh +1 -1
index 81ab6d4760aa..75d9093c1742 100755
@@ -5,7 +5,7 @@
 
 . $(dirname $0)/functions.sh
 
-grep -q kprobe_ftrace_ops /proc/kallsyms || skip "test-kprobe requires CONFIG_KPROBES_ON_FTRACE"
+grep -q kprobe_ftrace_ops /proc/kallsyms || skip_exit "test-kprobe requires CONFIG_KPROBES_ON_FTRACE"
 
 MOD_LIVEPATCH=test_klp_cmdline_lp
 MOD_KPROBE=test_klp_cmdline_kp
tools/testing/selftests/livepatch/test-livepatch.sh +58 -44

153 lines not on this page · raw message

tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +16 -0
index 5417573e80af..6c46ce575f5c 100644
@@ -7,9 +7,16 @@
 #include <linux/kernel.h>
 #include <linux/livepatch.h>
 
+#ifdef CONFIG_KLP_HAS_PROVIDES
+/*
+ * TODO: Add provides/obsoletes module parameters for the
+ * provides/obsoletes based tests (to be added later).
+ */
+#else
 static int replace;
 module_param(replace, int, 0644);
 MODULE_PARM_DESC(replace, "replace (default=0)");
+#endif
 
 static const char *const module_state[] = {
 	[MODULE_STATE_LIVE]	= "[MODULE_STATE_LIVE] Normal state",
@@ -72,12 +79,21 @@ static struct klp_object objs[] = {
 static struct klp_patch patch = {
 	.mod = THIS_MODULE,
 	.objs = objs,
+#ifndef CONFIG_KLP_HAS_PROVIDES
 	/* set .replace in the init function below for demo purposes */
+#endif
 };
 
 static int test_klp_callbacks_demo2_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	/*
+	 * TODO: Set provides/obsoletes from the module parameters
+	 * for the provides/obsoletes based tests (to be added later).
+	 */
+#else
 	patch.replace = replace;
+#endif
 	return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c +16 -0
index f2477b217aab..0c434a27672d 100644
@@ -7,9 +7,16 @@
 #include <linux/kernel.h>
 #include <linux/livepatch.h>
 
+#ifdef CONFIG_KLP_HAS_PROVIDES
+/*
+ * TODO: Add provides/obsoletes module parameters for the
+ * provides/obsoletes based tests (to be added later).
+ */
+#else
 static int replace;
 module_param(replace, int, 0644);
 MODULE_PARM_DESC(replace, "replace (default=0)");
+#endif
 
 #include <linux/seq_file.h>
 static int livepatch_meminfo_proc_show(struct seq_file *m, void *v)
@@ -36,12 +43,21 @@ static struct klp_object objs[] = {
 static struct klp_patch patch = {
 	.mod = THIS_MODULE,
 	.objs = objs,
+#ifndef CONFIG_KLP_HAS_PROVIDES
 	/* set .replace in the init function below for demo purposes */
+#endif
 };
 
 static int test_klp_meminfo_lp_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	/*
+	 * TODO: Set provides/obsoletes from the module parameters
+	 * for the provides/obsoletes based tests (to be added later).
+	 */
+#else
 	patch.replace = replace;
+#endif
 	return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +7 -0
index 57a4253acb01..4d419a58cf65 100644
@@ -142,7 +142,14 @@ static struct klp_patch patch = {
 	.mod = THIS_MODULE,
 	.objs = objs,
 	.states = states,
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	/*
+	 * TODO: Add provides/obsoletes module parameters for the
+	 * provides/obsoletes based tests (to be added later).
+	 */
+#else
 	.replace = true,
+#endif
 };
 
 static int test_klp_callbacks_demo_init(void)
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +7 -0
index c978ea4d5e67..4f70f0619967 100644
@@ -171,7 +171,14 @@ static struct klp_patch patch = {
 	.mod = THIS_MODULE,
 	.objs = objs,
 	.states = states,
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	/*
+	 * TODO: Add provides/obsoletes module parameters for the
+	 * provides/obsoletes based tests (to be added later).
+	 */
+#else
 	.replace = true,
+#endif
 };
 
 static int test_klp_callbacks_demo_init(void)
Signature · 1 line
2.52.0
#3 lore raw

PATCH v12 3/9 livepatch: Make klp_find_func() non static

In reply to Yafang Shao

Make klp_find_func() non static to allow its use in other source files by an upcoming patch.

While at it, rename the parameter @old_func to @func to better reflect its generic purpose.

---
 kernel/livepatch/core.c | 18 +++++++++---------
 kernel/livepatch/core.h |  1 +
 2 files changed, 10 insertions(+), 9 deletions(-)
kernel/livepatch/core.c +9 -9
index 517fe427ff92..89251fafe86e 100644
@@ -82,21 +82,21 @@ static bool klp_initialized(void)
 	return !!klp_root_kobj;
 }
 
-static struct klp_func *klp_find_func(struct klp_object *obj,
-				      struct klp_func *old_func)
+/* Check if @func is present in @obj. */
+struct klp_func *klp_find_func(struct klp_object *obj, struct klp_func *func)
 {
-	struct klp_func *func;
+	struct klp_func *obj_func;
 
-	klp_for_each_func(obj, func) {
+	klp_for_each_func(obj, obj_func) {
 		/*
 		 * Besides identical old_sympos, also consider old_sympos
 		 * of 0 and 1 are identical.
 		 */
-		if ((strcmp(old_func->old_name, func->old_name) == 0) &&
-		    ((old_func->old_sympos == func->old_sympos) ||
-		     (old_func->old_sympos == 0 && func->old_sympos == 1) ||
-		     (old_func->old_sympos == 1 && func->old_sympos == 0))) {
-			return func;
+		if ((strcmp(obj_func->old_name, func->old_name) == 0) &&
+		    ((obj_func->old_sympos == func->old_sympos) ||
+		     (obj_func->old_sympos == 0 && func->old_sympos == 1) ||
+		     (obj_func->old_sympos == 1 && func->old_sympos == 0))) {
+			return obj_func;
 		}
 	}
 
kernel/livepatch/core.h +1 -0
index 38209c7361b6..361a0917a03f 100644
@@ -17,6 +17,7 @@ void klp_free_patch_async(struct klp_patch *patch);
 void klp_free_replaced_patches_async(struct klp_patch *new_patch);
 void klp_unpatch_replaced_patches(struct klp_patch *new_patch);
 void klp_discard_nops(struct klp_patch *new_patch);
+struct klp_func *klp_find_func(struct klp_object *obj, struct klp_func *func);
 
 static inline bool klp_is_object_loaded(struct klp_object *obj)
 {
Signature · 1 line
2.52.0
#4 lore raw

PATCH v12 4/9 livepatch: Call klp_init_patch_early() earlier

In reply to Yafang Shao

Invoke klp_init_patch_early() during early initialization to avoid mixing for_each_() and for_each_*_static() variants later in the code.

No functional change. This prepares the code for an upcoming patch.

---
 kernel/livepatch/core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/livepatch/core.c +7 -7
index 89251fafe86e..c0879b5d9dc0 100644
@@ -1155,13 +1155,6 @@ int klp_enable_patch(struct klp_patch *patch)
 
 	mutex_lock(&klp_mutex);
 
-	if (!klp_is_patch_compatible(patch)) {
-		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
-			patch->mod->name);
-		mutex_unlock(&klp_mutex);
-		return -EINVAL;
-	}
-
 	if (!try_module_get(patch->mod)) {
 		mutex_unlock(&klp_mutex);
 		return -ENODEV;
@@ -1169,6 +1162,13 @@ int klp_enable_patch(struct klp_patch *patch)
 
 	klp_init_patch_early(patch);
 
+	if (!klp_is_patch_compatible(patch)) {
+		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
+			patch->mod->name);
+		ret = -EINVAL;
+		goto err;
+	}
+
 	ret = klp_init_patch(patch);
 	if (ret)
 		goto err;
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH v12 4/9] livepatch: Call klp_init_patch_early() earlier

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] livepatch: UAF on module unload if klp_is_patch_compatible() fails with DEBUG_KOBJECT_RELEASE --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] livepatch: UAF on module unload if klp_is_patch_compatible() fails with DEBUG_KOBJECT_RELEASE --

commit bd925cd8bf4694e6331eda23e1ebe254ec598efe
Author: Yafang Shao <[email protected]>

livepatch: Call klp_init_patch_early() earlier

Invoke klp_init_patch_early() during early initialization to avoid mixing for_each_() and for_each_*_static() variants later in the code. This prepares the code for an upcoming patch.

Quoted 19 lines
kernel/livepatch/core.c +0 -7
index c34306ecfb0bb..f088955de8557 100644
@@ -1145,13 +1145,6 @@ int klp_enable_patch(struct klp_patch *patch)
 
 	mutex_lock(&klp_mutex);
 
-	if (!klp_is_patch_compatible(patch)) {
-		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
-			patch->mod->name);
-		mutex_unlock(&klp_mutex);
-		return -EINVAL;
-	}
-
 	if (!try_module_get(patch->mod)) {
 		mutex_unlock(&klp_mutex);
 		return -ENODEV;

kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1159

Quoted 8 lines
kernel/livepatch/core.c +5 -0
@@ -1159,6 +1152,13 @@ int klp_enable_patch(struct klp_patch *patch)
 
 	klp_init_patch_early(patch);
 
+	if (!klp_is_patch_compatible(patch)) {
+		pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
+			patch->mod->name);
+		ret = -EINVAL;
+		goto err;

[Severity: Medium]
Can this sequence lead to a use-after-free regression during module unload if CONFIG_DEBUG_KOBJECT_RELEASE is enabled?

If klp_is_patch_compatible() fails, we jump to err. This triggers kobject teardown for objects that were initialized but never linked to a parent:

err:

    klp_free_patch_start(patch);
    ...
    klp_free_patch_finish(patch);

Because the child kobjects lack a parent linkage at this early stage, waiting on patch->finish completes without waiting for the child kobjects to be fully released.

If CONFIG_DEBUG_KOBJECT_RELEASE is enabled, the release is delayed. The function returns and the livepatch module is unloaded, freeing its static memory. Later, the delayed release work executes:

kernel/livepatch/core.c:klp_kobj_release_object()

    obj = container_of(kobj, struct klp_object, kobj);
    if (obj->dynamic)

Accessing obj->dynamic here would cause a use-after-free on the freed module memory.

Should we ensure all child kobjects are fully released before returning, or handle the initialization state differently?

Quoted 5 lines
kernel/livepatch/core.c +2 -0
+	}
+
 	ret = klp_init_patch(patch);
 	if (ret)
 		goto err;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

Re: [PATCH v12 4/9] livepatch: Call klp_init_patch_early() earlier

Quoted 3 lines · On Fri, Oct 16, 2026 at 1:47 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] livepatch: UAF on module unload if klp_is_patch_compatible() fails with DEBUG_KOBJECT_RELEASE

This pre-existing issue has already been fixed in the for-next branch. [0]

[0] https://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching.git/commit/?id=7f4ca0d0b40ac1ccd82ebaf1514c2d26c3db71c4

Signature · 2 lines
Regards
Yafang
#7 lore raw

PATCH v12 5/9 livepatch: Implement provides and obsoletes for scoped atomic replace

In reply to Yafang Shao

The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.

Replace it with a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the changes made by the related livepatch. Where the changes are a set of modified objects, functions, and used callbacks, shadow variable ids, and state ids.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that meets either of the following conditions:

1. It has the same provides id as the new patch,
2. Its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

klp-build gains -p/--provides and -r/--obsoletes options. The two parameters are not validated in klp-build, since the compiler and the kernel handle invalid values.

Enable the CONFIG_KLP_HAS_PROVIDES compile-time marker now that the provides/obsoletes interface is implemented.

---
 .../ABI/removed/sysfs-kernel-livepatch        |   7 +
 .../ABI/testing/sysfs-kernel-livepatch        |  22 ++-
 .../livepatch/cumulative-patches.rst          | 127 ++++++++++++++----
 Documentation/livepatch/livepatch.rst         |  29 ++--
 include/linux/livepatch.h                     |   8 +-
 kernel/livepatch/Kconfig                      |   2 +-
 kernel/livepatch/core.c                       |  80 ++++++++---
 kernel/livepatch/core.h                       |   1 +
 kernel/livepatch/state.c                      |  57 ++++++--
 kernel/livepatch/transition.c                 |  15 ++-
 scripts/livepatch/init.c                      |  19 ++-
 scripts/livepatch/klp-build                   |  23 ++--
 12 files changed, 301 insertions(+), 89 deletions(-)
 create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
Documentation/ABI/removed/sysfs-kernel-livepatch new file +7 -0
new file mode 100644
index 000000000000..87d57ba27326
@@ -0,0 +1,7 @@
+What:		/sys/kernel/livepatch/<patch>/replace
+Date:		Jun 2024
+KernelVersion:	6.11.0
+Contact:	[email protected]
+Description:
+		An attribute which indicates whether the patch supports
+		atomic-replace.
Documentation/ABI/testing/sysfs-kernel-livepatch +17 -5
index 3c3f36b32b57..e4874205c319 100644
@@ -47,13 +47,25 @@ Description:
 		disabled when the feature is used. See
 		Documentation/livepatch/livepatch.rst for more information.
 
-What:		/sys/kernel/livepatch/<patch>/replace
-Date:		Jun 2024
-KernelVersion:	6.11.0
+What:		/sys/kernel/livepatch/<patch>/provides
+Date:		Jun 2026
+KernelVersion:	7.4.0
 Contact:	[email protected]
 Description:
-		An attribute which indicates whether the patch supports
-		atomic-replace.
+		An attribute to show the provides id of this livepatch.
+		Livepatches with the same provides id replace each other.
+
+What:		/sys/kernel/livepatch/<patch>/obsoletes
+Date:		Jun 2026
+KernelVersion:	7.4.0
+Contact:	[email protected]
+Description:
+		An attribute to show the obsoletes ids of this livepatch.
+		The obsoletes ids are a comma-separated list of provides
+		ids that this patch obsoletes. When this livepatch is
+		loaded, any existing livepatch whose provides id matches
+		either this patch's provides id or any id in the obsoletes
+		list will be atomically replaced.
 
 What:		/sys/kernel/livepatch/<patch>/stack_order
 Date:		Jan 2025
Documentation/livepatch/cumulative-patches.rst +99 -28

145 lines not on this page · raw message

Documentation/livepatch/livepatch.rst +20 -9
index acb90164929e..2567e09c74fd 100644
@@ -347,15 +347,26 @@ to '0'.
 5.3. Replacing
 --------------
 
-All enabled patches might get replaced by a cumulative patch that
-has the .replace flag set.
-
-Once the new patch is enabled and the 'transition' finishes then
-all the functions (struct klp_func) associated with the replaced
-patches are removed from the corresponding struct klp_ops. Also
-the ftrace handler is unregistered and the struct klp_ops is
-freed when the related function is not modified by the new patch
-and func_stack list becomes empty.
+There can be only one active livepatch for a given ``provides`` id.
+A new livepatch atomically replaces any existing livepatch whose
+``provides`` id matches either the new patch's ``provides`` id or
+any id in the new patch's ``obsoletes`` list.
+
+A new livepatch that does not replace an existing one can coexist with it.
+In this case, the two livepatches must not modify the same function, or use
+the state with the same id. Any attempt to load an incompatible livepatch
+will be rejected by the kernel.
+
+Installing more livepatches in parallel is dangerous, especially
+when they are provided by different teams or vendors. The built-in
+checks for possible conflicts do not prevent potential
+incompatibilities and breakages.
+
+Once the transition is complete, all functions (``struct klp_func``)
+associated with the matching replaced patches are removed from the
+corresponding ``struct klp_ops``. If a function is no longer modified by
+the new patch and its ``func_stack`` list becomes empty, the ftrace
+handler is unregistered and the ``struct klp_ops`` is freed.
 
 See Documentation/livepatch/cumulative-patches.rst for more details.
 
include/linux/livepatch.h +6 -2
index 5f74f79c22b4..1e3a09e29a65 100644
@@ -127,7 +127,9 @@ struct klp_state {
  * @mod:	reference to the live patch module
  * @objs:	object entries for kernel objects to be patched
  * @states:	system states that can get modified
- * @replace:	replace all actively used patches
+ * @provides:	only one active livepatch per id
+ * @obsoletes:	replace given livepatch id(s)
+ * @nr_obsoletes: number of entries in the @obsoletes array
  * @list:	list node for global list of actively used patches
  * @kobj:	kobject for sysfs resources
  * @obj_list:	dynamic list of the object entries
@@ -141,7 +143,9 @@ struct klp_patch {
 	struct module *mod;
 	struct klp_object *objs;
 	struct klp_state *states;
-	bool replace;
+	unsigned int provides;
+	unsigned int *obsoletes;
+	unsigned int nr_obsoletes;
 
 	/* internal */
 	struct list_head list;
kernel/livepatch/Kconfig +1 -1
index d1eb971abb1a..d92603658f7e 100644
@@ -32,7 +32,7 @@ config KLP_BUILD
 	  Enable klp-build support
 
 config KLP_HAS_PROVIDES
-	def_bool n
+	def_bool y
 	help
 	  Compile-time marker for the livepatch provides/obsoletes support.
 	  It is always set when the kernel is built with the scoped atomic
kernel/livepatch/core.c +65 -15

153 lines not on this page · raw message

kernel/livepatch/core.h +1 -0
index 361a0917a03f..b03cfc58b660 100644
@@ -18,6 +18,7 @@ void klp_free_replaced_patches_async(struct klp_patch *new_patch);
 void klp_unpatch_replaced_patches(struct klp_patch *new_patch);
 void klp_discard_nops(struct klp_patch *new_patch);
 struct klp_func *klp_find_func(struct klp_object *obj, struct klp_func *func);
+bool klp_patch_replaces(struct klp_patch *new_patch, struct klp_patch *old_patch);
 
 static inline bool klp_is_object_loaded(struct klp_object *obj)
 {
kernel/livepatch/state.c +49 -8

82 lines not on this page · raw message

kernel/livepatch/transition.c +8 -7
index 2351a19ac2a9..ef70ff582da1 100644
@@ -89,7 +89,7 @@ static void klp_complete_transition(void)
 		 klp_transition_patch->mod->name,
 		 klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
 
-	if (klp_transition_patch->replace && klp_target_state == KLP_TRANSITION_PATCHED) {
+	if (klp_target_state == KLP_TRANSITION_PATCHED) {
 		klp_unpatch_replaced_patches(klp_transition_patch);
 		klp_discard_nops(klp_transition_patch);
 	}
@@ -496,10 +496,10 @@ void klp_try_complete_transition(void)
 	 * klp_complete_transition() but it is called also
 	 * from klp_cancel_transition().
 	 */
-	if (!patch->enabled)
-		klp_free_patch_async(patch);
-	else if (patch->replace)
+	if (patch->enabled)
 		klp_free_replaced_patches_async(patch);
+	else
+		klp_free_patch_async(patch);
 }
 
 /*
@@ -720,11 +720,12 @@ void klp_force_transition(void)
 		klp_update_patch_state(idle_task(cpu));
 
 	/* Set forced flag for patches being removed. */
-	if (klp_target_state == KLP_TRANSITION_UNPATCHED)
+	if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
 		klp_transition_patch->forced = true;
-	else if (klp_transition_patch->replace) {
+	} else {
 		klp_for_each_patch(patch) {
-			if (patch != klp_transition_patch)
+			if (patch != klp_transition_patch &&
+			    klp_patch_replaces(klp_transition_patch, patch))
 				patch->forced = true;
 		}
 	}
scripts/livepatch/init.c +13 -6
index f14d8c8fb35f..1eb124191cf2 100644
@@ -8,6 +8,11 @@
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/livepatch.h>
+#include <linux/string.h>
+
+#ifdef KLP_OBSOLETES
+static unsigned int klp_obsoletes[] = { KLP_OBSOLETES };
+#endif
 
 static struct klp_patch *patch;
 
@@ -50,8 +55,6 @@ static int __init livepatch_mod_init(void)
 		funcs = kzalloc(sizeof(struct klp_func) * (nr_funcs + 1), GFP_KERNEL);
 		if (!funcs) {
 			ret = -ENOMEM;
-			for (int j = 0; j < i; j++)
-				kfree(objs[i].funcs);
 			goto err_free_objs;
 		}
 
@@ -72,15 +75,19 @@ static int __init livepatch_mod_init(void)
 
 	/* TODO patch->states */
 
-#ifdef KLP_NO_REPLACE
-	patch->replace = false;
-#else
-	patch->replace = true;
+#ifdef KLP_PROVIDES
+	patch->provides = KLP_PROVIDES;
+#endif
+#ifdef KLP_OBSOLETES
+	patch->obsoletes = klp_obsoletes;
+	patch->nr_obsoletes = ARRAY_SIZE(klp_obsoletes);
 #endif
 
 	return klp_enable_patch(patch);
 
 err_free_objs:
+	for (int i = 0; i < nr_objs; i++)
+		kfree(objs[i].funcs);
 	kfree(objs);
 err_free_patch:
 	kfree(patch);
scripts/livepatch/klp-build +15 -8
index c4a7acf8edc3..95472224680a 100755
@@ -21,7 +21,8 @@ shopt -s lastpipe
 
 unset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE
 
-REPLACE=1
+PROVIDES=0
+OBSOLETES=""
 SHORT_CIRCUIT=0
 JOBS="$(getconf _NPROCESSORS_ONLN)"
 shopt -o xtrace | grep -q 'on' && XTRACE=1
@@ -132,7 +133,8 @@ Options:
    -f, --show-first-changed	Show address of first changed instruction
    -j, --jobs=<jobs>		Build jobs to run simultaneously [default: $JOBS]
    -o, --output=<file.ko>	Output file [default: livepatch-<patch-name>.ko]
-       --no-replace		Disable livepatch atomic replace
+   -p, --provides=<id>		Set the provides id for this livepatch
+   -r, --obsoletes=<ids>	Set the obsoletes ids array (e.g., "0,1,2")
    -v, --verbose		Pass V=1 to kernel/module builds
 
 Advanced Options:
@@ -159,8 +161,8 @@ process_args() {
 	local args
 	local patch
 
-	short="hfj:o:vdS:T"
-	long="help,show-first-changed,jobs:,output:,no-replace,verbose,debug,short-circuit:,keep-tmp"
+	short="hfj:o:p:r:vdS:T"
+	long="help,show-first-changed,jobs:,output:,provides:,obsoletes:,verbose,debug,short-circuit:,keep-tmp"
 
 	args=$(getopt --options "$short" --longoptions "$long" -- "$@") || {
 		echo; usage; exit
@@ -189,9 +191,13 @@ process_args() {
 				NAME="$(module_name_string "$NAME")"
 				shift 2
 				;;
-			--no-replace)
-				REPLACE=0
-				shift
+			-p | --provides)
+				PROVIDES="$2"
+				shift 2
+				;;
+			-r | --obsoletes)
+				OBSOLETES="$2"
+				shift 2
 				;;
 			-v | --verbose)
 				VERBOSE=1
@@ -847,7 +853,8 @@ build_patch_module() {
 
 	cflags=("-ffunction-sections")
 	cflags+=("-fdata-sections")
-	[[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE")
+	cflags+=("-DKLP_PROVIDES=$PROVIDES")
+	[[ -n "$OBSOLETES" ]] && cflags+=("-DKLP_OBSOLETES=$OBSOLETES")
 
 	cmd=("make")
 	if [[ -v VERBOSE ]]; then
Signature · 1 line
2.52.0
#8 lore raw

Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] livepatch: memory leak of dynamically allocated patch structures on klp_enable_patch failure --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] livepatch: memory leak of dynamically allocated patch structures on klp_enable_patch failure --

commit 160ccf1ed36fde593c80db0147adb4d9a0aa689f
Author: Yafang Shao <[email protected]>

livepatch: Implement provides and obsoletes for scoped atomic replace

This commit replaces the boolean replace flag in klp_patch with provides and obsoletes fields. It introduces a new flexible model to allow multiple independent livepatch sets to coexist safely on the same system.

Quoted 4 lines
scripts/livepatch/init.c +0 -0
index f14d8c8fb35fa..1eb124191cf22 100644

[ ... ]

Quoted 7 lines
scripts/livepatch/init.c +0 -3
@@ -72,15 +75,19 @@ static int __init livepatch_mod_init(void)
 
 	/* TODO patch->states */
 
-#ifdef KLP_NO_REPLACE
-	patch->replace = false;
-#else

scripts/livepatch/init.c static int __init livepatch_mod_init(void) · line 86

Quoted 10 lines
scripts/livepatch/init.c +6 -1
-	patch->replace = true;
+#ifdef KLP_PROVIDES
+	patch->provides = KLP_PROVIDES;
+#endif
+#ifdef KLP_OBSOLETES
+	patch->obsoletes = klp_obsoletes;
+	patch->nr_obsoletes = ARRAY_SIZE(klp_obsoletes);
 #endif
 
 	return klp_enable_patch(patch);

[Severity: Medium]
If klp_enable_patch() fails (e.g., due to patch incompatibility or early -ENODEV) and returns an error code, does this direct return bypass the err_free_objs cleanup path below?

The primary structures (patch, objs, and funcs) are allocated with kzalloc() earlier in livepatch_mod_init(). Because the core livepatch error path inside klp_enable_patch() treats these structures as statically provided by the module, it will not kfree() them upon failure.

Does this code permanently leak the patch, objs, and funcs structures? Would it be better to save the return status and goto err_free_objs on failure?

Quoted 7 lines
scripts/livepatch/init.c +2 -0
 err_free_objs:
+	for (int i = 0; i < nr_objs; i++)
+		kfree(objs[i].funcs);
 	kfree(objs);
 err_free_patch:
 	kfree(patch);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#9 lore raw

Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

Quoted 44 lines · On Fri, Oct 16, 2026 at 1:48 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] livepatch: memory leak of dynamically allocated patch structures on klp_enable_patch failure --

commit 160ccf1ed36fde593c80db0147adb4d9a0aa689f
Author: Yafang Shao <[email protected]>

livepatch: Implement provides and obsoletes for scoped atomic replace

This commit replaces the boolean replace flag in klp_patch with provides and obsoletes fields. It introduces a new flexible model to allow multiple independent livepatch sets to coexist safely on the same system.

Quoted 4 lines
scripts/livepatch/init.c +0 -0
index f14d8c8fb35fa..1eb124191cf22 100644

[ ... ]

Quoted 7 lines
scripts/livepatch/init.c +0 -3
@@ -72,15 +75,19 @@ static int __init livepatch_mod_init(void)
 
      /* TODO patch->states */
 
-#ifdef KLP_NO_REPLACE
-     patch->replace = false;
-#else

scripts/livepatch/init.c static int __init livepatch_mod_init(void) · line 86

Quoted 10 lines
scripts/livepatch/init.c +6 -1
-     patch->replace = true;
+#ifdef KLP_PROVIDES
+     patch->provides = KLP_PROVIDES;
+#endif
+#ifdef KLP_OBSOLETES
+     patch->obsoletes = klp_obsoletes;
+     patch->nr_obsoletes = ARRAY_SIZE(klp_obsoletes);
 #endif
 
      return klp_enable_patch(patch);

[Severity: Medium]
If klp_enable_patch() fails (e.g., due to patch incompatibility or early -ENODEV) and returns an error code, does this direct return bypass the err_free_objs cleanup path below?

Quoted 7 lines

The primary structures (patch, objs, and funcs) are allocated with kzalloc() earlier in livepatch_mod_init(). Because the core livepatch error path inside klp_enable_patch() treats these structures as statically provided by the module, it will not kfree() them upon failure.

Does this code permanently leak the patch, objs, and funcs structures? Would it be better to save the return status and goto err_free_objs on failure?

Good catch.
It seems to be a pre-existing bug, not introduced by this series. It could be fixed by the following change:

- return klp_enable_patch(patch);
+ ret = klp_enable_patch(patch);
+ if (ret)
+ goto err_free_objs;
+
+ return 0;

Signature · 2 lines
Regards
Yafang
maintainer Reviewed-by on v12 5/9
#10 lore raw

Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

In reply to Yafang Shao
Quoted 55 lines · Yafang Shao

The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.

Replace it with a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the changes made by the related livepatch. Where the changes are a set of modified objects, functions, and used callbacks, shadow variable ids, and state ids.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that meets either of the following conditions:

1. It has the same provides id as the new patch,
2. Its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

klp-build gains -p/--provides and -r/--obsoletes options. The two parameters are not validated in klp-build, since the compiler and the kernel handle invalid values.

Enable the CONFIG_KLP_HAS_PROVIDES compile-time marker now that the provides/obsoletes interface is implemented.

Documentation/ABI/testing/sysfs-kernel-livepatch +7 -5
@@ -47,13 +47,25 @@ Description:
 		disabled when the feature is used. See
 		Documentation/livepatch/livepatch.rst for more information.
 
-What:		/sys/kernel/livepatch/<patch>/replace
-Date:		Jun 2024
-KernelVersion:	6.11.0
+What:		/sys/kernel/livepatch/<patch>/provides
+Date:		Jun 2026
+KernelVersion:	7.4.0
 Contact:	[email protected]
 Description:
-		An attribute which indicates whether the patch supports
-		atomic-replace.
+		An attribute to show the provides id of this livepatch.
+		Livepatches with the same provides id replace each other.
+
+What:		/sys/kernel/livepatch/<patch>/obsoletes

Documentation/ABI/testing/sysfs-kernel-livepatch Description: · line 68

Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +10 -0
+Date:		Jun 2026
+KernelVersion:	7.4.0
+Contact:	[email protected]
+Description:
+		An attribute to show the obsoletes ids of this livepatch.
+		The obsoletes ids are a comma-separated list of provides
+		ids that this patch obsoletes. When this livepatch is
+		loaded, any existing livepatch whose provides id matches
+		either this patch's provides id or any id in the obsoletes
+		list will be atomically replaced.

This still describes the behavior in v10 where the obsoletes were handled symetrically. It should remove patches only with the matching provides.

Quoted 43 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +0 -0
 What:		/sys/kernel/livepatch/<patch>/stack_order
 Date:		Jan 2025
Documentation/livepatch/cumulative-patches.rst +5 -27
index 1931f318976a..5ce04424e09d 100644
@@ -2,33 +2,71 @@
 Atomic Replace & Cumulative Patches
 ===================================
 
-There might be dependencies between livepatches. If multiple patches need
-to do different changes to the same function(s) then we need to define
-an order in which the patches will be installed. And function implementations
-from any newer livepatch must be done on top of the older ones.
-
-This might become a maintenance nightmare. Especially when more patches
-modified the same function in different ways.
-
-An elegant solution comes with the feature called "Atomic Replace". It allows
-creation of so called "Cumulative Patches". They include all wanted changes
-from all older livepatches and completely replace them in one transition.
-
-Usage
------
-
-The atomic replace can be enabled by setting "replace" flag in struct klp_patch,
-for example::
-
-	static struct klp_patch patch = {
-		.mod = THIS_MODULE,
-		.objs = objs,
-		.replace = true,
-	};
-
-All processes are then migrated to use the code only from the new patch.
-Once the transition is finished, all older patches are automatically
-disabled.
+Livepatches are used to fix kernel bugs. New fixes need to be added over time.
+The fixes might be independent, but they might also depend on each other. This
+brings a challenge of how to keep the livepatched system safe and consistent.
+
+Part of the solution is the "Atomic Replace" feature, which allows the kernel to

Documentation/livepatch/cumulative-patches.rst line 19

Quoted 10 lines
Documentation/livepatch/cumulative-patches.rst +10 -0
+atomically replace an existing livepatch with another one. These newer
+livepatches are designed as "Cumulative Patches". They include all wanted
+changes from all older livepatches and completely replace them in one
+transition.
+
+The second part of the solution are ``provides`` and ``obsoletes`` fields in
+``struct klp_patch``, which allow the installation of multiple livepatches in
+parallel. A livepatch will atomically replace any already installed livepatch
+whose ``provides`` id matches either the new patch's ``provides`` id or any id
+in the new patch's ``obsoletes`` list.

Same here.

Documentation/livepatch/cumulative-patches.rst line 21

Quoted 3 lines

This might be used to fix independent

Documentation/livepatch/cumulative-patches.rst +2 -0
+problems separately, for example, the livepatches might be prepared by separate
+teams focusing on particular functionality or a subsystem.

Otherwise, I like the patch. The code and documentation is nice a clean.

Note: We repeate some things several times in the documentation. It makes
it harder to keep it consistent. But it is an existing problem. I think that we could do better but it is beyond the scope of this patchset.

With fixing the two outdated pieces of documentation:

Best Regards,
Petr

PS: I have to leave now. I hope that I'll be able to finish the review
by the end of the week. I think that we are getting close to get it into linux-next. Well, I would like to put it there on top of the merge commit for-7.4. I still have to send the pull request...

#11 lore raw

Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

Quoted 63 lines · Petr Mladek
Quoted 61 lines · Yafang Shao

The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.

Replace it with a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the changes made by the related livepatch. Where the changes are a set of modified objects, functions, and used callbacks, shadow variable ids, and state ids.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that meets either of the following conditions:

1. It has the same provides id as the new patch,
2. Its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

klp-build gains -p/--provides and -r/--obsoletes options. The two parameters are not validated in klp-build, since the compiler and the kernel handle invalid values.

Enable the CONFIG_KLP_HAS_PROVIDES compile-time marker now that the provides/obsoletes interface is implemented.

Documentation/ABI/testing/sysfs-kernel-livepatch +13 -5
@@ -47,13 +47,25 @@ Description:
              disabled when the feature is used. See
              Documentation/livepatch/livepatch.rst for more information.
 
-What:                /sys/kernel/livepatch/<patch>/replace
-Date:                Jun 2024
-KernelVersion:       6.11.0
+What:                /sys/kernel/livepatch/<patch>/provides
+Date:                Jun 2026
+KernelVersion:       7.4.0
 Contact:     [email protected]
 Description:
-             An attribute which indicates whether the patch supports
-             atomic-replace.
+             An attribute to show the provides id of this livepatch.
+             Livepatches with the same provides id replace each other.
+
+What:                /sys/kernel/livepatch/<patch>/obsoletes
+Date:                Jun 2026
+KernelVersion:       7.4.0
+Contact:     [email protected]
+Description:
+             An attribute to show the obsoletes ids of this livepatch.
+             The obsoletes ids are a comma-separated list of provides
Quoted 8 lines

Documentation/ABI/testing/sysfs-kernel-livepatch Description: · line 68

Quoted 4 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +4 -0
+             ids that this patch obsoletes. When this livepatch is
+             loaded, any existing livepatch whose provides id matches
+             either this patch's provides id or any id in the obsoletes
+             list will be atomically replaced.

This still describes the behavior in v10 where the obsoletes were handled symetrically. It should remove patches only with the matching provides.

I will update it.

Quoted 48 lines
Quoted 48 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +0 -0
 What:                /sys/kernel/livepatch/<patch>/stack_order
 Date:                Jan 2025
Documentation/livepatch/cumulative-patches.rst +9 -27
index 1931f318976a..5ce04424e09d 100644
@@ -2,33 +2,71 @@
 Atomic Replace & Cumulative Patches
 ===================================
 
-There might be dependencies between livepatches. If multiple patches need
-to do different changes to the same function(s) then we need to define
-an order in which the patches will be installed. And function implementations
-from any newer livepatch must be done on top of the older ones.
-
-This might become a maintenance nightmare. Especially when more patches
-modified the same function in different ways.
-
-An elegant solution comes with the feature called "Atomic Replace". It allows
-creation of so called "Cumulative Patches". They include all wanted changes
-from all older livepatches and completely replace them in one transition.
-
-Usage
------
-
-The atomic replace can be enabled by setting "replace" flag in struct klp_patch,
-for example::
-
-     static struct klp_patch patch = {
-             .mod = THIS_MODULE,
-             .objs = objs,
-             .replace = true,
-     };
-
-All processes are then migrated to use the code only from the new patch.
-Once the transition is finished, all older patches are automatically
-disabled.
+Livepatches are used to fix kernel bugs. New fixes need to be added over time.
+The fixes might be independent, but they might also depend on each other. This
+brings a challenge of how to keep the livepatched system safe and consistent.
+
+Part of the solution is the "Atomic Replace" feature, which allows the kernel to
+atomically replace an existing livepatch with another one. These newer
+livepatches are designed as "Cumulative Patches". They include all wanted
+changes from all older livepatches and completely replace them in one
+transition.
Quoted 8 lines

Documentation/livepatch/cumulative-patches.rst

Quoted 6 lines
Documentation/livepatch/cumulative-patches.rst +6 -0
+
+The second part of the solution are ``provides`` and ``obsoletes`` fields in
+``struct klp_patch``, which allow the installation of multiple livepatches in
+parallel. A livepatch will atomically replace any already installed livepatch
+whose ``provides`` id matches either the new patch's ``provides`` id or any id
+in the new patch's ``obsoletes`` list.

Same here.

will update it.

Quoted 11 lines

Documentation/livepatch/cumulative-patches.rst line 21

Quoted 4 lines

This might be used to fix independent

Documentation/livepatch/cumulative-patches.rst +2 -0
+problems separately, for example, the livepatches might be prepared by separate
+teams focusing on particular functionality or a subsystem.

Otherwise, I like the patch. The code and documentation is nice a clean.

Note: We repeate some things several times in the documentation. It makes
it harder to keep it consistent. But it is an existing problem. I think that we could do better but it is beyond the scope of this patchset.

Agreed. Some rules are described several times in different files. I will try to clean up the documentation once this series is merged.

Quoted 6 lines

With fixing the two outdated pieces of documentation:

Quoted 8 lines

Best Regards,
Petr

PS: I have to leave now. I hope that I'll be able to finish the review
by the end of the week. I think that we are getting close to get it into linux-next. Well, I would like to put it there on top of the merge commit for-7.4. I still have to send the pull request...

Thank you for the review! It is great to hear that the series is getting close to linux-next.

Signature · 2 lines
Regards
Yafang
maintainer
#12 lore raw

temporary broken selftest: was: Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

In reply to Yafang Shao
Quoted 28 lines · Yafang Shao

The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.

Replace it with a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the changes made by the related livepatch. Where the changes are a set of modified objects, functions, and used callbacks, shadow variable ids, and state ids.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that meets either of the following conditions:

1. It has the same provides id as the new patch,
2. Its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

Quoted 6 lines

klp-build gains -p/--provides and -r/--obsoletes options. The two parameters are not validated in klp-build, since the compiler and the kernel handle invalid values.

Enable the CONFIG_KLP_HAS_PROVIDES compile-time marker now that the provides/obsoletes interface is implemented.

I tried to run selftests when rebasing on top of this patch. And test-sysfs.sh fails with:

<paste>
# selftests: livepatch: test-sysfs.sh
# TEST: sysfs test ... ok
# TEST: sysfs test object/patched ... ok
# TEST: sysfs test stack_order value ... ERROR: Unexpected value in /sys/kernel/livepatch/test_klp_callbacks_demo/stack_order: 2 vs. 1 not ok 6 selftests: livepatch: test-sysfs.sh # exit=1
</paste>

The livepatches are not longer stacked and the test should get skipped. It is called only when:

	if [[ "$HAS_STACK_ORDER_ATTR" == "1" ]]; then
		start_test "sysfs test stack_order value"

But "stack_order" is still there at this stage. It is removed by the next 6th patch.

It would be nice to fix this to do not break bisectability. The following change might do the job:

tools/testing/selftests/livepatch/test-sysfs.sh +2 -1
index 32d3af0097ac..e48bf33207ca 100755
@@ -13,6 +13,7 @@ HAS_REPLACE_ATTR=0
 HAS_STACK_ORDER_ATTR=0
 
 setup_config
+detect_provides_attr
 
 # - load a livepatch and verifies the sysfs entries work as expected
 
@@ -154,7 +155,7 @@ livepatch: '$MOD_ATOMIC_REPLACE': unpatching complete
 % rmmod $MOD_ATOMIC_REPLACE"
 fi
 
-if [[ "$HAS_STACK_ORDER_ATTR" == "1" ]]; then
+if [[ "$HAS_STACK_ORDER_ATTR" == "1" && "$HAS_PROVIDES_ATTR" != "1" ]]; then
 	start_test "sysfs test stack_order value"
 
 	load_lp $MOD_LIVEPATCH

It should be merged into the 2nd patch which prepares the selftests for this change.

Best Regards,
Petr

#13 lore raw

Re: temporary broken selftest: was: Re: [PATCH v12 5/9] livepatch: Implement provides and obsoletes for scoped atomic replace

Quoted 83 lines · Petr Mladek
Quoted 28 lines · Yafang Shao

The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.

Replace it with a more flexible model using two new fields in struct klp_patch:

- provides: an unsigned int id identifying the changes made by the related livepatch. Where the changes are a set of modified objects, functions, and used callbacks, shadow variable ids, and state ids.

- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different scopes.

A new livepatch atomically replaces any existing livepatch that meets either of the following conditions:

1. It has the same provides id as the new patch,
2. Its provides id is listed in the new patch's obsoletes list.

A new livepatch that does not replace an existing one can coexist with it. In this case, the two livepatches must not modify the same function, or use the state with the same id. Any attempt to load an incompatible livepatch will be rejected by the kernel.

Quoted 6 lines

klp-build gains -p/--provides and -r/--obsoletes options. The two parameters are not validated in klp-build, since the compiler and the kernel handle invalid values.

Enable the CONFIG_KLP_HAS_PROVIDES compile-time marker now that the provides/obsoletes interface is implemented.

I tried to run selftests when rebasing on top of this patch. And test-sysfs.sh fails with:

<paste>
# selftests: livepatch: test-sysfs.sh
# TEST: sysfs test ... ok
# TEST: sysfs test object/patched ... ok
# TEST: sysfs test stack_order value ... ERROR: Unexpected value in /sys/kernel/livepatch/test_klp_callbacks_demo/stack_order: 2 vs. 1 not ok 6 selftests: livepatch: test-sysfs.sh # exit=1
</paste>

The livepatches are not longer stacked and the test should get skipped. It is called only when:

if [[ "$HAS_STACK_ORDER_ATTR" == "1" ]]; then
start_test "sysfs test stack_order value"

But "stack_order" is still there at this stage. It is removed by the next 6th patch.

It would be nice to fix this to do not break bisectability. The following change might do the job:

tools/testing/selftests/livepatch/test-sysfs.sh +2 -1
index 32d3af0097ac..e48bf33207ca 100755
@@ -13,6 +13,7 @@ HAS_REPLACE_ATTR=0
 HAS_STACK_ORDER_ATTR=0
 
 setup_config
+detect_provides_attr
 
 # - load a livepatch and verifies the sysfs entries work as expected
 
@@ -154,7 +155,7 @@ livepatch: '$MOD_ATOMIC_REPLACE': unpatching complete
 % rmmod $MOD_ATOMIC_REPLACE"
 fi
 
-if [[ "$HAS_STACK_ORDER_ATTR" == "1" ]]; then
+if [[ "$HAS_STACK_ORDER_ATTR" == "1" && "$HAS_PROVIDES_ATTR" != "1" ]]; then
        start_test "sysfs test stack_order value"
 
        load_lp $MOD_LIVEPATCH
Quoted 2 lines

It should be merged into the 2nd patch which prepares the selftests for this change.

Thanks for catching it.
I will update it.

Signature · 2 lines
Regards
Yafang
#14 lore raw

PATCH v12 6/9 livepatch: Deprecate stack_order

In reply to Yafang Shao

The stack_order sysfs attribute was added to determine which active livepatch is used for a given function. It is no longer needed because recent changes refuse loading another livepatch which would modify an already livepatched function unless the new livepatch would replace the conflicting one.

[Commit message authored by Petr.]

---
 .../ABI/removed/sysfs-kernel-livepatch        |  9 +++++++
 .../ABI/testing/sysfs-kernel-livepatch        |  9 -------
 kernel/livepatch/core.c                       | 24 -------------------
 3 files changed, 9 insertions(+), 33 deletions(-)
Documentation/ABI/removed/sysfs-kernel-livepatch +9 -0
index 87d57ba27326..f094bd18379f 100644
@@ -5,3 +5,12 @@ Contact:	[email protected]
 Description:
 		An attribute which indicates whether the patch supports
 		atomic-replace.
+
+What:		/sys/kernel/livepatch/<patch>/stack_order
+Date:		Jan 2025
+KernelVersion: 6.14.0
+Description:
+		This attribute specifies the sequence in which live patch modules
+		are applied to the system. If multiple live patches modify the same
+		function, the implementation with the biggest 'stack_order' number
+		is used, unless a transition is currently in progress.
Documentation/ABI/testing/sysfs-kernel-livepatch +0 -9
index e4874205c319..bb213ea9c135 100644
@@ -67,15 +67,6 @@ Description:
 		either this patch's provides id or any id in the obsoletes
 		list will be atomically replaced.
 
-What:		/sys/kernel/livepatch/<patch>/stack_order
-Date:		Jan 2025
-KernelVersion:	6.14.0
-Description:
-		This attribute specifies the sequence in which live patch modules
-		are applied to the system. If multiple live patches modify the same
-		function, the implementation with the biggest 'stack_order' number
-		is used, unless a transition is currently in progress.
-
 What:		/sys/kernel/livepatch/<patch>/<object>
 Date:		Nov 2014
 KernelVersion:	3.19.0
kernel/livepatch/core.c +0 -24
index 91e6ea53b337..e86fd092e3e5 100644
@@ -352,7 +352,6 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
  * /sys/kernel/livepatch/<patch>/force
  * /sys/kernel/livepatch/<patch>/provides
  * /sys/kernel/livepatch/<patch>/obsoletes
- * /sys/kernel/livepatch/<patch>/stack_order
  * /sys/kernel/livepatch/<patch>/<object>
  * /sys/kernel/livepatch/<patch>/<object>/patched
  * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
@@ -477,40 +476,17 @@ static ssize_t obsoletes_show(struct kobject *kobj,
 	return len;
 }
 
-static ssize_t stack_order_show(struct kobject *kobj,
-				struct kobj_attribute *attr, char *buf)
-{
-	struct klp_patch *patch, *this_patch;
-	int stack_order = 0;
-
-	this_patch = container_of(kobj, struct klp_patch, kobj);
-
-	mutex_lock(&klp_mutex);
-
-	klp_for_each_patch(patch) {
-		stack_order++;
-		if (patch == this_patch)
-			break;
-	}
-
-	mutex_unlock(&klp_mutex);
-
-	return sysfs_emit(buf, "%d\n", stack_order);
-}
-
 static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
 static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
 static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
 static struct kobj_attribute provides_kobj_attr = __ATTR_RO(provides);
 static struct kobj_attribute obsoletes_kobj_attr = __ATTR_RO(obsoletes);
-static struct kobj_attribute stack_order_kobj_attr = __ATTR_RO(stack_order);
 static struct attribute *klp_patch_attrs[] = {
 	&enabled_kobj_attr.attr,
 	&transition_kobj_attr.attr,
 	&force_kobj_attr.attr,
 	&provides_kobj_attr.attr,
 	&obsoletes_kobj_attr.attr,
-	&stack_order_kobj_attr.attr,
 	NULL
 };
 ATTRIBUTE_GROUPS(klp_patch);
Signature · 1 line
2.52.0
maintainer Reviewed-by Tested-by on v12 6/9
#15 lore raw

Re: [PATCH v12 6/9] livepatch: Deprecate stack_order

Quoted 11 lines · Yafang Shao

The stack_order sysfs attribute was added to determine which active livepatch is used for a given function. It is no longer needed because recent changes refuse loading another livepatch which would modify an already livepatched function unless the new livepatch would replace the conflicting one.

[Commit message authored by Petr.]

Looks good and passes selftests:

Best Regards,
Petr

#16 lore raw

PATCH v12 7/9 selftests/livepatch: Add provides/obsoletes test scenarios

In reply to Yafang Shao

Now that the legacy "replace" field has been replaced by the provides/obsoletes mechanism, add test scenarios to verify the new behavior.

The test modules gain provides/obsoletes module parameters, and test-provides-obsoletes.sh covers the new replace semantics: provides=0 replacement, same-provides replacement, obsoletes-based replacement and coexistence of different provides ids.

These tests are skipped on kernels that still support the legacy "replace" attribute.

---
 tools/testing/selftests/livepatch/Makefile    |   3 +-
 .../livepatch/test-provides-obsoletes.sh      | 229 ++++++++++++++++++
 .../test_modules/test_klp_callbacks_demo2.c   |  12 +-
 .../test_modules/test_klp_cmdline_lp.c        |  11 +-
 .../test_modules/test_klp_meminfo_lp.c        |  22 +-
 .../livepatch/test_modules/test_klp_state.c   |   5 +-
 .../livepatch/test_modules/test_klp_state2.c  |   5 +-
 7 files changed, 261 insertions(+), 26 deletions(-)
 create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh
tools/testing/selftests/livepatch/Makefile +2 -1
index a080eb54a215..38f98594d883 100644
@@ -11,7 +11,8 @@ TEST_PROGS := \
 	test-ftrace.sh \
 	test-sysfs.sh \
 	test-syscall.sh \
-	test-kprobe.sh
+	test-kprobe.sh \
+	test-provides-obsoletes.sh
 
 TEST_FILES := settings
 
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +229 -0

229 lines not on this page · raw message

tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +4 -8
index 6c46ce575f5c..93789470fe18 100644
@@ -8,10 +8,9 @@
 #include <linux/livepatch.h>
 
 #ifdef CONFIG_KLP_HAS_PROVIDES
-/*
- * TODO: Add provides/obsoletes module parameters for the
- * provides/obsoletes based tests (to be added later).
- */
+static unsigned int provides;
+module_param(provides, uint, 0444);
+MODULE_PARM_DESC(provides, "provides id (default=0)");
 #else
 static int replace;
 module_param(replace, int, 0644);
@@ -87,10 +86,7 @@ static struct klp_patch patch = {
 static int test_klp_callbacks_demo2_init(void)
 {
 #ifdef CONFIG_KLP_HAS_PROVIDES
-	/*
-	 * TODO: Set provides/obsoletes from the module parameters
-	 * for the provides/obsoletes based tests (to be added later).
-	 */
+	patch.provides = provides;
 #else
 	patch.replace = replace;
 #endif
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c +10 -1
index 88c6aed925fb..8a93f005a383 100644
@@ -6,8 +6,14 @@
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/livepatch.h>
-
 #include <linux/seq_file.h>
+
+#ifdef CONFIG_KLP_HAS_PROVIDES
+static unsigned int provides;
+module_param(provides, uint, 0444);
+MODULE_PARM_DESC(provides, "provides id (default=0)");
+#endif
+
 static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
 {
 	seq_printf(m, "%s: %s\n", THIS_MODULE->name,
@@ -36,6 +42,9 @@ static struct klp_patch patch = {
 
 static int test_klp_cmdline_lp_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	patch.provides = provides;
+#endif
 	return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c +14 -8
index 0c434a27672d..a3b433385148 100644
@@ -8,10 +8,15 @@
 #include <linux/livepatch.h>
 
 #ifdef CONFIG_KLP_HAS_PROVIDES
-/*
- * TODO: Add provides/obsoletes module parameters for the
- * provides/obsoletes based tests (to be added later).
- */
+static unsigned int provides;
+module_param(provides, uint, 0444);
+MODULE_PARM_DESC(provides, "provides id (default=0)");
+
+#define KLP_MAX_OBSOLETES 16
+static unsigned int obsoletes[KLP_MAX_OBSOLETES];
+static int nr_obsoletes;
+module_param_array(obsoletes, uint, &nr_obsoletes, 0444);
+MODULE_PARM_DESC(obsoletes, "obsoletes provides ids");
 #else
 static int replace;
 module_param(replace, int, 0644);
@@ -51,10 +56,11 @@ static struct klp_patch patch = {
 static int test_klp_meminfo_lp_init(void)
 {
 #ifdef CONFIG_KLP_HAS_PROVIDES
-	/*
-	 * TODO: Set provides/obsoletes from the module parameters
-	 * for the provides/obsoletes based tests (to be added later).
-	 */
+	patch.provides = provides;
+	if (nr_obsoletes > 0) {
+		patch.obsoletes = obsoletes;
+		patch.nr_obsoletes = nr_obsoletes;
+	}
 #else
 	patch.replace = replace;
 #endif
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +1 -4
index 4d419a58cf65..f53eead9d55b 100644
@@ -143,10 +143,7 @@ static struct klp_patch patch = {
 	.objs = objs,
 	.states = states,
 #ifdef CONFIG_KLP_HAS_PROVIDES
-	/*
-	 * TODO: Add provides/obsoletes module parameters for the
-	 * provides/obsoletes based tests (to be added later).
-	 */
+	/* provides=0 by default, replaces all provides=0 patches */
 #else
 	.replace = true,
 #endif
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +1 -4
index 4f70f0619967..5f07fe1be2b9 100644
@@ -172,10 +172,7 @@ static struct klp_patch patch = {
 	.objs = objs,
 	.states = states,
 #ifdef CONFIG_KLP_HAS_PROVIDES
-	/*
-	 * TODO: Add provides/obsoletes module parameters for the
-	 * provides/obsoletes based tests (to be added later).
-	 */
+	/* provides=0 by default, replaces all provides=0 patches */
 #else
 	.replace = true,
 #endif
Signature · 1 line
2.52.0
maintainer Reviewed-by on v12 7/9
#17 lore raw

Re: [PATCH v12 7/9] selftests/livepatch: Add provides/obsoletes test scenarios

Quoted 9 lines · Yafang Shao

Now that the legacy "replace" field has been replaced by the provides/obsoletes mechanism, add test scenarios to verify the new behavior.

The test modules gain provides/obsoletes module parameters, and test-provides-obsoletes.sh covers the new replace semantics: provides=0 replacement, same-provides replacement, obsoletes-based replacement and coexistence of different provides ids.

Quoted 6 lines

These tests are skipped on kernels that still support the legacy "replace" attribute.

Looks good to me and passes selftests:

Best Regards,
Petr

#18 lore raw

PATCH v12 8/9 selftests/livepatch: Add state test for provides/obsoletes

In reply to Yafang Shao

Livepatches with different provides ids must not share the same state id, unless the second livepatch replaces the first one via obsoletes.

Add provides/obsoletes module parameters to test_klp_state and test_klp_state2, and cover both cases: reusing a state id across different provides ids is rejected, while it is allowed when the new livepatch obsoletes the old one.

---
 .../livepatch/test-provides-obsoletes.sh      | 239 ++++++++++++++++++
 .../livepatch/test_modules/test_klp_state.c   |  36 ++-
 .../livepatch/test_modules/test_klp_state2.c  |  44 +++-
 3 files changed, 303 insertions(+), 16 deletions(-)
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +239 -0
index 78a644f6ec56..b92188fcf45f 100755
@@ -6,6 +6,8 @@
 
 MOD_MEMINFO=test_klp_meminfo_lp
 MOD_CMDLINE=test_klp_cmdline_lp
+MOD_STATE=test_klp_state
+MOD_STATE2=test_klp_state2
 
 setup_config
 detect_provides_attr
@@ -226,4 +228,241 @@ livepatch: '$MOD_MEMINFO': completing unpatching transition
 livepatch: '$MOD_MEMINFO': unpatching complete
 % rmmod $MOD_MEMINFO"
 
+
+# - load a livepatch with provides=1 that registers state ID 1
+# - try to load another livepatch with provides=1 that reuses the same
+#   state ID. The second one will replace the first one.
+# - disable and unload the remaining livepatch
+
+start_test "same state, same provides"
+
+load_lp $MOD_STATE state=1 provides=1
+load_lp $MOD_STATE2 state=1 provides=1
+
+disable_lp $MOD_STATE2
+unload_lp $MOD_STATE2
+unload_lp $MOD_STATE
+
+check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE'
+livepatch: '$MOD_STATE': initializing patching transition
+$MOD_STATE: pre_patch_callback: vmlinux
+$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE': starting patching transition
+livepatch: '$MOD_STATE': completing patching transition
+$MOD_STATE: post_patch_callback: vmlinux
+$MOD_STATE: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE': patching complete
+% insmod test_modules/$MOD_STATE2.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE2'
+livepatch: '$MOD_STATE2': initializing patching transition
+$MOD_STATE2: pre_patch_callback: vmlinux
+$MOD_STATE2: allocate_loglevel_state: space to store console_loglevel already allocated
+livepatch: '$MOD_STATE2': starting patching transition
+livepatch: '$MOD_STATE2': completing patching transition
+$MOD_STATE2: post_patch_callback: vmlinux
+$MOD_STATE2: fix_console_loglevel: taking over the console_loglevel change
+livepatch: '$MOD_STATE2': patching complete
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE2/enabled
+livepatch: '$MOD_STATE2': initializing unpatching transition
+$MOD_STATE2: pre_unpatch_callback: vmlinux
+$MOD_STATE2: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE2': starting unpatching transition
+livepatch: '$MOD_STATE2': completing unpatching transition
+$MOD_STATE2: post_unpatch_callback: vmlinux
+$MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE2': unpatching complete
+% rmmod $MOD_STATE2
+% rmmod $MOD_STATE"
+
+
+# - load a livepatch with provides=1 that registers state ID 1
+# - try to load another livepatch with provides=2 that reuses the same
+#   state ID. The second one must be rejected because livepatches with
+#   different provides ids must not share the same state id.
+# - disable and unload the remaining livepatch
+
+start_test "same state, misc provides"
+
+load_lp $MOD_STATE state=1 provides=1
+load_failing_mod $MOD_STATE2 state=1 provides=2
+
+disable_lp $MOD_STATE
+unload_lp $MOD_STATE
+
+check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE'
+livepatch: '$MOD_STATE': initializing patching transition
+$MOD_STATE: pre_patch_callback: vmlinux
+$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE': starting patching transition
+livepatch: '$MOD_STATE': completing patching transition
+$MOD_STATE: post_patch_callback: vmlinux
+$MOD_STATE: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE': patching complete
+% insmod test_modules/$MOD_STATE2.ko state=1 provides=2
+livepatch: Livepatch patch ($MOD_STATE2) is not compatible with the already installed livepatches.
+insmod: ERROR: could not insert module test_modules/$MOD_STATE2.ko: Invalid parameters
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE/enabled
+livepatch: '$MOD_STATE': initializing unpatching transition
+$MOD_STATE: pre_unpatch_callback: vmlinux
+$MOD_STATE: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE': starting unpatching transition
+livepatch: '$MOD_STATE': completing unpatching transition
+$MOD_STATE: post_unpatch_callback: vmlinux
+$MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE': unpatching complete
+% rmmod $MOD_STATE"
+
+
+# Take over system state change by a patch that obsoletes the old one.
+# Although the provides IDs are different, the second patch's obsoletes
+# list includes the first patch's provides ID, so it can replace the
+# first patch and reuse the same state ID.
+
+start_test "same states, replaced by obsoletes"
+
+load_lp $MOD_STATE state=1 provides=1
+load_lp $MOD_STATE2 state=1 provides=2 obsoletes=1
+unload_lp $MOD_STATE
+disable_lp $MOD_STATE2
+unload_lp $MOD_STATE2
+
+check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE'
+livepatch: '$MOD_STATE': initializing patching transition
+$MOD_STATE: pre_patch_callback: vmlinux
+$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE': starting patching transition
+livepatch: '$MOD_STATE': completing patching transition
+$MOD_STATE: post_patch_callback: vmlinux
+$MOD_STATE: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE': patching complete
+% insmod test_modules/$MOD_STATE2.ko state=1 provides=2 obsoletes=1
+livepatch: enabling patch '$MOD_STATE2'
+livepatch: '$MOD_STATE2': initializing patching transition
+$MOD_STATE2: pre_patch_callback: vmlinux
+$MOD_STATE2: allocate_loglevel_state: space to store console_loglevel already allocated
+livepatch: '$MOD_STATE2': starting patching transition
+livepatch: '$MOD_STATE2': completing patching transition
+$MOD_STATE2: post_patch_callback: vmlinux
+$MOD_STATE2: fix_console_loglevel: taking over the console_loglevel change
+livepatch: '$MOD_STATE2': patching complete
+% rmmod $MOD_STATE
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE2/enabled
+livepatch: '$MOD_STATE2': initializing unpatching transition
+$MOD_STATE2: pre_unpatch_callback: vmlinux
+$MOD_STATE2: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE2': starting unpatching transition
+livepatch: '$MOD_STATE2': completing unpatching transition
+$MOD_STATE2: post_unpatch_callback: vmlinux
+$MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE2': unpatching complete
+% rmmod $MOD_STATE2"
+
+
+# - load a livepatch that modifies system state (state=1) with
+#   provides=1, then another livepatch with the same provides id=1 that declares
+#   a different system state (state=2)
+# - the second livepatch would replace the first one due to the same
+#   provides id, but it does not handle the system state modified by
+#   the first one, therefore it is rejected as incompatible and the
+#   insmod must fail.
+# - verify the first livepatch is still alive, then disable and
+#   unload it.
+
+start_test "misc states, same provides"
+load_lp $MOD_STATE state=1 provides=1
+load_failing_mod $MOD_STATE2 state=2 provides=1
+
+disable_lp $MOD_STATE
+unload_lp $MOD_STATE
+
+check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE'
+livepatch: '$MOD_STATE': initializing patching transition
+$MOD_STATE: pre_patch_callback: vmlinux
+$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE': starting patching transition
+livepatch: '$MOD_STATE': completing patching transition
+$MOD_STATE: post_patch_callback: vmlinux
+$MOD_STATE: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE': patching complete
+% insmod test_modules/$MOD_STATE2.ko state=2 provides=1
+livepatch: Livepatch patch ($MOD_STATE2) is not compatible with the already installed livepatches.
+insmod: ERROR: could not insert module test_modules/$MOD_STATE2.ko: Invalid parameters
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE/enabled
+livepatch: '$MOD_STATE': initializing unpatching transition
+$MOD_STATE: pre_unpatch_callback: vmlinux
+$MOD_STATE: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE': starting unpatching transition
+livepatch: '$MOD_STATE': completing unpatching transition
+$MOD_STATE: post_unpatch_callback: vmlinux
+$MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE': unpatching complete
+% rmmod $MOD_STATE"
+
+
+# - load two livepatches with different provides ids (provides=1 and
+#   provides=2) that modify different system states (state=1 and
+#   state=2). Neither of them replaces the other, because their
+#   provides ids differ (and the obsoletes lists are empty), so they
+#   can coexist.
+# - verify the provides sysfs value of both livepatches
+# - disable and unload both livepatches
+
+start_test "misc states, misc provides"
+
+load_lp $MOD_STATE state=1 provides=1
+load_lp $MOD_STATE2 state=2 provides=2
+
+check_sysfs_value "$MOD_STATE" "provides" "1"
+check_sysfs_value "$MOD_STATE2" "provides" "2"
+
+disable_lp $MOD_STATE2
+unload_lp $MOD_STATE2
+disable_lp $MOD_STATE
+unload_lp $MOD_STATE
+
+check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
+livepatch: enabling patch '$MOD_STATE'
+livepatch: '$MOD_STATE': initializing patching transition
+$MOD_STATE: pre_patch_callback: vmlinux
+$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE': starting patching transition
+livepatch: '$MOD_STATE': completing patching transition
+$MOD_STATE: post_patch_callback: vmlinux
+$MOD_STATE: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE': patching complete
+% insmod test_modules/$MOD_STATE2.ko state=2 provides=2
+livepatch: enabling patch '$MOD_STATE2'
+livepatch: '$MOD_STATE2': initializing patching transition
+$MOD_STATE2: pre_patch_callback: vmlinux
+$MOD_STATE2: allocate_loglevel_state: allocating space to store console_loglevel
+livepatch: '$MOD_STATE2': starting patching transition
+livepatch: '$MOD_STATE2': completing patching transition
+$MOD_STATE2: post_patch_callback: vmlinux
+$MOD_STATE2: fix_console_loglevel: fixing console_loglevel
+livepatch: '$MOD_STATE2': patching complete
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE2/enabled
+livepatch: '$MOD_STATE2': initializing unpatching transition
+$MOD_STATE2: pre_unpatch_callback: vmlinux
+$MOD_STATE2: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE2': starting unpatching transition
+livepatch: '$MOD_STATE2': completing unpatching transition
+$MOD_STATE2: post_unpatch_callback: vmlinux
+$MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE2': unpatching complete
+% rmmod $MOD_STATE2
+% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE/enabled
+livepatch: '$MOD_STATE': initializing unpatching transition
+$MOD_STATE: pre_unpatch_callback: vmlinux
+$MOD_STATE: restore_console_loglevel: restoring console_loglevel
+livepatch: '$MOD_STATE': starting unpatching transition
+livepatch: '$MOD_STATE': completing unpatching transition
+$MOD_STATE: post_unpatch_callback: vmlinux
+$MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel
+livepatch: '$MOD_STATE': unpatching complete
+% rmmod $MOD_STATE"
+
 exit 0
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +30 -6
index f53eead9d55b..3511cf081fef 100644
@@ -9,10 +9,25 @@
 #include <linux/printk.h>
 #include <linux/livepatch.h>
 
-#define CONSOLE_LOGLEVEL_STATE 1
 /* Version 1 does not support migration. */
 #define CONSOLE_LOGLEVEL_STATE_VERSION 1
 
+static unsigned int state = 1;
+module_param(state, uint, 0444);
+MODULE_PARM_DESC(state, "console loglevel state (default=1)");
+
+#ifdef CONFIG_KLP_HAS_PROVIDES
+static unsigned int provides;
+module_param(provides, uint, 0444);
+MODULE_PARM_DESC(provides, "provides id (default=0)");
+
+#define KLP_MAX_OBSOLETES 16
+static unsigned int obsoletes[KLP_MAX_OBSOLETES];
+static int nr_obsoletes;
+module_param_array(obsoletes, uint, &nr_obsoletes, 0444);
+MODULE_PARM_DESC(obsoletes, "obsoletes provides ids");
+#endif
+
 static const char *const module_state[] = {
 	[MODULE_STATE_LIVE]	= "[MODULE_STATE_LIVE] Normal state",
 	[MODULE_STATE_COMING]	= "[MODULE_STATE_COMING] Full formed, running module_init",
@@ -35,7 +50,7 @@ static int allocate_loglevel_state(void)
 {
 	struct klp_state *loglevel_state;
 
-	loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
+	loglevel_state = klp_get_state(&patch, state);
 	if (!loglevel_state)
 		return -EINVAL;
 
@@ -52,7 +67,7 @@ static void fix_console_loglevel(void)
 {
 	struct klp_state *loglevel_state;
 
-	loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
+	loglevel_state = klp_get_state(&patch, state);
 	if (!loglevel_state)
 		return;
 
@@ -65,7 +80,7 @@ static void restore_console_loglevel(void)
 {
 	struct klp_state *loglevel_state;
 
-	loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
+	loglevel_state = klp_get_state(&patch, state);
 	if (!loglevel_state)
 		return;
 
@@ -77,7 +92,7 @@ static void free_loglevel_state(void)
 {
 	struct klp_state *loglevel_state;
 
-	loglevel_state = klp_get_state(&patch, CONSOLE_LOGLEVEL_STATE);
+	loglevel_state = klp_get_state(&patch, state);
 	if (!loglevel_state)
 		return;
 
@@ -133,7 +148,6 @@ static struct klp_object objs[] = {
 
 static struct klp_state states[] = {
 	{
-		.id = CONSOLE_LOGLEVEL_STATE,
 		.version = CONSOLE_LOGLEVEL_STATE_VERSION,
 	}, { }
 };
@@ -151,6 +165,16 @@ static struct klp_patch patch = {
 
 static int test_klp_callbacks_demo_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	patch.provides = provides;
+	if (nr_obsoletes > 0) {
+		patch.obsoletes = obsoletes;
+		patch.nr_obsoletes = nr_obsoletes;
+	}
+#else
+	patch.replace = true;
+#endif
+	states[0].id = state;
 	return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +34 -10

110 lines not on this page · raw message

Signature · 1 line
2.52.0
maintainer Reviewed-by Tested-by on v12 8/9
#19 lore raw

Re: [PATCH v12 8/9] selftests/livepatch: Add state test for provides/obsoletes

Quoted 12 lines · Yafang Shao

Livepatches with different provides ids must not share the same state id, unless the second livepatch replaces the first one via obsoletes.

Add provides/obsoletes module parameters to test_klp_state and test_klp_state2, and cover both cases: reusing a state id across different provides ids is rejected, while it is allowed when the new livepatch obsoletes the old one.

tools/testing/selftests/livepatch/test_modules/test_klp_state.c +0 -0
@@ -151,6 +165,16 @@ static struct klp_patch patch = {
 

tools/testing/selftests/livepatch/test_modules/test_klp_state.c static struct klp_patch patch = { · line 175

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +8 -0
 static int test_klp_callbacks_demo_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	patch.provides = provides;
+	if (nr_obsoletes > 0) {
+		patch.obsoletes = obsoletes;
+		patch.nr_obsoletes = nr_obsoletes;
+	}
+#else
+	patch.replace = true;

This changes the behavior. The original patch did not replace others.

I believe that it would break the last test in test-sysfs.sh. It passes with this patchset because the problematic test is skipped. But it would break the test when it is used for older kernels.

I guess that it is a cut-&-paste mistake ;-)

Quoted 11 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +2 -0
+#endif
+	states[0].id = state;
 	return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +0 -0
index 5f07fe1be2b9..aeb911b87d37 100644
@@ -180,6 +194,16 @@ static struct klp_patch patch = {
 

tools/testing/selftests/livepatch/test_modules/test_klp_state2.c static struct klp_patch patch = { · line 204

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +8 -0
 static int test_klp_callbacks_demo_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+	patch.provides = provides;
+	if (nr_obsoletes > 0) {
+		patch.obsoletes = obsoletes;
+		patch.nr_obsoletes = nr_obsoletes;
+	}
+#else
+	patch.replace = true;

Same here:

tools/testing/selftests/livepatch/test_modules/test_klp_state2.c static struct klp_patch patch = { · line 208

Quoted 4 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +2 -0
+#endif
+	states[0].id = state;
 	return klp_enable_patch(&patch);
 }

Otherwise, it looks good to me and passes the test.

With the "patch.replace = true;" lines removed:

Best Regards,
Petr

#20 lore raw

Re: [PATCH v12 8/9] selftests/livepatch: Add state test for provides/obsoletes

Quoted 28 lines · Petr Mladek
Quoted 12 lines · Yafang Shao

Livepatches with different provides ids must not share the same state id, unless the second livepatch replaces the first one via obsoletes.

Add provides/obsoletes module parameters to test_klp_state and test_klp_state2, and cover both cases: reusing a state id across different provides ids is rejected, while it is allowed when the new livepatch obsoletes the old one.

tools/testing/selftests/livepatch/test_modules/test_klp_state.c +0 -0
@@ -151,6 +165,16 @@ static struct klp_patch patch = {
 

tools/testing/selftests/livepatch/test_modules/test_klp_state.c static struct klp_patch patch = { · line 175

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +8 -0
 static int test_klp_callbacks_demo_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+     patch.provides = provides;
+     if (nr_obsoletes > 0) {
+             patch.obsoletes = obsoletes;
+             patch.nr_obsoletes = nr_obsoletes;
+     }
+#else
+     patch.replace = true;

This changes the behavior. The original patch did not replace others.

Quoted 6 lines

I believe that it would break the last test in test-sysfs.sh. It passes with this patchset because the problematic test is skipped. But it would break the test when it is used for older kernels.

I guess that it is a cut-&-paste mistake ;-)

Ah, my bad. I will update it.

Quoted 30 lines
Quoted 12 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +2 -0
+#endif
+     states[0].id = state;
      return klp_enable_patch(&patch);
 }
 
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +0 -0
index 5f07fe1be2b9..aeb911b87d37 100644
@@ -180,6 +194,16 @@ static struct klp_patch patch = {
 

tools/testing/selftests/livepatch/test_modules/test_klp_state2.c static struct klp_patch patch = { · line 204

Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +8 -0
 static int test_klp_callbacks_demo_init(void)
 {
+#ifdef CONFIG_KLP_HAS_PROVIDES
+     patch.provides = provides;
+     if (nr_obsoletes > 0) {
+             patch.obsoletes = obsoletes;
+             patch.nr_obsoletes = nr_obsoletes;
+     }
+#else
+     patch.replace = true;

Same here:

tools/testing/selftests/livepatch/test_modules/test_klp_state2.c static struct klp_patch patch = { · line 208

Quoted 4 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +2 -0
+#endif
+     states[0].id = state;
      return klp_enable_patch(&patch);
 }
Quoted 7 lines

Otherwise, it looks good to me and passes the test.

With the "patch.replace = true;" lines removed:

Thanks so much for taking the time to review this series.

Signature · 2 lines
Regards
Yafang
#21 lore raw

PATCH v12 9/9 selftests/livepatch: Add function test for provides/obsoletes

In reply to Yafang Shao

Livepatches with different provides ids must not modify the same function, unless they share the same provides id or the second livepatch obsoletes the first one.

Add test scenarios that verify this rule on the combination of patched functions and provides ids.

These tests are skipped on kernels that still support the legacy "replace" attribute.

---
 .../livepatch/test-provides-obsoletes.sh      | 188 ++++++++++++++++++
 .../selftests/livepatch/test_modules/Makefile |   1 +
 .../test_modules/test_klp_meminfo_lp2.c       |   2 +
 3 files changed, 191 insertions(+)
 create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +188 -0
index b92188fcf45f..fce421a45fdb 100755
@@ -5,6 +5,7 @@
 . $(dirname $0)/functions.sh
 
 MOD_MEMINFO=test_klp_meminfo_lp
+MOD_MEMINFO2=test_klp_meminfo_lp2
 MOD_CMDLINE=test_klp_cmdline_lp
 MOD_STATE=test_klp_state
 MOD_STATE2=test_klp_state2
@@ -465,4 +466,191 @@ $MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel
 livepatch: '$MOD_STATE': unpatching complete
 % rmmod $MOD_STATE"
 
+
+# - load two livepatches that modify the same function
+#   (meminfo_proc_show), both with provides=1. The second livepatch
+#   replaces the first one because of the same provides id, even
+#   though they share the patched function.
+# - verify that the first livepatch is gone from sysfs (replaced)
+#   and that the second one is enabled
+# - unload the replacing livepatch, then the replaced one
+
+start_test "same function, replaced by provides"
+
+load_lp $MOD_MEMINFO provides=1
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+
+load_lp $MOD_MEMINFO2 provides=1
+
+loop_until '[[ ! -e "$SYSFS_KLP_DIR/$MOD_MEMINFO" ]]' ||
+	die "failed to replace $MOD_MEMINFO"
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+
+check_sysfs_value "$MOD_MEMINFO2" "provides" "1"
+
+disable_lp $MOD_MEMINFO2
+unload_lp $MOD_MEMINFO2
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+
+unload_lp $MOD_MEMINFO
+
+check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1
+livepatch: enabling patch '$MOD_MEMINFO'
+livepatch: '$MOD_MEMINFO': initializing patching transition
+livepatch: '$MOD_MEMINFO': starting patching transition
+livepatch: '$MOD_MEMINFO': completing patching transition
+livepatch: '$MOD_MEMINFO': patching complete
+$MOD_MEMINFO: this has been live patched
+% insmod test_modules/$MOD_MEMINFO2.ko provides=1
+livepatch: enabling patch '$MOD_MEMINFO2'
+livepatch: '$MOD_MEMINFO2': initializing patching transition
+livepatch: '$MOD_MEMINFO2': starting patching transition
+livepatch: '$MOD_MEMINFO2': completing patching transition
+livepatch: '$MOD_MEMINFO2': patching complete
+$MOD_MEMINFO2: this has been live patched
+% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO2/enabled
+livepatch: '$MOD_MEMINFO2': initializing unpatching transition
+livepatch: '$MOD_MEMINFO2': starting unpatching transition
+livepatch: '$MOD_MEMINFO2': completing unpatching transition
+livepatch: '$MOD_MEMINFO2': unpatching complete
+% rmmod $MOD_MEMINFO2
+% rmmod $MOD_MEMINFO"
+
+
+# - load a livepatch that modifies meminfo_proc_show with provides=1,
+#   then try to load another livepatch that modifies the same
+#   function with a different provides id (provides=2).
+# - since the provides ids differ, the second livepatch would not
+#   replace the first one; the same function can not be handled by
+#   two different provides groups in parallel, so the load must fail.
+# - unload the first livepatch
+
+start_test "same function, can't be loaded in parallel"
+
+load_lp $MOD_MEMINFO provides=1
+load_failing_mod $MOD_MEMINFO2 provides=2
+
+disable_lp $MOD_MEMINFO
+unload_lp $MOD_MEMINFO
+
+check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1
+livepatch: enabling patch '$MOD_MEMINFO'
+livepatch: '$MOD_MEMINFO': initializing patching transition
+livepatch: '$MOD_MEMINFO': starting patching transition
+livepatch: '$MOD_MEMINFO': completing patching transition
+livepatch: '$MOD_MEMINFO': patching complete
+% insmod test_modules/$MOD_MEMINFO2.ko provides=2
+livepatch: Livepatch patch ($MOD_MEMINFO2) is not compatible with the already installed livepatches.
+insmod: ERROR: could not insert module test_modules/$MOD_MEMINFO2.ko: Invalid parameters
+% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO/enabled
+livepatch: '$MOD_MEMINFO': initializing unpatching transition
+livepatch: '$MOD_MEMINFO': starting unpatching transition
+livepatch: '$MOD_MEMINFO': completing unpatching transition
+livepatch: '$MOD_MEMINFO': unpatching complete
+% rmmod $MOD_MEMINFO"
+
+
+# - load a livepatch that modifies meminfo_proc_show with provides=1,
+#   then another livepatch with provides=1 that modifies a different
+#   function (cmdline_proc_show). The second one replaces the first
+#   one because of the same provides id.
+# - since the patched functions differ, the replacement does not need
+#   to restore the old function body; the transition only switches
+#   the ftrace nops, i.e. the atomic replace (nops) path.
+# - after the replacement /proc/meminfo is no longer patched, while
+#   /proc/cmdline is patched
+# - unload the replacing livepatch, then the replaced one
+
+start_test "different functions, atomic replace works (nops)"
+
+load_lp $MOD_MEMINFO provides=1
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+
+load_lp $MOD_CMDLINE provides=1
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+grep 'live patched' /proc/cmdline > /dev/kmsg
+
+disable_lp $MOD_CMDLINE
+unload_lp $MOD_CMDLINE
+unload_lp $MOD_MEMINFO
+
+check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1
+livepatch: enabling patch '$MOD_MEMINFO'
+livepatch: '$MOD_MEMINFO': initializing patching transition
+livepatch: '$MOD_MEMINFO': starting patching transition
+livepatch: '$MOD_MEMINFO': completing patching transition
+livepatch: '$MOD_MEMINFO': patching complete
+$MOD_MEMINFO: this has been live patched
+% insmod test_modules/$MOD_CMDLINE.ko provides=1
+livepatch: enabling patch '$MOD_CMDLINE'
+livepatch: '$MOD_CMDLINE': initializing patching transition
+livepatch: '$MOD_CMDLINE': starting patching transition
+livepatch: '$MOD_CMDLINE': completing patching transition
+livepatch: '$MOD_CMDLINE': patching complete
+$MOD_CMDLINE: this has been live patched
+% echo 0 > $SYSFS_KLP_DIR/$MOD_CMDLINE/enabled
+livepatch: '$MOD_CMDLINE': initializing unpatching transition
+livepatch: '$MOD_CMDLINE': starting unpatching transition
+livepatch: '$MOD_CMDLINE': completing unpatching transition
+livepatch: '$MOD_CMDLINE': unpatching complete
+% rmmod $MOD_CMDLINE
+% rmmod $MOD_MEMINFO"
+
+
+# - load two livepatches with different provides ids (1 and 2) that
+#   modify different functions (meminfo_proc_show and
+#   cmdline_proc_show). Neither of them replaces the other, so both
+#   livepatches must coexist.
+# - verify that both /proc/meminfo and /proc/cmdline are patched
+# - disable and unload both livepatches
+
+start_test "different functions, install in parallel"
+
+load_lp $MOD_MEMINFO provides=1
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+
+load_lp $MOD_CMDLINE provides=2
+
+grep 'live patched' /proc/meminfo > /dev/kmsg
+grep 'live patched' /proc/cmdline > /dev/kmsg
+
+disable_lp $MOD_MEMINFO
+unload_lp $MOD_MEMINFO
+disable_lp $MOD_CMDLINE
+unload_lp $MOD_CMDLINE
+
+check_result "% insmod test_modules/$MOD_MEMINFO.ko provides=1
+livepatch: enabling patch '$MOD_MEMINFO'
+livepatch: '$MOD_MEMINFO': initializing patching transition
+livepatch: '$MOD_MEMINFO': starting patching transition
+livepatch: '$MOD_MEMINFO': completing patching transition
+livepatch: '$MOD_MEMINFO': patching complete
+$MOD_MEMINFO: this has been live patched
+% insmod test_modules/$MOD_CMDLINE.ko provides=2
+livepatch: enabling patch '$MOD_CMDLINE'
+livepatch: '$MOD_CMDLINE': initializing patching transition
+livepatch: '$MOD_CMDLINE': starting patching transition
+livepatch: '$MOD_CMDLINE': completing patching transition
+livepatch: '$MOD_CMDLINE': patching complete
+$MOD_MEMINFO: this has been live patched
+$MOD_CMDLINE: this has been live patched
+% echo 0 > $SYSFS_KLP_DIR/$MOD_MEMINFO/enabled
+livepatch: '$MOD_MEMINFO': initializing unpatching transition
+livepatch: '$MOD_MEMINFO': starting unpatching transition
+livepatch: '$MOD_MEMINFO': completing unpatching transition
+livepatch: '$MOD_MEMINFO': unpatching complete
+% rmmod $MOD_MEMINFO
+% echo 0 > $SYSFS_KLP_DIR/$MOD_CMDLINE/enabled
+livepatch: '$MOD_CMDLINE': initializing unpatching transition
+livepatch: '$MOD_CMDLINE': starting unpatching transition
+livepatch: '$MOD_CMDLINE': completing unpatching transition
+livepatch: '$MOD_CMDLINE': unpatching complete
+% rmmod $MOD_CMDLINE"
+
 exit 0
tools/testing/selftests/livepatch/test_modules/Makefile +1 -0
index 732e0003930b..0c17754997d2 100644
@@ -2,6 +2,7 @@ TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
 KDIR ?= /lib/modules/$(shell uname -r)/build
 
 obj-m += test_klp_meminfo_lp.o \
+	test_klp_meminfo_lp2.o \
 	test_klp_callbacks_busy.o \
 	test_klp_callbacks_demo.o \
 	test_klp_callbacks_demo2.o \
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c new file +2 -0
new file mode 100644
index 000000000000..2b93c969b7ba
@@ -0,0 +1,2 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "test_klp_meminfo_lp.c"
Signature · 1 line
2.52.0
maintainer Reviewed-by Tested-by on v12 9/9
#22 lore raw

Re: [PATCH v12 9/9] selftests/livepatch: Add function test for provides/obsoletes

Quoted 4 lines · Yafang Shao

Livepatches with different provides ids must not modify the same function, unless they share the same provides id or the second livepatch obsoletes the first one.

Quoted 8 lines

Add test scenarios that verify this rule on the combination of patched functions and provides ids.

These tests are skipped on kernels that still support the legacy "replace" attribute.

Looks good to me and passes the selftests:

Best Regards,
Petr