livepatch: Add support for scoped atomic replace
Subsystems (3)
v8 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 any 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, or
3. the new patch's provides id is listed in its obsoletes list (the obsoletes relationship is symmetric).
Condition 3 keeps the model symmetric: a patch that declares "I obsolete provides id X" implicitly accepts that any future patch with provides id X may also replace it.
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
=======
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 | 30 +-
.../livepatch/cumulative-patches.rst | 99 ++-
Documentation/livepatch/livepatch.rst | 25 +-
include/linux/livepatch.h | 8 +-
kernel/livepatch/Kconfig | 14 +
kernel/livepatch/core.c | 124 ++--
kernel/livepatch/core.h | 2 +
kernel/livepatch/state.c | 58 +-
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 | 30 +
.../selftests/livepatch/test-callbacks.sh | 209 +++---
.../selftests/livepatch/test-ftrace.sh | 2 +-
.../selftests/livepatch/test-kprobe.sh | 12 +-
.../selftests/livepatch/test-livepatch.sh | 309 +++++----
.../livepatch/test-provides-obsoletes.sh | 628 ++++++++++++++++++
.../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 | 1 +
.../livepatch/test_modules/test_klp_state.c | 37 +-
.../livepatch/test_modules/test_klp_state2.c | 45 +-
...lp_syscall.c => test_klp_syscall_getpid.c} | 4 +-
30 files changed, 1385 insertions(+), 420 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
PATCH v8 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v8 1/9] selftests/livepatch: Clarify test module file names
Quoted 6 lines · Yafang Shao
Rename the test module files to be more descriptive of what they actually test, as suggested by Petr.
Suggested-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>
PATCH v8 2/9 selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
In reply to Yafang ShaoThe 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.
The provides/obsoletes based selftests will be added later after the provides/obsoletes are substituted.
--- .../testing/selftests/livepatch/functions.sh | 30 ++ .../selftests/livepatch/test-callbacks.sh | 209 ++++++------ .../selftests/livepatch/test-livepatch.sh | 303 +++++++++--------- .../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 + 7 files changed, 347 insertions(+), 241 deletions(-)
tools/testing/selftests/livepatch/functions.sh +30 -0
@@ -314,6 +314,15 @@ function start_test { log "===== TEST: $test =====" } +function skip_test { + local test="$1" + local reason="$2" + + echo -n "TEST: $test ... " + echo "SKIP ($reason)" + log "===== TEST: $test SKIPPED: $reason =====" +} + # check_result() - verify dmesg output # TODO - better filter, out of order msgs, etc? function check_result {
@@ -357,6 +366,27 @@ function does_sysfs_exist() { [[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]] } +# detect_provides_attr() - detect whether the running kernel supports the +# livepatch "provides" attribute and set HAS_PROVIDES_ATTR accordingly. +# The provides/obsoletes based tests are only run when HAS_PROVIDES_ATTR +# is set. +function detect_provides_attr() { + HAS_PROVIDES_ATTR=0 + + if [[ -r /proc/config.gz ]] && + zgrep -q "CONFIG_KLP_HAS_PROVIDES=y" /proc/config.gz 2>/dev/null; then + HAS_PROVIDES_ATTR=1 + return 0 + fi + + load_lp test_klp_cmdline_lp + if does_sysfs_exist test_klp_cmdline_lp "provides"; then + HAS_PROVIDES_ATTR=1 + fi + disable_lp test_klp_cmdline_lp + unload_lp test_klp_cmdline_lp +} + # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs # path permissions # modname - livepatch module creating the sysfs interface
tools/testing/selftests/livepatch/test-callbacks.sh +112 -97
221 lines not on this page · raw message
tools/testing/selftests/livepatch/test-livepatch.sh +159 -144
318 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifndef CONFIG_KLP_HAS_PROVIDES static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#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 ) { +#ifndef CONFIG_KLP_HAS_PROVIDES patch.replace = replace; +#else +/* +* TODO: Set provides/obsoletes from the module parameters +* for the provides/obsoletes based tests (to be added later). +*/ +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifndef CONFIG_KLP_HAS_PROVIDES static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#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 ) { +#ifndef CONFIG_KLP_HAS_PROVIDES patch.replace = replace; +#else +/* +* TODO: Set provides/obsoletes from the module parameters +* for the provides/obsoletes based tests (to be added later). +*/ +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +7 -0
@@ -142,7 +142,14 @@ static struct klp_patch patch = { .mod = THIS_MODULE, .objs = objs, .states = states, +#ifndef CONFIG_KLP_HAS_PROVIDES .replace = true, +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif };static int test_klp_callbacks_demo_init (void )
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +7 -0
@@ -171,7 +171,14 @@ static struct klp_patch patch = { .mod = THIS_MODULE, .objs = objs, .states = states, +#ifndef CONFIG_KLP_HAS_PROVIDES .replace = true, +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif };static int test_klp_callbacks_demo_init (void )
Signature · 1 line
2.52.0
Re: [PATCH v8 2/9] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 11 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwill 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.
Quoted 5 lines
The provides/obsoletes based selftests will be added later after the provides/obsoletes are substituted.
Suggested-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>
Shall we add functions for code blocks inside
if [[ "$HAS_PROVIDES_ATTR" != "1" ]]; then
etc.?
Other than this.
Re: [PATCH v8 2/9] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 16 lines · Song Liu
Quoted 14 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwill 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.
The provides/obsoletes based selftests will be added later after the provides/obsoletes are substituted.
Quoted 8 lines
Quoted 2 lines
Suggested-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>Shall we add functions for code blocks inside
if [[ "$HAS_PROVIDES_ATTR" != "1" ]]; thenetc.?
Makes sense. It will be clearer.
I will rework it in the next version.
Quoted 4 lines
Other than this.
Acked-by Song Liu <[email protected]>
Signature · 2 lines
Regards Yafang
PATCH v8 3/9 livepatch: Make klp_find_func() non static
In reply to Yafang ShaoMake 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
@@ -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
@@ -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
PATCH v8 4/9 livepatch: Call klp_init_patch_early() earlier
In reply to Yafang ShaoInvoke 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
@@ -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
[email protected]
AI reviewRe: [PATCH v8 4/9] livepatch: Call klp_init_patch_early() earlier
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Fatal UAF / Kernel Panic on module unload due to unparented kobjects triggering delayed work in freed memory. --
commit 92da60558ebea290d37fbe304d56429622a3eb02
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.
Quoted 4 lines
kernel/livepatch/core.c +0
-0
[ ... ]
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1154
Quoted 11 lines
kernel/livepatch/core.c +7
-0@@ -1152,10 +1145,17 @@ 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: High]
This is a pre-existing issue, but moving the compatibility check after klp_init_patch_early() makes it highly reachable when an incompatible patch is loaded. Could this lead to a use-after-free and kernel panic during module unload?
When klp_init_patch_early() runs, it calls kobject_init() for child obj and func objects. However, their parent pointers are not set until later in klp_init_patch() via kobject_add().
If klp_is_patch_compatible() fails here, the error path calls klp_free_patch_start() which puts these unparented child kobjects. Under CONFIG_DEBUG_KOBJECT_RELEASE, their release functions are scheduled asynchronously with a random delay.
Because the children lack parent pointers, they do not hold a reference to patch->kobj. As a result, klp_free_patch_finish() will complete immediately when waiting on patch->finish, and module_put(patch->mod) allows the module to unload, freeing the statically allocated obj and func structures.
Later, when delayed release functions like klp_kobj_release_object() finally execute, they will dereference obj->dynamic in freed memory.
Quoted 3 lines
kernel/livepatch/core.c +0
-0ret =klp_init_patch (patch);if (ret) goto err;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v8 4/9] livepatch: Call klp_init_patch_early() earlier
Quoted 5 lines · On Wed, Sep 9, 2026 at 10:56 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Fatal UAF / Kernel Panic on module unload due to unparented kobjects triggering delayed work in freed memory.
This has already been fixed by commit 7f4ca0d0b40a ("livepatch: Fix UAF of unregistered patch kobjects"), which is in the livepatching for-next branch:
It seems sashiko-bot's review was not generated against the livepatching for-next branch.
Signature · 2 lines
Regards Yafang
PATCH v8 5/9 livepatch: Implement provides and obsoletes for scoped atomic replace
In reply to Yafang ShaoThe 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 any 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, or
3. The new patch's provides id is listed in its obsoletes list (the obsoletes relationship is symmetric).
The obsoletes relationship is symmetric: two livepatches that obsolete each other are mutually exclusive. Loading either one atomically removes the other, so they can never be enabled at the same time. This allows swapping between two independently prepared livepatches that cover the same scope, in either direction.
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.
--- .../ABI/removed/sysfs-kernel-livepatch | 7 ++ .../ABI/testing/sysfs-kernel-livepatch | 25 ++++- .../livepatch/cumulative-patches.rst | 99 +++++++++++++------ Documentation/livepatch/livepatch.rst | 25 +++-- include/linux/livepatch.h | 8 +- kernel/livepatch/Kconfig | 14 +++ kernel/livepatch/core.c | 80 +++++++++++++-- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 58 +++++++++-- kernel/livepatch/transition.c | 15 +-- scripts/livepatch/init.c | 19 ++-- scripts/livepatch/klp-build | 23 +++-- 12 files changed, 291 insertions(+), 83 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
Documentation/ABI/removed/sysfs-kernel-livepatch new file +7 -0
@@ -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 +20 -5
@@ -47,13 +47,28 @@ 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. + The obsoletes relationship is symmetric: an existing + livepatch that obsoletes this patch's provides id will + also be atomically replaced by this patch. What: /sys/kernel/livepatch/<patch>/stack_order Date: Jan 2025
Documentation/livepatch/cumulative-patches.rst +71 -28
111 lines not on this page · raw message
Documentation/livepatch/livepatch.rst +16 -9
@@ -347,15 +347,22 @@ 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. + +The replacement is symmetric: if an existing livepatch's ``obsoletes`` +list contains the new patch's ``provides`` id, the new patch will also +atomically replace that existing livepatch. Consequently, two +livepatches that obsolete each other can never be enabled at the same +time, and either of them can be loaded to take over from the other. + +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
@@ -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 +14 -0
@@ -30,3 +30,17 @@ config KLP_BUILD select OBJTOOL help Enable klp-build support + +config KLP_HAS_PROVIDES + 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 + 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.
kernel/livepatch/core.c +70 -10
138 lines not on this page · raw message
kernel/livepatch/core.h +1 -0
@@ -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 +50 -8
83 lines not on this page · raw message
kernel/livepatch/transition.c +8 -7
@@ -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
@@ -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
@@ -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
PATCH v8 6/9 livepatch: Deprecate stack_order
In reply to Yafang ShaoThe 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
@@ -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
@@ -70,15 +70,6 @@ Description: livepatch that obsoletes this patch's provides id will also be atomically replaced by this patch. -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
@@ -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
PATCH v8 7/9 selftests/livepatch: Add provides/obsoletes test scenarios
In reply to Yafang ShaoNow 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 | 216 ++++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 20 +- .../test_modules/test_klp_cmdline_lp.c | 11 +- .../test_modules/test_klp_meminfo_lp.c | 30 ++- .../livepatch/test_modules/test_klp_state.c | 5 +- .../livepatch/test_modules/test_klp_state2.c | 5 +- 7 files changed, 256 insertions(+), 34 deletions(-) create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh
tools/testing/selftests/livepatch/Makefile +2 -1
@@ -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 +216 -0
216 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +8 -12
@@ -7,15 +7,14 @@#include <linux/kernel.h> #include <linux/livepatch.h> -#ifndef CONFIG_KLP_HAS_PROVIDES +#ifdef CONFIG_KLP_HAS_PROVIDES +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); +#else static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); -#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ #endif static const char *const module_state[] = {
@@ -86,13 +85,10 @@ static struct klp_patch patch = {static int test_klp_callbacks_demo2_init (void ) { -#ifndef CONFIG_KLP_HAS_PROVIDES - patch.replace = replace; +#ifdef CONFIG_KLP_HAS_PROVIDES + patch.provides = provides;#else -/* -* TODO: Set provides/obsoletes from the module parameters -* for the provides/obsoletes based tests (to be added later). -*/ + patch.replace = replace;#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c +10 -1
@@ -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,0644 ); +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 +18 -12
@@ -7,15 +7,20 @@#include <linux/kernel.h> #include <linux/livepatch.h> -#ifndef CONFIG_KLP_HAS_PROVIDES +#ifdef CONFIG_KLP_HAS_PROVIDES +static unsigned int provides; +module_param (provides, uint,0644 ); +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,0644 ); +MODULE_PARM_DESC (obsoletes,"obsoletes provides ids" ); +#else static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); -#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ #endif #include <linux/seq_file.h>
@@ -50,13 +55,14 @@ static struct klp_patch patch = {static int test_klp_meminfo_lp_init (void ) { -#ifndef CONFIG_KLP_HAS_PROVIDES - patch.replace = replace; +#ifdef CONFIG_KLP_HAS_PROVIDES + patch.provides = provides; +if (nr_obsoletes >0 ) { + patch.obsoletes = obsoletes; + patch.nr_obsoletes = nr_obsoletes; + }#else -/* -* TODO: Set provides/obsoletes from the module parameters -* for the provides/obsoletes based tests (to be added later). -*/ + patch.replace = replace;#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +1 -4
@@ -145,10 +145,7 @@ static struct klp_patch patch = {#ifndef CONFIG_KLP_HAS_PROVIDES .replace = true,#else -/* -* 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 */ #endif };
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +1 -4
@@ -174,10 +174,7 @@ static struct klp_patch patch = {#ifndef CONFIG_KLP_HAS_PROVIDES .replace = true,#else -/* -* 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 */ #endif };
Signature · 1 line
2.52.0
PATCH v8 8/9 selftests/livepatch: Add state test for provides/obsoletes
In reply to Yafang ShaoLivepatches 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 | 236 ++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 41 ++- .../livepatch/test_modules/test_klp_state2.c | 49 ++-- 3 files changed, 300 insertions(+), 26 deletions(-)
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +236 -0
@@ -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
@@ -213,4 +215,238 @@ livepatch: '$MOD_CMDLINE': unpatching complete % rmmod $MOD_CMDLINE % 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 + +disable_lp $MOD_STATE +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=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_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 +% 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" + exit 0
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +30 -11
90 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +34 -15
119 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v8 8/9] selftests/livepatch: Add state test for provides/obsoletes
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Unloading independent livepatches in FIFO order permanently alters the global…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Unloading independent livepatches in FIFO order permanently alters the global console_loglevel.
- [Medium] Mutable module parameter 'state' causes a memory leak and failure to restore kernel state if modified via sysfs after load.
- [Low] Missing sysfs value verification in test 'misc states, misc provides'.
--
commit 253fd167cb01a1b8aeead3420135a4b275836ef3
Author: Yafang Shao <[email protected]>
Subject: selftests/livepatch: Add state test for provides/obsoletes
This commit adds provides and obsoletes module parameters to test_klp_state and test_klp_state2 selftests. It introduces testing coverage to ensure that reusing a state ID across different provides IDs is rejected, while it is allowed when the new livepatch obsoletes the old one.
Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0@@ -213,4 +215,238 @@ livepatch: '$MOD_CMDLINE': unpatching complete
[ ... ]
tools/testing/selftests/livepatch/test-provides-obsoletes.sh livepatch: '$MOD_CMDLINE': unpatching complete · line 405
Quoted 13 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +13
-0+# - 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 +
[Severity: Low]
The test documentation states it will "verify the provides sysfs value of both livepatches". Is this verification missing here before we disable and unload them?
tools/testing/selftests/livepatch/test-provides-obsoletes.sh livepatch: '$MOD_CMDLINE': unpatching complete · line 316
Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +4
-0+disable_lp $MOD_STATE +unload_lp $MOD_STATE +disable_lp $MOD_STATE2 +unload_lp $MOD_STATE2
[Severity: Medium]
Does unloading these livepatches in FIFO order permanently alter the global console_loglevel?
If MOD_STATE saves the original loglevel and sets it to 15, and then MOD_STATE2 saves 15 and sets it to 15, wouldn't unloading MOD_STATE first restore the original loglevel, only for unloading MOD_STATE2 to subsequently overwrite it with 15? Should the teardown be LIFO to properly unwind the loglevel changes?
Quoted 2 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +2
-0+ +check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1
[ ... ]
Quoted 5 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +0
-0
tools/testing/selftests/livepatch/test_modules/test_klp_state.c line 16
Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +2
-1@@ -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,0644 );
[Severity: Medium]
Could this mutable module parameter cause a memory leak and failure to restore the kernel state if modified via sysfs after load?
Since the permissions are set to 0644, if root modifies the state parameter while the livepatch is loaded, subsequent callbacks like free_loglevel_state() using klp_get_state(&patch, state) will return NULL. Does this silently skip cleanup (kfree) and state restoration during patch unload?
This same issue also applies to test_klp_state2.c.
Quoted 2 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +2
-0+MODULE_PARM_DESC (state,"console loglevel state (default=1)" ); +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v8 8/9] selftests/livepatch: Add state test for provides/obsoletes
Quoted 34 lines · On Wed, Sep 9, 2026 at 10:57 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Unloading independent livepatches in FIFO order permanently alters the globalconsole_loglevel.
- [Medium] Mutable module parameter 'state' causes a memory leak and failure to restore kernel state if modified via sysfs after load.
- [Low] Missing sysfs value verification in test 'misc states, misc provides'.
--commit
253fd167cb01a1b8aeead3420135a4b275836ef3
Author: Yafang Shao <[email protected]>
Subject: selftests/livepatch: Add state test for provides/obsoletesThis commit adds
providesandobsoletesmodule parameters totest_klp_stateandtest_klp_state2selftests. It introduces testing coverage to ensure that reusing a state ID across different provides IDs is rejected, while it is allowed when the new livepatch obsoletes the old one.Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0@@ -213,4 +215,238 @@ livepatch: '$MOD_CMDLINE': unpatching complete[ ... ]
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+# - 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" +
Quoted 8 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh
Quoted 3 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +3
-0+load_lp $MOD_STATE state=1 provides=1 +load_lp $MOD_STATE2 state=2 provides=2 +[Severity: Low]
The test documentation states it will "verify the provides sysfs value of both livepatches". Is this verification missing here before we disable and unload them?
will update it
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +3 -0
@@ -404,6 +404,9 @@ 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_STATE unload_lp $MOD_STATE disable_lp $MOD_STATE2
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh
Quoted 5 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +4
-0+disable_lp $MOD_STATE +unload_lp $MOD_STATE +disable_lp $MOD_STATE2 +unload_lp $MOD_STATE2[Severity: Medium]
Does unloading these livepatches in FIFO order permanently alter the globalconsole_loglevel?
Quoted 5 lines
If
MOD_STATEsaves the original loglevel and sets it to 15, and thenMOD_STATE2saves 15 and sets it to 15, wouldn't unloadingMOD_STATEfirst restore the original loglevel, only for unloadingMOD_STATE2to subsequently overwrite it with 15? Should the teardown be LIFO to properly unwind the loglevel changes?
Ah, it should be LIFO
I will update it.
Quoted 23 lines
Quoted 3 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +2
-0+ +check_result "% insmod test_modules/$MOD_STATE.ko state=1 provides=1[ ... ]
Quoted 5 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +0
-0tools/testing/selftests/livepatch/test_modules/test_klp_state.c line 16
Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +2
-1@@ -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,0644 );[Severity: Medium]
Could this mutable module parameter cause a memory leak and failure to restore the kernel state if modified via sysfs after load?
Quoted 6 lines
Since the permissions are set to 0644, if root modifies the state parameter while the livepatch is loaded, subsequent callbacks like
free_loglevel_state()usingklp_get_state(&patch, state)will return NULL. Does this silently skip cleanup (kfree) and state restoration during patch unload?This same issue also applies to
test_klp_state2.c.
will change it to 0444 to fix it.
Signature · 2 lines
Regards Yafang
PATCH v8 9/9 selftests/livepatch: Add function test for provides/obsoletes
In reply to Yafang ShaoLivepatches 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 | 176 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_meminfo_lp2.c | 1 + 3 files changed, 178 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +176 -0
@@ -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
@@ -449,4 +450,179 @@ $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel livepatch: '$MOD_STATE2': unpatching complete % rmmod $MOD_STATE2" + +# - 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 +load_lp $MOD_MEMINFO2 provides=1 + +loop_until '[[ ! -e "$SYSFS_KLP_DIR/$MOD_MEMINFO" ]]' || + die "failed to replace $MOD_MEMINFO" +check_sysfs_value "$MOD_MEMINFO2" "provides" "1" + +disable_lp $MOD_MEMINFO2 +unload_lp $MOD_MEMINFO2 +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=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 +% 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/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
@@ -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 +1 -0
@@ -0,0 +1 @@ +#include "test_klp_meminfo_lp.c"
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v8 9/9] selftests/livepatch: Add function test for provides/obsoletes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The test fails to verify its own claim that a function is functionally unpatched after an atomic…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test fails to verify its own claim that a function is functionally unpatched after an atomic replacement. --
commit abfccb0026870ac46d37d3802586498b0874cba2
Author: Yafang Shao <[email protected]>
selftests/livepatch: Add function test for provides/obsoletes
This commit adds selftest scenarios for livepatches using the provides and obsoletes features to ensure patches with different provides IDs do not modify the same function. It verifies replacement and coexistence behaviors for different function combinations. Tests are skipped on legacy kernels.
Quoted 3 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0
[ ... ]
Quoted 5 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +5
-0+# - 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)"
tools/testing/selftests/livepatch/test-provides-obsoletes.sh $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel · line 550
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+ +load_lp $MOD_MEMINFO provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +load_lp $MOD_CMDLINE provides=1 + +grep 'live patched' /proc/cmdline > /dev/kmsg + +disable_lp $MOD_CMDLINE
[Severity: Medium]
The test documentation explicitly claims that after the replacement, /proc/meminfo is no longer patched while /proc/cmdline is patched. However, only /proc/cmdline is checked during this sequence.
Should there be an additional step to grep /proc/meminfo and verify it is functionally unpatched before disabling the livepatch? If the atomic replace logic had a flaw that accidentally left /proc/meminfo patched, it appears this verification would be missed and the test would still pass.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v8 9/9] selftests/livepatch: Add function test for provides/obsoletes
Quoted 41 lines · On Wed, Sep 9, 2026 at 10:56 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test fails to verify its own claim that a function is functionally unpatched after an atomic replacement. --commit
abfccb0026870ac46d37d3802586498b0874cba2
Author: Yafang Shao <[email protected]>selftests/livepatch: Add function test for provides/obsoletes
This commit adds selftest scenarios for livepatches using the provides and obsoletes features to ensure patches with different provides IDs do not modify the same function. It verifies replacement and coexistence behaviors for different function combinations. Tests are skipped on legacy kernels.
Quoted 3 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0[ ... ]
Quoted 5 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +5
-0+# - 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)"tools/testing/selftests/livepatch/test-provides-obsoletes.sh
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+ +load_lp $MOD_MEMINFO provides=1 + +grep 'live patched' /proc/meminfo > /dev/kmsg + +load_lp $MOD_CMDLINE provides=1 + +grep 'live patched' /proc/cmdline > /dev/kmsg + +disable_lp $MOD_CMDLINE[Severity: Medium]
The test documentation explicitly claims that after the replacement,/proc/meminfois no longer patched while/proc/cmdlineis patched. However, only/proc/cmdlineis checked during this sequence.
Quoted 5 lines
Should there be an additional step to grep
/proc/meminfoand verify it is functionally unpatched before disabling the livepatch? If the atomic replace logic had a flaw that accidentally left/proc/meminfopatched, it appears this verification would be missed and the test would still pass.
Nice catch.
I will update it with the following change
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +1 -0
@@ -545,6 +545,7 @@ 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
Signature · 2 lines
Regards Yafang