livepatch: Make livepatch states, callbacks, and shadow variables work together
Subsystems (3)
Petr Mladek
maintainercover livepatch: Make livepatch states, callbacks, and shadow variables work together
This POC is a material for the discussion "Simplify Livepatch Callbacks, Shadow Variables, and States handling" at LPC 2013, see
https://lpc.events/event/17/contributions/1541/
It obsoletes the patchset adding the garbage collection of shadow variables. This new solution is based on ideas from Nicolai Stange. And it should also be in sync with Josh's ideas mentioned into the thread about the garbage collection, see
https://lore.kernel.org/r/20230204235910.4j4ame5ntqogqi7m@treble
What is this all about?
There are three features provided by the kernel livepatching support:
+ callbacks:
They allow doing system modifications where the "simple" redirection to the fixed code is not enough. For example, allocate some data and allow to use them when all processes are patched.
There are four optional callbacks which might be called either when the livepatch or the livepatched object is loaded. It depends who is loaded first.
The are called at different stages of the livepatch transition:
pre_enable, post_enable, pre_disable, post_disable.
Only callbacks from the new livepatch are called during atomic replace. The motivation was that new livepatches should know how to handle the existing changes correctly. Also it simplified the semantic because it would be horrible when both callbacks from the old and new livepatch are called. The later one might break changes done by the earlier one.
They are defined per-object. The idea was that they might be needed when a livepatched module is loaded or unloaded.
+ shadow variables:
They allow attaching extra data to any existing data. For example, they allow to extend a structure. Or they allow to create a spin lock which might stay even when the livepatch gets atomically replaced.
They are defined per-patch but there is no real connection. There is just an allocate/get/free API.
+ states:
They were introduced to manage the life-cycle of changes done by the callbacks and shadow variables.
They should help especially when atomic replace is used. The new livepatch need to know what changes have already been done or which need to be reverted.
The states are defined per-patch. There was proposal to make them per-object but it was decided that it was not worth the complexity.
Each state might have a version which allows to maintain compatibility between the livepatches. Otherwise, there is no connection with the other features. The is just an API to check whether the state was in the previous patch so that the callbacks might do an informed decisions.
Observation:
+ States were supposed to help with the life-time of changes done by callbacks. But states are per-patch and callbacks are per-object. Also the API is hard to use.
+ Shadow variables were not connected with the states at all. It needs to be done by callbacks.
+ The decision that only the callbacks from the new livepatch gets called during atomic replace make downgrades complicated.
Better solution implemented by this POC:
+ Transform per-object callbacks to per-state callbacks so that the state might really control the life-cycle of the changes.
+ Change the semantic of the callbacks, so that they are called when the state is introduced or removed.
No callbacks are called when the state is just transferred during the atomic replace.
+ The disable/remove callbacks from the old livepatch are called from the old livepatch when the new one does not support them.
These callbacks have to be there anyway so that the livepatch can get disabled.
This nicely solves the problem with downgrades while keeping simple semantic.
+ A state might be associated with a shadow variable with the same ID.
It helps to maintain the life-cycle of the shadow variable.
The variable is automatically freed when the state is not longer supported during atomic replace or when the livepatch gets disabled.
Also the state callbacks might help to allocate the variable do do some checks before the transition starts. But it can be enabled only after all processes are transitioned.
It would prevent loading the livepatch when the shadow variable could not be used and the livepatch could cause problems.
+ State version is replaced with "block_disable" flag.
The versions are too generic and make things complicated.
In practice, the main question is whether the changes introduced by the state (callbacks) can be reverted or not. The livepatch could not be disabled or downgraded when the revert (state disable) is not supported.
What is done in this POC:
+ All changes in livepatch code are implemented.
+ The existing selftests are migrated [*]
What is missing:
+ The documentation is not updated.
+ More selftest might be needed [**]
[*] There is some mystery in a selftest when the migration gets
blocked, see the comments in the 5th patch.
[**] In fact, many selftests would deserve some cleanup and
better split into categories.
Petr Mladek (7):
livepatch: Add callbacks for introducing and removing states
livepatch: Allow to handle lifetime of shadow variables using the
livepatch state
livepatch: Use per-state callbacks in state API tests
livepatch: Do not use callbacks when testing sysfs interface
livepatch: Convert klp module callbacks tests into livepatch module
tests
livepatch: Remove the obsolete per-object callbacks
livepatching: Remove per-state version
Documentation/livepatch/callbacks.rst | 133 ----- Documentation/livepatch/index.rst | 1 - include/linux/livepatch.h | 75 ++- kernel/livepatch/core.c | 61 +- kernel/livepatch/core.h | 33 -- kernel/livepatch/state.c | 151 ++++- kernel/livepatch/state.h | 10 + kernel/livepatch/transition.c | 13 +- lib/livepatch/Makefile | 8 +- lib/livepatch/test_klp_callbacks_busy.c | 70 --- lib/livepatch/test_klp_callbacks_demo.c | 121 ---- lib/livepatch/test_klp_callbacks_demo2.c | 93 --- lib/livepatch/test_klp_callbacks_mod.c | 24 - lib/livepatch/test_klp_speaker.c | 185 ++++++ lib/livepatch/test_klp_speaker_livepatch.c | 253 ++++++++ lib/livepatch/test_klp_state.c | 178 ++++-- lib/livepatch/test_klp_state2.c | 190 +----- lib/livepatch/test_klp_state3.c | 2 +- samples/livepatch/Makefile | 3 - .../livepatch/livepatch-callbacks-busymod.c | 60 -- samples/livepatch/livepatch-callbacks-demo.c | 196 ------- samples/livepatch/livepatch-callbacks-mod.c | 41 -- tools/testing/selftests/livepatch/Makefile | 2 +- .../testing/selftests/livepatch/functions.sh | 46 ++ .../selftests/livepatch/test-callbacks.sh | 553 ------------------ .../selftests/livepatch/test-modules.sh | 539 +++++++++++++++++ .../testing/selftests/livepatch/test-state.sh | 80 ++- .../testing/selftests/livepatch/test-sysfs.sh | 48 +- 28 files changed, 1436 insertions(+), 1733 deletions(-) delete mode 100644 Documentation/livepatch/callbacks.rst delete mode 100644 lib/livepatch/test_klp_callbacks_busy.c delete mode 100644 lib/livepatch/test_klp_callbacks_demo.c delete mode 100644 lib/livepatch/test_klp_callbacks_demo2.c delete mode 100644 lib/livepatch/test_klp_callbacks_mod.c create mode 100644 lib/livepatch/test_klp_speaker.c create mode 100644 lib/livepatch/test_klp_speaker_livepatch.c delete mode 100644 samples/livepatch/livepatch-callbacks-busymod.c delete mode 100644 samples/livepatch/livepatch-callbacks-demo.c delete mode 100644 samples/livepatch/livepatch-callbacks-mod.c delete mode 100755 tools/testing/selftests/livepatch/test-callbacks.sh create mode 100755 tools/testing/selftests/livepatch/test-modules.sh
Signature · 1 line
2.35.3
Petr Mladek
maintainerPATCH 1/7 livepatch: Add callbacks for introducing and removing states
The basic livepatch functionality is to redirect problematic functions to a fixed or improved variants. In addition, there are two features helping with more problematic situations:
+ pre_patch(), post_patch(), pre_unpatch(), post_unpatch() callbacks might be called before and after the respective transitions. For example, post_patch() callback might enable some functionality at the end of the transition when the entire system is using the new code.
+ Shadow variables allow to add new items into structures or other data objects.
The practice has shown that these features were hard to use with the atomic replace feature. The new livepatch usually just adds more fixes. But it might also remove problematic ones.
Originally, any version of the livepatch was allowed to replace any older or newer version of the patch. It was not clear how to handle the extra features. The new patch did not know whether to run the callbacks or if the changes were already done by the current livepatch. Or if it has to revert some changes or free shadow variables whey they would not longer be supported.
It was even more complicated because only the callbacks from the newly installed livepatch were called. It means that older livepatch might not be able to revert changes supported only by newer livepatches.
The above problems were supposed to be solved by adding livepatch states. Each livepatch might define which states are supported. The states are versioned. The livepatch core checks if the newly installed livepatch is able to handle all states used by the currently installed livepatch.
Though the practice has shown that the states API was not easy to use either. It was not well connected with the callbacks and shadow variables. The states are per-patch. The callbacks are per-object. The livepatch does not know about the supported shadow variables at all.
As a first step, new per-state callbacks are introduced:
+ "setup" is called before the livepatch is applied but only when the state is new.
It might be used to allocate some memory. Or it might check if the state change is safe on the running system.
If it fails, the patch will not be enabled.
+ "enable" is called after the livepatch is applied but only when the state is new.
It might be used to enable using some functionality provided by the livepatch after the entire system is livepatched.
+ "disable" is called before the livepatch is disabled or replaced.
When replacing, the callback is called only when the new livepatch does not support the related state. And it uses the implementation from the to-be-replaced livepatch. The to-be-replaced livepatch needed the callback to allow disabling the livepatch anyway. The new livepatch does not need to know anything about the state.
It might be used to disable using some functionality which will not longer be supported after the livepatch gets disabled.
+ "release" is called after the livepatch was disabled or replaced. There are the same rules for replacement as for "disable" callback.
It might be used for freeing some memory or unused shadow variables.
These callbacks are going to replace the existing ones. It would cause some changes:
+ The new callbacks are not called when a livepatched object is loaded or removed later.
The practice shows that per-object callbacks are not worth supporting. In a rare case, when a per-object callback is needed. the livepatch might register a custom module notifier.
+ The new callbacks are called only when the state is introduced or removed. It does not handle the situation when the newly installed livepatch continues using an existing state.
The practice shows that this is exactly what is needed. In the rare case when this is not enough, an extra takeover might be done in the module->init() callback.
The per-state callbacks are called in similar code paths as the per-object ones. Especially, the ordering against the other operations is the same. Though, there are some obvious and less obvious changes:
+ The per-state callbacks are called for the entire patch instead of loaded object. It means that they called outside the for-each-object cycle.
+ The per-state callbacks are called when a state is introduced or obsoleted. Both variants might happen when the atomic replace is used.
+ In __klp_enable_patch(), the per-state callbacks are called before the smp_wmb() while the per-object ones are called later.
The new location makes more sense. The setup of the state should be ready before processes start being transferred.
The per-object callbacks were called after the barrier. They were using and already existing for-cycle. And nobody did mind about the ordering.
--- include/linux/livepatch.h | 28 ++++++++ kernel/livepatch/core.c | 19 +++++- kernel/livepatch/state.c | 118 ++++++++++++++++++++++++++++++++++ kernel/livepatch/state.h | 9 +++ kernel/livepatch/transition.c | 8 +++ 5 files changed, 180 insertions(+), 2 deletions(-)
include/linux/livepatch.h +28 -0
@@ -129,15 +129,43 @@ struct klp_object {bool patched; }; +struct klp_patch ; +struct klp_state ; + +/** +* struct klp_state_callbacks - callbacks manipulating the state +* @setup: executed before code patching when the state is added +* @enable: executed after code patching when the state is added +* @disable: executed before code unpatching when the state is removed +* @release: executed after code unpatching when the state is removed +* @setup_succeeded: internal state used by a rollback on error +* +* All callbacks are optional. +* +* @setup callback returns 0 on success and an error code otherwise. +* Any error prevents enabling the livepatch. @disable() callbacks +* are then called to rollback @enable callbacks which has already +* succeeded before. +*/ +struct klp_state_callbacks { +int (*setup)(struct klp_patch *patch,struct klp_state *state); +void (*enable)(struct klp_patch *patch,struct klp_state *state); +void (*disable)(struct klp_patch *patch,struct klp_state *state); +void (*release)(struct klp_patch *patch,struct klp_state *state); +bool setup_succeeded; +}; +/** * struct klp_state - state of the system modified by the livepatch * @id: system state identifier (non-zero) * @version: version of the change +* @callbacks: optional callbacks used when introducing or removing the state * @data: custom data */ struct klp_state {unsigned long id;unsigned int version; +struct klp_state_callbacks callbacks;void *data; };
kernel/livepatch/core.c +17 -2
@@ -975,6 +975,8 @@ static int __klp_disable_patch(struct klp_patch *patch)klp_init_transition (patch,KLP_UNPATCHED ); +klp_disable_states (patch); +klp_for_each_object (patch, obj)if (obj->patched)klp_pre_unpatch_callback (obj);
@@ -1010,6 +1012,13 @@ static int __klp_enable_patch(struct klp_patch *patch)klp_init_transition (patch,KLP_PATCHED ); + ret =klp_setup_states (patch); +if (ret) + goto err; + +if (patch->replace) +klp_disable_obsolete_states (patch); +/* * Enforce the order of the func->transition writes in * klp_init_transition() and the ops->func_stack writes in
@@ -1027,14 +1036,14 @@ static int __klp_enable_patch(struct klp_patch *patch)if (ret) {pr_warn ("pre-patch callback failed for object '%s'\n" ,klp_is_module (obj) ? obj->name :"vmlinux" ); - goto err; + goto err_states; } ret =klp_patch_object (obj);if (ret) {pr_warn ("failed to patch object '%s'\n" ,klp_is_module (obj) ? obj->name :"vmlinux" ); - goto err; + goto err_states; } }
@@ -1043,6 +1052,12 @@ static int __klp_enable_patch(struct klp_patch *patch)klp_try_complete_transition ();return 0 ; + +err_states: +if (patch->replace) +klp_enable_obsolete_states (patch); + +klp_release_states (patch); err:pr_warn ("failed to enable patch '%s'\n" , patch->mod->name);
kernel/livepatch/state.c +118 -0
121 lines not on this page · raw message
kernel/livepatch/state.h +9 -0
@@ -5,5 +5,14 @@#include <linux/livepatch.h> bool klp_is_patch_compatible (struct klp_patch *patch); +int klp_setup_states (struct klp_patch *patch); +void klp_enable_states (struct klp_patch *patch); +void klp_disable_states (struct klp_patch *patch); +void klp_release_states (struct klp_patch *patch); + +void klp_enable_obsolete_states (struct klp_patch *patch); +void klp_disable_obsolete_states (struct klp_patch *patch); +void klp_release_obsolete_states (struct klp_patch *patch); +#endif /* _LIVEPATCH_STATE_H */
kernel/livepatch/transition.c +8 -0
@@ -12,6 +12,7 @@#include <linux/static_call.h> #include "core.h" #include "patch.h" +#include "state.h" #include "transition.h" #define MAX_STACK_ENTRIES 100
@@ -101,6 +102,7 @@ static void klp_complete_transition(void)if (klp_transition_patch->replace && klp_target_state ==KLP_PATCHED ) {klp_unpatch_replaced_patches (klp_transition_patch);klp_discard_nops (klp_transition_patch); +klp_release_obsolete_states (klp_transition_patch); }if (klp_target_state ==KLP_UNPATCHED ) {
@@ -140,6 +142,12 @@ static void klp_complete_transition(void) task->patch_state =KLP_UNDEFINED ; } +if (klp_target_state ==KLP_PATCHED ) { +klp_enable_states (klp_transition_patch); + }else if (klp_target_state ==KLP_UNPATCHED ) { +klp_release_states (klp_transition_patch); + } +klp_for_each_object (klp_transition_patch, obj) {if (!klp_is_object_loaded (obj))continue ;
Signature · 1 line
2.35.3
kernel test robot
build botRe: [POC 1/7] livepatch: Add callbacks for introducing and removing states
kernel test robot noticed the following build warnings:
Hi Petr,
kernel test robot noticed the following build warnings:
[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on shuah-kselftest/fixes linus/master v6.6 next-20231110] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Petr-Mladek/livepatch-Add-callbacks-for-introducing-and-removing-states/20231111-014906 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next patch link: https://lore.kernel.org/r/20231110170428.6664-2-pmladek%40suse.com patch subject: [POC 1/7] livepatch: Add callbacks for introducing and removing states config: x86_64-randconfig-006-20231111 (https://download.01.org/0day-ci/archive/20231111/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231111/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/livepatch/state.c:121:6: warning: no previous prototype for 'is_state_in_other_patches' [-Wmissing-prototypes]
121 | bool is_state_in_other_patches(struct klp_patch *patch, struct klp_state *state)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
vim +/is_state_in_other_patches +121 kernel/livepatch/state.c
120
> 121 bool is_state_in_other_patches(struct klp_patch *patch, struct klp_state *state)
122 {
123 struct klp_patch *old_patch;
124 struct klp_state *old_state;
125
126 klp_for_each_patch(old_patch) {
127 if (old_patch == patch)
128 continue;
129
130 klp_for_each_state(old_patch, old_state) {
131 if (old_state->id == state->id)
132 return true;
133 }
134 }
135
136 return false;
137 }
138
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [POC 1/7] livepatch: Add callbacks for introducing and removing states
In reply to Petr Mladekkernel test robot noticed the following build warnings:
Hi Petr,
kernel test robot noticed the following build warnings:
[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on shuah-kselftest/fixes linus/master v6.6 next-20231110] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Petr-Mladek/livepatch-Add-callbacks-for-introducing-and-removing-states/20231111-014906 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next patch link: https://lore.kernel.org/r/20231110170428.6664-2-pmladek%40suse.com patch subject: [POC 1/7] livepatch: Add callbacks for introducing and removing states config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20231111/[email protected]/config) compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231111/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/livepatch/state.c:121:6: warning: no previous prototype for function 'is_state_in_other_patches' [-Wmissing-prototypes]
bool is_state_in_other_patches(struct klp_patch *patch, struct klp_state *state)
^ kernel/livepatch/state.c:121:1: note: declare 'static' if the function is not intended to be used outside of this translation unit bool is_state_in_other_patches(struct klp_patch *patch, struct klp_state *state)
^
static
1 warning generated.
vim +/is_state_in_other_patches +121 kernel/livepatch/state.c
120
> 121 bool is_state_in_other_patches(struct klp_patch *patch, struct klp_state *state)
122 {
123 struct klp_patch *old_patch;
124 struct klp_state *old_state;
125
126 klp_for_each_patch(old_patch) {
127 if (old_patch == patch)
128 continue;
129
130 klp_for_each_state(old_patch, old_state) {
131 if (old_state->id == state->id)
132 return true;
133 }
134 }
135
136 return false;
137 }
138
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Petr Mladek
maintainerPATCH 2/7 livepatch: Allow to handle lifetime of shadow variables using the livepatch state
In reply to Petr MladekThe handling of the lifetime of the shadow variables is not easy when the atomic replace is used. The new patch does not know if a shadow variable has already been used by the previous livepatch. Or if there is a shadow variable which will not longer be used.
Shadow variables are almost always used together with callbacks. At least @post_unpatch callback is used to free not longer used shadow variables. And sometimes @post_patch and @pre_unpatch callbacks are used to enable or disable the use of the shadow variables. It is needed when the shadow variable can be used only when the entire system is able to handle them.
All this gets even more complicated because the original callbacks are called only from the new livepatch when atomic replace is used. Newly created livepatches might be able to handle obsolete shadow variables so the upgrade would work. But older livepatches do not know anything about later introduced shadow variables. They might leak during downgrade. And they might contain outdated information when another upgrade would start using them again.
All problems are better solved with the new callbacks associated with a livepatch state. They are called when the state is first introduced and when it gets obsolete. Also the callbacks are called from the patch where the state was supported. So that even downgrade might be safe.
Let's make it official. A shadow variable might be associated with a livepatch state by setting the new "state.is_shadow" flag and using the same "id" in both struct klp_shadow and struct klp_state.
The shadow variable will then have the same lifetime as the livepatch state. It allows to free obsolete shadow variables automatically without the need to add a callback.
The generic callback will free the shadow variables using state->callbacks.shadow_dtor callback when provided.
--- include/linux/livepatch.h | 15 ++++++++++----- kernel/livepatch/state.c | 14 ++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-)
include/linux/livepatch.h +10 -5
@@ -132,12 +132,18 @@ struct klp_object {struct klp_patch ;struct klp_state ; +typedef int (*klp_shadow_ctor_t )(void *obj, +void *shadow_data, +void *ctor_data); +typedef void (*klp_shadow_dtor_t )(void *obj,void *shadow_data); +/** * struct klp_state_callbacks - callbacks manipulating the state * @setup: executed before code patching when the state is added * @enable: executed after code patching when the state is added * @disable: executed before code unpatching when the state is removed * @release: executed after code unpatching when the state is removed +* @shadow_dtor: destructor for the related shadow variable * @setup_succeeded: internal state used by a rollback on error * * All callbacks are optional.
@@ -152,6 +158,7 @@ struct klp_state_callbacks {void (*enable)(struct klp_patch *patch,struct klp_state *state);void (*disable)(struct klp_patch *patch,struct klp_state *state);void (*release)(struct klp_patch *patch,struct klp_state *state); +klp_shadow_dtor_t shadow_dtor;bool setup_succeeded; };
@@ -160,12 +167,15 @@ struct klp_state_callbacks {* @id: system state identifier (non-zero) * @version: version of the change * @callbacks: optional callbacks used when introducing or removing the state +* @is_shadow: the state handles lifetime of a shadow variable +* with the same @id * @data: custom data */ struct klp_state {unsigned long id;unsigned int version;struct klp_state_callbacks callbacks; +bool is_shadow;void *data; };
@@ -240,11 +250,6 @@ static inline bool klp_have_reliable_stack(void)IS_ENABLED (CONFIG_HAVE_RELIABLE_STACKTRACE ); } -typedef int (*klp_shadow_ctor_t )(void *obj, -void *shadow_data, -void *ctor_data); -typedef void (*klp_shadow_dtor_t )(void *obj,void *shadow_data); -void *klp_shadow_get (void *obj,unsigned long id); void *klp_shadow_alloc(void *obj, unsigned long id, size_t size, gfp_t gfp_flags,
kernel/livepatch/state.c +10 -4
@@ -198,11 +198,17 @@ void klp_release_states(struct klp_patch *patch)if (is_state_in_other_patches (patch, state))continue ; -if (!state->callbacks.release) -continue ; - -if (state->callbacks.setup_succeeded) +if (state->callbacks.release && state->callbacks.setup_succeeded) state->callbacks.release (patch, state); + +if (state->is_shadow) +klp_shadow_free_all (state->id, state->callbacks.shadow_dtor); + +/* +* The @release callback is supposed to restore the original +* state before the @setup callback was called. +*/ + state->callbacks.setup_succeeded =0 ; } }
Signature · 1 line
2.35.3
Miroslav Benes
maintainerRe: [POC 2/7] livepatch: Allow to handle lifetime of shadow variables using the livepatch state
Quoted 7 lines
kernel/livepatch/state.c +0
-0@@ -198,11 +198,17 @@ void klp_release_states(struct klp_patch *patch)if (is_state_in_other_patches (patch, state))continue ;
kernel/livepatch/state.c void klp_release_states(struct klp_patch *patch) · line 205
Quoted 10 lines
kernel/livepatch/state.c +4
-4-if (!state->callbacks.release) -continue ; - -if (state->callbacks.setup_succeeded) +if (state->callbacks.release && state->callbacks.setup_succeeded) state->callbacks.release (patch, state); + +if (state->is_shadow) +klp_shadow_free_all (state->id, state->callbacks.shadow_dtor);
The following
kernel/livepatch/state.c void klp_release_states(struct klp_patch *patch) · line 211
Quoted 5 lines
kernel/livepatch/state.c +5
-0+/* +* The @release callback is supposed to restore the original +* state before the @setup callback was called. +*/ + state->callbacks.setup_succeeded =0 ;
should go to the previous patch perhaps?
Miroslav
Petr Mladek
maintainerRe: [POC 2/7] livepatch: Allow to handle lifetime of shadow variables using the livepatch state
Quoted 20 lines · Miroslav Benes
Quoted 7 lines
kernel/livepatch/state.c +0
-0@@ -198,11 +198,17 @@ void klp_release_states(struct klp_patch *patch)if (is_state_in_other_patches (patch, state))continue ;kernel/livepatch/state.c void klp_release_states(struct klp_patch *patch) · line 205
Quoted 10 lines
kernel/livepatch/state.c +4
-4-if (!state->callbacks.release) -continue ; - -if (state->callbacks.setup_succeeded) +if (state->callbacks.release && state->callbacks.setup_succeeded) state->callbacks.release (patch, state); + +if (state->is_shadow) +klp_shadow_free_all (state->id, state->callbacks.shadow_dtor);The following
Quoted 7 lines
kernel/livepatch/state.c void klp_release_states(struct klp_patch *patch) · line 211
Quoted 5 lines
kernel/livepatch/state.c +5
-0+/* +* The @release callback is supposed to restore the original +* state before the @setup callback was called. +*/ + state->callbacks.setup_succeeded =0 ;should go to the previous patch perhaps?
Great catch!
I am going to refactor the code in the next version so that it would look like:
void klp_states_post_unpatch(struct klp_patch *patch)
{
struct klp_state *state;
klp_for_each_state(patch, state) {
if (is_state_in_other_patches(patch, state))
continue;
if (!state->callbacks.pre_patch_succeeded)
continue;
if (state->callbacks.post_unpatch)
state->callbacks.post_unpatch(patch, state);+ if (state->is_shadow)
+ klp_shadow_free_all(state->id, state->callbacks.shadow_dtor);
+
state->callbacks.pre_patch_succeeded = 0; } }
Best Regards,
Petr
Petr Mladek
maintainerPATCH 3/7 livepatch: Use per-state callbacks in state API tests
In reply to Petr MladekRecent changes in the livepatch core have allowed to connect states, shadow variables, and callbacks. Use these new features in the state tests.
Use the shadow variable API to store the original loglevel. It is better suited for this purpose than directly accessing the .data pointer in state klp_state.
Another big advantage is that the shadow variable is preserved when the current patch is replaced by a new version. As a result, there is not need to copy the pointer.
Finally, the lifetime of the shadow variable is connected with the lifetime of the state. It is freed automatically when it is not longer supported.
This results into the following changes in the code:
+ Rename CONSOLE_LOGLEVEL_STATE -> CONSOLE_LOGLEVEL_FIX_ID because it will be used also the for shadow variable
+ Remove the extra code for module coming and going states because the new callback are per-state.
+ Remove callbacks needed to transfer the pointer between states.
+ Keep the versioning of the state to prevent downgrade. The problem is artificial because no callbacks are needed to transfer or free the shadow variable anymore.
--- lib/livepatch/test_klp_state.c | 113 +++++------ lib/livepatch/test_klp_state2.c | 190 +----------------- .../testing/selftests/livepatch/test-state.sh | 44 ++-- 3 files changed, 76 insertions(+), 271 deletions(-)
lib/livepatch/test_klp_state.c +57 -56
@@ -9,108 +9,109 @@#include <linux/printk.h> #include <linux/livepatch.h> -#define CONSOLE_LOGLEVEL_STATE 1 -/* Version 1 does not support migration. */ -#define CONSOLE_LOGLEVEL_STATE_VERSION 1 +#define CONSOLE_LOGLEVEL_FIX_ID 1 -static const char *const module_state[] = { - [MODULE_STATE_LIVE ] ="[MODULE_STATE_LIVE] Normal state" , - [MODULE_STATE_COMING ] ="[MODULE_STATE_COMING] Full formed, running module_init" , - [MODULE_STATE_GOING ] ="[MODULE_STATE_GOING] Going away" , - [MODULE_STATE_UNFORMED ] ="[MODULE_STATE_UNFORMED] Still setting it up" , -}; - -static void callback_info (const char *callback,struct klp_object *obj) -{ -if (obj->mod) -pr_info ("%s: %s -> %s\n" , callback, obj->mod->name, - module_state[obj->mod->state]); -else -pr_info ("%s: vmlinux\n" , callback); -} +/* +* Version of the state which defines compatibility of livepaches. +* The value is artificial. It set just for testing the compatibility +* checks. In reality, all versions are compatible because all +* the callbacks do nothing and the shadow variable clean up +* is done by the core. +*/ +#ifndef CONSOLE_LOGLEVEL_FIX_VERSION +#define CONSOLE_LOGLEVEL_FIX_VERSION 1 +#endif static struct klp_patch patch;static int allocate_loglevel_state (void ) { -struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) -return -EINVAL ; +/* Make sure that the shadow variable does not exist yet. */ + shadow_console_loglevel = +klp_shadow_alloc (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID , +sizeof (*shadow_console_loglevel),GFP_KERNEL , +NULL ,NULL ); - loglevel_state->data =kzalloc (sizeof (console_loglevel),GFP_KERNEL ); -if (!loglevel_state->data) +if (!shadow_console_loglevel) { +pr_err ("%s: failed to allocated shadow variable for storing original loglevel\n" , + __func__);return -ENOMEM ; + }pr_info ("%s: allocating space to store console_loglevel\n" , __func__); +return 0 ; }static void fix_console_loglevel (void ) { -struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) + shadow_console_loglevel = + (int *)klp_shadow_get (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ); +if (!shadow_console_loglevel)return ;pr_info ("%s: fixing console_loglevel\n" , __func__); - *(int *)loglevel_state->data = console_loglevel; + *shadow_console_loglevel = console_loglevel; console_loglevel =CONSOLE_LOGLEVEL_MOTORMOUTH ; }static void restore_console_loglevel (void ) { -struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) + shadow_console_loglevel = + (int *)klp_shadow_get (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ); +if (!shadow_console_loglevel)return ;pr_info ("%s: restoring console_loglevel\n" , __func__); - console_loglevel = *(int *)loglevel_state->data; + console_loglevel = *shadow_console_loglevel; }static void free_loglevel_state (void ) { -struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) + shadow_console_loglevel = + (int *)klp_shadow_get (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ); +if (!shadow_console_loglevel)return ;pr_info ("%s: freeing space for the stored console_loglevel\n" , __func__); -kfree (loglevel_state->data); +klp_shadow_free (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ,NULL ); } -/* Executed on object patching (ie, patch enablement) */ -static int pre_patch_callback (struct klp_object *obj) +/* Executed before patching when the state is new. */ +static int setup_state_callback (struct klp_patch *patch,struct klp_state *state) { -callback_info (__func__, obj); +pr_info ("%s: state %lu\n" , __func__, state->id);return allocate_loglevel_state (); } -/* Executed on object unpatching (ie, patch disablement) */ -static void post_patch_callback (struct klp_object *obj) +/* Executed after patching when the state is new. */ +static void enable_state_callback (struct klp_patch *patch,struct klp_state *state) { -callback_info (__func__, obj); +pr_info ("%s: state %lu\n" , __func__, state->id);fix_console_loglevel (); } -/* Executed on object unpatching (ie, patch disablement) */ -static void pre_unpatch_callback (struct klp_object *obj) +/* Executed before unpatching when the state is obsoleted. */ +static void disable_state_callback (struct klp_patch *patch,struct klp_state *state) { -callback_info (__func__, obj); +pr_info ("%s: state %lu\n" , __func__, state->id);restore_console_loglevel (); } -/* Executed on object unpatching (ie, patch disablement) */ -static void post_unpatch_callback (struct klp_object *obj) +/* Executed after unpatching when the state is obsoleted. */ +static void release_state_callback (struct klp_patch *patch,struct klp_state *state) { -callback_info (__func__, obj); +pr_info ("%s: state %lu\n" , __func__, state->id);free_loglevel_state (); }
@@ -122,19 +123,19 @@ static struct klp_object objs[] = { { .name = NULL, /* vmlinux */ .funcs = no_funcs, - .callbacks = { - .pre_patch = pre_patch_callback, - .post_patch = post_patch_callback, - .pre_unpatch = pre_unpatch_callback, - .post_unpatch = post_unpatch_callback, - }, }, { } };static struct klp_state states[] = { { - .id =CONSOLE_LOGLEVEL_STATE , - .version =CONSOLE_LOGLEVEL_STATE_VERSION , + .id =CONSOLE_LOGLEVEL_FIX_ID , + .version =CONSOLE_LOGLEVEL_FIX_VERSION , + .callbacks = { + .setup = setup_state_callback, + .enable = enable_state_callback, + .disable = disable_state_callback, + .release = release_state_callback, + }, }, { } };
lib/livepatch/test_klp_state2.c +3 -187
194 lines not on this page · raw message
tools/testing/selftests/livepatch/test-state.sh +16 -28
130 lines not on this page · raw message
Signature · 1 line
2.35.3
Miroslav Benes
maintainerRe: [POC 3/7] livepatch: Use per-state callbacks in state API tests
Hi,
Quoted 24 lines · Petr Mladek
Recent changes in the livepatch core have allowed to connect states, shadow variables, and callbacks. Use these new features in the state tests.
Use the shadow variable API to store the original loglevel. It is better suited for this purpose than directly accessing the .data pointer in state
klp_state.Another big advantage is that the shadow variable is preserved when the current patch is replaced by a new version. As a result, there is not need to copy the pointer.
Finally, the lifetime of the shadow variable is connected with the lifetime of the state. It is freed automatically when it is not longer supported.
This results into the following changes in the code:
+ Rename
CONSOLE_LOGLEVEL_STATE->CONSOLE_LOGLEVEL_FIX_IDbecause it will be used also the for shadow variable+ Remove the extra code for module coming and going states because the new callback are per-state.
Quoted 8 lines
+ Remove callbacks needed to transfer the pointer between states.
+ Keep the versioning of the state to prevent downgrade. The problem is artificial because no callbacks are needed to transfer or free the shadow variable anymore.
Signed-off-by Petr Mladek <[email protected]>
it is much cleaner now.
[...]
Quoted 17 lines
static int
allocate_loglevel_state(void)
{lib/livepatch/test_klp_state.c +7
-6-struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) -return -EINVAL ; +/* Make sure that the shadow variable does not exist yet. */ + shadow_console_loglevel = +klp_shadow_alloc (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID , +sizeof (*shadow_console_loglevel),GFP_KERNEL , +NULL ,NULL ); - loglevel_state->data =kzalloc (sizeof (console_loglevel),GFP_KERNEL ); -if (!loglevel_state->data) +if (!shadow_console_loglevel) {
lib/livepatch/test_klp_state.c line 47
Quoted 10 lines
lib/livepatch/test_klp_state.c +4
-0+pr_err ("%s: failed to allocated shadow variable for storing original loglevel\n" , + __func__);return -ENOMEM ; + }pr_info ("%s: allocating space to store console_loglevel\n" , __func__); +return 0 ; }
Would it make sense to set is_shadow to 1 here? I mean you would pass klp_state down to allocate_loglevel_state() from setup callback and set its is_shadow member here. Because then...
Quoted 7 lines
static void
free_loglevel_state(void)
{lib/livepatch/test_klp_state.c +1
-3-struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state)
lib/livepatch/test_klp_state.c line 88
Quoted 10 lines
lib/livepatch/test_klp_state.c +4
-1+ shadow_console_loglevel = + (int *)klp_shadow_get (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ); +if (!shadow_console_loglevel)return ;pr_info ("%s: freeing space for the stored console_loglevel\n" , __func__); -kfree (loglevel_state->data); +klp_shadow_free (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ,NULL ); }
would not be needed. And release callback neither.
Or am I wrong?
We can even have both ways implemented to demonstrate different approaches...
Miroslav
Petr Mladek
maintainerRe: [POC 3/7] livepatch: Use per-state callbacks in state API tests
Quoted 63 lines · Miroslav Benes
Hi,
Quoted 24 lines · Petr Mladek
Recent changes in the livepatch core have allowed to connect states, shadow variables, and callbacks. Use these new features in the state tests.
Use the shadow variable API to store the original loglevel. It is better suited for this purpose than directly accessing the .data pointer in state
klp_state.Another big advantage is that the shadow variable is preserved when the current patch is replaced by a new version. As a result, there is not need to copy the pointer.
Finally, the lifetime of the shadow variable is connected with the lifetime of the state. It is freed automatically when it is not longer supported.
This results into the following changes in the code:
+ Rename
CONSOLE_LOGLEVEL_STATE->CONSOLE_LOGLEVEL_FIX_IDbecause it will be used also the for shadow variable+ Remove the extra code for module coming and going states because the new callback are per-state.
Quoted 8 lines
+ Remove callbacks needed to transfer the pointer between states.
+ Keep the versioning of the state to prevent downgrade. The problem is artificial because no callbacks are needed to transfer or free the shadow variable anymore.
Signed-off-by Petr Mladek <[email protected]>it is much cleaner now.
[...]
Quoted 22 lines
static int
allocate_loglevel_state(void)
{lib/livepatch/test_klp_state.c +10
-6-struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state) -return -EINVAL ; +/* Make sure that the shadow variable does not exist yet. */ + shadow_console_loglevel = +klp_shadow_alloc (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID , +sizeof (*shadow_console_loglevel),GFP_KERNEL , +NULL ,NULL ); - loglevel_state->data =kzalloc (sizeof (console_loglevel),GFP_KERNEL ); -if (!loglevel_state->data) +if (!shadow_console_loglevel) { +pr_err ("%s: failed to allocated shadow variable for storing original loglevel\n" , + __func__);return -ENOMEM ; + }
Quoted 9 lines
lib/livepatch/test_klp_state.c line 47
Quoted 5 lines
lib/livepatch/test_klp_state.c +1
-0pr_info ("%s: allocating space to store console_loglevel\n" , __func__); +return 0 ; }Would it make sense to set
is_shadowto 1 here? I mean you would passklp_statedown toallocate_loglevel_state()from setup callback and set itsis_shadowmember here. Because then...
Right.
Quoted 20 lines
Quoted 7 lines
static void
free_loglevel_state(void)
{lib/livepatch/test_klp_state.c +1
-3-struct klp_state *loglevel_state; +int *shadow_console_loglevel; - loglevel_state =klp_get_state (&patch,CONSOLE_LOGLEVEL_STATE ); -if (!loglevel_state)lib/livepatch/test_klp_state.c line 88
Quoted 10 lines
lib/livepatch/test_klp_state.c +4
-1+ shadow_console_loglevel = + (int *)klp_shadow_get (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ); +if (!shadow_console_loglevel)return ;pr_info ("%s: freeing space for the stored console_loglevel\n" , __func__); -kfree (loglevel_state->data); +klp_shadow_free (&console_loglevel,CONSOLE_LOGLEVEL_FIX_ID ,NULL ); }would not be needed. And release callback neither.
Quoted 1 line
Or am I wrong?
No, you are perfectly right.
Quoted 2 lines
We can even have both ways implemented to demonstrate different approaches...
I have implemented only your approach ;-)
That said, I am going to keep the callback so that the selftest could check that it is called at the right time. But the callback will only print the message. And a comment would explain that is not really needed.
Also I am going to add a .state_dtor callback so that we could test the shadow variable is freed. The callback will only print a message. It is a simple shadow variable and the memory is freed automatically together with the struct klp_shadow.
Best Regards,
Petr
Petr Mladek
maintainerPATCH 4/7 livepatch: Do not use callbacks when testing sysfs interface
In reply to Petr MladekThe per-object callbacks have been obsoleted by per-state callbacks. As a result, the callbacks test modules have been obsoleted by updated klp state tests.
The callbacks test modules are re-used in the sysfs selftests. It would be possible to replace them by klp state test modules but the newly generated logs are hard to review because there is a lot of noise caused by the callbacks.
Instead, introduce a simple test module in a "Hello World" style and corresponding livepatch. The expected log can be reviewed easily.
The test module might be later extended to provide more functionality which might be used in more tests. It would allow to create tests focusing on some particular feature with an easier output.
--- lib/livepatch/Makefile | 2 + lib/livepatch/test_klp_speaker.c | 34 +++++++++++++ lib/livepatch/test_klp_speaker_livepatch.c | 50 +++++++++++++++++++ .../testing/selftests/livepatch/test-sysfs.sh | 48 ++++++++---------- 4 files changed, 106 insertions(+), 28 deletions(-) create mode 100644 lib/livepatch/test_klp_speaker.c create mode 100644 lib/livepatch/test_klp_speaker_livepatch.c
lib/livepatch/Makefile +2 -0
@@ -9,6 +9,8 @@ obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \ test_klp_callbacks_mod.o \ test_klp_livepatch.o \ test_klp_shadow_vars.o \ + test_klp_speaker.o \ + test_klp_speaker_livepatch.o \ test_klp_state.o \ test_klp_state2.o \ test_klp_state3.o
lib/livepatch/test_klp_speaker.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2023 SUSE + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#ifndef _VER_NAME +#define _VER_NAME (name) name +#endif + +#include <linux/module.h> +#include <linux/printk.h> + +noinline +void speaker_welcome(void) +{ +pr_info ("%s: Hello, World!\n" , __func__); +} + +static int test_klp_speaker_init (void ) +{ +pr_info ("%s\n" , __func__); + +return 0 ; +} + +static void test_klp_speaker_exit (void ) +{ +pr_info ("%s\n" , __func__); +} + +module_init (test_klp_speaker_init); +module_exit (test_klp_speaker_exit); +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Livepatch test: test functions" );
lib/livepatch/test_klp_speaker_livepatch.c new file +50 -0
50 lines not on this page · raw message
tools/testing/selftests/livepatch/test-sysfs.sh +20 -28
59 lines not on this page · raw message
Signature · 1 line
2.35.3
Petr Mladek
maintainerPATCH 5/7 livepatch: Convert klp module callbacks tests into livepatch module tests
In reply to Petr MladekThe original livepatch callbacks has been obsoleted by the livepatch state callbacks.
The important difference is that the original callbacks were associated with the livepatched object. The state callbacks are associated with the livepatch states which are associated with the livepatch.
As a result, some per-object callbacks functionality could not be preserved with the new per-state callbacks. Namely, it is not possible to change a state when a livepatched module is loaded or unloaded. This usecase it considered rare and not worth the complexity.
Instead, the callback self-tests are transformed into testing the new state callbacks. Some tests do not use the callbacks any longer. But they still test some basic scenarios which is not tested by other tests.
Many features are added to the test_klp_speaker module:
- The parameter "welcome" allows to call the livepatched function speaker_welcome.
- The parameter "waiting_welcome" allows to a trigger a workqueue work which might busy wait in a livepatched function and block the transition.
It requires storing pointer to speaker_wait_and_welcome() so that the original one could be called from the livepatch.
Alternative solution would be to livepatch speaker_wait_and_welcome() but it would require passing pointer to the completion structure from the livepatched module.
Another tricky part is the initialization of the work structure. It must be initialized in the module init() callback so that it can be flushed in the exit() callback even when it was not used. But it must be initialized also in waiting_welcome_set() because the module parameter callbacks are proceed before calling the module init() callback.
- The same sources are used to build two target modules. The livepatched functions have versioned names. It makes it easier to distinguish messages printed by this functions and the livepatched variants.
It is solved by using macro for definition of the functions.
Many features are added also into the livepatch module:
- The parameter "add_applause" allows to add "[APPLAUSE " string into the message printed by speaker_welcome(). The string is stored in a shadow variable which is:
- associated with a state.
- allocated and initialized to "[] " in setup() state callback.
- set to "[APPLAUSE ]" in enable() state callback.
- set back to "[] " in disable() state callback.
- freed in release() state callback.
Allows testing state callbacks. It even allows to distinguish the state after setup() and enable() callbacks.
- The parameter "setup_ret" allow to force a negative return value to the callback allocation the shadow variable.
- The parameter "noreplace" allows to disable patch.replace parameter and install the livepatch in parallel with others.
- The same sources are used for building two livepatches. It allows writing tests with more livepatches.
The livepatched functions are versioned. Both livepatches can livepatch both speaker test modules.
But they use the same shadow variable for storing the "add_applause" feature. It allows testing passing the states and shadow variables with atomic replace.
But obviously, "add_applause" parameter could no be used for both livepatches when they are loaded in parallel using the "noreplace" parameter.
Finally, remove the obsolete and unused "callback" test modules and livepatches.
TODO:
- Debug why "waiting_welcome" does not block the transition. The livepatched function is not on the stack from unknown reasons. See the comments in test-module.h for more details.
- Better organize the tests. test-module.sh combines tests of various aspects which might better be suited somewhere else.
As a first step, test-callbacks.sh has been renamed to test-modules.sh. But there still might be a better name.
- Split this huge patch. Add the various features and tests in more steps.
--- lib/livepatch/Makefile | 6 +- lib/livepatch/test_klp_callbacks_busy.c | 70 --- lib/livepatch/test_klp_callbacks_demo.c | 121 ---- lib/livepatch/test_klp_callbacks_demo2.c | 93 --- lib/livepatch/test_klp_callbacks_mod.c | 24 - lib/livepatch/test_klp_speaker.c | 153 ++++- lib/livepatch/test_klp_speaker_livepatch.c | 209 ++++++- tools/testing/selftests/livepatch/Makefile | 2 +- .../testing/selftests/livepatch/functions.sh | 29 + .../selftests/livepatch/test-callbacks.sh | 553 ------------------ .../selftests/livepatch/test-modules.sh | 539 +++++++++++++++++ 11 files changed, 929 insertions(+), 870 deletions(-) delete mode 100644 lib/livepatch/test_klp_callbacks_busy.c delete mode 100644 lib/livepatch/test_klp_callbacks_demo.c delete mode 100644 lib/livepatch/test_klp_callbacks_demo2.c delete mode 100644 lib/livepatch/test_klp_callbacks_mod.c delete mode 100755 tools/testing/selftests/livepatch/test-callbacks.sh create mode 100755 tools/testing/selftests/livepatch/test-modules.sh
lib/livepatch/Makefile +2 -4
@@ -3,14 +3,12 @@ # Makefile for livepatch test code. obj-$(CONFIG_TEST_LIVEPATCH) += test_klp_atomic_replace.o \ - test_klp_callbacks_demo.o \ - test_klp_callbacks_demo2.o \ - test_klp_callbacks_busy.o \ - test_klp_callbacks_mod.o \ test_klp_livepatch.o \ test_klp_shadow_vars.o \ test_klp_speaker.o \ + test_klp_speaker2.o \ test_klp_speaker_livepatch.o \ + test_klp_speaker_livepatch2.o \ test_klp_state.o \ test_klp_state2.o \ test_klp_state3.o
lib/livepatch/test_klp_callbacks_busy.c deleted +0 -70
70 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_demo.c deleted +0 -121
121 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_demo2.c deleted +0 -93
93 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_mod.c deleted +0 -24
@@ -1,24 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (C) 2018 Joe Lawrence <[email protected]> - -#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt - -#include <linux/module.h> -#include <linux/kernel.h> - -static int test_klp_callbacks_mod_init (void ) -{ -pr_info ("%s\n" , __func__); -return 0 ; -} - -static void test_klp_callbacks_mod_exit (void ) -{ -pr_info ("%s\n" , __func__); -} - -module_init (test_klp_callbacks_mod_init); -module_exit (test_klp_callbacks_mod_exit); -MODULE_LICENSE ("GPL" ); -MODULE_AUTHOR ("Joe Lawrence <[email protected]>" ); -MODULE_DESCRIPTION ("Livepatch test: target module" );
lib/livepatch/test_klp_speaker.c +152 -1
175 lines not on this page · raw message
lib/livepatch/test_klp_speaker_livepatch.c +206 -3
240 lines not on this page · raw message
tools/testing/selftests/livepatch/Makefile +1 -1
@@ -3,7 +3,7 @@ TEST_PROGS_EXTENDED := functions.sh TEST_PROGS := \ test-livepatch.sh \ - test-callbacks.sh \ + test-modules.sh \ test-shadow-vars.sh \ test-state.sh \ test-ftrace.sh \
tools/testing/selftests/livepatch/functions.sh +29 -0
@@ -277,6 +277,20 @@ function set_pre_patch_ret { die "failed to set pre_patch_ret parameter for $mod module" } +# read_module_param(modname, param) +# modname - module name which provides the given parameter +# param - parameter name to be read +function read_module_param { + local mod="$1"; shift + local param="$1" + + log "% cat /sys/module/$mod/parameters/$param" + ret=$(cat /sys/module/$mod/parameters/$param 2>&1) + if [[ "$ret" != "" ]]; then + die "$ret" + fi +} + function start_test { local test="$1"
@@ -336,9 +350,24 @@ function check_sysfs_value() { local rel_path="$1"; shift local expected_value="$1"; shift +# echo "mod=$mod" +# echo "rel_path=$rel_path" +# echo "expected_value=$expected_value" local path="$KLP_SYSFS_DIR/$mod/$rel_path" local value=`cat $path` if test "$value" != "$expected_value" ; then die "Unexpected value in $path: $expected_value vs. $value" fi } + +# check_object_patched(livepatch_module, objname, expected_value) +# livepatch_module - livepatch module creating the sysfs interface +# objname - livepatched object to be checked +# expected_value - expected value read from the file +function check_object_patched() { + local livepatch_module="$1"; shift + local objname="$1"; shift + local expected_value="$1"; shift + + check_sysfs_value "$livepatch_module" "$objname/patched" "$expected_value" +}
tools/testing/selftests/livepatch/test-callbacks.sh deleted +0 -553
553 lines not on this page · raw message
tools/testing/selftests/livepatch/test-modules.sh new file +539 -0
539 lines not on this page · raw message
Signature · 1 line
2.35.3
kernel test robot
build botRe: [POC 5/7] livepatch: Convert klp module callbacks tests into livepatch module tests
kernel test robot noticed the following build errors:
Hi Petr,
kernel test robot noticed the following build errors:
[auto build test ERROR on shuah-kselftest/next]
[also build test ERROR on shuah-kselftest/fixes linus/master v6.6 next-20231110] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Petr-Mladek/livepatch-Add-callbacks-for-introducing-and-removing-states/20231111-014906 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next patch link: https://lore.kernel.org/r/20231110170428.6664-6-pmladek%40suse.com patch subject: [POC 5/7] livepatch: Convert klp module callbacks tests into livepatch module tests config: s390-defconfig (https://download.01.org/0day-ci/archive/20231111/[email protected]/config) compiler: s390-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231111/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> make[5]: *** No rule to make target 'lib/livepatch/test_klp_speaker2.o', needed by 'lib/livepatch/'. >> make[5]: *** No rule to make target 'lib/livepatch/test_klp_speaker_livepatch2.o', needed by 'lib/livepatch/'.
make[5]: *** [scripts/Makefile.build:243: lib/livepatch/test_klp_speaker.o] Error 1 make[5]: *** [scripts/Makefile.build:243: lib/livepatch/test_klp_speaker_livepatch.o] Error 1 make[5]: Target 'lib/livepatch/' not remade because of errors.
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Miroslav Benes
maintainerRe: [POC 5/7] livepatch: Convert klp module callbacks tests into livepatch module tests
In reply to Petr Mladeklib/livepatch/test_klp_speaker.c line 15
Quoted 12 lines
lib/livepatch/test_klp_speaker.c +4
-0@@ -9,23 +9,174 @@#include <linux/module.h> #include <linux/printk.h> +#include <linux/delay.h> +#include <linux/sysfs.h> + +#include "test_klp_speaker.h"
the header file does not exist.
As you mentioned, it would definitely help if you managed to split the patch somehow.
Miroslav
Petr Mladek
maintainerPATCH 6/7 livepatch: Remove the obsolete per-object callbacks
In reply to Petr MladekAll selftests have been migrated to the new per-state callbacks. And the obsoleted per-object callbacks can be safely removed.
FIXME:
This patch is removing the sample module and documentation without any replacement. They obviously have to be converted for the state-callbacks.
It has been postponed until the approach has been approved in the POC stage.
--- Documentation/livepatch/callbacks.rst | 133 ------------ Documentation/livepatch/index.rst | 1 - include/linux/livepatch.h | 25 --- kernel/livepatch/core.c | 29 --- kernel/livepatch/core.h | 33 --- kernel/livepatch/transition.c | 9 - samples/livepatch/Makefile | 3 - .../livepatch/livepatch-callbacks-busymod.c | 60 ------ samples/livepatch/livepatch-callbacks-demo.c | 196 ------------------ samples/livepatch/livepatch-callbacks-mod.c | 41 ---- 10 files changed, 530 deletions(-) delete mode 100644 Documentation/livepatch/callbacks.rst delete mode 100644 samples/livepatch/livepatch-callbacks-busymod.c delete mode 100644 samples/livepatch/livepatch-callbacks-demo.c delete mode 100644 samples/livepatch/livepatch-callbacks-mod.c
Documentation/livepatch/callbacks.rst deleted +0 -133
@@ -1,133 +0,0 @@ -====================== -(Un)patching Callbacks -====================== - -Livepatch (un)patch-callbacks provide a mechanism for livepatch modules -to execute callback functions when a kernel object is (un)patched. They -can be considered a **power feature** that **extends livepatching abilities** -to include: - - - Safe updates to global data - - - "Patches" to init and probe functions - - - Patching otherwise unpatchable code (i.e. assembly) - -In most cases, (un)patch callbacks will need to be used in conjunction -with memory barriers and kernel synchronization primitives, like -mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues. - -1. Motivation -============= - -Callbacks differ from existing kernel facilities: - - - Module init/exit code doesn't run when disabling and re-enabling a - patch. - - - A module notifier can't stop a to-be-patched module from loading. - -Callbacks are part of the klp_object structure and their implementation -is specific to that klp_object. Other livepatch objects may or may not -be patched, irrespective of the target klp_object's current state. - -2. Callback types -================= - -Callbacks can be registered for the following livepatch actions: - - * Pre-patch - - before a klp_object is patched - - * Post-patch - - after a klp_object has been patched and is active - across all tasks - - * Pre-unpatch - - before a klp_object is unpatched (ie, patched code is - active), used to clean up post-patch callback - resources - - * Post-unpatch - - after a klp_object has been patched, all code has - been restored and no tasks are running patched code, - used to cleanup pre-patch callback resources - -3. How it works -=============== - -Each callback is optional, omitting one does not preclude specifying any -other. However, the livepatching core executes the handlers in -symmetry: pre-patch callbacks have a post-unpatch counterpart and -post-patch callbacks have a pre-unpatch counterpart. An unpatch -callback will only be executed if its corresponding patch callback was -executed. Typical use cases pair a patch handler that acquires and -configures resources with an unpatch handler tears down and releases -those same resources. - -A callback is only executed if its host klp_object is loaded. For -in-kernel vmlinux targets, this means that callbacks will always execute -when a livepatch is enabled/disabled. For patch target kernel modules, -callbacks will only execute if the target module is loaded. When a -module target is (un)loaded, its callbacks will execute only if the -livepatch module is enabled. - -The pre-patch callback, if specified, is expected to return a status -code (0 for success, -ERRNO on error). An error status code indicates -to the livepatching core that patching of the current klp_object is not -safe and to stop the current patching request. (When no pre-patch -callback is provided, the transition is assumed to be safe.) If a -pre-patch callback returns failure, the kernel's module loader will: - - - Refuse to load a livepatch, if the livepatch is loaded after - targeted code. - - or: - - - Refuse to load a module, if the livepatch was already successfully - loaded. - -No post-patch, pre-unpatch, or post-unpatch callbacks will be executed -for a given klp_object if the object failed to patch, due to a failed -pre_patch callback or for any other reason. - -If a patch transition is reversed, no pre-unpatch handlers will be run -(this follows the previously mentioned symmetry -- pre-unpatch callbacks -will only occur if their corresponding post-patch callback executed). - -If the object did successfully patch, but the patch transition never -started for some reason (e.g., if another object failed to patch), -only the post-unpatch callback will be called. - -4. Use cases -============ - -Sample livepatch modules demonstrating the callback API can be found in -samples/livepatch/ directory. These samples were modified for use in -kselftests and can be found in the lib/livepatch directory. - -Global data update ------------------- - -A pre-patch callback can be useful to update a global variable. For -example, 75ff39ccc1bd ("tcp: make challenge acks less predictable") -changes a global sysctl, as well as patches the tcp_send_challenge_ack() -function. - -In this case, if we're being super paranoid, it might make sense to -patch the data *after* patching is complete with a post-patch callback, -so that tcp_send_challenge_ack() could first be changed to read -sysctl_tcp_challenge_ack_limit with READ_ONCE. - -__init and probe function patches support ------------------------------------------ - -Although __init and probe functions are not directly livepatch-able, it -may be possible to implement similar updates via pre/post-patch -callbacks. - -The commit ``48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST")`` change the way that -virtnet_probe() initialized its driver's net_device features. A -pre/post-patch callback could iterate over all such devices, making a -similar change to their hw_features value. (Client functions of the -value may need to be updated accordingly.)
Documentation/livepatch/index.rst +0 -1
@@ -8,7 +8,6 @@ Kernel Livepatching :maxdepth: 1 livepatch - callbacks cumulative-patches module-elf-format shadow-vars
include/linux/livepatch.h +0 -25
@@ -77,30 +77,6 @@ struct klp_func {bool transition; }; -struct klp_object ; - -/** -* struct klp_callbacks - pre/post live-(un)patch callback structure -* @pre_patch: executed before code patching -* @post_patch: executed after code patching -* @pre_unpatch: executed before code unpatching -* @post_unpatch: executed after code unpatching -* @post_unpatch_enabled: flag indicating if post-unpatch callback -* should run -* -* All callbacks are optional. Only the pre-patch callback, if provided, -* will be unconditionally executed. If the parent klp_object fails to -* patch for any reason, including a non-zero error status returned from -* the pre-patch callback, no further callbacks will be executed. -*/ -struct klp_callbacks { -int (*pre_patch)(struct klp_object *obj); -void (*post_patch)(struct klp_object *obj); -void (*pre_unpatch)(struct klp_object *obj); -void (*post_unpatch)(struct klp_object *obj); -bool post_unpatch_enabled; -}; -/** * struct klp_object - kernel object structure for live patching * @name: module name (or NULL for vmlinux)
@@ -118,7 +94,6 @@ struct klp_object {/* external */ const char *name;struct klp_func *funcs; -struct klp_callbacks callbacks;/* internal */ struct kobject kobj;
kernel/livepatch/core.c +0 -29
@@ -965,8 +965,6 @@ static int klp_init_patch(struct klp_patch *patch)static int __klp_disable_patch (struct klp_patch *patch) { -struct klp_object *obj; -if (WARN_ON (!patch->enabled))return -EINVAL ;
@@ -977,10 +975,6 @@ static int __klp_disable_patch(struct klp_patch *patch)klp_disable_states (patch); -klp_for_each_object (patch, obj) -if (obj->patched) -klp_pre_unpatch_callback (obj); -/* * Enforce the order of the func->transition writes in * klp_init_transition() and the TIF_PATCH_PENDING writes in
@@ -1032,13 +1026,6 @@ static int __klp_enable_patch(struct klp_patch *patch)if (!klp_is_object_loaded (obj))continue ; - ret =klp_pre_patch_callback (obj); -if (ret) { -pr_warn ("pre-patch callback failed for object '%s'\n" , -klp_is_module (obj) ? obj->name :"vmlinux" ); - goto err_states; - } - ret =klp_patch_object (obj);if (ret) { pr_warn("failed to patch object '%s'\n",
@@ -1214,14 +1201,10 @@ static void klp_cleanup_module_patches_limited(struct module *mod,if (!klp_is_module (obj) ||strcmp (obj->name, mod->name))continue ; -if (patch != klp_transition_patch) -klp_pre_unpatch_callback (obj); -pr_notice ("reverting patch '%s' on unloading module '%s'\n" , patch->mod->name, obj->mod->name);klp_unpatch_object (obj); -klp_post_unpatch_callback (obj);klp_clear_object_relocs (patch, obj);klp_free_object_loaded (obj);break ;
@@ -1268,25 +1251,13 @@ int klp_module_coming(struct module *mod)pr_notice ("applying patch '%s' to loading module '%s'\n" , patch->mod->name, obj->mod->name); - ret =klp_pre_patch_callback (obj); -if (ret) { -pr_warn ("pre-patch callback failed for object '%s'\n" , - obj->name); - goto err; - } - ret =klp_patch_object (obj);if (ret) {pr_warn ("failed to apply patch '%s' to module '%s' (%d)\n" , patch->mod->name, obj->mod->name, ret); - -klp_post_unpatch_callback (obj); goto err; } -if (patch != klp_transition_patch) -klp_post_patch_callback (obj); -break ; } }
kernel/livepatch/core.h +0 -33
@@ -23,37 +23,4 @@ static inline bool klp_is_object_loaded(struct klp_object *obj) return !obj->name || obj->mod; } -static inline int klp_pre_patch_callback (struct klp_object *obj) -{ -int ret =0 ; - -if (obj->callbacks.pre_patch) - ret = (*obj->callbacks.pre_patch)(obj); - - obj->callbacks.post_unpatch_enabled = !ret; - -return ret; -} - -static inline void klp_post_patch_callback (struct klp_object *obj) -{ -if (obj->callbacks.post_patch) - (*obj->callbacks.post_patch)(obj); -} - -static inline void klp_pre_unpatch_callback (struct klp_object *obj) -{ -if (obj->callbacks.pre_unpatch) - (*obj->callbacks.pre_unpatch)(obj); -} - -static inline void klp_post_unpatch_callback (struct klp_object *obj) -{ -if (obj->callbacks.post_unpatch_enabled && - obj->callbacks.post_unpatch) - (*obj->callbacks.post_unpatch)(obj); - - obj->callbacks.post_unpatch_enabled = false; -} -#endif /* _LIVEPATCH_CORE_H */
kernel/livepatch/transition.c +0 -9
@@ -148,15 +148,6 @@ static void klp_complete_transition(void)klp_release_states (klp_transition_patch); } -klp_for_each_object (klp_transition_patch, obj) { -if (!klp_is_object_loaded (obj)) -continue ; -if (klp_target_state ==KLP_PATCHED ) -klp_post_patch_callback (obj); -else if (klp_target_state ==KLP_UNPATCHED ) -klp_post_unpatch_callback (obj); - } -pr_notice ("'%s': %s complete\n" , klp_transition_patch->mod->name, klp_target_state ==KLP_PATCHED ?"patching" :"unpatching" );
samples/livepatch/Makefile +0 -3
@@ -3,6 +3,3 @@ obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-sample.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-mod.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-fix1.o obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-shadow-fix2.o -obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-demo.o -obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-mod.o -obj-$(CONFIG_SAMPLE_LIVEPATCH) += livepatch-callbacks-busymod.o
samples/livepatch/livepatch-callbacks-busymod.c deleted +0 -60
60 lines not on this page · raw message
samples/livepatch/livepatch-callbacks-demo.c deleted +0 -196
196 lines not on this page · raw message
samples/livepatch/livepatch-callbacks-mod.c deleted +0 -41
41 lines not on this page · raw message
Signature · 1 line
2.35.3
Petr Mladek
maintainerPATCH 7/7 livepatching: Remove per-state version
In reply to Petr MladekThe livepatch state API was added to help with maintaining:
+ changes done by livepatch callbasks
+ lifetime of shadow variables
The original API was hard to use. Both objectives are better handled by the new per-state callbacks. They are called when the state is introduced or removed. There is also support for automatically freeing obsolete shadow variables.
The new callbacks changed the view of compatibility. The livepatch can be replaced to any older one as long the current livepatch is able to disable the obsolete state.
As a result, the new patch does not need to support the currently used states. The current patch will be able to disable them.
The remaining question is what to do with the per-state version. It was supposed to allow doing more modifications on an existing state. The experience shows that it is not needed in practice.
Well, it still might make sense to prevent downgrade when the state could not be disabled easily or when the author does not want to deal with it.
Replace the per-state version with per-state block_disable flag. It allows to handle several scenarios:
+ prevent disabling the livepatch when it does not support a particular state disablement.
+ prevent replacing the livepatch when the state is not supported by the new patch and the current patch does not support a particular state disablement.
+ allow to replace the livepatch with a new one which would support the particular state disablement.
--- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 17 ++- kernel/livepatch/state.c | 27 +++-- kernel/livepatch/state.h | 1 + lib/livepatch/test_klp_state.c | 101 +++++++++++++----- lib/livepatch/test_klp_state2.c | 2 - lib/livepatch/test_klp_state3.c | 2 +- .../testing/selftests/livepatch/functions.sh | 17 +++ .../testing/selftests/livepatch/test-state.sh | 40 ++++--- 9 files changed, 158 insertions(+), 56 deletions(-)
include/linux/livepatch.h +5 -2
@@ -140,16 +140,16 @@ struct klp_state_callbacks {/** * struct klp_state - state of the system modified by the livepatch * @id: system state identifier (non-zero) -* @version: version of the change * @callbacks: optional callbacks used when introducing or removing the state +* @block_disable: the state disablement is not supported * @is_shadow: the state handles lifetime of a shadow variable * with the same @id * @data: custom data */ struct klp_state {unsigned long id; -unsigned int version;struct klp_state_callbacks callbacks; +bool block_disable;bool is_shadow;void *data; };
@@ -159,7 +159,9 @@ struct klp_state {* @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched * @states: system states that can get modified +* version: livepatch version (optional) * @replace: replace all actively used patches +* * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
@@ -173,6 +175,7 @@ struct klp_patch {struct module *mod;struct klp_object *objs;struct klp_state *states; +unsigned int version;bool replace;/* internal */
kernel/livepatch/core.c +12 -5
@@ -373,6 +373,13 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, goto out; } +if (patch->enabled &&klp_patch_disable_blocked (patch)) { +pr_err ("The livepatch '%s' does not support disable\n" , + patch->mod->name); + ret = -EINVAL ; + goto out; + } +/* * Allow to reverse a pending transition in both ways. It might be * necessary to complete the transition without forcing and breaking
@@ -1097,10 +1104,10 @@ int klp_enable_patch(struct klp_patch *patch)if (!klp_is_patch_compatible (patch)) {pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); + patch->mod->name);mutex_unlock (&klp_mutex);return -EINVAL ; - } + }if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);
@@ -1111,17 +1118,17 @@ int klp_enable_patch(struct klp_patch *patch) ret =klp_init_patch (patch);if (ret) - goto err; + goto unlock_free; ret =__klp_enable_patch (patch);if (ret) - goto err; + goto unlock_free;mutex_unlock (&klp_mutex);return 0 ; -err: +unlock_free:klp_free_patch_start (patch);mutex_unlock (&klp_mutex);
kernel/livepatch/state.c +21 -6
@@ -91,23 +91,26 @@ static bool klp_is_state_compatible(struct klp_patch *patch, state =klp_get_state (patch, old_state->id); -/* A cumulative livepatch must handle all already modified states. */ -if (!state) -return !patch->replace; +if (!state && old_state->block_disable) +return false; -return state->version >= old_state->version; +return true; }/* * Check that the new livepatch will not break the existing system states. -* Cumulative patches must handle all already modified states. -* Non-cumulative patches can touch already modified states. +* The patch could replace existing patches only when the obsolete +* states can be disabled. */ bool klp_is_patch_compatible (struct klp_patch *patch) {struct klp_patch *old_patch;struct klp_state *old_state; +/* Non-cumulative patches are always compatible. */ +if (!patch->replace) +return true; +klp_for_each_patch (old_patch) {klp_for_each_state (old_patch, old_state) {if (!klp_is_state_compatible (patch, old_state))
@@ -118,6 +121,18 @@ bool klp_is_patch_compatible(struct klp_patch *patch)return true; } +bool klp_patch_disable_blocked (struct klp_patch *patch) +{ +struct klp_state *state; + +klp_for_each_state (patch, state) { +if (state->block_disable) +return true; + } + +return false; +} +bool is_state_in_other_patches (struct klp_patch *patch,struct klp_state *state) {struct klp_patch *old_patch;
kernel/livepatch/state.h +1 -0
@@ -5,6 +5,7 @@#include <linux/livepatch.h> bool klp_is_patch_compatible (struct klp_patch *patch); +bool klp_patch_disable_blocked (struct klp_patch *patch);int klp_setup_states (struct klp_patch *patch);void klp_enable_states (struct klp_patch *patch);void klp_disable_states (struct klp_patch *patch);
lib/livepatch/test_klp_state.c +77 -24
125 lines not on this page · raw message
lib/livepatch/test_klp_state2.c +0 -2
@@ -1,7 +1,5 @@// SPDX-License-Identifier: GPL-2.0 // Copyright (C) 2019 SUSE -#define CONSOLE_LOGLEVEL_FIX_VERSION 2 -/* The console loglevel fix is the same in the next cumulative patch. */ #include "test_klp_state.c"
lib/livepatch/test_klp_state3.c +1 -1
@@ -2,4 +2,4 @@// Copyright (C) 2019 SUSE /* The console loglevel fix is the same in the next cumulative patch. */ -#include "test_klp_state2.c" +#include "test_klp_state.c"
tools/testing/selftests/livepatch/functions.sh +17 -0
@@ -277,6 +277,23 @@ function set_pre_patch_ret { die "failed to set pre_patch_ret parameter for $mod module" } +# set_module_param(modname, param, val) +# modname - module name to set +# param - name of the parameter to set +# val - value to set +function set_module_param { + local mod="$1"; shift + local param="$1"; shift + local val="$1" + + log "% echo $val > /sys/module/$mod/parameters/$param" + echo "$val" > "/sys/module/$mod/parameters/$param" + + # Wait for sysfs value to hold ... + loop_until '[[ $(cat "/sys/module/$mod/parameters/$param") == "$val" ]]' || + die "failed to set parameter $param for $mod module to the value $val" +} + # read_module_param(modname, param) # modname - module name which provides the given parameter # param - parameter name to be read
tools/testing/selftests/livepatch/test-state.sh +24 -16
@@ -132,12 +132,14 @@ livepatch: '$MOD_LIVEPATCH2': unpatching complete start_test "incompatible cumulative livepatches" -load_lp $MOD_LIVEPATCH2 -load_failing_mod $MOD_LIVEPATCH -disable_lp $MOD_LIVEPATCH2 -unload_lp $MOD_LIVEPATCH2 +load_lp $MOD_LIVEPATCH2 block_state_disable=1 +load_failing_mod $MOD_LIVEPATCH no_state=1 +# load the livepatch again with default features (state and disable supported) +load_lp $MOD_LIVEPATCH +disable_lp $MOD_LIVEPATCH +unload_lp $MOD_LIVEPATCH -check_result "% modprobe $MOD_LIVEPATCH2 +check_result "% modprobe $MOD_LIVEPATCH2 block_state_disable=1 livepatch: enabling patch '$MOD_LIVEPATCH2' livepatch: '$MOD_LIVEPATCH2': initializing patching transition $MOD_LIVEPATCH2: setup_state_callback: state 1
@@ -147,18 +149,24 @@ livepatch: '$MOD_LIVEPATCH2': completing patching transition $MOD_LIVEPATCH2: enable_state_callback: state 1 $MOD_LIVEPATCH2: fix_console_loglevel: fixing console_loglevel livepatch: '$MOD_LIVEPATCH2': patching complete -% modprobe $MOD_LIVEPATCH +% modprobe $MOD_LIVEPATCH no_state=1 livepatch: Livepatch patch ($MOD_LIVEPATCH) is not compatible with the already installed livepatches. modprobe: ERROR: could not insert '$MOD_LIVEPATCH': Invalid argument -% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH2/enabled -livepatch: '$MOD_LIVEPATCH2': initializing unpatching transition -$MOD_LIVEPATCH2: disable_state_callback: state 1 -$MOD_LIVEPATCH2: restore_console_loglevel: restoring console_loglevel -livepatch: '$MOD_LIVEPATCH2': starting unpatching transition -livepatch: '$MOD_LIVEPATCH2': completing unpatching transition -$MOD_LIVEPATCH2: release_state_callback: state 1 -$MOD_LIVEPATCH2: free_loglevel_state: freeing space for the stored console_loglevel -livepatch: '$MOD_LIVEPATCH2': unpatching complete -% rmmod $MOD_LIVEPATCH2" +% modprobe $MOD_LIVEPATCH +livepatch: enabling patch '$MOD_LIVEPATCH' +livepatch: '$MOD_LIVEPATCH': initializing patching transition +livepatch: '$MOD_LIVEPATCH': starting patching transition +livepatch: '$MOD_LIVEPATCH': completing patching transition +livepatch: '$MOD_LIVEPATCH': patching complete +% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled +livepatch: '$MOD_LIVEPATCH': initializing unpatching transition +$MOD_LIVEPATCH: disable_state_callback: state 1 +$MOD_LIVEPATCH: restore_console_loglevel: restoring console_loglevel +livepatch: '$MOD_LIVEPATCH': starting unpatching transition +livepatch: '$MOD_LIVEPATCH': completing unpatching transition +$MOD_LIVEPATCH: release_state_callback: state 1 +$MOD_LIVEPATCH: free_loglevel_state: freeing space for the stored console_loglevel +livepatch: '$MOD_LIVEPATCH': unpatching complete +% rmmod $MOD_LIVEPATCH" exit 0
Signature · 1 line
2.35.3
Miroslav Benes
maintainerRe: [POC 7/7] livepatching: Remove per-state version
Quoted 21 lines · Petr Mladek
The livepatch state API was added to help with maintaining:
+ changes done by livepatch callbasks
+ lifetime of shadow variablesThe original API was hard to use. Both objectives are better handled by the new per-state callbacks. They are called when the state is introduced or removed. There is also support for automatically freeing obsolete shadow variables.
The new callbacks changed the view of compatibility. The livepatch can be replaced to any older one as long the current livepatch is able to disable the obsolete state.
As a result, the new patch does not need to support the currently used states. The current patch will be able to disable them.
The remaining question is what to do with the per-state version. It was supposed to allow doing more modifications on an existing state. The experience shows that it is not needed in practice.
Quoted 6 lines
Well, it still might make sense to prevent downgrade when the state could not be disabled easily or when the author does not want to deal with it.
Replace the per-state version with per-state
block_disableflag. It allows to handle several scenarios:
I have no opinion to be honest. block_disable flag might be sufficient in the end.
[...]
Quoted 11 lines
include/linux/livepatch.h +2
-0@@ -159,7 +159,9 @@ struct klp_state {* @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched * @states: system states that can get modified +* version: livepatch version (optional) * @replace: replace all actively used patches +* * @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 179
Quoted 5 lines
include/linux/livepatch.h +1
-0@@ -173,6 +175,7 @@ struct klp_patch {struct module *mod;struct klp_object *objs;struct klp_state *states; +unsigned int version;bool replace;
Is it still needed then? What would be the use case?
[...]
Quoted 25 lines
kernel/livepatch/core.c +0
-0/* * Allow to reverse a pending transition in both ways. It might be kernel/livepatch/core.c +3
-3* necessary to complete the transition without forcing and breaking @@ -1097,10 +1104,10 @@ int klp_enable_patch(struct klp_patch *patch)if (!klp_is_patch_compatible (patch)) {pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); + patch->mod->name);mutex_unlock (&klp_mutex);return -EINVAL ; - } + }if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);@@ -1111,17 +1118,17 @@ int klp_enable_patch(struct klp_patch *patch) ret =klp_init_patch (patch);if (ret) - goto err; + goto unlock_free; ret =__klp_enable_patch (patch);if (ret)
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1132
Quoted 10 lines
kernel/livepatch/core.c +2
-2- goto err; + goto unlock_free;mutex_unlock (&klp_mutex);return 0 ; -err: +unlock_free:klp_free_patch_start (patch);
Unrelated changes.
Quoted 6 lines
kernel/livepatch/state.c +2
-2/* * Check that the new livepatch will not break the existing system states. -* Cumulative patches must handle all already modified states. -* Non-cumulative patches can touch already modified states. +* The patch could replace existing patches only when the obsolete +* states can be disabled.
kernel/livepatch/state.c static bool klp_is_state_compatible(struct klp_patch *patch, · line 113
Quoted 10 lines
kernel/livepatch/state.c +4
-0*/ bool klp_is_patch_compatible (struct klp_patch *patch) {struct klp_patch *old_patch;struct klp_state *old_state; +/* Non-cumulative patches are always compatible. */ +if (!patch->replace) +return true; +
Cumulative != atomic replace. Those are two different things.
Miroslav
Petr Mladek
maintainerRe: [POC 7/7] livepatching: Remove per-state version
Quoted 45 lines · Miroslav Benes
Quoted 21 lines · Petr Mladek
The livepatch state API was added to help with maintaining:
+ changes done by livepatch callbasks
+ lifetime of shadow variablesThe original API was hard to use. Both objectives are better handled by the new per-state callbacks. They are called when the state is introduced or removed. There is also support for automatically freeing obsolete shadow variables.
The new callbacks changed the view of compatibility. The livepatch can be replaced to any older one as long the current livepatch is able to disable the obsolete state.
As a result, the new patch does not need to support the currently used states. The current patch will be able to disable them.
The remaining question is what to do with the per-state version. It was supposed to allow doing more modifications on an existing state. The experience shows that it is not needed in practice.
Quoted 6 lines
Well, it still might make sense to prevent downgrade when the state could not be disabled easily or when the author does not want to deal with it.
Replace the per-state version with per-state
block_disableflag. It allows to handle several scenarios:I have no opinion to be honest.
block_disableflag might be sufficient in the end.[...]
Quoted 10 lines
include/linux/livepatch.h +2
-0@@ -159,7 +159,9 @@ struct klp_state {* @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched * @states: system states that can get modified +* version: livepatch version (optional) * @replace: replace all actively used patches +* * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
Quoted 8 lines
include/linux/livepatch.h struct klp_patch { · line 179
Quoted 6 lines
include/linux/livepatch.h +1
-0@@ -173,6 +175,7 @@ struct klp_patch {struct module *mod;struct klp_object *objs;struct klp_state *states; +unsigned int version;bool replace;Is it still needed then? What would be the use case?
Heh, I think that I actually wanted to remove the version completely. This change is not mentioned in the changelog. And the version is no longer used in the selftests.
I am going to remove it in the next version of the patchset.
Quoted 12 lines
Quoted 12 lines
kernel/livepatch/state.c +2
-2/* * Check that the new livepatch will not break the existing system states. -* Cumulative patches must handle all already modified states. -* Non-cumulative patches can touch already modified states. +* The patch could replace existing patches only when the obsolete +* states can be disabled. */ bool klp_is_patch_compatible (struct klp_patch *patch) {struct klp_patch *old_patch;struct klp_state *old_state;
Quoted 6 lines
kernel/livepatch/state.c static bool klp_is_state_compatible(struct klp_patch *patch, · line 113
Quoted 4 lines
kernel/livepatch/state.c +4
-0+/* Non-cumulative patches are always compatible. */ +if (!patch->replace) +return true; +Cumulative != atomic replace. Those are two different things.
I see. :-)
Best Regards,
Petr
Josh Poimboeuf
maintainerQuoted 9 lines · Petr Mladek
This POC is a material for the discussion "Simplify Livepatch Callbacks, Shadow Variables, and States handling" at LPC 2013, see
https://lpc.events/event/17/contributions/1541/It obsoletes the patchset adding the garbage collection of shadow variables. This new solution is based on ideas from Nicolai Stange. And it should also be in sync with Josh's ideas mentioned into the thread about the garbage collection, see
https://lore.kernel.org/r/20230204235910.4j4ame5ntqogqi7m@treble
Nice! I like how it brings the "features" together and makes them easy to use. This looks like a vast improvement.
Was there a reason to change the naming? I'm thinking
setup / enable / disable / release
is less precise than
pre_patch / post_patch / pre_unpatch / post_unpatch.
Also, I'm thinking "replaced" instead of "obsolete" would be more consistent with the existing terminology.
For example, in __klp_enable_patch():
ret = klp_setup_states(patch); if (ret) goto err; if (patch->replace) klp_disable_obsolete_states(patch);
it's not immediately clear why "disable obsolete" would be the "replace" counterpart to "setup".
Similarly, in klp_complete_transition():
if (klp_transition_patch->replace && klp_target_state == KLP_PATCHED) {
klp_unpatch_replaced_patches(klp_transition_patch);
klp_discard_nops(klp_transition_patch);
klp_release_obsolete_states(klp_transition_patch);
}it's a little jarring to have "unpatch replaced" followed by "release obsolete".
So instead of:
int klp_setup_states(struct klp_patch *patch); void klp_enable_states(struct klp_patch *patch); void klp_disable_states(struct klp_patch *patch); void klp_release_states(struct klp_patch *patch); void klp_enable_obsolete_states(struct klp_patch *patch); void klp_disable_obsolete_states(struct klp_patch *patch); void klp_release_obsolete_states(struct klp_patch *patch);
how about something like:
int klp_states_pre_patch(void); void klp_states_post_patch(void); void klp_states_pre_unpatch(void); void klp_states_post_unpatch(void); void klp_states_post_patch_replaced(void); void klp_states_pre_unpatch_replaced(void); void klp_states_post_unpatch_replaced(void);
?
(note that passing klp_transition_patch might be optional since state.c already has visibility to it anyway)
Signature · 1 line
Josh
Miroslav Benes
maintainerQuoted 39 lines · Josh Poimboeuf
Quoted 9 lines · Petr Mladek
This POC is a material for the discussion "Simplify Livepatch Callbacks, Shadow Variables, and States handling" at LPC 2013, see
https://lpc.events/event/17/contributions/1541/It obsoletes the patchset adding the garbage collection of shadow variables. This new solution is based on ideas from Nicolai Stange. And it should also be in sync with Josh's ideas mentioned into the thread about the garbage collection, see
https://lore.kernel.org/r/20230204235910.4j4ame5ntqogqi7m@trebleNice! I like how it brings the "features" together and makes them easy to use. This looks like a vast improvement.
Was there a reason to change the naming? I'm thinking
setup / enable / disable / release
is less precise than
pre_patch/post_patch/pre_unpatch/post_unpatch.Also, I'm thinking "replaced" instead of "obsolete" would be more consistent with the existing terminology.
For example, in
__klp_enable_patch():ret = klp_setup_states(patch); if (ret) goto err; if (patch->replace) klp_disable_obsolete_states(patch);it's not immediately clear why "disable obsolete" would be the "replace" counterpart to "setup".
Similarly, in
klp_complete_transition():
Quoted 8 lines
if (klp_transition_patch->replace && klp_target_state == KLP_PATCHED) { klp_unpatch_replaced_patches(klp_transition_patch); klp_discard_nops(klp_transition_patch); klp_release_obsolete_states(klp_transition_patch); }it's a little jarring to have "unpatch replaced" followed by "release obsolete".
I agree. I would also stick to the existing naming. It is clearer to me.
Miroslav
Petr Mladek
maintainerQuoted 16 lines · Miroslav Benes
Quoted 14 lines · Josh Poimboeuf
Quoted 9 lines · Petr Mladek
This POC is a material for the discussion "Simplify Livepatch Callbacks, Shadow Variables, and States handling" at LPC 2013, see
https://lpc.events/event/17/contributions/1541/It obsoletes the patchset adding the garbage collection of shadow variables. This new solution is based on ideas from Nicolai Stange. And it should also be in sync with Josh's ideas mentioned into the thread about the garbage collection, see
https://lore.kernel.org/r/20230204235910.4j4ame5ntqogqi7m@trebleNice! I like how it brings the "features" together and makes them easy to use. This looks like a vast improvement.
Quoted 7 lines
Quoted 7 lines
Was there a reason to change the naming? I'm thinking
setup / enable / disable / release
is less precise than
pre_patch/post_patch/pre_unpatch/post_unpatch.
I wanted to point out that the callbacks are called _only_ when the states are added or removed. They are not called when the state is preserved during the atomic replace.
The pre_patch... naming scheme might create an assumption that they are _always_ called during the livepatch transition.
But I see that the new names might be misleading in other ways.
I am going to use the "pre_patch..." names in the next version of the patchset.
Quoted 17 lines
Quoted 17 lines
Also, I'm thinking "replaced" instead of "obsolete" would be more consistent with the existing terminology.
For example, in
__klp_enable_patch():ret = klp_setup_states(patch); if (ret) goto err; if (patch->replace) klp_disable_obsolete_states(patch);it's not immediately clear why "disable obsolete" would be the "replace" counterpart to "setup".
Similarly, in
klp_complete_transition():
Quoted 8 lines
Quoted 8 lines
if (klp_transition_patch->replace && klp_target_state == KLP_PATCHED) { klp_unpatch_replaced_patches(klp_transition_patch); klp_discard_nops(klp_transition_patch); klp_release_obsolete_states(klp_transition_patch); }it's a little jarring to have "unpatch replaced" followed by "release obsolete".
I see.
Quoted 1 line
I agree. I would also stick to the existing naming. It is clearer to me.
Fair enough.
Best Regards,
Petr
Miroslav Benes
maintainerHi Petr,
Quoted 9 lines · Petr Mladek
This POC is a material for the discussion "Simplify Livepatch Callbacks, Shadow Variables, and States handling" at LPC 2013, see
https://lpc.events/event/17/contributions/1541/It obsoletes the patchset adding the garbage collection of shadow variables. This new solution is based on ideas from Nicolai Stange. And it should also be in sync with Josh's ideas mentioned into the thread about the garbage collection, see
https://lore.kernel.org/r/20230204235910.4j4ame5ntqogqi7m@treble
looks good to me. It is a huge improvement I would say.
As you mention elsewhere, it would also be nice to include some documentation and samples in the next revision.
The selftests would need to be ported to the new infrastructure.
Do we still need klp_state->data member? Now that it can be easily coupled with shadow variables, is there a reason to preserve it?
Miroslav
Quoted 3 lines · Miroslav Benes
Do we still need
klp_state->datamember? Now that it can be easily coupled with shadow variables, is there a reason to preserve it?
I would say yes, it could point to e.g. some lock protecting an associated shadow variable's usage. Or be used to conveniently pass on any kind of data between subsequent livepatches.
Thanks,
Nicolai
Signature · 3 lines
SUSE Software Solutions Germany GmbH, Frankenstraße 146, 90461 Nürnberg, Germany GF: Ivo Totev, Andrew McDonald, Werner Knoblich (HRB 36809, AG Nürnberg)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Petr Mladek
maintainerQuoted 5 lines · Nicolai Stange
Quoted 3 lines · Miroslav Benes
Do we still need
klp_state->datamember? Now that it can be easily coupled with shadow variables, is there a reason to preserve it?
Good point. I have actually forgot the pointer completely.
Quoted 3 lines
I would say yes, it could point to e.g. some lock protecting an associated shadow variable's usage. Or be used to conveniently pass on any kind of data between subsequent livepatches.
Honestly, I would prefer to remove the klp_state->data member. I can't find a safe way how to maintain it.
The pointer is in struct klp_state. It means that any livepatch supporting the state has its own copy of the pointer. We could copy the value in __klp_enable_patch() but where?
Before calling klp_states_pre_patch() or after?
It might somehow work when the value is set only once (in a pre_patch callback). But what if anyone tries to modify the pointer. What if there are more livepatches using the state at the same time (not using the atomic replace) and each livepatch has different value.
From my POV, the klp_state->data pointer is a potentially dangerous thing with not well defined semantic. IMHO, it is not worth it.
It the livepatches need a new synchronization mechanism, they might allocate the lock in yet another shadow variable.
Do I miss anything, please?
Could you come up with a reasonable semantic, please?
Best Regards,
Petr