livepatch: Add support for scoped atomic replace
Subsystems (3)
v13 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
=======
v12->v13:
- clarify the replacement conditions (Petr)
- update the system state compatibility rules in system-state.rst to match the new conflict rules
- fix temporary selftests broken in #2 (Petr)
- remove the "patch.replace = true;" in #8 (Petr)
- add the new file ABI/removed/sysfs-kernel-livepatch to MAINTAINERS
- The UAF in livepatch_mod_init() is fixed by a standalone patch:
https://lore.kernel.org/live-patching/[email protected]/
v12: https://lore.kernel.org/live-patching/[email protected]/
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
- 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
Documentation/ABI/removed/sysfs-kernel-livepatch | 16 + Documentation/ABI/testing/sysfs-kernel-livepatch | 26 +- Documentation/livepatch/cumulative-patches.rst | 127 +++- Documentation/livepatch/livepatch.rst | 29 +- Documentation/livepatch/system-state.rst | 22 +- MAINTAINERS | 1 + include/linux/livepatch.h | 8 +- kernel/livepatch/Kconfig | 15 + kernel/livepatch/core.c | 126 ++-- kernel/livepatch/core.h | 2 + kernel/livepatch/state.c | 70 ++- kernel/livepatch/transition.c | 15 +- scripts/livepatch/init.c | 18 +- scripts/livepatch/klp-build | 23 +- tools/testing/selftests/livepatch/Makefile | 3 +- tools/testing/selftests/livepatch/functions.sh | 42 +- .../testing/selftests/livepatch/test-callbacks.sh | 43 +- tools/testing/selftests/livepatch/test-ftrace.sh | 2 +- tools/testing/selftests/livepatch/test-kprobe.sh | 15 +- .../testing/selftests/livepatch/test-livepatch.sh | 108 ++-- .../selftests/livepatch/test-provides-obsoletes.sh | 656 +++++++++++++++++++++ tools/testing/selftests/livepatch/test-syscall.sh | 2 +- tools/testing/selftests/livepatch/test-sysfs.sh | 9 +- .../selftests/livepatch/test_modules/Makefile | 9 +- .../test_modules/test_klp_atomic_replace.c | 57 -- .../test_modules/test_klp_callbacks_demo2.c | 12 + .../livepatch/test_modules/test_klp_cmdline_kp.c | 38 ++ .../livepatch/test_modules/test_klp_cmdline_lp.c | 60 ++ .../livepatch/test_modules/test_klp_kprobe.c | 38 -- .../livepatch/test_modules/test_klp_livepatch.c | 51 -- .../livepatch/test_modules/test_klp_meminfo_lp.c | 79 +++ .../livepatch/test_modules/test_klp_meminfo_lp2.c | 2 + .../livepatch/test_modules/test_klp_state.c | 38 +- .../livepatch/test_modules/test_klp_state2.c | 46 +- .../livepatch/test_modules/test_klp_syscall.c | 131 ---- .../test_modules/test_klp_syscall_getpid.c | 131 ++++ 36 files changed, 1548 insertions(+), 522 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_kp.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp2.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_syscall_getpid.c
Signature · 1 line
1.8.3.1
PATCH v13 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 +- .../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_modules/test_klp_atomic_replace.c | 57 --------- .../livepatch/test_modules/test_klp_cmdline_kp.c | 38 ++++++ .../livepatch/test_modules/test_klp_cmdline_lp.c | 51 ++++++++ .../livepatch/test_modules/test_klp_kprobe.c | 38 ------ .../livepatch/test_modules/test_klp_livepatch.c | 51 -------- .../livepatch/test_modules/test_klp_meminfo_lp.c | 57 +++++++++ .../livepatch/test_modules/test_klp_syscall.c | 131 --------------------- .../test_modules/test_klp_syscall_getpid.c | 131 +++++++++++++++++++++ 14 files changed, 295 insertions(+), 295 deletions(-) delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_kp.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c delete mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_syscall_getpid.c
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_atomic_replace.c deleted +0 -57
57 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_kp.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2024 Marcos Paulo de Souza <[email protected]> +// Copyright (C) 2024 Michael Vetter <[email protected]> + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/kprobes.h> + +static bool has_post_handler = true; +module_param (has_post_handler, bool,0444 ); + +static void __kprobes post_handler (struct kprobe *p,struct pt_regs *regs, +unsigned long flags) +{ +} + +static struct kprobe kp = { + .symbol_name ="cmdline_proc_show" , +}; + +static int __init cmdline_kp_init (void ) +{ +if (has_post_handler) + kp.post_handler = post_handler; + +return register_kprobe (&kp); +} + +static void __exit cmdline_kp_exit(void) +{ +unregister_kprobe (&kp); +} + +module_init(cmdline_kp_init) +module_exit(cmdline_kp_exit) +MODULE_LICENSE("GPL"); +MODULE_AUTHOR ("Michael Vetter <[email protected]>" ); +MODULE_DESCRIPTION ("Livepatch test: cmdline kprobe" );
tools/testing/selftests/livepatch/test_modules/test_klp_cmdline_lp.c new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_kprobe.c deleted +0 -38
@@ -1,38 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (C) 2024 Marcos Paulo de Souza <[email protected]> -// Copyright (C) 2024 Michael Vetter <[email protected]> - -#include <linux/kernel.h> -#include <linux/module.h> -#include <linux/kprobes.h> - -static bool has_post_handler = true; -module_param (has_post_handler, bool,0444 ); - -static void __kprobes post_handler (struct kprobe *p,struct pt_regs *regs, -unsigned long flags) -{ -} - -static struct kprobe kp = { - .symbol_name ="cmdline_proc_show" , -}; - -static int __init kprobe_init (void ) -{ -if (has_post_handler) - kp.post_handler = post_handler; - -return register_kprobe (&kp); -} - -static void __exit kprobe_exit(void) -{ -unregister_kprobe (&kp); -} - -module_init(kprobe_init) -module_exit(kprobe_exit) -MODULE_LICENSE("GPL"); -MODULE_AUTHOR ("Michael Vetter <[email protected]>" ); -MODULE_DESCRIPTION ("Livepatch test: kprobe function" );
tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c deleted +0 -51
51 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_meminfo_lp.c new file +57 -0
57 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c deleted +0 -131
131 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_syscall_getpid.c new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v13 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.
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 +++ tools/testing/selftests/livepatch/functions.sh | 42 ++++++++- .../testing/selftests/livepatch/test-callbacks.sh | 43 ++++++--- tools/testing/selftests/livepatch/test-kprobe.sh | 3 +- .../testing/selftests/livepatch/test-livepatch.sh | 102 ++++++++++++--------- tools/testing/selftests/livepatch/test-sysfs.sh | 3 +- .../test_modules/test_klp_callbacks_demo2.c | 16 ++++ .../livepatch/test_modules/test_klp_meminfo_lp.c | 16 ++++ .../livepatch/test_modules/test_klp_state.c | 7 ++ .../livepatch/test_modules/test_klp_state2.c | 7 ++ 10 files changed, 189 insertions(+), 64 deletions(-)
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 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 +2 -1
@@ -5,7 +5,8 @@ . $(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-sysfs.sh +2 -1
@@ -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
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> +#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 void post_unpatch_callback(struct klp_object *obj)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
@@ -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 int livepatch_meminfo_proc_show(struct seq_file *m, void *v)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
@@ -142,7 +142,14 @@ static void post_unpatch_callback(struct klp_object *obj) .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
@@ -171,7 +171,14 @@ static void post_unpatch_callback(struct klp_object *obj) .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
1.8.3.1
PATCH v13 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_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
1.8.3.1
PATCH v13 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
1.8.3.1
PATCH v13 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 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/removed/sysfs-kernel-livepatch | 7 ++ Documentation/ABI/testing/sysfs-kernel-livepatch | 21 +++- Documentation/livepatch/cumulative-patches.rst | 127 ++++++++++++++++++----- Documentation/livepatch/livepatch.rst | 29 ++++-- Documentation/livepatch/system-state.rst | 22 ++-- MAINTAINERS | 1 + include/linux/livepatch.h | 8 +- kernel/livepatch/Kconfig | 3 +- kernel/livepatch/core.c | 82 ++++++++++++--- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 70 ++++++++++--- kernel/livepatch/transition.c | 15 +-- scripts/livepatch/init.c | 18 ++-- scripts/livepatch/klp-build | 23 ++-- 14 files changed, 321 insertions(+), 106 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 +16 -5
@@ -47,13 +47,24 @@ 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 + an 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
@@ -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.
Documentation/livepatch/system-state.rst +12 -10
@@ -6,9 +6,10 @@ Some users are really reluctant to reboot a system. This brings the need to provide more livepatches and maintain some compatibility between them. Maintaining more livepatches is much easier with cumulative livepatches. -Each new livepatch completely replaces any older one. It can keep, -add, and even remove fixes. And it is typically safe to replace any version -of the livepatch with any other one thanks to the atomic replace feature. +A new version of a cumulative livepatch with the same ``provides`` id +completely replaces the older one. It can keep, add, and even remove fixes. +And it is typically safe to replace any version of the livepatch with any +other one thanks to the atomic replace feature. The problems might come with shadow variables and callbacks. They might change the system behavior or state so that it is no longer safe to
@@ -70,14 +71,15 @@ The check is done when the livepatch is enabled. The rules are: - Any completely new system state modification is allowed. - - System state modifications with the same or higher version are allowed - for already modified system states. + - A livepatch that replaces an existing one must handle all system + state modifications from the livepatches that are being replaced. + The version of the state must be the same or higher than the + version of the state that is being replaced. - - Cumulative livepatches must handle all system state modifications from - already installed livepatches. - - - Non-cumulative livepatches are allowed to touch already modified - system states. + - A livepatch that does not replace any existing one must not touch + already modified system states. It would conflict with the already + installed livepatches and the load would get rejected, see + the conflict rules in Documentation/livepatch/cumulative-patches.rst. 3. Supported scenarios ======================
MAINTAINERS +1 -0
@@ -15237,6 +15237,7 @@ M: Song Liu <[email protected]> L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching.git +F: Documentation/ABI/removed/sysfs-kernel-livepatch F: Documentation/ABI/testing/sysfs-kernel-livepatch F: Documentation/livepatch/ F: arch/powerpc/include/asm/livepatch.h
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 +2 -1
@@ -32,7 +32,8 @@ config KLP_BUILD Enable klp-build support config KLP_HAS_PROVIDES - def_bool n + def_bool y + depends on LIVEPATCH 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 +66 -16
154 lines not on this page · raw message
kernel/livepatch/core.h +1 -0
@@ -18,6 +18,7 @@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 +56 -14
105 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 +12 -6
@@ -9,6 +9,10 @@#include <linux/slab.h> #include <linux/livepatch.h> +#ifdef KLP_OBSOLETES +static unsigned int klp_obsoletes[] = {KLP_OBSOLETES }; +#endif +static struct klp_patch *patch;static int __init livepatch_mod_init (void )
@@ -50,8 +54,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[j].funcs); goto err_free_objs; }
@@ -72,15 +74,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
@@ -857,7 +863,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
1.8.3.1
PATCH v13 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.]
--- Documentation/ABI/removed/sysfs-kernel-livepatch | 9 +++++++++ Documentation/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
@@ -66,15 +66,6 @@ Description: loaded, any existing livepatch whose provides id matches an 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
@@ -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
1.8.3.1
PATCH v13 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 +- .../selftests/livepatch/test-provides-obsoletes.sh | 229 +++++++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 12 +- .../livepatch/test_modules/test_klp_cmdline_lp.c | 11 +- .../livepatch/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
@@ -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
@@ -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 void post_unpatch_callback(struct klp_object *obj)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
@@ -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 int livepatch_cmdline_proc_show(struct seq_file *m, void *v)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
@@ -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 int livepatch_meminfo_proc_show(struct seq_file *m, void *v)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
@@ -143,10 +143,7 @@ static void post_unpatch_callback(struct klp_object *obj) .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
@@ -172,10 +172,7 @@ static void post_unpatch_callback(struct klp_object *obj) .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
1.8.3.1
PATCH v13 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.
--- .../selftests/livepatch/test-provides-obsoletes.sh | 239 +++++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 34 ++- .../livepatch/test_modules/test_klp_state2.c | 42 +++- 3 files changed, 299 insertions(+), 16 deletions(-)
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +239 -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
@@ -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 +28 -6
@@ -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 void post_unpatch_callback(struct klp_object *obj)static struct klp_state states[] = { { - .id =CONSOLE_LOGLEVEL_STATE , .version =CONSOLE_LOGLEVEL_STATE_VERSION , }, { } };
@@ -151,6 +165,14 @@ static void post_unpatch_callback(struct klp_object *obj)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; + } +#endif + states[0 ].id = state;return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +32 -10
108 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH v13 8/9] selftests/livepatch: Add state test for provides/obsoletes
Quoted 9 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_stateandtest_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.[...]
Signature · 1 line
Miroslav
PATCH v13 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.
--- .../selftests/livepatch/test-provides-obsoletes.sh | 188 +++++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../livepatch/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
@@ -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
@@ -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
@@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "test_klp_meminfo_lp.c"
Signature · 1 line
1.8.3.1
Re: [PATCH v13 9/9] selftests/livepatch: Add function test for provides/obsoletes
Quoted 8 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.
Add test scenarios that verify this rule on the combination of patched functions and provides ids.
[...]
Signature · 1 line
Miroslav
Petr Mladek
maintainerQuoted 57 lines · Yafang Shao
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.
Quoted 8 lines
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.
I double checked the patchset and it looks ready for linux-next.
Just to be sure, Joe, Josh, anyone else, would you still like to review this patchset, please? I am going to commit it early next week unless anyone complains ;-)
Best Regards,
Petr
Quoted 6 lines · Yafang Shao
selftests/livepatch: Clarify test module file names selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
livepatch: Makeklp_find_func()non static
livepatch: Callklp_init_patch_early()earlier
livepatch: Implement provides and obsoletes for scoped atomic replace
livepatch: Deprecatestack_order
s/Deprecate/Remove/
Quoted 4 lines
selftests/livepatch: Add provides/obsoletes test scenarios selftests/livepatch: Add state test for provides/obsoletes selftests/livepatch: Add function test for provides/obsoletes
With the subject fixed on the above patch:
Thanks!
Signature · 1 line
Josh
Quoted 11 lines · Josh Poimboeuf
Quoted 6 lines · Yafang Shao
selftests/livepatch: Clarify test module file names selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
livepatch: Makeklp_find_func()non static
livepatch: Callklp_init_patch_early()earlier
livepatch: Implement provides and obsoletes for scoped atomic replace
livepatch: Deprecatestack_orders/Deprecate/Remove/
Quoted 8 lines
Quoted 4 lines
selftests/livepatch: Add provides/obsoletes test scenarios selftests/livepatch: Add state test for provides/obsoletes selftests/livepatch: Add function test for provides/obsoletes
With the subject fixed on the above patch:
Acked-by Josh Poimboeuf <[email protected]>
Thanks for your review.
Hello Petr,
Would you like me to send a new version with the change above, or will you make the change on your end?
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerQuoted 19 lines · Yafang Shao
Quoted 18 lines · Josh Poimboeuf
Quoted 6 lines · Yafang Shao
selftests/livepatch: Clarify test module file names selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
livepatch: Makeklp_find_func()non static
livepatch: Callklp_init_patch_early()earlier
livepatch: Implement provides and obsoletes for scoped atomic replace
livepatch: Deprecatestack_orders/Deprecate/Remove/
Quoted 4 lines
selftests/livepatch: Add provides/obsoletes test scenarios selftests/livepatch: Add state test for provides/obsoletes selftests/livepatch: Add function test for provides/obsoletes
With the subject fixed on the above patch:
Quoted 8 lines
Quoted 1 line
Acked-by Josh Poimboeuf <[email protected]>Thanks for your review.
Hello Petr,
Would you like me to send a new version with the change above, or will you make the change on your end?
I'll do the change when committing.
Best Regards,
Petr
Petr Mladek
maintainerQuoted 57 lines · Yafang Shao
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.
Quoted 8 lines
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.
JFYI, the patchset has been committed into livepatching.git, branch for-7.5/scoped-atomic-replace.
Note that I have fixed the subject of the 6th patch as suggested by Josh, see
https://git.kernel.org/pub/scm/linux/kernel/git/livepatching/livepatching.git/log/?h=for-7.5/scoped-atomic-replace
Best Regards,
Petr