livepatch: Introduce replace set support
Subsystems (3)
v7 cover livepatch: Introduce replace set support
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 adding a "replace set" to support scenarios where specific livepatches can be selectively replaced or skipped.
This patchset introduces a more flexible model using two new fields in struct klp_patch:
- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes list
Additionally, this design deprecates the traditional non-atomic-replace model. 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 replace set, developers now have a selective option to keep specific livepatches persistent while maintaining atomic replacement capabilities elsewhere.
At present, KLP state, shadow variables, and callbacks are not integrated with the new replace_set 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
Future Work
===========
- Allow provides and obsoletes to be configured dynamically at module load time, rather than being fixed at build time.
Notes for sashiko-bot
=====================
In your review of v5, you found the following issues:
- A malformed livepatch module with a missing old_name triggers a NULL pointer dereference in klp_find_func().
This has already been addressed by commit 1a921fd13c31e ("livepatch: Fix NULL pointer dereference in klp_find_func()").
- When CONFIG_DEBUG_KOBJECT_RELEASE is enabled, an error during patch initialization causes a use-after-free during module unload due to the delayed kobject release.
This is addressed by the patch posted at
https://lore.kernel.org/live-patching/[email protected]/
- Other review issues are addressed in this version; details in the changes section.
It is based on livepatching tree's for-next branch.
Changes
=======
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 (8):
livepatch: Make klp_find_func() non static
livepatch: Call klp_init_patch_early() earlier
livepatch: Implement replace set for scoped atomic replace
livepatch: Deprecate stack_order
selftests/livepatch: Adapt atomic replace tests to provides/obsoletes selftests/livepatch: Add provides/obsoletes test scenarios selftests/livepatch: Add test for state ID conflict across provides selftests/livepatch: Add test for function conflict across provides
.../ABI/removed/sysfs-kernel-livepatch | 16 + .../ABI/testing/sysfs-kernel-livepatch | 27 +- .../livepatch/cumulative-patches.rst | 93 +++-- Documentation/livepatch/livepatch.rst | 23 +- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 114 +++--- kernel/livepatch/core.h | 2 + kernel/livepatch/state.c | 57 ++- kernel/livepatch/transition.c | 11 +- scripts/livepatch/init.c | 71 +++- scripts/livepatch/klp-build | 78 +++- tools/testing/selftests/livepatch/Makefile | 3 +- .../testing/selftests/livepatch/functions.sh | 15 + .../selftests/livepatch/test-callbacks.sh | 6 + .../selftests/livepatch/test-livepatch.sh | 6 + .../livepatch/test-provides-obsoletes.sh | 370 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 15 + .../test_modules/test_klp_atomic_replace.c | 22 ++ .../test_modules/test_klp_callbacks_demo2.c | 12 + .../test_modules/test_klp_livepatch.c | 9 + .../test_modules/test_klp_provides.c | 72 ++++ .../livepatch/test_modules/test_klp_state.c | 13 + .../livepatch/test_modules/test_klp_state2.c | 23 ++ 23 files changed, 942 insertions(+), 123 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.c
Signature · 1 line
2.52.0
PATCH v7 1/8 livepatch: Make klp_find_func() non static
Make klp_find_func() non static to allow its use in other source files by an upcoming patch.
While at it, rename the parameter @old_func to @func to better reflect its generic purpose.
--- kernel/livepatch/core.c | 18 +++++++++--------- kernel/livepatch/core.h | 1 + 2 files changed, 10 insertions(+), 9 deletions(-)
kernel/livepatch/core.c +9 -9
@@ -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
Re: [PATCH v7 for-next 1/8] livepatch: Make klp_find_func() non static
Quoted 8 lines · Yafang Shao
Make
klp_find_func()non static to allow its use in other source files by an upcoming patch.While at it, rename the parameter @old_func to @func to better reflect its generic purpose.
[...]
Signature · 1 line
Miroslav
PATCH v7 2/8 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 | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/livepatch/core.c +8 -7
@@ -1145,13 +1145,6 @@ int klp_enable_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); -if (!klp_is_patch_compatible (patch)) { -pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); -mutex_unlock (&klp_mutex); -return -EINVAL ; - } -if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);return -ENODEV ;
@@ -1159,6 +1152,14 @@ 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 v7 for-next 2/8] 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] Potential use-after-free of kobject delayed work structs during error cleanup if CONFIG_DEBUG_KOBJECT_RELEASE is enabled. --
--- Patch [2]: [PATCH v7 for-next 2/8] livepatch: Call klp_init_patch_early() earlier --- commit b5f9135d749ffc4d6ea6c6a68a0fc6489e7a1cbf
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 3 lines
kernel/livepatch/core.c +0
-0
[ ... ]
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1160
Quoted 10 lines
kernel/livepatch/core.c +6
-0@@ -1152,6 +1152,14 @@ 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 does this error path expose a use-after-free of kobject delayed work structs when CONFIG_DEBUG_KOBJECT_RELEASE is enabled?
Because kobject_add() has not yet run (it runs later in klp_init_patch()), the child kobjects (obj and func) do not have their parent pointers set to patch->kobj during this cleanup path.
When the error path jumps to err and calls klp_free_patch_start(), it drops references to the child kobjects. Since their parent pointers aren't set, putting the child kobjects does not elevate or pin the parent patch->kobj.
Consequently, the wait_for_completion() in klp_free_patch_finish() only waits for the patch kobject to finish releasing, but not the child kobjects:
kernel/livepatch/core.c:klp_free_patch_finish() {
...
kobject_put(&patch->kobj);
wait_for_completion(&patch->finish);
/* Put the module after the last access to struct klp_patch. */
if (!patch->forced)
module_put(patch->mod);
}This allows the module memory to be completely freed while the delayed work for the child obj and func kobjects (which reside in the now-freed module memory) might still be queued. When the workqueue executes them, it results in a use-after-free.
While this issue already existed for klp_init_patch() failures, moving klp_is_patch_compatible() after klp_init_patch_early() makes it trivially reachable by simply providing an incompatible livepatch.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v7 for-next 2/8] livepatch: Call klp_init_patch_early() earlier
Quoted 5 lines · On Tue, Aug 25, 2026 at 8:06 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Potential use-after-free of kobject delayed work structs during error cleanup ifCONFIG_DEBUG_KOBJECT_RELEASEis enabled.
Seems you didn't read the "Notes for sashiko-bot" carefully ;)
--
Regards
Yafang
Josh Poimboeuf
maintainerRe: [PATCH v7 for-next 2/8] livepatch: Call klp_init_patch_early() earlier
In reply to Yafang ShaoQuoted 35 lines · Yafang Shao
Invoke
klp_init_patch_early()during early initialization to avoid mixingfor_each_()and for_each_*_static() variants later in the code.No functional change. This prepares the code for an upcoming patch.
Suggested-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>Acked-by Song Liu <[email protected]>--- kernel/livepatch/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)kernel/livepatch/core.c +1
-7@@ -1145,13 +1145,6 @@ int klp_enable_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); -if (!klp_is_patch_compatible (patch)) { -pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); -mutex_unlock (&klp_mutex); -return -EINVAL ; - } -if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);return -ENODEV ;@@ -1159,6 +1152,14 @@ int klp_enable_patch(struct klp_patch *patch)klp_init_patch_early (patch); +if (!klp_is_patch_compatible (patch)) {
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1165
Quoted 10 lines
kernel/livepatch/core.c +7
-0+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;
Stray newline.
Signature · 1 line
Josh
Re: [PATCH v7 for-next 2/8] livepatch: Call klp_init_patch_early() earlier
Quoted 36 lines · Josh Poimboeuf
Quoted 34 lines · Yafang Shao
Invoke
klp_init_patch_early()during early initialization to avoid mixingfor_each_()and for_each_*_static() variants later in the code.No functional change. This prepares the code for an upcoming patch.
Suggested-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>Acked-by Song Liu <[email protected]>--- kernel/livepatch/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)kernel/livepatch/core.c +0
-7@@ -1145,13 +1145,6 @@ int klp_enable_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); -if (!klp_is_patch_compatible (patch)) { -pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); -mutex_unlock (&klp_mutex); -return -EINVAL ; - } -if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);return -ENODEV ;@@ -1159,6 +1152,14 @@ int klp_enable_patch(struct klp_patch *patch)klp_init_patch_early (patch);
Quoted 13 lines
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1165
Quoted 11 lines
kernel/livepatch/core.c +8
-0+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;Stray newline.
I will update it.
Signature · 2 lines
Regards Yafang
Re: [PATCH v7 for-next 2/8] livepatch: Call klp_init_patch_early() earlier
In reply to Yafang ShaoQuoted 4 lines · Yafang Shao
Invoke
klp_init_patch_early()during early initialization to avoid mixingfor_each_()and for_each_*_static() variants later in the code.No functional change. This prepares the code for an upcoming patch.
Signature · 1 line
Miroslav
PATCH v7 3/8 livepatch: Implement replace set 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 patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes list
The klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Two helper functions are introduced:
- klp_patch_replaceable(): checks if an new patch will replace the old patch
- klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.
--- .../ABI/testing/sysfs-kernel-livepatch | 22 ++++- .../livepatch/cumulative-patches.rst | 93 +++++++++++++------ Documentation/livepatch/livepatch.rst | 23 +++-- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 69 ++++++++++++-- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 57 ++++++++++-- kernel/livepatch/transition.c | 11 ++- scripts/livepatch/init.c | 71 +++++++++++++- scripts/livepatch/klp-build | 78 ++++++++++++++-- 10 files changed, 351 insertions(+), 81 deletions(-)
Documentation/ABI/testing/sysfs-kernel-livepatch +17 -5
@@ -47,13 +47,25 @@ Description: disabled when the feature is used. See Documentation/livepatch/livepatch.rst for more information. -What: /sys/kernel/livepatch/<patch>/replace -Date: Jun 2024 -KernelVersion: 6.11.0 +What: /sys/kernel/livepatch/<patch>/provides +Date: Jun 2026 +KernelVersion: 7.4.0 Contact: [email protected] Description: - An attribute which indicates whether the patch supports - atomic-replace. + An attribute to show the provides id of this livepatch. + Only one active livepatch per provides id is allowed. + +What: /sys/kernel/livepatch/<patch>/obsoletes +Date: Jun 2026 +KernelVersion: 7.4.0 +Contact: [email protected] +Description: + An attribute to show the obsoletes ids of this livepatch. + The obsoletes ids are a comma-separated list of provides + ids that this patch obsoletes. When this livepatch is + loaded, any existing livepatch whose provides id matches + either this patch's provides id or any id in the obsoletes + list will be atomically replaced. What: /sys/kernel/livepatch/<patch>/stack_order Date: Jan 2025
Documentation/livepatch/cumulative-patches.rst +65 -28
105 lines not on this page · raw message
Documentation/livepatch/livepatch.rst +14 -9
@@ -347,15 +347,20 @@ 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. + +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. + +Patches with a different ``provides`` id are not affected by this +process and remain active. This allows for the independent management +and stacking of multiple, non-conflicting livepatch sets. See Documentation/livepatch/cumulative-patches.rst for more details.
include/linux/livepatch.h +5 -2
@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
@@ -137,7 +138,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/core.c +59 -10
127 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 +49 -8
82 lines not on this page · raw message
kernel/livepatch/transition.c +6 -5
@@ -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); }
@@ -498,7 +498,7 @@ void klp_try_complete_transition(void)*/ if (!patch->enabled)klp_free_patch_async (patch); -else if (patch->replace) +else klp_free_replaced_patches_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 +66 -5
101 lines not on this page · raw message
scripts/livepatch/klp-build +69 -9
120 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Contradiction between documentation/commit message and actual code regarding the 'obsoletes'…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Contradiction between documentation/commit message and actual code regarding the 'obsoletes' list. --
--- Patch [3]: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace --- commit 32e1bcc9ed17f1580951816746e0cfa7cadbca0b
Author: Yafang Shao <[email protected]>
livepatch: Implement replace set for scoped atomic replace
This commit replaces the boolean replace flag with a more flexible model using provides and obsoletes fields to allow multiple independent livepatch sets to coexist on the same system.
Quoted 4 lines
The klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
[Severity: Medium]
Is this description accurate? The code and inline comments in the build script and init.c explicitly state that the provides ID is not included in the obsoletes list, and the kernel instead relies on explicitly checking the provides ID directly.
Quoted 4 lines
Documentation/livepatch/cumulative-patches.rst +0
-0
[ ... ]
Documentation/livepatch/cumulative-patches.rst line 39
Quoted 11 lines
Documentation/livepatch/cumulative-patches.rst +11
-0+Provides and Obsoletes +----------------------- + +The ``provides`` field in ``struct klp_patch`` is an unsigned integer that +identifies the livepatch's replace set. By default, it is 0. + +The ``obsoletes`` field is an optional array of unsigned integers that +specifies additional ``provides`` ids to be replaced when this patch is +loaded. By default, it includes the patch's own ``provides`` id, ensuring +that a new patch always replaces any existing patch with the same +``provides`` id.
[Severity: Medium]
This isn't a code bug, but does this documentation match the implementation? The text here states that the obsoletes field automatically includes the patch's own provides id by default.
Quoted 4 lines
scripts/livepatch/klp-build +0
-0
[ ... ]
scripts/livepatch/klp-build process_args() { · line 259
Quoted 7 lines
scripts/livepatch/klp-build +3
-0@@ -235,6 +254,37 @@ process_args() { exit 1 fi + # Remove duplicates from obsoletes (if specified) + # Note: provides ID is not added here; the kernel will replace + # livepatch with the same provides ID.
[Severity: Medium]
Here the build script explicitly notes that the provides ID is not added to the obsoletes list. Could the commit message and the documentation in cumulative-patches.rst be updated to match this behavior to avoid confusion?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 3 lines · On Tue, Aug 25, 2026 at 7:59 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Contradiction between documentation/commit message and actual code regarding the 'obsoletes' list.
Ah, the provides ID is not included in the obsoletes list. I will update it.
Signature · 2 lines
Regards Yafang
Josh Poimboeuf
maintainerRe: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoQuoted 20 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes list
Quoted 8 lines
The klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Two helper functions are introduced:
-klp_patch_replaceable(): checks if an new patch will replace the old patch
Should be klp_patch_replaces()?
Quoted 3 lines
-
klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.
I'm not sure the commit log really needs to list some functions it adds, that's already evident in the patch.
Quoted 15 lines
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 7 lines
include/linux/livepatch.h +3
-1@@ -137,7 +138,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;
nr_obsoletes is missing from the kernel-doc above.
include/linux/livepatch.h static int __init livepatch_mod_init(void) · line 78
Quoted 12 lines
include/linux/livepatch.h +4
-3@@ -72,15 +71,76 @@ static int __init livepatch_mod_init(void)/* TODO patch->states */ -#ifdef KLP_NO_REPLACE - patch->replace = false; +#ifdef KLP_PROVIDES + patch->provides =KLP_PROVIDES ;#else - patch->replace = true; + patch->provides =0 ; +#endif
'patch' is already zeroed, so the #else isn't needed.
Quoted 44 lines
include/linux/livepatch.h +44
-0+#ifdef KLP_OBSOLETES +/* +* Parse KLP_OBSOLETES string (format: "1,2,3") and convert to +* unsigned int array for patch->obsoletes. +* +* Note: The provides ID is not included here; the kernel will +* replace livepatch with the same provides ID. +*/ + { +unsigned int *obs_array; +unsigned int count =1 ; +char *obsoletes_str; +char *token, *str; +int i =0 ; + +for (str = (char *)KLP_OBSOLETES ; *str; str++) { +if (*str ==',' ) + count++; + } + + obsoletes_str =kstrdup (KLP_OBSOLETES ,GFP_KERNEL ); +if (!obsoletes_str) { + ret = -ENOMEM ; + goto err_free_objs; + } + + obs_array =kmalloc_array (count,sizeof (unsigned int ),GFP_KERNEL ); +if (!obs_array) { +kfree (obsoletes_str); + ret = -ENOMEM ; + goto err_free_objs; + } + + str = obsoletes_str; +while ((token =strsep (&str,"," )) !=NULL ) { +unsigned int val; + + ret =kstrtouint (token,10 , &val); +if (ret) { +kfree (obsoletes_str); +kfree (obs_array); + goto err_free_objs; + } + obs_array[i++] = val;
include/linux/livepatch.h static int __init livepatch_mod_init(void) · line 132
Quoted 10 lines
include/linux/livepatch.h +10
-0+ } + + patch->obsoletes = obs_array; + patch->nr_obsoletes = i; + +if (i >0 ) +pr_info ("obsoletes patch ids: %s\n" ,KLP_OBSOLETES ); + +kfree (obsoletes_str); + }
Can we do something simpler like this?
#ifdef KLP_OBSOLETES
static unsigned int klp_obsoletes[] = { KLP_OBSOLETES }; #endif
And in livepatch_mod_init():
#ifdef KLP_PROVIDES patch->provides = KLP_PROVIDES; #endif #ifdef KLP_OBSOLETES patch->obsoletes = klp_obsoletes; patch->nr_obsoletes = ARRAY_SIZE(klp_obsoletes);
#endif
Quoted 22 lines
include/linux/livepatch.h +5
-2+++ b/scripts/livepatch/klp-build@@ -21,7 +21,8 @@ shopt -s lastpipe unset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE -REPLACE=1 +PROVIDES=0 +OBSOLETES="" SHORT_CIRCUIT=0JOBS ="$(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:
include/linux/livepatch.h Advanced Options: · line 152
Quoted 5 lines
include/linux/livepatch.h +0
-1@@ -147,7 +149,6 @@ Advanced Options:EOF } -usage () {
Unnecessary whitespace change
Quoted 25 lines
include/linux/livepatch.h +21
-0@@ -235,6 +254,37 @@ process_args() { exit 1 fi +# Remove duplicates from obsoletes (if specified) +# Note : provides ID is not added here; the kernel will replace +# livepatch with the same provides ID. + if [[ -n "$OBSOLETES" ]]; then + local obsoletes_clean="${OBSOLETES//[ ]/}" + local IFS=',' + local -a obs_array=() + local obs_id + local already_exists + + for obs_id in $obsoletes_clean; do + if [[ -n "$obs_id" ]]; then + already_exists=0 + for existing in "${obs_array[@]}"; do + if [[ "$existing" -eq "$obs_id" ]]; then + already_exists=1 + break + fi + done + + if [[ "$already_exists" -eq 0 ]]; then
include/linux/livepatch.h process_args() { · line 287
Quoted 10 lines
include/linux/livepatch.h +10
-0+ obs_array+=("$obs_id") +fi + fi +done + + local IFS=',' + OBSOLETES="${obs_array[*]}" + unsetIFS + fi +
What's the point of this? Do we expect duplicates, and if so, wouldn't it be the kernel's job to handle that?
Signature · 1 line
Josh
Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 27 lines · Josh Poimboeuf
Quoted 25 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes listThe klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Quoted 5 lines
Quoted 3 lines
Two helper functions are introduced:
-klp_patch_replaceable(): checks if an new patch will replace the old patchShould be
klp_patch_replaces()?
right.
Quoted 7 lines
Quoted 4 lines
-
klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.I'm not sure the commit log really needs to list some functions it adds, that's already evident in the patch.
I will remove them from the commit log.
Quoted 17 lines
Quoted 17 lines
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;
Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 6 lines
include/linux/livepatch.h +3
-1struct klp_object *objs;struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;
nr_obsoletesis missing from the kernel-doc above.
will add it
Quoted 5 lines
Quoted 5 lines
include/linux/livepatch.h +0
-0@@ -72,15 +71,76 @@ static int __init livepatch_mod_init(void)/* TODO patch->states */
Quoted 10 lines
include/linux/livepatch.h static int __init livepatch_mod_init(void) · line 78
Quoted 8 lines
include/linux/livepatch.h +4
-3-#ifdef KLP_NO_REPLACE - patch->replace = false; +#ifdef KLP_PROVIDES + patch->provides =KLP_PROVIDES ;#else - patch->replace = true; + patch->provides =0 ; +#endif 'patch' is already zeroed, so the #else isn't needed.
will remove it.
Quoted 64 lines
Quoted 45 lines
include/linux/livepatch.h +44
-0+#ifdef KLP_OBSOLETES +/* +* Parse KLP_OBSOLETES string (format: "1,2,3") and convert to +* unsigned int array for patch->obsoletes. +* +* Note: The provides ID is not included here; the kernel will +* replace livepatch with the same provides ID. +*/ + { +unsigned int *obs_array; +unsigned int count =1 ; +char *obsoletes_str; +char *token, *str; +int i =0 ; + +for (str = (char *)KLP_OBSOLETES ; *str; str++) { +if (*str ==',' ) + count++; + } + + obsoletes_str =kstrdup (KLP_OBSOLETES ,GFP_KERNEL ); +if (!obsoletes_str) { + ret = -ENOMEM ; + goto err_free_objs; + } + + obs_array =kmalloc_array (count,sizeof (unsigned int ),GFP_KERNEL ); +if (!obs_array) { +kfree (obsoletes_str); + ret = -ENOMEM ; + goto err_free_objs; + } + + str = obsoletes_str; +while ((token =strsep (&str,"," )) !=NULL ) { +unsigned int val; + + ret =kstrtouint (token,10 , &val); +if (ret) { +kfree (obsoletes_str); +kfree (obs_array); + goto err_free_objs; + } + obs_array[i++] = val;include/linux/livepatch.h static int __init livepatch_mod_init(void) · line 132
Quoted 10 lines
include/linux/livepatch.h +10
-0+ } + + patch->obsoletes = obs_array; + patch->nr_obsoletes = i; + +if (i >0 ) +pr_info ("obsoletes patch ids: %s\n" ,KLP_OBSOLETES ); + +kfree (obsoletes_str); + }Can we do something simpler like this?
#ifdef
KLP_OBSOLETES
static unsigned int klp_obsoletes[] = {KLP_OBSOLETES}; #endifAnd in
livepatch_mod_init():
Quoted 7 lines
#ifdef KLP_PROVIDES patch->provides = KLP_PROVIDES; #endif #ifdef KLP_OBSOLETES patch->obsoletes = klp_obsoletes; patch->nr_obsoletes = ARRAY_SIZE(klp_obsoletes);#endif
good point.
I will update it.
Quoted 25 lines
Quoted 25 lines
include/linux/livepatch.h +5
-2+++ b/scripts/livepatch/klp-build@@ -21,7 +21,8 @@ shopt -s lastpipe unset DEBUG_CLONE DIFF_CHECKSUM SKIP_CLEANUP VERBOSE XTRACE -REPLACE=1 +PROVIDES=0 +OBSOLETES="" SHORT_CIRCUIT=0JOBS ="$(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:@@ -147,7 +149,6 @@ Advanced Options:
Quoted 6 lines
include/linux/livepatch.h Advanced Options: · line 152
Quoted 4 lines
include/linux/livepatch.h +0
-1EOF } -usage () {Unnecessary whitespace change
will update it.
Quoted 30 lines
Quoted 30 lines
include/linux/livepatch.h +25
-0@@ -235,6 +254,37 @@ process_args() { exit 1 fi +# Remove duplicates from obsoletes (if specified) +# Note : provides ID is not added here; the kernel will replace +# livepatch with the same provides ID. + if [[ -n "$OBSOLETES" ]]; then + local obsoletes_clean="${OBSOLETES//[ ]/}" + local IFS=',' + local -a obs_array=() + local obs_id + local already_exists + + for obs_id in $obsoletes_clean; do + if [[ -n "$obs_id" ]]; then + already_exists=0 + for existing in "${obs_array[@]}"; do + if [[ "$existing" -eq "$obs_id" ]]; then + already_exists=1 + break + fi + done + + if [[ "$already_exists" -eq 0 ]]; then + obs_array+=("$obs_id") + fi + fi + done
Quoted 8 lines
include/linux/livepatch.h process_args() { · line 287
Quoted 6 lines
include/linux/livepatch.h +6
-0+ + local IFS=',' +OBSOLETES ="${obs_array[*]}" + unsetIFS + fi +What's the point of this? Do we expect duplicates,
Duplicated ids are indeed a user input error (e.g. -r "1,1,2,3") and should not be expected.
Quoted 2 lines
and if so, wouldn't
it be the kernel's job to handle that?
On the kernel side the duplicates are safe:klp_patch_replaces() iterates over the obsoletes array and compares each entry against the other patches' provides, so a duplicated id is simply idempotent - the only side effect is duplicated ids shown in the /sys/kernel/livepatch/<patch>/obsoletes attribute.
Silently deduplicating in klp-build was masking the input mistake. Agreed on the fail-fast approach: klp-build will now error out with a clear message when duplicate obsoletes ids are detected. The list passed to the kernel is then guaranteed clean, and the sysfs side effect cannot happen.
Signature · 2 lines
Regards Yafang
Josh Poimboeuf
maintainerRe: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 47 lines · Yafang Shao
Quoted 25 lines · Josh Poimboeuf
Quoted 25 lines
scripts/livepatch/klp-build +25
-0+ # Remove duplicates from obsoletes (if specified) + # Note: provides ID is not added here; the kernel will replace + # livepatch with the same provides ID. + if [[ -n "$OBSOLETES" ]]; then + local obsoletes_clean="${OBSOLETES//[ ]/}" + local IFS=',' + local -a obs_array=() + local obs_id + local already_exists + + for obs_id in $obsoletes_clean; do + if [[ -n "$obs_id" ]]; then + already_exists=0 + for existing in "${obs_array[@]}"; do + if [[ "$existing" -eq "$obs_id" ]]; then + already_exists=1 + break + fi + done + + if [[ "$already_exists" -eq 0 ]]; then + obs_array+=("$obs_id") + fi + fi + doneQuoted 8 lines
scripts/livepatch/klp-build process_args() { · line 287
Quoted 6 lines
scripts/livepatch/klp-build +6
-0+ + local IFS=',' + OBSOLETES="${obs_array[*]}" + unset IFS + fi +What's the point of this? Do we expect duplicates,
Duplicated ids are indeed a user input error (e.g. -r "1,1,2,3") and should not be expected.
Quoted 2 lines
and if so, wouldn't
it be the kernel's job to handle that?On the kernel side the duplicates are safe:
klp_patch_replaces()iterates over the obsoletes array and compares each entry against the other patches' provides, so a duplicated id is simply idempotent - the only side effect is duplicated ids shown in the /sys/kernel/livepatch/<patch>/obsoletes attribute.
Quoted 5 lines
Silently deduplicating in klp-build was masking the input mistake. Agreed on the fail-fast approach: klp-build will now error out with a clear message when duplicate obsoletes ids are detected. The list passed to the kernel is then guaranteed clean, and the sysfs side effect cannot happen.
There are other ways of building livepatches beyond klp-build, and this wouldn't prevent those other patch generation methods from making the same mistake.
But since it doesn't actually break anything, and the user is not likely to be setting multiple obsoletes, much less introducing duplicates, I would say just remove the check altogether. It's not really a bug and I doubt that somebody who is careless enough to have duplicate obsoletes would notice or care that sysfs also has duplicates... and if they do care, they should fix their patch :-)
For similar reasons I think some of the other error checking isn't really needed (invalid comma usage, negative number check) as those will either fail the build or will give the user what they asked for, not dissimilar from providing a buggy patch.
Then I think the majority of those klp-build changes aren't needed, and it can be a simple passthrough of the user cmdline:
cflags+=("-DKLP_OBSOLETES=$OBSOLETES")
Signature · 1 line
Josh
Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 71 lines · Josh Poimboeuf
Quoted 47 lines · Yafang Shao
Quoted 25 lines · Josh Poimboeuf
Quoted 25 lines
scripts/livepatch/klp-build +25
-0+ # Remove duplicates from obsoletes (if specified) + # Note: provides ID is not added here; the kernel will replace + # livepatch with the same provides ID. + if [[ -n "$OBSOLETES" ]]; then + local obsoletes_clean="${OBSOLETES//[ ]/}" + local IFS=',' + local -a obs_array=() + local obs_id + local already_exists + + for obs_id in $obsoletes_clean; do + if [[ -n "$obs_id" ]]; then + already_exists=0 + for existing in "${obs_array[@]}"; do + if [[ "$existing" -eq "$obs_id" ]]; then + already_exists=1 + break + fi + done + + if [[ "$already_exists" -eq 0 ]]; then + obs_array+=("$obs_id") + fi + fi + doneQuoted 8 lines
scripts/livepatch/klp-build process_args() { · line 287
Quoted 6 lines
scripts/livepatch/klp-build +6
-0+ + local IFS=',' + OBSOLETES="${obs_array[*]}" + unset IFS + fi +What's the point of this? Do we expect duplicates,
Duplicated ids are indeed a user input error (e.g. -r "1,1,2,3") and should not be expected.
Quoted 2 lines
and if so, wouldn't
it be the kernel's job to handle that?On the kernel side the duplicates are safe:
klp_patch_replaces()iterates over the obsoletes array and compares each entry against the other patches' provides, so a duplicated id is simply idempotent - the only side effect is duplicated ids shown in the /sys/kernel/livepatch/<patch>/obsoletes attribute.Quoted 5 lines
Silently deduplicating in klp-build was masking the input mistake. Agreed on the fail-fast approach: klp-build will now error out with a clear message when duplicate obsoletes ids are detected. The list passed to the kernel is then guaranteed clean, and the sysfs side effect cannot happen.
There are other ways of building livepatches beyond klp-build, and this wouldn't prevent those other patch generation methods from making the same mistake.
But since it doesn't actually break anything, and the user is not likely to be setting multiple obsoletes, much less introducing duplicates, I would say just remove the check altogether. It's not really a bug and I doubt that somebody who is careless enough to have duplicate obsoletes would notice or care that sysfs also has duplicates... and if they do care, they should fix their patch :-)
For similar reasons I think some of the other error checking isn't really needed (invalid comma usage, negative number check) as those will either fail the build or will give the user what they asked for, not dissimilar from providing a buggy patch.
Quoted 4 lines
Then I think the majority of those klp-build changes aren't needed, and it can be a simple passthrough of the user cmdline:
cflags+=("-DKLP_OBSOLETES=$OBSOLETES")
Thanks for your suggestion.
I will update it.
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerReplace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoQuoted 28 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 7 lines
include/linux/livepatch.h +3
-1@@ -137,7 +138,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;
This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?
The current code would allow to replace B with A when there is _no_ real conflict in the livaptched objects, functions, and states.
But does it make sense?
Reasoning: Livepatch B obsoleted the livepatch A for a reason.
It sounds like they should not be enabled at the same time.
Special case:
Should we allow to install a livepatch with non-zero provides when a livepatch with zero provides is installed.
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:0
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch A be installed in parallel with B?
Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.
Best Regards,
Petr
Re: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 32 lines · Petr Mladek
Quoted 30 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;
Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 5 lines
include/linux/livepatch.h +3
-1struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
right.
Quoted 7 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Quoted 6 lines
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
Good question.
I believe it's best to refuse to load it, as doing otherwise might introduce potential issues. I will update this rule in the next version.
Quoted 10 lines
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?
No, I don't believe B should be replaced by A. In this case, if B is already enabled, A should fail to load.
Quoted 20 lines
The current code would allow to replace B with A when there is _no_ real conflict in the livaptched objects, functions, and states.
But does it make sense?
Reasoning: Livepatch B obsoleted the livepatch A for a reason.
It sounds like they should not be enabled at the same time.Special case:
Should we allow to install a livepatch with non-zero provides when a livepatch with zero provides is installed.
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:0
Quoted 8 lines
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch A be installed in parallel with B?Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.
So, let's just not treat '0' as a special case?
Signature · 2 lines
Regards Yafang
Re: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 75 lines · Yafang Shao
Quoted 32 lines · Petr Mladek
Quoted 30 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 5 lines
include/linux/livepatch.h +3
-1struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
right.
Quoted 7 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Quoted 6 lines
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
Good question.
I believe it's best to refuse to load it, as doing otherwise might introduce potential issues. I will update this rule in the next version.Quoted 6 lines
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1
Quoted 7 lines
Quoted 4 lines
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?No, I don't believe B should be replaced by A. In this case, if B is already enabled, A should fail to load.
This brings up another question regarding large server fleets. In our production environment, if a new livepatch introduces a regression, we always roll back to the old version. For example, if A is the old version and B is the new one, we will roll back to A if B causes issues. In that case, if we refuse to load A while B is already loaded, we can't roll back. (In practice, though, we haven't rolled back a single livepatch after rolling out 40 versions on our 6.1.y stable kernel.)
However, this isn't an unfixable issue. In the future, I plan to introduce dynamical provides IDs and obsoletes IDs at load time, allowing us to change IDs on demand
Quoted 35 lines
Quoted 21 lines
The current code would allow to replace B with A when there is _no_ real conflict in the livaptched objects, functions, and states.
But does it make sense?
Reasoning: Livepatch B obsoleted the livepatch A for a reason.
It sounds like they should not be enabled at the same time.Special case:
Should we allow to install a livepatch with non-zero provides when a livepatch with zero provides is installed.
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:0Quoted 8 lines
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch A be installed in parallel with B?Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.So, let's just not treat '0' as a special case?
--
Regards
Yafang
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 81 lines · Yafang Shao
Quoted 80 lines · Yafang Shao
Quoted 32 lines · Petr Mladek
Quoted 30 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 5 lines
include/linux/livepatch.h +3
-1struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
right.
Quoted 7 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Quoted 6 lines
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
Good question.
I believe it's best to refuse to load it, as doing otherwise might introduce potential issues. I will update this rule in the next version.Quoted 10 lines
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?
Quoted 6 lines
Quoted 2 lines
No, I don't believe B should be replaced by A. In this case, if B is already enabled, A should fail to load.
This brings up another question regarding large server fleets. In our production environment, if a new livepatch introduces a regression, we always roll back to the old version.
Good point. Well, note that the above example with two livepatches is artificial. More realistic example would be with three livepatches, for example:
+ livepatch A: funcs[] = {a, b}; provides = 1;
+ livepatch B: funcs[] = {c, d}; provides = 2;
+ livepatch C: funcs[] = {a, b, c, d, e}; provides = 1; obsoletes[] = {2}
Now, imagine that C does some semantic changes in the function 'e' so that all other functions {a,b,c,d} have to be updated accordingly.
You could not install B when C is installed. The functions {c,d} would break because they won't be compatible with the rest.
The only safe solution would be to replace:
1. C with B and install A later
2. C with A and install B later
3. C with A+B atomically
The 3rd solution would be the best. But it might need some significant changes in the core code. We would need to handle an array of transition patches instead of just one.
But we might allow 1st and 2nd solution after all.
Summary: We should not allow to install B in parallel with C.
Instead, we might allow to replace C with B.
Quoted 6 lines
For example, if A is the old
version and B is the new one, we will roll back to A if B causes issues. In that case, if we refuse to load A while B is already loaded, we can't roll back. (In practice, though, we haven't rolled back a single livepatch after rolling out 40 versions on our 6.1.y stable kernel.)
Good to know. We should keep it simple. For example, it might be nice to allow atomit update to more livepatches but it probably is not worth the effort.
Quoted 3 lines
However, this isn't an unfixable issue. In the future, I plan to introduce dynamical provides IDs and obsoletes IDs at load time, allowing us to change IDs on demand
This sounds hacky and dangerous. IMHO, this should not be needed if we allow to replace C with B in the above example.
Best Rergards,
Petr
Re: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 110 lines · Petr Mladek
Quoted 81 lines · Yafang Shao
Quoted 80 lines · Yafang Shao
Quoted 32 lines · Petr Mladek
Quoted 30 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 5 lines
include/linux/livepatch.h +3
-1struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
right.
Quoted 7 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Quoted 6 lines
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
Good question.
I believe it's best to refuse to load it, as doing otherwise might introduce potential issues. I will update this rule in the next version.Quoted 10 lines
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?Quoted 6 lines
Quoted 2 lines
No, I don't believe B should be replaced by A. In this case, if B is already enabled, A should fail to load.
This brings up another question regarding large server fleets. In our production environment, if a new livepatch introduces a regression, we always roll back to the old version.
Good point. Well, note that the above example with two livepatches is artificial. More realistic example would be with three livepatches, for example:
+ livepatch A: funcs[] = {a, b}; provides = 1;
+ livepatch B: funcs[] = {c, d}; provides = 2;
+ livepatch C: funcs[] = {a, b, c, d, e}; provides = 1; obsoletes[] = {2}Now, imagine that C does some semantic changes in the function 'e' so that all other functions {a,b,c,d} have to be updated accordingly.
You could not install B when C is installed. The functions {c,d} would break because they won't be compatible with the rest.
The only safe solution would be to replace:
1. C with B and install A later
2. C with A and install B later
3. C with A+B atomically
Quoted 8 lines
The 3rd solution would be the best. But it might need some significant changes in the core code. We would need to handle an array of transition patches instead of just one.
But we might allow 1st and 2nd solution after all.
Summary: We should not allow to install B in parallel with C.
Instead, we might allow to replace C with B.
Sounds good.
Quoted 12 lines
Quoted 7 lines
For example, if A is the old
version and B is the new one, we will roll back to A if B causes issues. In that case, if we refuse to load A while B is already loaded, we can't roll back. (In practice, though, we haven't rolled back a single livepatch after rolling out 40 versions on our 6.1.y stable kernel.)Good to know. We should keep it simple. For example, it might be nice to allow atomit update to more livepatches but it probably is not worth the effort.
Quoted 6 lines
Quoted 3 lines
However, this isn't an unfixable issue. In the future, I plan to introduce dynamical provides IDs and obsoletes IDs at load time, allowing us to change IDs on demand
This sounds hacky and dangerous. IMHO, this should not be needed if we allow to replace C with B in the above example.
Thanks for your suggestion.
I will proceed with implementing this update accordingly
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoQuoted 50 lines · Yafang Shao
Quoted 32 lines · Petr Mladek
Quoted 30 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
include/linux/livepatch.h +2
-1@@ -123,7 +123,8 @@ 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) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries @@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 143
Quoted 5 lines
include/linux/livepatch.h +3
-1struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;This is a different sematic in compare with the other arrays. I guess that you wanted to allow obsoleting livepatch with '0' ID.
right.
Quoted 6 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
Quoted 5 lines
Quoted 1 line
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Ah, I have missed that the current code does not treat '0' special. I was confused by the following sentence in the commit message:
<paste>
By default (provides=0), any livepatch replaces any other livepatch.
</paste>
and this paragraph in the documentation:
<paste>
It should be emphasized that the preferred and most secure way is to always use the default ``provides = 0``. In this mode, any livepatch replaces any other livepatch, preventing any unexpected interactions between incompatible livepatches.
</paste>
I understood it the way that ``provides = 0`` was supposed to be special and always replace all other livepatches.
I think that I was affected by Miroslav. I had an off-list discussion with him about this some time ago. My understanding is that Miroslav would prefer to somehow preserve the original ``replace = true`` when the livepatch patch replaced anything.
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
I am not completely sure that this the special handling is a good idea. A better solution might be an option which might be used when loading the livepatch. e.g.
insmod livepatch.ko replace_all=y
Any opinions?
Miroslav?
Quoted 16 lines
Quoted 4 lines
Another question:
Should we allow to enable a livepatch when its provides id is obsoleted by a currently enabled livepatch?
Good question.
I believe it's best to refuse to load it, as doing otherwise might introduce potential issues. I will update this rule in the next version.Quoted 6 lines
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:2, obsoletes:1
Quoted 7 lines
Quoted 4 lines
Now, two scenarios:
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch B get replaced by livepatch A?No, I don't believe B should be replaced by A. In this case, if B is already enabled, A should fail to load.
This is my preference as well.
Quoted 21 lines
Quoted 21 lines
The current code would allow to replace B with A when there is _no_ real conflict in the livaptched objects, functions, and states.
But does it make sense?
Reasoning: Livepatch B obsoleted the livepatch A for a reason.
It sounds like they should not be enabled at the same time.Special case:
Should we allow to install a livepatch with non-zero provides when a livepatch with zero provides is installed.
For example, let's have:
+ Livepatch A: provides:1
+ Livepatch B: provides:0Now, two scenarios:
Quoted 8 lines
Quoted 6 lines
1. Livepatch A can be replaced by livepatch B. This is easy.
2. Can livepatch A be installed in parallel with B?Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.So, let's just not treat '0' as a special case?
I am not sure.
I personally think that '0' should not be special. A better solution for a forced cleanup is the "replace_all" module option.
The module option would need to be implemented in the livepatch code. But it will need some support in the livepatch core as well, either a flag in struct klp_patch or parameter in klp_enable_patch(). IMHO, the flag in struct klp_patch might be more practical.
Best Regards,
Petr
Song Liu
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
On Thu, Sep 3, 2026 at 2:27 AM Petr Mladek <[email protected]> wrote:
[...]
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.So, let's just not treat '0' as a special case?
I am not sure.
I personally think that '0' should not be special. A better solution for a forced cleanup is the "
replace_all" module option.
If we want '0' to be special, let's make it "not replace anything". Then folks who prefer to have all the live patch loaded at the same time have an easy solution.
Quoted 4 lines
The module option would need to be implemented in the livepatch code. But it will need some support in the livepatch core as well, either a flag in struct
klp_patchor parameter inklp_enable_patch(). IMHO, the flag in structklp_patchmight be more practical.
What is the use case of replace_all?
Thanks,
Song
Re: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 9 lines · Song Liu
On Thu, Sep 3, 2026 at 2:27 AM Petr Mladek <[email protected]> wrote:
[...]Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Reasoning: The livepatch B replaces everything because it wants
to be the only installed livepatch. It sounds weird to "break" it by installing A in parallel later again.So, let's just not treat '0' as a special case?
Quoted 8 lines
Quoted 4 lines
I am not sure.
I personally think that '0' should not be special. A better solution for a forced cleanup is the "
replace_all" module option.If we want '0' to be special, let's make it "not replace anything". Then folks who prefer to have all the live patch loaded at the same time have an easy solution.
Perhaps we should set aside the special handling of '0' for now. We can always add it later if real use cases emerge.
Quoted 7 lines
Quoted 5 lines
The module option would need to be implemented in the livepatch code. But it will need some support in the livepatch core as well, either a flag in struct
klp_patchor parameter inklp_enable_patch(). IMHO, the flag in structklp_patchmight be more practical.What is the use case of
replace_all?
With provides/obsoletes, the default behavior is 'replace all' if the user doesn't set any IDs, because there can only be one livepatch enabled. However, if the user explicitly sets provides and obsoletes IDs, they are expected to understand how this feature works. Personally, I lean towards not adding this replace_all flag.
Signature · 2 lines
Regards Yafang
Miroslav Benes
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Petr MladekQuoted 35 lines
Quoted 10 lines
Quoted 6 lines
But '0' is special. It obsoletes anything. Maybe, we could make it even more special and say that it can't obsoleted. Then we would be able to use it as the trailing element in the array...
I do not have strong opinion about this. It is just an idea.
Making '0' special is good for backward compatibility, but it complicates usage for users. Therefore, I prefer not to treat '0' as a special case.
Ah, I have missed that the current code does not treat '0' special. I was confused by the following sentence in the commit message:
<paste>
By default (provides=0), any livepatch replaces any other livepatch.
</paste>and this paragraph in the documentation:
<paste>
It should be emphasized that the preferred and most secure way is to always use the default ``provides = 0``. In this mode, any livepatch replaces any other livepatch, preventing any unexpected interactions between incompatible livepatches.
</paste>I understood it the way that ``provides = 0`` was supposed to be special and always replace all other livepatches.
I think that I was affected by Miroslav. I had an off-list discussion with him about this some time ago. My understanding is that Miroslav would prefer to somehow preserve the original ``replace = true`` when the livepatch patch replaced anything.
Quoted 7 lines
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
My intention was not to regress and disallow a use case which is currently supported. That is to replace everything applied no matter what.
If I take my SUSE hat on, I think we can happily live with provides being set to a random number and be done with it. A customer with their own live patches applied is on their own anyway so we can ignore it.
[ changing hats ]
So I only wanted to preserve the behaviour for a potential user out there relying on it. Maybe not worth it if it complicates the implementation. It can be emulated using provides/obsoletes and the fact that a user/admin/distributor should have their systems under control. If not, it can lead to weird states anyway (yes, I still think that allowing different parties to livepatch the same fleet is a call for trouble :)).
Miroslav
Song Liu
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
On Wed, Sep 9, 2026 at 5:32 AM Miroslav Benes <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 8 lines
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
My intention was not to regress and disallow a use case which is currently supported. That is to replace everything applied no matter what.
I am curious about this use case. Do we really have users who:
- Load patch A, with replace=false;
- Load patch B, with replace=false;
- Load patch C, with replace=true, replacing both A and B?
I think this is not good practice anyway. Instead, users would either use a bunch of patches with replace=false; or only load one patch at a time, and keep replace=true for all of them.
Did I miss any reasonable users or use cases here?
Thanks,
Song
Quoted 14 lines
If I take my SUSE hat on, I think we can happily live with provides being set to a random number and be done with it. A customer with their own live patches applied is on their own anyway so we can ignore it.
[ changing hats ]
So I only wanted to preserve the behaviour for a potential user out there relying on it. Maybe not worth it if it complicates the implementation. It can be emulated using provides/obsoletes and the fact that a user/admin/distributor should have their systems under control. If not, it can lead to weird states anyway (yes, I still think that allowing different parties to livepatch the same fleet is a call for trouble :)).
Miroslav
Miroslav Benes
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 19 lines · Song Liu
On Wed, Sep 9, 2026 at 5:32 AM Miroslav Benes <[email protected]> wrote:
[...]Quoted 11 lines
Quoted 8 lines
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
My intention was not to regress and disallow a use case which is currently supported. That is to replace everything applied no matter what.
I am curious about this use case. Do we really have users who:
- Load patch A, with replace=false;
- Load patch B, with replace=false;
- Load patch C, with replace=true, replacing both A and B?
Quoted 5 lines
I think this is not good practice anyway. Instead, users would either use a bunch of patches with replace=false; or only load one patch at a time, and keep replace=true for all of them.
Did I miss any reasonable users or use cases here?
I am not aware of anyone specific but the scenario above does not look too crazy to me. And the experience taught me that if something is possible, there is definitely someone out there doing it. However, as I said, I think the current implementation of the scoped replace should be enough for everybody. If not, we will learn about the use case in a couple of years.
Miroslav
Re: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 26 lines · Miroslav Benes
Quoted 23 lines · Song Liu
On Wed, Sep 9, 2026 at 5:32 AM Miroslav Benes <[email protected]> wrote:
[...]Quoted 11 lines
Quoted 8 lines
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
My intention was not to regress and disallow a use case which is currently supported. That is to replace everything applied no matter what.
I am curious about this use case. Do we really have users who:
- Load patch A, with replace=false;
- Load patch B, with replace=false;
- Load patch C, with replace=true, replacing both A and B?I think this is not good practice anyway. Instead, users would either use a bunch of patches with replace=false; or only load one patch at a time, and keep replace=true for all of them.
Quoted 7 lines
Quoted 1 line
Did I miss any reasonable users or use cases here?
I am not aware of anyone specific but the scenario above does not look too crazy to me. And the experience taught me that if something is possible, there is definitely someone out there doing it. However, as I said, I think the current implementation of the scoped replace should be enough for everybody.
Right. With the current implementation, a user can still replace all existing livepatches, for example by listing all of their provides IDs in the new livepatch's obsoletes. So the legacy "replace everything" behavior can be emulated without any kernel changes.
I will keep it as is.
Quoted 2 lines
If not, we will learn about the use case in a couple of years.
Agreed, let's see the feedback from the end users of this new mechanism.
Signature · 2 lines
Regards Yafang
Song Liu
maintainerRe: Replace rules: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Miroslav BenesQuoted 26 lines · Miroslav Benes
Quoted 23 lines · Song Liu
On Wed, Sep 9, 2026 at 5:32 AM Miroslav Benes <[email protected]> wrote:
[...]Quoted 11 lines
Quoted 8 lines
It might be useful for OS providers who want to make sure that their kernel critical fixes can be installed on the user system. Otherwise, customers might complain that some update failed, ...
Of course, it has a drawback that any livepatch with ``prov ides= 0`` would wipe any 3rd party livepatches, even when they are against 3rd party modules.
My intention was not to regress and disallow a use case which is currently supported. That is to replace everything applied no matter what.
I am curious about this use case. Do we really have users who:
- Load patch A, with replace=false;
- Load patch B, with replace=false;
- Load patch C, with replace=true, replacing both A and B?I think this is not good practice anyway. Instead, users would either use a bunch of patches with replace=false; or only load one patch at a time, and keep replace=true for all of them.
Quoted 8 lines
Quoted 1 line
Did I miss any reasonable users or use cases here?
I am not aware of anyone specific but the scenario above does not look too crazy to me. And the experience taught me that if something is possible, there is definitely someone out there doing it. However, as I said, I think the current implementation of the scoped replace should be enough for everybody. If not, we will learn about the use case in a couple of years.
Agreed that current version is capable to cover all these cases. Let's ship it. :)
Thanks,
Song
Petr Mladek
maintainerdocumentation: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoQuoted 10 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
What is the "replace set"? The term is used few times in the commit message, documentation, comments but it is not defined anywhere.
IMHO, the term is a bit misleding, both "replace" and "set" parts.
1. My understanding is that "provides" is an "id" which is connected by a set of objects, and functions which are modified (replaced?) by the livepatch? Here the "replace" would mean "replaced functionality".
But the provides id also covers the related callbacks, shadow variables and livepatch states. Here the word "replace" does not fit.
2. We are talking about "atomic replace". Here one livepatch replaces one or more other livepatches. Here the word "replace" would mean "replace livepatch(es)". But does the livepach replace one or more livepatch sets?
Do we need this term at all?
IMHO, we could live better without it, for example:
<proposal>
Subject: livepatch: Implement provides and obsoletes for scoped atomic replace
- provides: an usinged 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.
</proposal>
Quoted 3 lines
- 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 replace sets.
We should mention here the special meanting of the "0" id. It enforces the most secure mode when only one livepatch can be enabled at any time.
Also it is important to explain here also the compatibility rules. They are important part of the design.
This approach allows to have more livepatches installed in parallel but only when they are not conflicting. They would conflict if they attempt to livepatch the same function or use livepatch states with the same ID.
Quoted 10 lines
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes listThe klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Quoted 6 lines
Two helper functions are introduced:
-klp_patch_replaceable(): checks if an new patch will replace the old patch
-klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.
I agree with Josh. There is no need to mention these two new functions in the commit message.
Quoted 30 lines
Suggested-by Song Liu <[email protected]>Suggested-by Joe Lawrence <[email protected]>Suggested-by Petr Mladek <[email protected]>Co-developed-by Petr Mladek <[email protected]>Signed-off-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>Acked-by Song Liu <[email protected]>--- .../ABI/testing/sysfs-kernel-livepatch | 22 ++++- .../livepatch/cumulative-patches.rst | 93 +++++++++++++------ Documentation/livepatch/livepatch.rst | 23 +++-- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 69 ++++++++++++-- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 57 ++++++++++-- kernel/livepatch/transition.c | 11 ++- scripts/livepatch/init.c | 71 +++++++++++++- scripts/livepatch/klp-build | 78 ++++++++++++++-- 10 files changed, 351 insertions(+), 81 deletions(-)Documentation/ABI/testing/sysfs-kernel-livepatch +0
-2@@ -47,13 +47,25 @@ Description: disabled when the feature is used. See Documentation/livepatch/livepatch.rst for more information. -What: /sys/kernel/livepatch/<patch>/replace -Date: Jun 2024
Documentation/ABI/testing/sysfs-kernel-livepatch Description: · line 56
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +5
-3-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. + Only one active livepatch per provides id is allowed.
This sounds like you could not load another livepatch with the same id. But you actually could. We should make it clear that it would replace the existing one. I suggest something like:
<proposal>
An attribute to show the provides id of this livepatch. Livepatches with the same provides id replace each other.
</proposal
Quoted 51 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +12
-0+ +What: /sys/kernel/livepatch/<patch>/obsoletes +Date: Jun 2026 +KernelVersion: 7.4.0 +Contact: [email protected] +Description: + An attribute to show the obsoletes ids of this livepatch. + The obsoletes ids are a comma-separated list of provides + ids that this patch obsoletes. When this livepatch is + loaded, any existing livepatch whose provides id matches + either this patch's provides id or any id in the obsoletes + list will be atomically replaced. What: /sys/kernel/livepatch/<patch>/stack_order Date: Jan 2025Documentation/livepatch/cumulative-patches.rst +1
-27@@ -2,33 +2,66 @@ Atomic Replace & Cumulative Patches =================================== -There might be dependencies between livepatches. If multiple patches need -to do different changes to the same function(s) then we need to define -an order in which the patches will be installed. And function implementations -from any newer livepatch must be done on top of the older ones. - -This might become a maintenance nightmare. Especially when more patches -modified the same function in different ways. - -An elegant solution comes with the feature called "Atomic Replace". It allows -creation of so called "Cumulative Patches". They include all wanted changes -from all older livepatches and completely replace them in one transition. - -Usage ------ - -The atomic replace can be enabled by setting "replace" flag in struct klp_patch, -for example:: - - static struct klp_patch patch = { - .mod = THIS_MODULE, - .objs = objs, - .replace = true, - }; - -All processes are then migrated to use the code only from the new patch. -Once the transition is finished, all older patches are automatically -disabled. +Livepatches are used to fix kernel bugs. New fixes need to be added over time.
Documentation/livepatch/cumulative-patches.rst line 15
Quoted 10 lines
Documentation/livepatch/cumulative-patches.rst +10
-0+The fixes might be independent, but they might also depend on each other. This +brings a challenge of how to keep the livepatched system safe and consistent. + +Part of the solution is the "Atomic Replace" feature, which allows the kernel to +atomically replace an existing livepatch with another one. These newer +livepatches are designed as "Cumulative Patches". They include all wanted +changes from all older livepatches and completely replace them in one +transition. + +The second part of the solution is the newly introduced ``provides`` and
s/is the newly introduced/are/
"the newly introduced" would fit into the commit message but not into the documentation. The fields are new _now_. But the documentation should make sense as long as the feature is there.
Documentation/livepatch/cumulative-patches.rst line 27
Quoted 12 lines
Documentation/livepatch/cumulative-patches.rst +12
-0+``obsoletes`` fields in ``struct klp_patch``, which allow the installation of +multiple livepatches in parallel. A livepatch will atomically replace any +already installed livepatch whose ``provides`` id matches either the new +patch's ``provides`` id or any id in the new patch's ``obsoletes`` list. +This might be used to fix independent problems separately, for example, the +livepatches might be prepared by separate teams focusing on particular +functionality or a subsystem. + +It should be emphasized that the preferred and most secure way is to always use +the default ``provides = 0``. In this mode, any livepatch replaces any other +livepatch, preventing any unexpected interactions between incompatible +livepatches.
We should make it clear here that '0' is really special. It basically provides and obsoletes livepatches with any other IDs.
Documentation/livepatch/cumulative-patches.rst line 33
Quoted 5 lines
Documentation/livepatch/cumulative-patches.rst +5
-0+Provides and Obsoletes +----------------------- + +The ``provides`` field in ``struct klp_patch`` is an unsigned integer that +identifies the livepatch's replace set. By default, it is 0.
I would get rid of the "replace set" term.
Documentation/livepatch/cumulative-patches.rst
Quoted 3 lines
Documentation/livepatch/cumulative-patches.rst +3
-0+The ``obsoletes`` field is an optional array of unsigned integers that +specifies additional ``provides`` ids to be replaced when this patch is +loaded.
The above part is good.
Quoted 3 lines
By default, it includes the patch's own ``provides`` id, ensuring +that a new patch always replaces any existing patch with the same +``provides`` id.
This sentence is confusing. My undestanding is that we do not need to mention patch's own provides id in the obsoletes array. I would just omit this sentence.
Quoted 62 lines
Documentation/livepatch/cumulative-patches.rst +30
-1+ +For example:: + + static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, + .provides = 0, + }; + +Any ``provides`` value might be associated with a set of livepatched symbols, +callbacks, shadow variables, and state IDs. By definition, there can only ever +be one active livepatch for a given ``provides`` id. + +On the contrary, livepatches with a different ``provides`` id 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. + +Atomic Replace +-------------- + +A livepatch with a given ``provides`` id is replaced by another livepatch +with the same ``provides`` id, or whose ``obsoletes`` list includes that id. +All processes are migrated to use the code only from the new patch. Once +the transition is finished, the older patch is disabled. Patches with a +different ``provides`` id are not affected and remain active. Ftrace handlers are transparently removed from functions that are no longer modified by the new cumulative patch.@@ -64,7 +97,11 @@ Limitations: - Once the operation finishes, there is no straightforward way to reverse it and restore the replaced patches atomically. - A good practice is to set .replace flag in any released livepatch. + A good practice is to use only one (default) ``provides`` id. It + makes sure that there always will be only one enabled livepatch + on the system. The consistency model will ensure a safe update + between two versions. It prevents potential problems with installing + two livepatches doing incompatible functional changes. Then re-adding an older livepatch is equivalent to downgrading to that patch. This is safe as long as the livepatches do _not_ do extra modifications in (un)patching callbacks or in the module_init()Documentation/livepatch/livepatch.rst +4
-9@@ -347,15 +347,20 @@ 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.
Documentation/livepatch/livepatch.rst to '0'. · line 363
Quoted 10 lines
Documentation/livepatch/livepatch.rst +10
-0+ +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. + +Patches with a different ``provides`` id are not affected by this +process and remain active. This allows for the independent management +and stacking of multiple, non-conflicting livepatch sets.
I would avoid the word "stacking". The livepatch code has this term connected with func->stack_node and ops->func_stack. It allows to associate more struct klp_func entries with the same ftrace handler. The last or the last-but-one entry is then used depending on the state of the transition.
By other words, the stacking is used in the livepatching code when two livepatches modify the same function. But such livepatches are _conflicting_.
By other words, the sentence: "allows independent.*stacking of multiple, non-conflicting livepatch sets" does not make sense.
Documentation/livepatch/livepatch.rst to '0'. · line 365
Quoted 1 line
Documentation/livepatch/livepatch.rst +0
-0See Documentation/livepatch/cumulative-patches.rst for more details.
Best Regards,
Petr
Re: documentation: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 42 lines · Petr Mladek
Quoted 10 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
What is the "replace set"? The term is used few times in the commit message, documentation, comments but it is not defined anywhere.
IMHO, the term is a bit misleding, both "replace" and "set" parts.
1. My understanding is that "provides" is an "id" which is connected by a set of objects, and functions which are modified (replaced?) by the livepatch? Here the "replace" would mean "replaced functionality".
But the provides id also covers the related callbacks, shadow variables and livepatch states. Here the word "replace" does not fit.
2. We are talking about "atomic replace". Here one livepatch replaces one or more other livepatches. Here the word "replace" would mean "replace livepatch(es)". But does the livepach replace one or more livepatch sets?
Do we need this term at all?
IMHO, we could live better without it, for example:
<proposal>
Subject: livepatch: Implement provides and obsoletes for scoped atomic replace
Quoted 5 lines
- provides: an usinged 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.
</proposal>
Thanks for your suggestion.
I will update it.
Quoted 11 lines
Quoted 4 lines
- 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 replace sets.
We should mention here the special meanting of the "0" id. It enforces the most secure mode when only one livepatch can be enabled at any time.
Also it is important to explain here also the compatibility rules. They are important part of the design.
Ah, if you prefer to make '0' a special case, I will implement it that way. This brings up the question you mentioned in another email: should we allow loading another ID when livepatch ID 0 is already enabled? It seems the answer is to reject the other ID.
Quoted 23 lines
This approach allows to have more livepatches installed in parallel but only when they are not conflicting. They would conflict if they attempt to livepatch the same function or use livepatch states with the same ID.
Quoted 10 lines
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes listThe klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Quoted 6 lines
Two helper functions are introduced:
-klp_patch_replaceable(): checks if an new patch will replace the old patch
-klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.
Quoted 2 lines
I agree with Josh. There is no need to mention these two new functions in the commit message.
I will update it.
Quoted 46 lines
Quoted 31 lines
Suggested-by Song Liu <[email protected]>Suggested-by Joe Lawrence <[email protected]>Suggested-by Petr Mladek <[email protected]>Co-developed-by Petr Mladek <[email protected]>Signed-off-by Petr Mladek <[email protected]>Signed-off-by Yafang Shao <[email protected]>Acked-by Song Liu <[email protected]>--- .../ABI/testing/sysfs-kernel-livepatch | 22 ++++- .../livepatch/cumulative-patches.rst | 93 +++++++++++++------ Documentation/livepatch/livepatch.rst | 23 +++-- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 69 ++++++++++++-- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 57 ++++++++++-- kernel/livepatch/transition.c | 11 ++- scripts/livepatch/init.c | 71 +++++++++++++- scripts/livepatch/klp-build | 78 ++++++++++++++-- 10 files changed, 351 insertions(+), 81 deletions(-)Documentation/ABI/testing/sysfs-kernel-livepatch +0
-2@@ -47,13 +47,25 @@ Description: disabled when the feature is used. See Documentation/livepatch/livepatch.rst for more information. -What: /sys/kernel/livepatch/<patch>/replace -Date: Jun 2024Documentation/ABI/testing/sysfs-kernel-livepatch Description: · line 56
Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +5
-3-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. + Only one active livepatch per provides id is allowed.This sounds like you could not load another livepatch with the same id. But you actually could. We should make it clear that it would replace the existing one. I suggest something like:
Quoted 4 lines
<proposal>
An attribute to show the provides id of this livepatch. Livepatches with the same provides id replace each other.
</proposal
Thanks for your suggestion.
I will update it.
Quoted 65 lines
Quoted 52 lines
Documentation/ABI/testing/sysfs-kernel-livepatch +12
-0+ +What: /sys/kernel/livepatch/<patch>/obsoletes +Date: Jun 2026 +KernelVersion: 7.4.0 +Contact: [email protected] +Description: + An attribute to show the obsoletes ids of this livepatch. + The obsoletes ids are a comma-separated list of provides + ids that this patch obsoletes. When this livepatch is + loaded, any existing livepatch whose provides id matches + either this patch's provides id or any id in the obsoletes + list will be atomically replaced. What: /sys/kernel/livepatch/<patch>/stack_order Date: Jan 2025Documentation/livepatch/cumulative-patches.rst +1
-27@@ -2,33 +2,66 @@ Atomic Replace & Cumulative Patches =================================== -There might be dependencies between livepatches. If multiple patches need -to do different changes to the same function(s) then we need to define -an order in which the patches will be installed. And function implementations -from any newer livepatch must be done on top of the older ones. - -This might become a maintenance nightmare. Especially when more patches -modified the same function in different ways. - -An elegant solution comes with the feature called "Atomic Replace". It allows -creation of so called "Cumulative Patches". They include all wanted changes -from all older livepatches and completely replace them in one transition. - -Usage ------ - -The atomic replace can be enabled by setting "replace" flag in struct klp_patch, -for example:: - - static struct klp_patch patch = { - .mod = THIS_MODULE, - .objs = objs, - .replace = true, - }; - -All processes are then migrated to use the code only from the new patch. -Once the transition is finished, all older patches are automatically -disabled. +Livepatches are used to fix kernel bugs. New fixes need to be added over time.Documentation/livepatch/cumulative-patches.rst line 15
Quoted 10 lines
Documentation/livepatch/cumulative-patches.rst +10
-0+The fixes might be independent, but they might also depend on each other. This +brings a challenge of how to keep the livepatched system safe and consistent. + +Part of the solution is the "Atomic Replace" feature, which allows the kernel to +atomically replace an existing livepatch with another one. These newer +livepatches are designed as "Cumulative Patches". They include all wanted +changes from all older livepatches and completely replace them in one +transition. + +The second part of the solution is the newly introduced ``provides`` ands/is the newly introduced/are/
Quoted 3 lines
"the newly introduced" would fit into the commit message but not into the documentation. The fields are new _now_. But the documentation should make sense as long as the feature is there.
I will update it.
Quoted 17 lines
Documentation/livepatch/cumulative-patches.rst line 27
Quoted 13 lines
Documentation/livepatch/cumulative-patches.rst +12
-0+``obsoletes`` fields in ``struct klp_patch``, which allow the installation of +multiple livepatches in parallel. A livepatch will atomically replace any +already installed livepatch whose ``provides`` id matches either the new +patch's ``provides`` id or any id in the new patch's ``obsoletes`` list. +This might be used to fix independent problems separately, for example, the +livepatches might be prepared by separate teams focusing on particular +functionality or a subsystem. + +It should be emphasized that the preferred and most secure way is to always use +the default ``provides = 0``. In this mode, any livepatch replaces any other +livepatch, preventing any unexpected interactions between incompatible +livepatches.We should make it clear here that '0' is really special. It basically provides and obsoletes livepatches with any other IDs.
Quoted 7 lines
Documentation/livepatch/cumulative-patches.rst line 33
Quoted 5 lines
Documentation/livepatch/cumulative-patches.rst +5
-0+Provides and Obsoletes +----------------------- + +The ``provides`` field in ``struct klp_patch`` is an unsigned integer that +identifies the livepatch's replace set. By default, it is 0.I would get rid of the "replace set" term.
I will update it.
Quoted 7 lines
Documentation/livepatch/cumulative-patches.rst
Quoted 4 lines
Documentation/livepatch/cumulative-patches.rst +3
-0+The ``obsoletes`` field is an optional array of unsigned integers that +specifies additional ``provides`` ids to be replaced when this patch is +loaded.The above part is good.
Quoted 7 lines
Quoted 3 lines
By default, it includes the patch's own ``provides`` id, ensuring +that a new patch always replaces any existing patch with the same +``provides`` id.
This sentence is confusing. My undestanding is that we do not need to mention patch's own provides id in the obsoletes array. I would just omit this sentence.
I will update it.
Quoted 80 lines
Quoted 63 lines
Documentation/livepatch/cumulative-patches.rst +30
-1+ +For example:: + + static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, + .provides = 0, + }; + +Any ``provides`` value might be associated with a set of livepatched symbols, +callbacks, shadow variables, and state IDs. By definition, there can only ever +be one active livepatch for a given ``provides`` id. + +On the contrary, livepatches with a different ``provides`` id 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. + +Atomic Replace +-------------- + +A livepatch with a given ``provides`` id is replaced by another livepatch +with the same ``provides`` id, or whose ``obsoletes`` list includes that id. +All processes are migrated to use the code only from the new patch. Once +the transition is finished, the older patch is disabled. Patches with a +different ``provides`` id are not affected and remain active. Ftrace handlers are transparently removed from functions that are no longer modified by the new cumulative patch.@@ -64,7 +97,11 @@ Limitations: - Once the operation finishes, there is no straightforward way to reverse it and restore the replaced patches atomically. - A good practice is to set .replace flag in any released livepatch. + A good practice is to use only one (default) ``provides`` id. It + makes sure that there always will be only one enabled livepatch + on the system. The consistency model will ensure a safe update + between two versions. It prevents potential problems with installing + two livepatches doing incompatible functional changes. Then re-adding an older livepatch is equivalent to downgrading to that patch. This is safe as long as the livepatches do _not_ do extra modifications in (un)patching callbacks or in the module_init()Documentation/livepatch/livepatch.rst +4
-9@@ -347,15 +347,20 @@ 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.Documentation/livepatch/livepatch.rst to '0'. · line 363
Quoted 10 lines
Documentation/livepatch/livepatch.rst +10
-0+ +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. + +Patches with a different ``provides`` id are not affected by this +process and remain active. This allows for the independent management +and stacking of multiple, non-conflicting livepatch sets.I would avoid the word "stacking". The livepatch code has this term connected with
func->stack_nodeandops->func_stack. It allows to associate more structklp_funcentries with the same ftrace handler. The last or the last-but-one entry is then used depending on the state of the transition.
Quoted 6 lines
By other words, the stacking is used in the livepatching code when two livepatches modify the same function. But such livepatches are _conflicting_.
By other words, the sentence: "allows independent.*stacking of multiple, non-conflicting livepatch sets" does not make sense.
Thanks for your detailed explanation. I will update it.
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerRe: documentation: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 10 lines · Yafang Shao
Quoted 9 lines · Petr Mladek
Quoted 3 lines · Yafang Shao
- 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 replace sets.
We should mention here the special meanting of the "0" id. It enforces the most secure mode when only one livepatch can be enabled at any time.
Quoted 7 lines
Quoted 2 lines
Also it is important to explain here also the compatibility rules. They are important part of the design.
Ah, if you prefer to make '0' a special case, I will implement it that way. This brings up the question you mentioned in another email: should we allow loading another ID when livepatch ID 0 is already enabled? It seems the answer is to reject the other ID.
We first need to decide about the compatibility rules. Let's continue the discussion in the other thread "Replace rules...", see
https://lore.kernel.org/all/[email protected]/
Best Regards,
Petr
Petr Mladek
maintainercode cleanup: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoQuoted 36 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
kernel/livepatch/core.c +8
-2@@ -448,13 +449,32 @@ static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,return count; } -static ssize_t replace_show (struct kobject *kobj, +static ssize_t provides_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {struct klp_patch *patch; patch = container_of(kobj, struct klp_patch, kobj); -return sysfs_emit (buf,"%d\n" , patch->replace); +return sysfs_emit (buf,"%u\n" , patch->provides); +} + +static ssize_t obsoletes_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +struct klp_patch *patch;
kernel/livepatch/core.c static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, · line 474
Quoted 10 lines
kernel/livepatch/core.c +10
-0+unsigned int i; +int len =0 ; + + patch = container_of(kobj, struct klp_patch, kobj); +if (!patch->obsoletes || !patch->nr_obsoletes) +return sysfs_emit (buf,"\n" ); + +for (i =0 ; i < patch->nr_obsoletes; i++) + len +=sysfs_emit_at (buf, len,"%u%s" , patch->obsoletes[i], + i < patch->nr_obsoletes -1 ?"," :"" );
It would be easier if we swithed the oder of %u and %s:
len += sysfs_emit_at(buf, len, "%s%u", patch->obsoletes[i], i == 0, "" : ",");
Quoted 9 lines
kernel/livepatch/core.c +3
-0+ + len +=sysfs_emit_at (buf, len,"\n" ); +return len; } static ssize_t stack_order_show(struct kobject *kobj,
kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 502
Quoted 6 lines
kernel/livepatch/transition.c +1
-1@@ -498,7 +498,7 @@ void klp_try_complete_transition(void)*/ if (!patch->enabled)klp_free_patch_async (patch); -else if (patch->replace) +else klp_free_replaced_patches_async (patch);
Nit: I would use this opportunity and invert the logic.
Positive checks are always easier to read.
I mean to use:
if (patch->enabled)
klp_free_replaced_patches_async(patch);
else
klp_free_patch_async(patch);kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 504
Quoted 2 lines
kernel/livepatch/transition.c +0
-0}
Best Regards,
Petr
Re: code cleanup: was: Re: [PATCH v7 for-next 3/8] livepatch: Implement replace set for scoped atomic replace
Quoted 51 lines · Petr Mladek
Quoted 36 lines · Yafang Shao
The current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct
klp_patch:- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- 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 replace sets.
kernel/livepatch/core.c +8
-2@@ -448,13 +449,32 @@ static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,return count; } -static ssize_t replace_show (struct kobject *kobj, +static ssize_t provides_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {struct klp_patch *patch; patch = container_of(kobj, struct klp_patch, kobj); -return sysfs_emit (buf,"%d\n" , patch->replace); +return sysfs_emit (buf,"%u\n" , patch->provides); +} + +static ssize_t obsoletes_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +struct klp_patch *patch;kernel/livepatch/core.c static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, · line 474
Quoted 10 lines
kernel/livepatch/core.c +10
-0+unsigned int i; +int len =0 ; + + patch = container_of(kobj, struct klp_patch, kobj); +if (!patch->obsoletes || !patch->nr_obsoletes) +return sysfs_emit (buf,"\n" ); + +for (i =0 ; i < patch->nr_obsoletes; i++) + len +=sysfs_emit_at (buf, len,"%u%s" , patch->obsoletes[i], + i < patch->nr_obsoletes -1 ?"," :"" );It would be easier if we swithed the oder of %u and %s:
Quoted 2 lines
len += sysfs_emit_at(buf, len, "%s%u", patch->obsoletes[i],
i == 0, "" : ",");I will update it.
Quoted 18 lines
Quoted 10 lines
kernel/livepatch/core.c +3
-0+ + len +=sysfs_emit_at (buf, len,"\n" ); +return len; } static ssize_t stack_order_show(struct kobject *kobj,kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 502
Quoted 6 lines
kernel/livepatch/transition.c +1
-1@@ -498,7 +498,7 @@ void klp_try_complete_transition(void)*/ if (!patch->enabled)klp_free_patch_async (patch); -else if (patch->replace) +else klp_free_replaced_patches_async (patch);Nit: I would use this opportunity and invert the logic.
Quoted 8 lines
Positive checks are always easier to read.
I mean to use:
if (patch->enabled)
klp_free_replaced_patches_async(patch);
else
klp_free_patch_async(patch);Thanks for your suggestion.
I will update it.
Signature · 2 lines
Regards Yafang
PATCH v7 4/8 livepatch: Deprecate stack_order
In reply to Yafang Shaostack_order is no longer needed for atomic-replace livepatches, as a single function can only be modified by a unique replace_set. Since this livepatch sysfs interface is mainly intended for kernel developers, backward compatibility does not need to be maintained.
--- .../ABI/removed/sysfs-kernel-livepatch | 16 +++++++++++++ .../ABI/testing/sysfs-kernel-livepatch | 9 ------- kernel/livepatch/core.c | 24 ------------------- 3 files changed, 16 insertions(+), 33 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
Documentation/ABI/removed/sysfs-kernel-livepatch new file +16 -0
@@ -0,0 +1,16 @@ +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. + +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
@@ -67,15 +67,6 @@ Description: either this patch's provides id or any id in the obsoletes list will be atomically replaced. -What: /sys/kernel/livepatch/<patch>/stack_order -Date: Jan 2025 -KernelVersion: 6.14.0 -Description: - This attribute specifies the sequence in which live patch modules - are applied to the system. If multiple live patches modify the same - function, the implementation with the biggest 'stack_order' number - is used, unless a transition is currently in progress. - What: /sys/kernel/livepatch/<patch>/<object> Date: Nov 2014 KernelVersion: 3.19.0
kernel/livepatch/core.c +0 -24
@@ -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
Josh Poimboeuf
maintainerRe: [PATCH v7 for-next 4/8] livepatch: Deprecate stack_order
Quoted 21 lines · Yafang Shao
stack_orderis no longer needed for atomic-replace livepatches, as a single function can only be modified by a uniquereplace_set. Since this livepatch sysfs interface is mainly intended for kernel developers, backward compatibility does not need to be maintained.Signed-off-by Yafang Shao <[email protected]>Cc Wardenjohn <[email protected]>Acked-by Song Liu <[email protected]>--- .../ABI/removed/sysfs-kernel-livepatch | 16 +++++++++++++ .../ABI/testing/sysfs-kernel-livepatch | 9 ------- kernel/livepatch/core.c | 24 ------------------- 3 files changed, 16 insertions(+), 33 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatchDocumentation/ABI/removed/sysfs-kernel-livepatch new file +0
-0
Documentation/ABI/removed/sysfs-kernel-livepatch line 7
Quoted 7 lines
Documentation/ABI/removed/sysfs-kernel-livepatch new file +7
-0@@ -0,0 +1,16 @@ +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.
This part belongs in that other patch which removed it?
Signature · 1 line
Josh
Re: [PATCH v7 for-next 4/8] livepatch: Deprecate stack_order
Quoted 17 lines · Josh Poimboeuf
Quoted 15 lines · Yafang Shao
stack_orderis no longer needed for atomic-replace livepatches, as a single function can only be modified by a uniquereplace_set. Since this livepatch sysfs interface is mainly intended for kernel developers, backward compatibility does not need to be maintained.Signed-off-by Yafang Shao <[email protected]>Cc Wardenjohn <[email protected]>Acked-by Song Liu <[email protected]>--- .../ABI/removed/sysfs-kernel-livepatch | 16 +++++++++++++ .../ABI/testing/sysfs-kernel-livepatch | 9 ------- kernel/livepatch/core.c | 24 ------------------- 3 files changed, 16 insertions(+), 33 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
Quoted 15 lines
Documentation/ABI/removed/sysfs-kernel-livepatch line 7
Quoted 13 lines
Documentation/ABI/removed/sysfs-kernel-livepatch new file +7
-0@@ -0,0 +1,16 @@ +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.This part belongs in that other patch which removed it?
My bad.
I will update it.
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerRe: [PATCH v7 for-next 4/8] livepatch: Deprecate stack_order
In reply to Yafang ShaoQuoted 2 lines · Yafang Shao
stack_orderis no longer needed for atomic-replace livepatches, as a single function can only be modified by a uniquereplace_set.
I would avoid "replace_set". As explained in 3rd patch, the term "replace set" is ambiguous. And "replace_set" with the underscore does not exits. I would write something like:
<proposal>
The stack_order sysfs attribute was added to determine which active livepatch is used for a given function. It is no longer needed because recent changes refuse loading another livepatch which would modify an already livepatched function unless the new livepatch would replace the conflicting one.
</proposal>
Quoted 2 lines
Since this livepatch sysfs interface is mainly intended for kernel developers, backward compatibility does not need to be maintained.
Best Regards,
Petr
Re: [PATCH v7 for-next 4/8] livepatch: Deprecate stack_order
Quoted 9 lines · Petr Mladek
Quoted 2 lines · Yafang Shao
stack_orderis no longer needed for atomic-replace livepatches, as a single function can only be modified by a uniquereplace_set.I would avoid "
replace_set". As explained in 3rd patch, the term "replace set" is ambiguous. And "replace_set" with the underscore does not exits. I would write something like:
Quoted 7 lines
<proposal>
Thestack_ordersysfs 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.
</proposal>
Thanks for the suggestion.
I will update it.
Signature · 2 lines
Regards Yafang
PATCH v7 5/8 selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
In reply to Yafang ShaoThe legacy "replace" field in struct klp_patch was replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.
Adapt the selftests so that they build and run on both old and new kernels:
- test_modules/Makefile detects whether the kernel header still declares the "replace" field. If so, it passes KLP_HAS_REPLACE to the modules via ccflags-y.
- test_klp_atomic_replace.c and other source files using .replace guard their replace module parameter and .replace assignment with #ifdef KLP_HAS_REPLACE. The #else branches are left as TODO placeholders for the upcoming provides/obsoletes based tests.
- functions.sh adds detect_replace_attr(), which loads a temporary livepatch and checks for the /sys/kernel/livepatch/<patch>/replace attribute, setting HAS_REPLACE_ATTR accordingly.
- test-livepatch.sh and test-callbacks.sh run the tests that depend on multiple livepatches coexisting or on the atomic replace behavior ("multiple livepatches" and "atomic replace") only when HAS_REPLACE_ATTR is set. These scenarios do not apply to the new kernel, where provides=0 replaces any other livepatch by default.
The provides/obsoletes based selftests will be added later.
--- tools/testing/selftests/livepatch/functions.sh | 15 +++++++++++++++ .../selftests/livepatch/test-callbacks.sh | 6 ++++++ .../selftests/livepatch/test-livepatch.sh | 6 ++++++ .../selftests/livepatch/test_modules/Makefile | 14 ++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 16 ++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 16 ++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 7 +++++++ .../livepatch/test_modules/test_klp_state2.c | 7 +++++++ 8 files changed, 87 insertions(+)
tools/testing/selftests/livepatch/functions.sh +15 -0
@@ -351,6 +351,21 @@ function does_sysfs_exist() { [[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]] } +# detect_replace_attr() - detect whether the running kernel still exposes +# the legacy "replace" sysfs attribute and set HAS_REPLACE_ATTR +# accordingly. The atomic replace tests are only run when the attribute +# is present. The provides/obsoletes based tests will be added later. +function detect_replace_attr() { + HAS_REPLACE_ATTR=0 + + load_lp test_klp_livepatch + if does_sysfs_exist test_klp_livepatch "replace"; then + HAS_REPLACE_ATTR=1 + fi + disable_lp test_klp_livepatch + unload_lp test_klp_livepatch +} + # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs # path permissions # modname - livepatch module creating the sysfs interface
tools/testing/selftests/livepatch/test-callbacks.sh +6 -0
@@ -11,6 +11,8 @@ MOD_TARGET_BUSY=test_klp_callbacks_busy setup_config +detect_replace_attr + # Test a combination of loading a kernel module and a livepatch that # patches a function in the first module. Load the target module
@@ -451,6 +453,7 @@ $MOD_TARGET_BUSY: busymod_work_func exit $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Test loading multiple livepatches. This test-case is mainly for comparing # with the next test-case. #
@@ -499,8 +502,10 @@ $MOD_LIVEPATCH: post_unpatch_callback: vmlinux livepatch: '$MOD_LIVEPATCH': unpatching complete % rmmod $MOD_LIVEPATCH2 % rmmod $MOD_LIVEPATCH" +fi +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Load multiple livepatches, but the second as an 'atomic-replace' # patch. When the latter loads, the original livepatch should be # disabled and *none* of its pre/post-unpatch callbacks executed. On
@@ -548,6 +553,7 @@ $MOD_LIVEPATCH2: post_unpatch_callback: vmlinux livepatch: '$MOD_LIVEPATCH2': unpatching complete % rmmod $MOD_LIVEPATCH2 % rmmod $MOD_LIVEPATCH" +fi exit 0
tools/testing/selftests/livepatch/test-livepatch.sh +6 -0
@@ -13,6 +13,8 @@ MOD_TARGET_PATCH=test_klp_mod_patch setup_config +detect_replace_attr + # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior
@@ -49,6 +51,7 @@ livepatch: '$MOD_LIVEPATCH1': unpatching complete % rmmod $MOD_LIVEPATCH1" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior # - load another livepatch and verify that both livepatches are active
@@ -107,8 +110,10 @@ livepatch: '$MOD_LIVEPATCH1': starting unpatching transition livepatch: '$MOD_LIVEPATCH1': completing unpatching transition livepatch: '$MOD_LIVEPATCH1': unpatching complete % rmmod $MOD_LIVEPATCH1" +fi +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior # - load two additional livepatches and check the number of livepatch modules
@@ -196,6 +201,7 @@ livepatch: '$MOD_REPLACE': starting unpatching transition livepatch: '$MOD_REPLACE': completing unpatching transition livepatch: '$MOD_REPLACE': unpatching complete % rmmod $MOD_REPLACE" +fi # - load a target module that provides /proc/test_klp_mod_target with
tools/testing/selftests/livepatch/test_modules/Makefile +14 -0
@@ -16,6 +16,20 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_SRC := $(if $(wildcard $(srctree)/include/linux/livepatch.h), \ + $(srctree), $(KDIR)) +KLP_HAS_REPLACE := $(shell grep -q 'bool replace' \ + $(KLP_SRC)/include/linux/livepatch.h 2>/dev/null \ + && echo y) +ifeq ($(KLP_HAS_REPLACE),y) +ccflags-y += -DKLP_HAS_REPLACE +endif + # Ensure that KDIR exists, otherwise skip the compilation modules: ifneq ("$(wildcard $(KDIR))", "")
tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifdef KLP_HAS_REPLACE 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, +#ifdef KLP_HAS_REPLACE/* set .replace in the init function below for demo purposes */ +#endif };static int test_klp_atomic_replace_init (void ) { +#ifdef KLP_HAS_REPLACE 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_callbacks_demo2.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifdef KLP_HAS_REPLACE 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, +#ifdef KLP_HAS_REPLACE/* set .replace in the init function below for demo purposes */ +#endif };static int test_klp_callbacks_demo2_init (void ) { +#ifdef KLP_HAS_REPLACE 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, +#ifdef KLP_HAS_REPLACE .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, +#ifdef KLP_HAS_REPLACE .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
Josh Poimboeuf
maintainerRe: [PATCH v7 for-next 5/8] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 4 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwas replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.
Then wouldn't the test failure be working as designed?
Signature · 1 line
Josh
Re: [PATCH v7 for-next 5/8] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 8 lines · Josh Poimboeuf
Quoted 4 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwas replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.Then wouldn't the test failure be working as designed?
Agreed that dropping the legacy field makes the old tests fail to build - that is the expected consequence of the API change, not a bug.
The point of this patch is not to hide that failure. The suite still runs on older kernels that keep the "replace" field, and we do not want a new kernel to take down the *entire* livepatch selftest set just because one module no longer compiles. The #ifdef keeps one test tree working on both kernels.
However, the current patch skips the replace scenarios silently, which is a real problem: a green run would hide the missing coverage. I'll make the skip explicit: print a SKIP line in the log when HAS_REPLACE_ATTR is not set, so the output shows exactly what is not covered, such as:
if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then
# ... atomic replace scenario ... else echo "SKIP: legacy replace attribute not present; replaced by provides/obsoletes (tests to be added later in this series)" fi
I'll fold this into the next revision. If you'd prefer the legacy scenarios dropped entirely (and only provides/obsoletes based tests kept), I can do that instead.
Signature · 2 lines
Regards Yafang
Petr Mladek
maintainerRe: [PATCH v7 for-next 5/8] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
In reply to Yafang ShaoQuoted 81 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwas replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.Adapt the selftests so that they build and run on both old and new kernels:
- test_modules/Makefile detects whether the kernel header still declares the "replace" field. If so, it passes
KLP_HAS_REPLACEto the modules via ccflags-y.-
test_klp_atomic_replace.cand other source files using .replace guard their replace module parameter and .replace assignment with #ifdefKLP_HAS_REPLACE. The #else branches are left as TODO placeholders for the upcoming provides/obsoletes based tests.-
functions.shaddsdetect_replace_attr(), which loads a temporary livepatch and checks for the /sys/kernel/livepatch/<patch>/replace attribute, settingHAS_REPLACE_ATTRaccordingly.-
test-livepatch.shandtest-callbacks.shrun the tests that depend on multiple livepatches coexisting or on the atomic replace behavior ("multiple livepatches" and "atomic replace") only whenHAS_REPLACE_ATTRis set. These scenarios do not apply to the new kernel, where provides=0 replaces any other livepatch by default.The provides/obsoletes based selftests will be added later.
Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- tools/testing/selftests/livepatch/functions.sh | 15 +++++++++++++++ .../selftests/livepatch/test-callbacks.sh | 6 ++++++ .../selftests/livepatch/test-livepatch.sh | 6 ++++++ .../selftests/livepatch/test_modules/Makefile | 14 ++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 16 ++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 16 ++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 7 +++++++ .../livepatch/test_modules/test_klp_state2.c | 7 +++++++ 8 files changed, 87 insertions(+)tools/testing/selftests/livepatch/functions.sh +15
-0@@ -351,6 +351,21 @@ function does_sysfs_exist() { [[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]] } +# detect_replace_attr() - detect whether the running kernel still exposes +# the legacy "replace" sysfs attribute and set HAS_REPLACE_ATTR +# accordingly. The atomic replace tests are only run when the attribute +# is present. The provides/obsoletes based tests will be added later. +function detect_replace_attr() { + HAS_REPLACE_ATTR=0 + + load_lp test_klp_livepatch + if does_sysfs_exist test_klp_livepatch "replace"; then + HAS_REPLACE_ATTR=1 + fi + disable_lp test_klp_livepatch + unload_lp test_klp_livepatch +} + # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs # path permissions # modname - livepatch module creating the sysfs interfacetools/testing/selftests/livepatch/test_modules/Makefile +7
-0@@ -16,6 +16,20 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_SRC := $(if $(wildcard $(srctree)/include/linux/livepatch.h), \ + $(srctree), $(KDIR))
tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 35
Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/Makefile +7
-0+KLP_HAS_REPLACE := $(shell grep -q 'bool replace' \ + $(KLP_SRC)/include/linux/livepatch.h 2>/dev/null \ + && echo y) +ifeq ($(KLP_HAS_REPLACE),y) +ccflags-y += -DKLP_HAS_REPLACE +endif + # Ensure that KDIR exists, otherwise skip the compilation modules: ifneq ("$(wildcard $(KDIR))", "")
These two ways to detect the feature are dirty hacks. It might be acceptable in selftests. But I wonder if we could do better.
Alternative solution might be to define:
HAVE_KLP_PROVIDES as true in kernel/livepatch/Kconfig
We could then use in the Makefile:
ifeq ($(HAVE_KLP_PROVIDES),y) ccflags-y += -DHAVE_KLP_PROVIDES endif
And we could zgrep /proc/config.gz in the shell scripts. For example, see kconfig_has() in tools/testing/selftests/firmware/fw_lib.sh
Note 1: It will work only when we change the state of HAVE_KLP_PROVIDES
in the patch when the provides/obsoletes are substituted.
Note 2: We should update the selftests before we change the behavior.
Otherwise, we would break bisection.
tools/testing/selftests/livepatch/test-callbacks.sh $MOD_TARGET_BUSY: busymod_work_func exit · line 459
Quoted 12 lines
tools/testing/selftests/livepatch/test-callbacks.sh +1
-0@@ -451,6 +453,7 @@ $MOD_TARGET_BUSY: busymod_work_func exit $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Test loading multiple livepatches. This test-case is mainly for comparing # with the next test-case. #
Nit: Marcos made several tests conditional and he updated indentation
after adding the if/fi.
I could live with both approaches. It just would be nice to be consistent.
And I like the idea to explicitly mention that a test has been skipped because a feature is not available.
Best Regards,
Petr
Re: [PATCH v7 for-next 5/8] selftests/livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 111 lines · Petr Mladek
Quoted 81 lines · Yafang Shao
The legacy "replace" field in struct
klp_patchwas replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.Adapt the selftests so that they build and run on both old and new kernels:
- test_modules/Makefile detects whether the kernel header still declares the "replace" field. If so, it passes
KLP_HAS_REPLACEto the modules via ccflags-y.-
test_klp_atomic_replace.cand other source files using .replace guard their replace module parameter and .replace assignment with #ifdefKLP_HAS_REPLACE. The #else branches are left as TODO placeholders for the upcoming provides/obsoletes based tests.-
functions.shaddsdetect_replace_attr(), which loads a temporary livepatch and checks for the /sys/kernel/livepatch/<patch>/replace attribute, settingHAS_REPLACE_ATTRaccordingly.-
test-livepatch.shandtest-callbacks.shrun the tests that depend on multiple livepatches coexisting or on the atomic replace behavior ("multiple livepatches" and "atomic replace") only whenHAS_REPLACE_ATTRis set. These scenarios do not apply to the new kernel, where provides=0 replaces any other livepatch by default.The provides/obsoletes based selftests will be added later.
Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- tools/testing/selftests/livepatch/functions.sh | 15 +++++++++++++++ .../selftests/livepatch/test-callbacks.sh | 6 ++++++ .../selftests/livepatch/test-livepatch.sh | 6 ++++++ .../selftests/livepatch/test_modules/Makefile | 14 ++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 16 ++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 16 ++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 7 +++++++ .../livepatch/test_modules/test_klp_state2.c | 7 +++++++ 8 files changed, 87 insertions(+)tools/testing/selftests/livepatch/functions.sh +15
-0@@ -351,6 +351,21 @@ function does_sysfs_exist() { [[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]] } +# detect_replace_attr() - detect whether the running kernel still exposes +# the legacy "replace" sysfs attribute and set HAS_REPLACE_ATTR +# accordingly. The atomic replace tests are only run when the attribute +# is present. The provides/obsoletes based tests will be added later. +function detect_replace_attr() { + HAS_REPLACE_ATTR=0 + + load_lp test_klp_livepatch + if does_sysfs_exist test_klp_livepatch "replace"; then + HAS_REPLACE_ATTR=1 + fi + disable_lp test_klp_livepatch + unload_lp test_klp_livepatch +} + # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs # path permissions # modname - livepatch module creating the sysfs interfacetools/testing/selftests/livepatch/test_modules/Makefile +7
-0@@ -16,6 +16,20 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_SRC := $(if $(wildcard $(srctree)/include/linux/livepatch.h), \ + $(srctree), $(KDIR))tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 35
Quoted 10 lines
tools/testing/selftests/livepatch/test_modules/Makefile +7
-0+KLP_HAS_REPLACE := $(shell grep -q 'bool replace' \ + $(KLP_SRC)/include/linux/livepatch.h 2>/dev/null \ + && echo y) +ifeq ($(KLP_HAS_REPLACE),y) +ccflags-y += -DKLP_HAS_REPLACE +endif + # Ensure that KDIR exists, otherwise skip the compilation modules: ifneq ("$(wildcard $(KDIR))", "")These two ways to detect the feature are dirty hacks. It might be acceptable in selftests. But I wonder if we could do better.
Alternative solution might be to define:
HAVE_KLP_PROVIDESas true inkernel/livepatch/KconfigWe could then use in the
Makefile:ifeq ($(HAVE_KLP_PROVIDES),y) ccflags-y += -DHAVE_KLP_PROVIDES endifAnd we could zgrep
/proc/config.gzin the shell scripts. For example, seekconfig_has()intools/testing/selftests/firmware/fw_lib.sh
Quoted 5 lines
Note 1: It will work only when we change the state of
HAVE_KLP_PROVIDES
in the patch when the provides/obsoletes are substituted.Note 2: We should update the selftests before we change the behavior.
Otherwise, we would break bisection.
Good point.
I will update it.
Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/livepatch/test-callbacks.sh +0
-0tools/testing/selftests/livepatch/test-callbacks.sh $MOD_TARGET_BUSY: busymod_work_func exit · line 459
Quoted 7 lines
tools/testing/selftests/livepatch/test-callbacks.sh +1
-0@@ -451,6 +453,7 @@ $MOD_TARGET_BUSY: busymod_work_func exit $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Test loading multiple livepatches. This test-case is mainly for comparing # with the next test-case. #Nit: Marcos made several tests conditional and he updated indentation
after adding the if/fi.
Quoted 2 lines
I could live with both approaches. It just would be nice to be consistent.
I will update it
Quoted 6 lines
And I like the idea to explicitly mention that a test has been skipped because a feature is not available.
Best Regards,
Petr
Signature · 2 lines
Regards Yafang
PATCH v7 6/8 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.
Fill in the #else branches of the test modules that were left as TODO placeholders in the previous patch:
- test_klp_atomic_replace.c gains provides and obsoletes module parameters for the provides/obsoletes based tests.
- test_klp_callbacks_demo2.c gains a provides module parameter.
- test_klp_livepatch.c gains a provides module parameter so that it can be loaded with a specific provides id for coexistence tests.
- test_klp_state.c and test_klp_state2.c add a comment in the #else branch noting that provides=0 (the default) is equivalent to the previous .replace=true behavior.
Add test-provides.sh with four test scenarios:
1. provides=0 replaces provides=0: two livepatches with provides=0 that modify different functions; the second replaces the first.
2. Same provides replaces: two livepatches with provides=1 that modify different functions; the second replaces the first.
3. Obsoletes replaces: a livepatch with provides=2 and obsoletes=[1] replaces a livepatch with provides=1. The provides/obsoletes sysfs values are verified.
4. provides=0 coexists with provides=1: two livepatches with different provides ids that modify different functions coexist, verifying that provides=0 does not replace a patch with a different provides id.
The provides/obsoletes based tests only run on kernels that no longer support the legacy "replace" attribute.
--- tools/testing/selftests/livepatch/Makefile | 3 +- .../livepatch/test-provides-obsoletes.sh | 174 ++++++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 22 ++- .../test_modules/test_klp_callbacks_demo2.c | 12 +- .../test_modules/test_klp_livepatch.c | 9 + .../livepatch/test_modules/test_klp_state.c | 5 +- .../livepatch/test_modules/test_klp_state2.c | 5 +- 7 files changed, 205 insertions(+), 25 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 +174 -0
174 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c +14 -8
@@ -12,10 +12,15 @@ 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). -*/ +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" );#endif #include <linux/seq_file.h>
@@ -53,10 +58,11 @@ static int test_klp_atomic_replace_init(void)#ifdef KLP_HAS_REPLACE patch.replace = replace;#else -/* -* 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; + }#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +4 -8
@@ -12,10 +12,9 @@ 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). -*/ +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" );#endif static const char *const module_state[] = {
@@ -89,10 +88,7 @@ static int test_klp_callbacks_demo2_init(void)#ifdef KLP_HAS_REPLACE patch.replace = replace;#else -/* -* TODO: Set provides/obsoletes from the module parameters -* for the provides/obsoletes based tests (to be added later). -*/ + patch.provides = provides;#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c +9 -0
@@ -7,6 +7,12 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); +#endif +#include <linux/seq_file.h> static int livepatch_cmdline_proc_show (struct seq_file *m,void *v) {
@@ -36,6 +42,9 @@ static struct klp_patch patch = {static int test_klp_livepatch_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +#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 = {#ifdef KLP_HAS_REPLACE .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 = {#ifdef KLP_HAS_REPLACE .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
Petr Mladek
maintainerRe: [PATCH v7 for-next 6/8] selftests/livepatch: Add provides/obsoletes test scenarios
Quoted 72 lines · Yafang Shao
Now that the legacy "replace" field has been replaced by the provides/obsoletes mechanism, add test scenarios to verify the new behavior.
Fill in the #else branches of the test modules that were left as TODO placeholders in the previous patch:
-
test_klp_atomic_replace.cgains provides and obsoletes module parameters for the provides/obsoletes based tests.-
test_klp_callbacks_demo2.cgains a provides module parameter.-
test_klp_livepatch.cgains a provides module parameter so that it can be loaded with a specific provides id for coexistence tests.-
test_klp_state.candtest_klp_state2.cadd a comment in the #else branch noting that provides=0 (the default) is equivalent to the previous .replace=true behavior.Add
test-provides.shwith four test scenarios:1. provides=0 replaces provides=0: two livepatches with provides=0 that modify different functions; the second replaces the first.
2. Same provides replaces: two livepatches with provides=1 that modify different functions; the second replaces the first.
3. Obsoletes replaces: a livepatch with provides=2 and obsoletes=[1] replaces a livepatch with provides=1. The provides/obsoletes sysfs values are verified.
4. provides=0 coexists with provides=1: two livepatches with different provides ids that modify different functions coexist, verifying that provides=0 does not replace a patch with a different provides id.
The provides/obsoletes based tests only run on kernels that no longer support the legacy "replace" attribute.
Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- tools/testing/selftests/livepatch/Makefile | 3 +- .../livepatch/test-provides-obsoletes.sh | 174 ++++++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 22 ++- .../test_modules/test_klp_callbacks_demo2.c | 12 +- .../test_modules/test_klp_livepatch.c | 9 + .../livepatch/test_modules/test_klp_state.c | 5 +- .../livepatch/test_modules/test_klp_state2.c | 5 +- 7 files changed, 205 insertions(+), 25 deletions(-) create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.shtools/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 := settingstools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +0
-0
tools/testing/selftests/livepatch/test-provides-obsoletes.sh line 8
Quoted 8 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +8
-0@@ -0,0 +1,174 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Yafang Shao <[email protected]> + +. $(dirname $0)/functions.sh + +MOD_ATOMIC=test_klp_atomic_replace +MOD_LIVEPATCH=test_klp_livepatch
I always get lost in the various test modules and have to look what is inside. It might help me if the name better describes the content, for example, rename:
+ test_klp_atomic_replace -> test_klp_proc_memset_livepatch
+ test_klp_livepatch -> test_klp_proc_cmdline_livepatch
So, it is clear that they "livepatch" a given "proc" interface. And it is rather clear that they livepatch different functions.
The names are long. The following might still be better than the current state:
+ test_klp_memset_lp
+ test_klp_cmdline_lp
Best Regards,
Petr
Re: [PATCH v7 for-next 6/8] selftests/livepatch: Add provides/obsoletes test scenarios
Quoted 90 lines · Petr Mladek
Quoted 72 lines · Yafang Shao
Now that the legacy "replace" field has been replaced by the provides/obsoletes mechanism, add test scenarios to verify the new behavior.
Fill in the #else branches of the test modules that were left as TODO placeholders in the previous patch:
-
test_klp_atomic_replace.cgains provides and obsoletes module parameters for the provides/obsoletes based tests.-
test_klp_callbacks_demo2.cgains a provides module parameter.-
test_klp_livepatch.cgains a provides module parameter so that it can be loaded with a specific provides id for coexistence tests.-
test_klp_state.candtest_klp_state2.cadd a comment in the #else branch noting that provides=0 (the default) is equivalent to the previous .replace=true behavior.Add
test-provides.shwith four test scenarios:1. provides=0 replaces provides=0: two livepatches with provides=0 that modify different functions; the second replaces the first.
2. Same provides replaces: two livepatches with provides=1 that modify different functions; the second replaces the first.
3. Obsoletes replaces: a livepatch with provides=2 and obsoletes=[1] replaces a livepatch with provides=1. The provides/obsoletes sysfs values are verified.
4. provides=0 coexists with provides=1: two livepatches with different provides ids that modify different functions coexist, verifying that provides=0 does not replace a patch with a different provides id.
The provides/obsoletes based tests only run on kernels that no longer support the legacy "replace" attribute.
Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- tools/testing/selftests/livepatch/Makefile | 3 +- .../livepatch/test-provides-obsoletes.sh | 174 ++++++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 22 ++- .../test_modules/test_klp_callbacks_demo2.c | 12 +- .../test_modules/test_klp_livepatch.c | 9 + .../livepatch/test_modules/test_klp_state.c | 5 +- .../livepatch/test_modules/test_klp_state2.c | 5 +- 7 files changed, 205 insertions(+), 25 deletions(-) create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.shtools/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 := settingstools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +0
-0tools/testing/selftests/livepatch/test-provides-obsoletes.sh line 8
Quoted 8 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +8
-0@@ -0,0 +1,174 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Yafang Shao <[email protected]> + +. $(dirname $0)/functions.sh + +MOD_ATOMIC=test_klp_atomic_replace +MOD_LIVEPATCH=test_klp_livepatchI always get lost in the various test modules and have to look what is inside. It might help me if the name better describes the content, for example, rename:
+
test_klp_atomic_replace->test_klp_proc_memset_livepatch
+test_klp_livepatch->test_klp_proc_cmdline_livepatch
Quoted 8 lines
So, it is clear that they "livepatch" a given "proc" interface. And it is rather clear that they livepatch different functions.
The names are long. The following might still be better than the current state:
+
test_klp_memset_lp
+test_klp_cmdline_lp
Makes sense
I will do it.
Signature · 2 lines
Regards Yafang
PATCH v7 7/8 selftests/livepatch: Add test for state ID conflict across provides
In reply to Yafang ShaoLivepatches with different provides ids must not share the same state id. If a second livepatch attempts to reuse a state id already registered by a livepatch with a different provides id, the loading will fail. However, if the second livepatch's obsoletes list includes the first livepatch's provides id, the second livepatch replaces the first one and may reuse the same state id.
Add provides and obsoletes module parameters to test_klp_state.c and test_klp_state2.c (guarded by #ifndef KLP_HAS_REPLACE) so that they can be loaded with different provides ids and obsoletes ids.
Add a "state id conflict across provides" test scenario to test-provides.sh:
- Load test_klp_state with provides=1, which registers state ID 1.
- Attempt to load test_klp_state2 with provides=2, which reuses the same state ID 1. The second livepatch is rejected because livepatches with different provides ids must not share the same state id.
- Disable and unload the remaining livepatch.
Add a "taking over system state via obsoletes" test scenario to test-state.sh:
- Load test_klp_state with provides=10, which registers state ID 1.
- Load test_klp_state2 with provides=20 obsoletes=10, which reuses the same state ID 1. Although the provides ids differ, the second livepatch replaces the first one because its obsoletes list includes the first livepatch's provides id (10). The state is taken over successfully.
- Unload the replaced livepatch, then disable and unload the second livepatch.
--- .../livepatch/test-provides-obsoletes.sh | 86 +++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 9 ++ .../livepatch/test_modules/test_klp_state2.c | 19 ++++ 3 files changed, 114 insertions(+)
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +86 -0
@@ -6,6 +6,8 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch +MOD_STATE=test_klp_state +MOD_STATE2=test_klp_state2 setup_config
@@ -171,4 +173,88 @@ livepatch: '$MOD_ATOMIC': completing unpatching transition livepatch: '$MOD_ATOMIC': unpatching complete % rmmod $MOD_ATOMIC" +# - 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 "state id conflict across provides" + +load_lp $MOD_STATE provides=1 +load_failing_mod $MOD_STATE2 provides=2 + +disable_lp $MOD_STATE +unload_lp $MOD_STATE + +check_result "% insmod test_modules/$MOD_STATE.ko 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 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 "taking over system state via obsoletes" + +load_lp $MOD_STATE provides=10 +load_lp $MOD_STATE2 provides=20 obsoletes=10 +unload_lp $MOD_STATE +disable_lp $MOD_STATE2 +unload_lp $MOD_STATE2 + +check_result "% insmod test_modules/$MOD_STATE.ko provides=10 +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 provides=20 obsoletes=10 +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" + exit 0
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +9 -0
@@ -9,6 +9,12 @@#include <linux/printk.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); +#endif +#define CONSOLE_LOGLEVEL_STATE 1/* Version 1 does not support migration. */ #define CONSOLE_LOGLEVEL_STATE_VERSION 1
@@ -151,6 +157,9 @@ static struct klp_patch patch = {static int test_klp_callbacks_demo_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +19 -0
@@ -9,6 +9,18 @@#include <linux/printk.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +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" ); +#endif +#define CONSOLE_LOGLEVEL_STATE 1/* Version 2 supports migration. */ #define CONSOLE_LOGLEVEL_STATE_VERSION 2
@@ -180,6 +192,13 @@ static struct klp_patch patch = {static int test_klp_callbacks_demo_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +if (nr_obsoletes >0 ) { + patch.obsoletes = obsoletes; + patch.nr_obsoletes = nr_obsoletes; + } +#endif return klp_enable_patch (&patch); }
Signature · 1 line
2.52.0
Petr Mladek
maintainerRe: [PATCH v7 for-next 7/8] selftests/livepatch: Add test for state ID conflict across provides
Quoted 57 lines · Yafang Shao
Livepatches with different provides ids must not share the same state id. If a second livepatch attempts to reuse a state id already registered by a livepatch with a different provides id, the loading will fail. However, if the second livepatch's obsoletes list includes the first livepatch's provides id, the second livepatch replaces the first one and may reuse the same state id.
Add provides and obsoletes module parameters to
test_klp_state.candtest_klp_state2.c(guarded by #ifndefKLP_HAS_REPLACE) so that they can be loaded with different provides ids and obsoletes ids.Add a "state id conflict across provides" test scenario to
test-provides.sh:- Load
test_klp_statewith provides=1, which registers state ID 1.
- Attempt to loadtest_klp_state2with provides=2, which reuses the same state ID 1. The second livepatch is rejected because livepatches with different provides ids must not share the same state id.
- Disable and unload the remaining livepatch.Add a "taking over system state via obsoletes" test scenario to
test-state.sh:- Load
test_klp_statewith provides=10, which registers state ID 1.
- Loadtest_klp_state2with provides=20 obsoletes=10, which reuses the same state ID 1. Although the provides ids differ, the second livepatch replaces the first one because its obsoletes list includes the first livepatch's provides id (10). The state is taken over successfully.
- Unload the replaced livepatch, then disable and unload the second livepatch.Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- .../livepatch/test-provides-obsoletes.sh | 86 +++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 9 ++ .../livepatch/test_modules/test_klp_state2.c | 19 ++++ 3 files changed, 114 insertions(+)tools/testing/selftests/livepatch/test-provides-obsoletes.sh +2
-0@@ -6,6 +6,8 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch +MOD_STATE=test_klp_state +MOD_STATE2=test_klp_state2 setup_config@@ -171,4 +173,88 @@ livepatch: '$MOD_ATOMIC': completing unpatching transition livepatch: '$MOD_ATOMIC': unpatching complete % rmmod $MOD_ATOMIC"
tools/testing/selftests/livepatch/test-provides-obsoletes.sh livepatch: '$MOD_ATOMIC': completing unpatching transition · line 185
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+# - 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 "state id conflict across provides" + +load_lp $MOD_STATE provides=1 +load_failing_mod $MOD_STATE2 provides=2
It is not obvious why it should fail because we do not know the used state id. It might be better to define it via a parameter. It would even allow to test more variants:
# same state, same provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=1 provides=1
# same state, misc provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_failing_mod $MOD_STATE_LP2 state=1 provides=2
# same states, replaced by obsoletes load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=1 provides=2 obsoletes=1
# misc states, same provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_failing_mod $MOD_STATE_LP2 state=2 provides=1
# misc states, misc provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=2 provides=2
Best Regards,
Petr
Re: [PATCH v7 for-next 7/8] selftests/livepatch: Add test for state ID conflict across provides
Quoted 86 lines · Petr Mladek
Quoted 57 lines · Yafang Shao
Livepatches with different provides ids must not share the same state id. If a second livepatch attempts to reuse a state id already registered by a livepatch with a different provides id, the loading will fail. However, if the second livepatch's obsoletes list includes the first livepatch's provides id, the second livepatch replaces the first one and may reuse the same state id.
Add provides and obsoletes module parameters to
test_klp_state.candtest_klp_state2.c(guarded by #ifndefKLP_HAS_REPLACE) so that they can be loaded with different provides ids and obsoletes ids.Add a "state id conflict across provides" test scenario to
test-provides.sh:- Load
test_klp_statewith provides=1, which registers state ID 1.
- Attempt to loadtest_klp_state2with provides=2, which reuses the same state ID 1. The second livepatch is rejected because livepatches with different provides ids must not share the same state id.
- Disable and unload the remaining livepatch.Add a "taking over system state via obsoletes" test scenario to
test-state.sh:- Load
test_klp_statewith provides=10, which registers state ID 1.
- Loadtest_klp_state2with provides=20 obsoletes=10, which reuses the same state ID 1. Although the provides ids differ, the second livepatch replaces the first one because its obsoletes list includes the first livepatch's provides id (10). The state is taken over successfully.
- Unload the replaced livepatch, then disable and unload the second livepatch.Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- .../livepatch/test-provides-obsoletes.sh | 86 +++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 9 ++ .../livepatch/test_modules/test_klp_state2.c | 19 ++++ 3 files changed, 114 insertions(+)tools/testing/selftests/livepatch/test-provides-obsoletes.sh +2
-0@@ -6,6 +6,8 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch +MOD_STATE=test_klp_state +MOD_STATE2=test_klp_state2 setup_config@@ -171,4 +173,88 @@ livepatch: '$MOD_ATOMIC': completing unpatching transition livepatch: '$MOD_ATOMIC': unpatching complete % rmmod $MOD_ATOMIC"tools/testing/selftests/livepatch/test-provides-obsoletes.sh livepatch: '$MOD_ATOMIC': completing unpatching transition · line 185
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+# - 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 "state id conflict across provides" + +load_lp $MOD_STATE provides=1 +load_failing_mod $MOD_STATE2 provides=2It is not obvious why it should fail because we do not know the used state id. It might be better to define it via a parameter. It would even allow to test more variants:
# same state, same provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=1 provides=1# same state, misc provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_failing_mod $MOD_STATE_LP2 state=1 provides=2# same states, replaced by obsoletes load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=1 provides=2 obsoletes=1
Quoted 7 lines
# misc states, same provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_failing_mod $MOD_STATE_LP2 state=2 provides=1# misc states, misc provides load_lp $MOD_STATE_LP1 state=1 provides=1 load_lp $MOD_STATE_LP2 state=2 provides=2
Makes sense.
I will update it.
Signature · 2 lines
Regards Yafang
PATCH v7 8/8 selftests/livepatch: Add test for function conflict across provides
In reply to Yafang ShaoLivepatches with different provides ids must not modify the same function. If a second livepatch attempts to modify a function that has already been modified by a loaded livepatch with a different provides id, the loading will fail. If the second livepatch shares the same provides id, or if its obsoletes list contains the first livepatch's provides id, it will load successfully and replace the first one.
Add a new test module test_klp_provides.c that patches meminfo_proc_show (the same function as test_klp_atomic_replace) so that the two modules can be loaded with different provides ids to test the function conflict detection.
Add three test scenarios to test-provides.sh:
1. Function conflict across provides: load test_klp_atomic_replace with provides=1, then attempt to load test_klp_provides with provides=2. The second livepatch is rejected because livepatches with different provides ids and no obsoletes must not modify the same function.
2. Function replace within same provides: load test_klp_atomic_replace with provides=1, then load test_klp_provides with provides=1. The second livepatch loads successfully and replaces the first one because they share the same provides id.
3. Function replace across provides with obsoletes: load test_klp_atomic_replace with provides=1, then load test_klp_provides with provides=2 and obsoletes=[1]. The second livepatch loads successfully and replaces the first one because the obsoletes list allows it to replace a livepatch with a different provides id.
--- .../livepatch/test-provides-obsoletes.sh | 110 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_provides.c | 72 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.c
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +110 -0
@@ -8,6 +8,7 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch MOD_STATE=test_klp_state MOD_STATE2=test_klp_state2 +MOD_PROVIDES=test_klp_provides setup_config
@@ -257,4 +258,113 @@ $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel livepatch: '$MOD_STATE2': unpatching complete % rmmod $MOD_STATE2" + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - try to load another livepatch with provides=2 that modifies the +# same function. The second one must be rejected because livepatches +# with different provides ids and no obsoletes must not modify +# the same function. +# - disable and unload the remaining livepatch + +start_test "function conflict across provides" + +load_lp $MOD_ATOMIC provides=1 +load_failing_mod $MOD_PROVIDES provides=2 + +disable_lp $MOD_ATOMIC +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=2 +livepatch: Livepatch patch ($MOD_PROVIDES) is not compatible with the already installed livepatches. +insmod: ERROR: could not insert module test_modules/$MOD_PROVIDES.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$MOD_ATOMIC/enabled +livepatch: '$MOD_ATOMIC': initializing unpatching transition +livepatch: '$MOD_ATOMIC': starting unpatching transition +livepatch: '$MOD_ATOMIC': completing unpatching transition +livepatch: '$MOD_ATOMIC': unpatching complete +% rmmod $MOD_ATOMIC" + + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - load another livepatch with provides=1 that modifies the same +# function. The second one loads successfully because livepatches +# with the same provides id replace each other. +# - disable and unload the remaining livepatch + +start_test "function replace within same provides" + +load_lp $MOD_ATOMIC provides=1 +load_lp $MOD_PROVIDES provides=1 + +check_sysfs_value "$MOD_PROVIDES" "enabled" "1" + +disable_lp $MOD_PROVIDES +unload_lp $MOD_PROVIDES +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=1 +livepatch: enabling patch '$MOD_PROVIDES' +livepatch: '$MOD_PROVIDES': initializing patching transition +livepatch: '$MOD_PROVIDES': starting patching transition +livepatch: '$MOD_PROVIDES': completing patching transition +livepatch: '$MOD_PROVIDES': patching complete +% echo 0 > $SYSFS_KLP_DIR/$MOD_PROVIDES/enabled +livepatch: '$MOD_PROVIDES': initializing unpatching transition +livepatch: '$MOD_PROVIDES': starting unpatching transition +livepatch: '$MOD_PROVIDES': completing unpatching transition +livepatch: '$MOD_PROVIDES': unpatching complete +% rmmod $MOD_PROVIDES +% rmmod $MOD_ATOMIC" + + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - load another livepatch with provides=2 and obsoletes=[1] that +# modifies the same function. The second one loads successfully +# because the obsoletes list allows it to replace the first one +# even though they have different provides ids. +# - disable and unload the remaining livepatch + +start_test "function replace across provides with obsoletes" + +load_lp $MOD_ATOMIC provides=1 +load_lp $MOD_PROVIDES provides=2 obsoletes=1 + +check_sysfs_value "$MOD_PROVIDES" "enabled" "1" + +disable_lp $MOD_PROVIDES +unload_lp $MOD_PROVIDES +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=2 obsoletes=1 +livepatch: enabling patch '$MOD_PROVIDES' +livepatch: '$MOD_PROVIDES': initializing patching transition +livepatch: '$MOD_PROVIDES': starting patching transition +livepatch: '$MOD_PROVIDES': completing patching transition +livepatch: '$MOD_PROVIDES': patching complete +% echo 0 > $SYSFS_KLP_DIR/$MOD_PROVIDES/enabled +livepatch: '$MOD_PROVIDES': initializing unpatching transition +livepatch: '$MOD_PROVIDES': starting unpatching transition +livepatch: '$MOD_PROVIDES': completing unpatching transition +livepatch: '$MOD_PROVIDES': unpatching complete +% rmmod $MOD_PROVIDES +% rmmod $MOD_ATOMIC" + exit 0
tools/testing/selftests/livepatch/test_modules/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-m += test_klp_atomic_replace.o \ test_klp_livepatch.o \ test_klp_mod_patch.o \ test_klp_mod_target.o \ + test_klp_provides.o \ test_klp_shadow_vars.o \ test_klp_state.o \ test_klp_state2.o \
tools/testing/selftests/livepatch/test_modules/test_klp_provides.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.52.0
Petr Mladek
maintainerRe: [PATCH v7 for-next 8/8] selftests/livepatch: Add test for function conflict across provides
Quoted 45 lines · Yafang Shao
Livepatches with different provides ids must not modify the same function. If a second livepatch attempts to modify a function that has already been modified by a loaded livepatch with a different provides id, the loading will fail. If the second livepatch shares the same provides id, or if its obsoletes list contains the first livepatch's provides id, it will load successfully and replace the first one.
Add a new test module
test_klp_provides.cthat patchesmeminfo_proc_show(the same function astest_klp_atomic_replace) so that the two modules can be loaded with different provides ids to test the function conflict detection.Add three test scenarios to
test-provides.sh:1. Function conflict across provides: load
test_klp_atomic_replacewith provides=1, then attempt to loadtest_klp_provideswith provides=2. The second livepatch is rejected because livepatches with different provides ids and no obsoletes must not modify the same function.2. Function replace within same provides: load
test_klp_atomic_replacewith provides=1, then loadtest_klp_provideswith provides=1. The second livepatch loads successfully and replaces the first one because they share the same provides id.3. Function replace across provides with obsoletes: load
test_klp_atomic_replacewith provides=1, then loadtest_klp_provideswith provides=2 and obsoletes=[1]. The second livepatch loads successfully and replaces the first one because the obsoletes list allows it to replace a livepatch with a different provides id.Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- .../livepatch/test-provides-obsoletes.sh | 110 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_provides.c | 72 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.ctools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0
tools/testing/selftests/livepatch/test-provides-obsoletes.sh MOD_ATOMIC=test_klp_atomic_replace · line 11
Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +1
-0@@ -8,6 +8,7 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch MOD_STATE=test_klp_state MOD_STATE2=test_klp_state2 +MOD_PROVIDES=test_klp_provides
The new module duplicates test_klp_atomic_replace.c. We want to avoid this. Please, create the copy module by using
#include <original_module.c>
Also it would really help to use a better naming. As I suggeted in the review of the 6th patch:
+ test_klp_proc_meminfo_livepatch
+ test_klp_proc_meminfo_livepatch2
or
+ test_klp_meminfo_lp
+ test_klp_meminfo_lp2
Then it will be clear that these are liveapatches and they modify the same function.
Then it would be more obvious when they should succeed and when fails:
# same function, replaced by provides load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_MEMINFO_LP2 provides=1
# same function, can't be loaded in parallel load_lp $MOD_MEMINFO_LP provides=1 load_failing_mod $MOD_MEMINFO_LP2 provides=2
# different functions, atomic replace works (nops) load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_CMDLINE_LP provides=1
# different functions, install in parallel load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_CMDLINE_LP provides=2
Best Regards,
Petr
Re: [PATCH v7 for-next 8/8] selftests/livepatch: Add test for function conflict across provides
Quoted 82 lines · Petr Mladek
Quoted 45 lines · Yafang Shao
Livepatches with different provides ids must not modify the same function. If a second livepatch attempts to modify a function that has already been modified by a loaded livepatch with a different provides id, the loading will fail. If the second livepatch shares the same provides id, or if its obsoletes list contains the first livepatch's provides id, it will load successfully and replace the first one.
Add a new test module
test_klp_provides.cthat patchesmeminfo_proc_show(the same function astest_klp_atomic_replace) so that the two modules can be loaded with different provides ids to test the function conflict detection.Add three test scenarios to
test-provides.sh:1. Function conflict across provides: load
test_klp_atomic_replacewith provides=1, then attempt to loadtest_klp_provideswith provides=2. The second livepatch is rejected because livepatches with different provides ids and no obsoletes must not modify the same function.2. Function replace within same provides: load
test_klp_atomic_replacewith provides=1, then loadtest_klp_provideswith provides=1. The second livepatch loads successfully and replaces the first one because they share the same provides id.3. Function replace across provides with obsoletes: load
test_klp_atomic_replacewith provides=1, then loadtest_klp_provideswith provides=2 and obsoletes=[1]. The second livepatch loads successfully and replaces the first one because the obsoletes list allows it to replace a livepatch with a different provides id.Assisted-by Comagic:DeepSeek-V4-FlashSigned-off-by Yafang Shao <[email protected]>--- .../livepatch/test-provides-obsoletes.sh | 110 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_provides.c | 72 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.ctools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0tools/testing/selftests/livepatch/test-provides-obsoletes.sh MOD_ATOMIC=test_klp_atomic_replace · line 11
Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +1
-0@@ -8,6 +8,7 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch MOD_STATE=test_klp_state MOD_STATE2=test_klp_state2 +MOD_PROVIDES=test_klp_providesThe new module duplicates
test_klp_atomic_replace.c. We want to avoid this. Please, create the copy module by using#include <original_module.c>Also it would really help to use a better naming. As I suggeted in the review of the 6th patch:
+
test_klp_proc_meminfo_livepatch
+test_klp_proc_meminfo_livepatch2or
+
test_klp_meminfo_lp
+test_klp_meminfo_lp2Then it will be clear that these are liveapatches and they modify the same function.
Then it would be more obvious when they should succeed and when fails:
# same function, replaced by provides load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_MEMINFO_LP2 provides=1# same function, can't be loaded in parallel load_lp $MOD_MEMINFO_LP provides=1 load_failing_mod $MOD_MEMINFO_LP2 provides=2
Quoted 7 lines
# different functions, atomic replace works (nops) load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_CMDLINE_LP provides=1# different functions, install in parallel load_lp $MOD_MEMINFO_LP provides=1 load_lp $MOD_CMDLINE_LP provides=2
Thanks for your suggestion.
I will update it.
Signature · 2 lines
Regards Yafang