[PATCH v12 00/12]
Subsystems (4)
Petr Mladek
maintainerv12 cover [PATCH v12 00/12]
livepatch: Atomic replace feature
The atomic replace allows to create cumulative patches. They are useful when you maintain many livepatches and want to remove one that is lower on the stack. In addition it is very useful when more patches touch the same function and there are dependencies between them.
This version does another big refactoring based on feedback against v11[*]. In particular, it removes the registration step, changes the API and handling of livepatch dependencies. The aim is to keep the number of possible variants on a sane level. It helps the keep the feature "easy" to use and maintain.
[*] https://lkml.kernel.org/r/[email protected]
Changes against v11:
+ Functional changes:
+ Livepatches get automatically unregistered when disabled. Note that the sysfs interface disappears at this point. It simplifies the API and code. The only drawback is that the patch can be enabled again only by reloading the module.
+ Refuse to load conflicting patches. The same function can be patched again only by a new cumulative patch that replaces all older ones.
+ Non-conflicting patches can be loaded and disabled in any order.
+ API related changes:
+ Change void *new_func -> unsigned long new_addr in struct klp_func.
+ Several new macros to hide implementation details and avoid casting when defining struct klp-func and klp_object.
+ Remove obsolete klp_register_patch() klp_unregister_patch() API
+ Change in selftest against v4:
+ Use new macros to define struct klp_func and klp_object.
+ Remove klp_register_patch()/klp_unregister_patch() calls.
+ Replace load_mod() + wait_for_transition() with three variants load_mod(), load_lp(), load_lp_nowait(). IMHO, it is easier to use because we need to detect the end of transaction another way after disable_lp() now.
+ Replaced unload_mod() with two variants unload_mod(), unload_lp() to match the above change.
+ Wait for the end of transition in disable_lp() instead of the unreliable check of the sysfs interface.
Note that I did not touch the logs with expected result. They stay exactly the same as in v4 posted by Joe. I hope that it is a good sign ;-)
Changes against v10:
+ Bug fixes and functional changes:
+ Handle Nops in klp_ftrace_handled() to avoid infinite loop [Mirek]
+ Really add dynamically allocated klp_object into the list [Petr]
+ Clear patch->replace when transition finishes [Josh]
+ Refactoring and clean up [Josh]:
+ Replace enum types with bools
+ Avoid using ERR_PTR
+ Remove too paranoid warnings
+ Distinguish registered patches by a flag instead of a list
+ Squash some functions
+ Update comments, documentation, and commit messages
+ Squashed and split patches to do more controversial changes later
Changes against v9:
+ Fixed check of valid NOPs for already loaded objects, regression introduced in v9 [Joe, Mirek]
+ Allow to replace even disabled patches [Evgenii]
Changes against v8:
+ Fixed handling of statically defined struct klp_object with empty array of functions [Joe, Mirek]
+ Removed redundant func->new_func assignment for NOPs [Mirek]
+ Improved some wording [Mirek]
Changes against v7:
+ Fixed handling of NOPs for not-yet-loaded modules
+ Made klp_replaced_patches list static [Mirek]
+ Made klp_free_object() public later [Mirek]
+ Fixed several reported typos [Mirek, Joe]
+ Updated documentation according to the feedback [Joe]
+ Added some Acks [Mirek]
Changes against v6:
+ used list_move when disabling replaced patches [Jason]
+ renamed KLP_FUNC_ORIGINAL -> KLP_FUNC_STATIC [Mirek]
+ used klp_is_func_type() in klp_unpatch_object() [Mirek]
+ moved static definition of klp_get_or_add_object() [Mirek]
+ updated comment about synchronization in forced mode [Mirek]
+ added user documentation
+ fixed several typos
Jason Baron (2):
livepatch: Use lists to manage patches, objects and functions
livepatch: Add atomic replace
Joe Lawrence (1):
selftests/livepatch: introduce tests
Petr Mladek (9):
livepatch: Change void *new_func -> unsigned long new_addr in struct
klp_func
livepatch: Helper macros to define livepatch structures
livepatch: Shuffle klp_enable_patch()/klp_disable_patch() code
livepatch: Consolidate klp_free functions
livepatch: Refuse to unload only livepatches available during a forced
transition
livepatch: Simplify API by removing registration step
livepatch: Remove Nop structures when unused
livepatch: Atomic replace and cumulative patches documentation
livepatch: Remove ordering and refuse loading conflicting patches
Documentation/livepatch/callbacks.txt | 489 +----------- Documentation/livepatch/cumulative-patches.txt | 105 +++ Documentation/livepatch/livepatch.txt | 131 ++-- MAINTAINERS | 1 + include/linux/livepatch.h | 84 ++- kernel/livepatch/core.c | 833 ++++++++++++++------- kernel/livepatch/core.h | 4 + kernel/livepatch/patch.c | 41 +- kernel/livepatch/patch.h | 1 + kernel/livepatch/transition.c | 26 +- lib/Kconfig.debug | 21 + lib/Makefile | 2 + lib/livepatch/Makefile | 15 + lib/livepatch/test_klp_atomic_replace.c | 53 ++ lib/livepatch/test_klp_callbacks_busy.c | 43 ++ lib/livepatch/test_klp_callbacks_demo.c | 109 +++ lib/livepatch/test_klp_callbacks_demo2.c | 89 +++ lib/livepatch/test_klp_callbacks_mod.c | 24 + lib/livepatch/test_klp_livepatch.c | 47 ++ lib/livepatch/test_klp_shadow_vars.c | 236 ++++++ samples/livepatch/livepatch-callbacks-demo.c | 68 +- samples/livepatch/livepatch-sample.c | 26 +- samples/livepatch/livepatch-shadow-fix1.c | 34 +- samples/livepatch/livepatch-shadow-fix2.c | 34 +- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/Makefile | 8 + tools/testing/selftests/livepatch/README | 43 ++ tools/testing/selftests/livepatch/config | 1 + tools/testing/selftests/livepatch/functions.sh | 203 +++++ .../testing/selftests/livepatch/test-callbacks.sh | 587 +++++++++++++++ .../testing/selftests/livepatch/test-livepatch.sh | 168 +++++ .../selftests/livepatch/test-shadow-vars.sh | 60 ++ 32 files changed, 2603 insertions(+), 984 deletions(-) create mode 100644 Documentation/livepatch/cumulative-patches.txt create mode 100644 lib/livepatch/Makefile create mode 100644 lib/livepatch/test_klp_atomic_replace.c create mode 100644 lib/livepatch/test_klp_callbacks_busy.c create mode 100644 lib/livepatch/test_klp_callbacks_demo.c create mode 100644 lib/livepatch/test_klp_callbacks_demo2.c create mode 100644 lib/livepatch/test_klp_callbacks_mod.c create mode 100644 lib/livepatch/test_klp_livepatch.c create mode 100644 lib/livepatch/test_klp_shadow_vars.c create mode 100644 tools/testing/selftests/livepatch/Makefile create mode 100644 tools/testing/selftests/livepatch/README create mode 100644 tools/testing/selftests/livepatch/config create mode 100644 tools/testing/selftests/livepatch/functions.sh create mode 100755 tools/testing/selftests/livepatch/test-callbacks.sh create mode 100755 tools/testing/selftests/livepatch/test-livepatch.sh create mode 100755 tools/testing/selftests/livepatch/test-shadow-vars.sh
Signature · 1 line
2.13.7
Petr Mladek
maintainerPATCH v12 1/12 livepatch: Change void *new_func -> unsigned long new_addr in struct klp_func
The address of the to be patched function and new function is stored in struct klp_func as:
void *new_func; unsigned long old_addr;
The different naming scheme and type is derived from the way how the addresses are set. @old_addr is assigned at runtime using kallsyms-based search. @new_func is statically initialized, for example:
static struct klp_func funcs[] = {
{
.old_name = "cmdline_proc_show",
.new_func = livepatch_cmdline_proc_show,
}, { }};
This patch changes void *new_func -> unsigned long new_addr. It removes some confusion when these address are later used in the code. It is motivated by a followup patch that adds special NOP struct klp_func where we want to assign func->new_func = func->old_addr respectively func->new_addr = func->old_addr.
This patch does not modify the existing behavior.
IMPORTANT: This patch modifies ABI. The patches will need to use, for example:
static struct klp_func funcs[] = {
{
.old_name = "cmdline_proc_show",
.new_addr = (unsigned long)livepatch_cmdline_proc_show,
}, { }};
--- include/linux/livepatch.h | 6 +++--- kernel/livepatch/core.c | 4 ++-- kernel/livepatch/patch.c | 2 +- kernel/livepatch/transition.c | 4 ++-- samples/livepatch/livepatch-callbacks-demo.c | 2 +- samples/livepatch/livepatch-sample.c | 2 +- samples/livepatch/livepatch-shadow-fix1.c | 4 ++-- samples/livepatch/livepatch-shadow-fix2.c | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-)
include/linux/livepatch.h +3 -3
@@ -37,7 +37,7 @@/** * struct klp_func - function structure for live patching * @old_name: name of the function to be patched -* @new_func: pointer to the patched function code +* @new_addr: address of the new function (function pointer) * @old_sympos: a hint indicating which symbol position the old function * can be found (optional) * @old_addr: the address of the function being patched
@@ -66,7 +66,7 @@struct klp_func {/* external */ const char *old_name; -void *new_func; +unsigned long new_addr;/* * The old_sympos field is optional and can be used to resolve * duplicate symbol names in livepatch objects. If this field is zero,
@@ -157,7 +157,7 @@ struct klp_patch {#define klp_for_each_func (obj, func) \ for (func = obj->funcs; \ - func->old_name || func->new_func || func->old_sympos; \ + func->old_name || func->new_addr || func->old_sympos; \ func++)int klp_register_patch (struct klp_patch *);
kernel/livepatch/core.c +2 -2
@@ -675,7 +675,7 @@ static void klp_free_patch(struct klp_patch *patch)static int klp_init_func (struct klp_object *obj,struct klp_func *func) { -if (!func->old_name || !func->new_func) +if (!func->old_name || !func->new_addr)return -EINVAL ;if (strlen (func->old_name) >=KSYM_NAME_LEN )
@@ -733,7 +733,7 @@ static int klp_init_object_loaded(struct klp_patch *patch,return -ENOENT ; } - ret =kallsyms_lookup_size_offset ((unsigned long )func->new_func, + ret =kallsyms_lookup_size_offset (func->new_addr, &func->new_size,NULL );if (!ret) { pr_err("kallsyms size lookup failed for '%s' replacement\n",
kernel/livepatch/patch.c +1 -1
@@ -118,7 +118,7 @@ static void notrace klp_ftrace_handler(unsigned long ip, } } -klp_arch_set_pc (regs, (unsigned long )func->new_func); +klp_arch_set_pc (regs, func->new_addr); unlock:preempt_enable_notrace (); }
kernel/livepatch/transition.c +2 -2
@@ -217,7 +217,7 @@ static int klp_check_stack_func(struct klp_func *func,* Check for the to-be-unpatched function * (the func itself). */ - func_addr = (unsigned long )func->new_func; + func_addr = func->new_addr; func_size = func->new_size; }else {/*
@@ -235,7 +235,7 @@ static int klp_check_stack_func(struct klp_func *func,struct klp_func *prev; prev =list_next_entry (func, stack_node); - func_addr = (unsigned long )prev->new_func; + func_addr = prev->new_addr; func_size = prev->new_size; } }
samples/livepatch/livepatch-callbacks-demo.c +1 -1
@@ -153,7 +153,7 @@ static struct klp_func no_funcs[] = {static struct klp_func busymod_funcs[] = { { .old_name ="busymod_work_func" , - .new_func = patched_work_func, + .new_addr = (unsigned long )patched_work_func, }, { } };
samples/livepatch/livepatch-sample.c +1 -1
@@ -51,7 +51,7 @@ static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)static struct klp_func funcs[] = { { .old_name ="cmdline_proc_show" , - .new_func = livepatch_cmdline_proc_show, + .new_addr = (unsigned long )livepatch_cmdline_proc_show, }, { } };
samples/livepatch/livepatch-shadow-fix1.c +2 -2
@@ -130,11 +130,11 @@ void livepatch_fix1_dummy_free(struct dummy *d)static struct klp_func funcs[] = { { .old_name ="dummy_alloc" , - .new_func = livepatch_fix1_dummy_alloc, + .new_addr = (unsigned long )livepatch_fix1_dummy_alloc, }, { .old_name ="dummy_free" , - .new_func = livepatch_fix1_dummy_free, + .new_addr = (unsigned long )livepatch_fix1_dummy_free, }, { } };
samples/livepatch/livepatch-shadow-fix2.c +2 -2
@@ -107,11 +107,11 @@ void livepatch_fix2_dummy_free(struct dummy *d)static struct klp_func funcs[] = { { .old_name ="dummy_check" , - .new_func = livepatch_fix2_dummy_check, + .new_addr = (unsigned long )livepatch_fix2_dummy_check, }, { .old_name ="dummy_free" , - .new_func = livepatch_fix2_dummy_free, + .new_addr = (unsigned long )livepatch_fix2_dummy_free, }, { } };
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 01/12] livepatch: Change void *new_func -> unsigned long new_addr in struct klp_func
Quoted 34 lines · Petr Mladek
The address of the to be patched function and new function is stored in struct
klp_funcas:void *new_func; unsigned long old_addr;The different naming scheme and type is derived from the way how the addresses are set. @old_addr is assigned at runtime using kallsyms-based search. @new_func is statically initialized, for example:
static struct klp_func funcs[] = { { .old_name = "cmdline_proc_show", .new_func = livepatch_cmdline_proc_show, }, { }};
This patch changes void
*new_func-> unsigned longnew_addr. It removes some confusion when these address are later used in the code. It is motivated by a followup patch that adds special NOP structklp_funcwhere we want to assignfunc->new_func=func->old_addrrespectivelyfunc->new_addr=func->old_addr.This patch does not modify the existing behavior.
IMPORTANT: This patch modifies ABI. The patches will need to use, for example:
static struct klp_func funcs[] = { { .old_name = "cmdline_proc_show", .new_addr = (unsigned long)livepatch_cmdline_proc_show, }, { }
Quoted 4 lines
};
Suggested-by Josh Poimboeuf <[email protected]>Signed-off-by Petr Mladek <[email protected]>
I'm not convinced the patch makes things any better. The next patch slightly improves it, but still. Is new_func really such a problem?
Thanks,
Miroslav
Petr Mladek
maintainerPATCH v12 2/12 livepatch: Helper macros to define livepatch structures
In reply to Petr MladekThe definition of struct klp_func might be a bit confusing. The original function is defined by name as a string. The new function is defined by name as a function pointer casted to unsigned long.
This patch adds helper macros that hide the different types. The functions are defined just by the name. For example:
static struct klp_func funcs[] = {
{
.old_name = "function_A",
.new_addr = (unsigned long)livepatch_function_A,
}, {
.old_name = "function_B",
.new_addr = (unsigned long)livepatch_function_B,
}, { }
};can be defined as:
static struct klp_func funcs[] = {
KLP_FUNC(function_A,
livepatch_function_A),
KLP_FUNC(function_B,
livepatch_function_B),
KLP_FUNC_END
};
Just for completeness, this patch adds similar macros to define struct klp_object. For example,
static struct klp_object objs[] = {
{
/* name being NULL means vmlinux */
.funcs = funcs_vmlinux,
}, {
.name = "module_A",
.funcs = funcs_module_A,
}, {
.name = "module_B",
.funcs = funcs_module_B,
}, { }
};can be defined as:
static struct klp_object objs[] = {
KLP_VMLINUX(funcs_vmlinux),
KLP_OBJECT(module_A,
funcs_module_A),
KLP_OBJECT(module_B,
funcs_module_B),
KLP_OBJECT_END
};
--- include/linux/livepatch.h | 40 ++++++++++++++++++++ samples/livepatch/livepatch-callbacks-demo.c | 55 +++++++++++----------------- samples/livepatch/livepatch-sample.c | 13 +++---- samples/livepatch/livepatch-shadow-fix1.c | 20 ++++------ samples/livepatch/livepatch-shadow-fix2.c | 20 ++++------ 5 files changed, 83 insertions(+), 65 deletions(-)
include/linux/livepatch.h +40 -0
@@ -152,6 +152,46 @@ struct klp_patch {struct completion finish; }; +#define KLP_FUNC (_old_func, _new_func) { \ + .old_name = #_old_func, \ + .new_addr = (unsigned long)(_new_func), \ + } +#define KLP_FUNC_POS (_old_func, _new_func, _sympos) { \ + .old_name = #_old_func, \ + .new_addr = (unsigned long)_new_func, \ + .sympos = _sympos, \ + } +#define KLP_FUNC_END { } + +#define KLP_OBJECT (_obj, _funcs) { \ + .name = #_obj, \ + .funcs = _funcs, \ + } +#define KLP_OBJECT_CALLBACKS (_obj, _funcs, \ + _pre_patch, _post_patch, \ + _pre_unpatch, _post_unpatch) { \ + .name = #_obj, \ + .funcs = _funcs, \ + .callbacks.pre_patch = _pre_patch, \ + .callbacks.post_patch = _post_patch, \ + .callbacks.pre_unpatch = _pre_unpatch, \ + .callbacks.post_unpatch = _post_unpatch, \ + } +/* name being NULL means vmlinux */ +#define KLP_VMLINUX (_funcs) { \ + .funcs = _funcs, \ + } +#define KLP_VMLINUX_CALLBACKS (_funcs, \ + _pre_patch, _post_patch, \ + _pre_unpatch, _post_unpatch) { \ + .funcs = _funcs, \ + .callbacks.pre_patch = _pre_patch, \ + .callbacks.post_patch = _post_patch, \ + .callbacks.pre_unpatch = _pre_unpatch, \ + .callbacks.post_unpatch = _post_unpatch, \ + } +#define KLP_OBJECT_END { } +#define klp_for_each_object (patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++)
samples/livepatch/livepatch-callbacks-demo.c +22 -33
67 lines not on this page · raw message
samples/livepatch/livepatch-sample.c +5 -8
@@ -49,17 +49,14 @@ static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) }static struct klp_func funcs[] = { - { - .old_name ="cmdline_proc_show" , - .new_addr = (unsigned long )livepatch_cmdline_proc_show, - }, { } +KLP_FUNC (cmdline_proc_show, + livepatch_cmdline_proc_show), +KLP_FUNC_END };static struct klp_object objs[] = { - { -/* name being NULL means vmlinux */ - .funcs = funcs, - }, { } +KLP_VMLINUX (funcs), +KLP_OBJECT_END };static struct klp_patch patch = {
samples/livepatch/livepatch-shadow-fix1.c +8 -12
@@ -128,21 +128,17 @@ void livepatch_fix1_dummy_free(struct dummy *d) }static struct klp_func funcs[] = { - { - .old_name ="dummy_alloc" , - .new_addr = (unsigned long )livepatch_fix1_dummy_alloc, - }, - { - .old_name ="dummy_free" , - .new_addr = (unsigned long )livepatch_fix1_dummy_free, - }, { } +KLP_FUNC (dummy_alloc, + livepatch_fix1_dummy_alloc), +KLP_FUNC (dummy_free, + livepatch_fix1_dummy_free), +KLP_FUNC_END };static struct klp_object objs[] = { - { - .name ="livepatch_shadow_mod" , - .funcs = funcs, - }, { } +KLP_OBJECT (livepatch_shadow_mod, + funcs), +KLP_OBJECT_END };static struct klp_patch patch = {
samples/livepatch/livepatch-shadow-fix2.c +8 -12
@@ -105,21 +105,17 @@ void livepatch_fix2_dummy_free(struct dummy *d) }static struct klp_func funcs[] = { - { - .old_name ="dummy_check" , - .new_addr = (unsigned long )livepatch_fix2_dummy_check, - }, - { - .old_name ="dummy_free" , - .new_addr = (unsigned long )livepatch_fix2_dummy_free, - }, { } +KLP_FUNC (dummy_check, + livepatch_fix2_dummy_check), +KLP_FUNC (dummy_free, + livepatch_fix2_dummy_free), +KLP_FUNC_END };static struct klp_object objs[] = { - { - .name ="livepatch_shadow_mod" , - .funcs = funcs, - }, { } +KLP_OBJECT (livepatch_shadow_mod, + funcs), +KLP_OBJECT_END };static struct klp_patch patch = {
Signature · 1 line
2.13.7
Petr Mladek
maintainerPATCH v12 3/12 livepatch: Shuffle klp_enable_patch()/klp_disable_patch() code
In reply to Petr MladekWe are going to simplify the API and code by removing the registration step. This would require calling init/free functions from enable/disable ones.
This patch just moves the code the code to prevent more forward declarations.
This patch does not change the code except of two forward declarations.
--- kernel/livepatch/core.c | 330 ++++++++++++++++++++++++------------------------ 1 file changed, 166 insertions(+), 164 deletions(-)
kernel/livepatch/core.c +166 -164
@@ -278,170 +278,6 @@ static int klp_write_object_relocations(struct module *pmod,return ret; } -static int __klp_disable_patch (struct klp_patch *patch) -{ -struct klp_object *obj; - -if (WARN_ON (!patch->enabled)) -return -EINVAL ; - -if (klp_transition_patch) -return -EBUSY ; - -/* enforce stacking: only the last enabled patch can be disabled */ -if (!list_is_last (&patch->list, &klp_patches) && -list_next_entry (patch, list)->enabled) -return -EBUSY ; - -klp_init_transition (patch,KLP_UNPATCHED ); - -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 -* klp_start_transition(). In the rare case where klp_ftrace_handler() -* is called shortly after klp_update_patch_state() switches the task, -* this ensures the handler sees that func->transition is set. -*/ -smp_wmb (); - -klp_start_transition (); -klp_try_complete_transition (); - patch->enabled = false; - -return 0 ; -} - -/** -* klp_disable_patch() - disables a registered patch -* @patch: The registered, enabled patch to be disabled -* -* Unregisters the patched functions from ftrace. -* -* Return: 0 on success, otherwise error -*/ -int klp_disable_patch (struct klp_patch *patch) -{ -int ret; - -mutex_lock (&klp_mutex); - -if (!klp_is_patch_registered (patch)) { - ret = -EINVAL ; - goto err; - } - -if (!patch->enabled) { - ret = -EINVAL ; - goto err; - } - - ret =__klp_disable_patch (patch); - -err: -mutex_unlock (&klp_mutex); -return ret; -} -EXPORT_SYMBOL_GPL (klp_disable_patch); - -static int __klp_enable_patch (struct klp_patch *patch) -{ -struct klp_object *obj; -int ret; - -if (klp_transition_patch) -return -EBUSY ; - -if (WARN_ON (patch->enabled)) -return -EINVAL ; - -/* enforce stacking: only the first disabled patch can be enabled */ -if (patch->list.prev != &klp_patches && - !list_prev_entry (patch, list)->enabled) -return -EBUSY ; - -/* -* A reference is taken on the patch module to prevent it from being -* unloaded. -*/ -if (!try_module_get (patch->mod)) -return -ENODEV ; - -pr_notice ("enabling patch '%s'\n" , patch->mod->name); - -klp_init_transition (patch,KLP_PATCHED ); - -/* -* Enforce the order of the func->transition writes in -* klp_init_transition() and the ops->func_stack writes in -* klp_patch_object(), so that klp_ftrace_handler() will see the -* func->transition updates before the handler is registered and the -* new funcs become visible to the handler. -*/ -smp_wmb (); - -klp_for_each_object (patch, obj) { -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; - } - - ret =klp_patch_object (obj); -if (ret) { -pr_warn ("failed to patch object '%s'\n" , -klp_is_module (obj) ? obj->name :"vmlinux" ); - goto err; - } - } - -klp_start_transition (); -klp_try_complete_transition (); - patch->enabled = true; - -return 0 ; -err: -pr_warn ("failed to enable patch '%s'\n" , patch->mod->name); - -klp_cancel_transition (); -return ret; -} - -/** -* klp_enable_patch() - enables a registered patch -* @patch: The registered, disabled patch to be enabled -* -* Performs the needed symbol lookups and code relocations, -* then registers the patched functions with ftrace. -* -* Return: 0 on success, otherwise error -*/ -int klp_enable_patch (struct klp_patch *patch) -{ -int ret; - -mutex_lock (&klp_mutex); - -if (!klp_is_patch_registered (patch)) { - ret = -EINVAL ; - goto err; - } - - ret =__klp_enable_patch (patch); - -err: -mutex_unlock (&klp_mutex); -return ret; -} -EXPORT_SYMBOL_GPL (klp_enable_patch); -/* * Sysfs Interface *
@@ -454,6 +290,8 @@ EXPORT_SYMBOL_GPL(klp_enable_patch);* /sys/kernel/livepatch/<patch>/<object> * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> */ +static int __klp_disable_patch (struct klp_patch *patch); +static int __klp_enable_patch (struct klp_patch *patch);static ssize_t enabled_store (struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t count)
@@ -904,6 +742,170 @@ int klp_register_patch(struct klp_patch *patch) }EXPORT_SYMBOL_GPL (klp_register_patch); +static int __klp_disable_patch (struct klp_patch *patch) +{ +struct klp_object *obj; + +if (WARN_ON (!patch->enabled)) +return -EINVAL ; + +if (klp_transition_patch) +return -EBUSY ; + +/* enforce stacking: only the last enabled patch can be disabled */ +if (!list_is_last (&patch->list, &klp_patches) && +list_next_entry (patch, list)->enabled) +return -EBUSY ; + +klp_init_transition (patch,KLP_UNPATCHED ); + +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 +* klp_start_transition(). In the rare case where klp_ftrace_handler() +* is called shortly after klp_update_patch_state() switches the task, +* this ensures the handler sees that func->transition is set. +*/ +smp_wmb (); + +klp_start_transition (); +klp_try_complete_transition (); + patch->enabled = false; + +return 0 ; +} + +/** +* klp_disable_patch() - disables a registered patch +* @patch: The registered, enabled patch to be disabled +* +* Unregisters the patched functions from ftrace. +* +* Return: 0 on success, otherwise error +*/ +int klp_disable_patch (struct klp_patch *patch) +{ +int ret; + +mutex_lock (&klp_mutex); + +if (!klp_is_patch_registered (patch)) { + ret = -EINVAL ; + goto err; + } + +if (!patch->enabled) { + ret = -EINVAL ; + goto err; + } + + ret =__klp_disable_patch (patch); + +err: +mutex_unlock (&klp_mutex); +return ret; +} +EXPORT_SYMBOL_GPL (klp_disable_patch); + +static int __klp_enable_patch (struct klp_patch *patch) +{ +struct klp_object *obj; +int ret; + +if (klp_transition_patch) +return -EBUSY ; + +if (WARN_ON (patch->enabled)) +return -EINVAL ; + +/* enforce stacking: only the first disabled patch can be enabled */ +if (patch->list.prev != &klp_patches && + !list_prev_entry (patch, list)->enabled) +return -EBUSY ; + +/* +* A reference is taken on the patch module to prevent it from being +* unloaded. +*/ +if (!try_module_get (patch->mod)) +return -ENODEV ; + +pr_notice ("enabling patch '%s'\n" , patch->mod->name); + +klp_init_transition (patch,KLP_PATCHED ); + +/* +* Enforce the order of the func->transition writes in +* klp_init_transition() and the ops->func_stack writes in +* klp_patch_object(), so that klp_ftrace_handler() will see the +* func->transition updates before the handler is registered and the +* new funcs become visible to the handler. +*/ +smp_wmb (); + +klp_for_each_object (patch, obj) { +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; + } + + ret =klp_patch_object (obj); +if (ret) { +pr_warn ("failed to patch object '%s'\n" , +klp_is_module (obj) ? obj->name :"vmlinux" ); + goto err; + } + } + +klp_start_transition (); +klp_try_complete_transition (); + patch->enabled = true; + +return 0 ; +err: +pr_warn ("failed to enable patch '%s'\n" , patch->mod->name); + +klp_cancel_transition (); +return ret; +} + +/** +* klp_enable_patch() - enables a registered patch +* @patch: The registered, disabled patch to be enabled +* +* Performs the needed symbol lookups and code relocations, +* then registers the patched functions with ftrace. +* +* Return: 0 on success, otherwise error +*/ +int klp_enable_patch (struct klp_patch *patch) +{ +int ret; + +mutex_lock (&klp_mutex); + +if (!klp_is_patch_registered (patch)) { + ret = -EINVAL ; + goto err; + } + + ret =__klp_enable_patch (patch); + +err: +mutex_unlock (&klp_mutex); +return ret; +} +EXPORT_SYMBOL_GPL (klp_enable_patch); +/* * Remove parts of patches that touch a given kernel module. The list of * patches processed might be limited. When limit is NULL, all patches
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 03/12] livepatch: Shuffle klp_enable_patch()/klp_disable_patch() code
Quoted 6 lines · Petr Mladek
We are going to simplify the API and code by removing the registration step. This would require calling init/free functions from enable/disable ones.
This patch just moves the code the code to prevent more forward declarations.
s/the code the code/the code/
Quoted 3 lines
This patch does not change the code except of two forward declarations.
Signed-off-by Petr Mladek <[email protected]>
Miroslav
Petr Mladek
maintainerPATCH v12 4/12 livepatch: Consolidate klp_free functions
In reply to Petr MladekThe code for freeing livepatch structures is a bit scattered and tricky:
+ direct calls to klp_free_*_limited() and kobject_put() are used to release partially initialized objects
+ klp_free_patch() removes the patch from the public list
and releases all objects except for patch->kobj
+ object_put(&patch->kobj) and the related wait_for_completion()
are called directly outside klp_mutex; this code is duplicated;
Now, we are going to remove the registration stage to simplify the API and the code. This would require handling more situations in klp_enable_patch() error paths.
More importantly, we are going to add a feature called atomic replace. It will need to dynamically create func and object structures. We will want to reuse the existing init() and free() functions. This would create even more error path scenarios.
This patch implements a more clever free functions:
+ checks kobj.state_initialized instead of @limit
+ initializes patch->list early so that the check for empty list always works
+ The action(s) that has to be done outside klp_mutex are done in separate klp_free_patch_end() function. It waits only when patch->kobj was really released via the _begin() part.
Note that it is safe to put patch->kobj under klp_mutex. It calls the release callback only when the reference count reaches zero. Therefore it does not block any related sysfs operation that took a reference and might eventually wait for klp_mutex.
Note that __klp_free_patch() is split because it will be later used in a _nowait() variant. Also klp_free_patch_end() makes sense because it will later get more complicated.
This patch does not change the existing behavior.
--- include/linux/livepatch.h | 2 ++ kernel/livepatch/core.c | 92 +++++++++++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 35 deletions(-)
include/linux/livepatch.h +2 -0
@@ -138,6 +138,7 @@ struct klp_object {* @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @enabled: the patch is enabled (but operation may be incomplete) +* @wait_free: wait until the patch is freed * @finish: for waiting till it is safe to remove the patch module */ struct klp_patch {
@@ -149,6 +150,7 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; +bool wait_free;struct completion finish; };
kernel/livepatch/core.c +57 -35
150 lines not on this page · raw message
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 04/12] livepatch: Consolidate klp_free functions
Quoted 27 lines · Petr Mladek
The code for freeing livepatch structures is a bit scattered and tricky:
+ direct calls to klp_free_*_limited() and
kobject_put()are used to release partially initialized objects+
klp_free_patch()removes the patch from the public listand releases all objects except for patch->kobj+
object_put(&patch->kobj)and the relatedwait_for_completion()are called directly outside klp_mutex; this code is duplicated;Now, we are going to remove the registration stage to simplify the API and the code. This would require handling more situations in
klp_enable_patch()error paths.More importantly, we are going to add a feature called atomic replace. It will need to dynamically create func and object structures. We will want to reuse the existing
init()andfree()functions. This would create even more error path scenarios.This patch implements a more clever free functions:
+ checks kobj.state_initialized instead of @limit
+ initializes
patch->listearly so that the check for empty list always works
Quoted 8 lines
+ The action(s) that has to be done outside
klp_mutexare done in separateklp_free_patch_end()function. It waits only whenpatch->kobjwas really released via the_begin()part.Note that it is safe to put
patch->kobjunderklp_mutex. It calls the release callback only when the reference count reaches zero. Therefore it does not block any related sysfs operation that took a reference and might eventually wait forklp_mutex.
This seems to be the reason of the issue which lockdep reported. The patch moved kobject_put(&patch->kobj) under klp_mutex. Perhaps I cannot read kernfs code properly today, but I fail to understand why it is supposed to be safe.
Indeed, if it is safe, lockdep report is false positive.
Quoted 3 lines
Note that
__klp_free_patch()is split because it will be later used in a_nowait()variant. Alsoklp_free_patch_end()makes sense because it will later get more complicated.
There are no _begin() and _end() functions in the patch.
Quoted 8 lines
This patch does not change the existing behavior.
Signed-off-by Petr Mladek <[email protected]>Cc Josh Poimboeuf <[email protected]>Cc Jessica Yu <[email protected]>Cc Jiri Kosina <[email protected]>Cc Jason Baron <[email protected]>Acked-by Miroslav Benes <[email protected]>
My Acked-by here is a bit premature.
Quoted 41 lines
--- include/linux/livepatch.h | 2 ++ kernel/livepatch/core.c | 92 +++++++++++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 35 deletions(-)include/linux/livepatch.h +2
-0@@ -138,6 +138,7 @@ struct klp_object {* @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @enabled: the patch is enabled (but operation may be incomplete) +* @wait_free: wait until the patch is freed * @finish: for waiting till it is safe to remove the patch module */ struct klp_patch {@@ -149,6 +150,7 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; +bool wait_free;struct completion finish; };kernel/livepatch/core.c +1
-6@@ -465,17 +465,15 @@ static struct kobj_type klp_ktype_func = { .sysfs_ops = &kobj_sysfs_ops, }; -/* -* Free all functions' kobjects in the array up to some limit. When limit is -* NULL, all kobjects are freed. -*/ -static void klp_free_funcs_limited (struct klp_object *obj, -struct klp_func *limit) +static void klp_free_funcs (struct klp_object *obj) {
kernel/livepatch/core.c static struct kobj_type klp_ktype_func = { · line 477
Quoted 10 lines
kernel/livepatch/core.c +5
-2struct klp_func *func; -for (func = obj->funcs; func->old_name && func != limit; func++) -kobject_put (&func->kobj); +klp_for_each_func (obj, func) { +/* Might be called from klp_init_patch() error path. */ +if (func->kobj.state_initialized) +kobject_put (&func->kobj); + } }
Just for the record, it is a slightly suboptimal because now we iterate through the whole list. We could add break to else branch, I think, but it's not necessary.
Quoted 16 lines
kernel/livepatch/core.c +1
-7/* Clean up when a patched object is unloaded */ @@ -489,26 +487,59 @@ static void klp_free_object_loaded(struct klp_object *obj) func->old_addr =0 ; } -/* -* Free all objects' kobjects in the array up to some limit. When limit is -* NULL, all kobjects are freed. -*/ -static void klp_free_objects_limited (struct klp_patch *patch, -struct klp_object *limit) +static void klp_free_objects (struct klp_patch *patch) {struct klp_object *obj; -for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
kernel/livepatch/core.c static void klp_free_object_loaded(struct klp_object *obj) · line 501
Quoted 10 lines
kernel/livepatch/core.c +6
-2-klp_free_funcs_limited (obj,NULL ); -kobject_put (&obj->kobj); +klp_for_each_object (patch, obj) { +klp_free_funcs (obj); + +/* Might be called from klp_init_patch() error path. */ +if (obj->kobj.state_initialized) +kobject_put (&obj->kobj); } }
Same here, of course.
Regards,
Miroslav
Petr Mladek
maintainerRe: [PATCH v12 04/12] livepatch: Consolidate klp_free functions
Quoted 38 lines · Miroslav Benes
Quoted 27 lines · Petr Mladek
The code for freeing livepatch structures is a bit scattered and tricky:
+ direct calls to klp_free_*_limited() and
kobject_put()are used to release partially initialized objects+
klp_free_patch()removes the patch from the public listand releases all objects except for patch->kobj+
object_put(&patch->kobj)and the relatedwait_for_completion()are called directly outside klp_mutex; this code is duplicated;Now, we are going to remove the registration stage to simplify the API and the code. This would require handling more situations in
klp_enable_patch()error paths.More importantly, we are going to add a feature called atomic replace. It will need to dynamically create func and object structures. We will want to reuse the existing
init()andfree()functions. This would create even more error path scenarios.This patch implements a more clever free functions:
+ checks kobj.state_initialized instead of @limit
+ initializes
patch->listearly so that the check for empty list always worksQuoted 8 lines
+ The action(s) that has to be done outside
klp_mutexare done in separateklp_free_patch_end()function. It waits only whenpatch->kobjwas really released via the_begin()part.Note that it is safe to put
patch->kobjunderklp_mutex. It calls the release callback only when the reference count reaches zero. Therefore it does not block any related sysfs operation that took a reference and might eventually wait forklp_mutex.
Quoted 4 lines
This seems to be the reason of the issue which lockdep reported. The patch moved
kobject_put(&patch->kobj)underklp_mutex. Perhaps I cannot read kernfs code properly today, but I fail to understand why it is supposed to be safe.
My expectation was that any read/write operation on the related sysfs interface took reference of the kobject. Then kobject_put() would just decrement a reference counter and postpone the real removal until all other operations were finished.
But it seems that the read/write operations take reference on another (kernfs_node) object and do not block releasing the kobject by kobject_put().
Quoted 18 lines
Quoted 18 lines
kernel/livepatch/core.c +1
-6@@ -465,17 +465,15 @@ static struct kobj_type klp_ktype_func = { .sysfs_ops = &kobj_sysfs_ops, }; -/* -* Free all functions' kobjects in the array up to some limit. When limit is -* NULL, all kobjects are freed. -*/ -static void klp_free_funcs_limited (struct klp_object *obj, -struct klp_func *limit) +static void klp_free_funcs (struct klp_object *obj) {struct klp_func *func;
Quoted 12 lines
kernel/livepatch/core.c static struct kobj_type klp_ktype_func = { · line 477
Quoted 8 lines
kernel/livepatch/core.c +5
-2-for (func = obj->funcs; func->old_name && func != limit; func++) -kobject_put (&func->kobj); +klp_for_each_func (obj, func) { +/* Might be called from klp_init_patch() error path. */ +if (func->kobj.state_initialized) +kobject_put (&func->kobj); + } }Just for the record, it is a slightly suboptimal because now we iterate through the whole list. We could add break to else branch, I think, but it's not necessary.
Interesting optimization. It would keep the limit and work at this stage.
But it would stop working once we add the dynamically allocated structures. They are allocated and initialized in two separate cycles. We need to free all allocated structures when any initialization fails.
Best Regards,
Petr
Petr Mladek
maintainerPATCH v12 5/12 livepatch: Refuse to unload only livepatches available during a forced transition
In reply to Petr Mladekmodule_put() is currently never called in klp_complete_transition() when klp_force is set. As a result, we might keep the reference count even when klp_enable_patch() fails and klp_cancel_transition() is called.
This might make an assumption that a module might get blocked in some strange init state. Fortunately, it is not the case. The reference count is ignored when mod->init fails and erroneous modules are always removed.
Anyway, this might make some confusion. Instead, this patch moves the global klp_forced flag into struct klp_patch. As a result, we block only modules that might still be in use after a forced transition. Newly loaded livepatches might be eventually completely removed later.
It is not a big deal. But the code is at least consistent with the reality.
--- include/linux/livepatch.h | 2 ++ kernel/livepatch/core.c | 4 +++- kernel/livepatch/core.h | 1 + kernel/livepatch/transition.c | 10 +++++----- 4 files changed, 11 insertions(+), 6 deletions(-)
include/linux/livepatch.h +2 -0
@@ -138,6 +138,7 @@ struct klp_object {* @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @enabled: the patch is enabled (but operation may be incomplete) +* @forced: was involved in a forced transition * @wait_free: wait until the patch is freed * @finish: for waiting till it is safe to remove the patch module */
@@ -150,6 +151,7 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; +bool forced;bool wait_free;struct completion finish; };
kernel/livepatch/core.c +3 -1
@@ -45,7 +45,8 @@*/ DEFINE_MUTEX (klp_mutex); -static LIST_HEAD (klp_patches); +/* Registered patches */ +LIST_HEAD (klp_patches);static struct kobject *klp_root_kobj;
@@ -660,6 +661,7 @@ static int klp_init_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); patch->enabled = false; + patch->forced = false;INIT_LIST_HEAD (&patch->list);init_completion (&patch->finish);
kernel/livepatch/core.h +1 -0
@@ -5,6 +5,7 @@#include <linux/livepatch.h> extern struct mutex klp_mutex; +extern struct list_head klp_patches;static inline bool klp_is_object_loaded (struct klp_object *obj) {
kernel/livepatch/transition.c +5 -5
@@ -33,8 +33,6 @@ struct klp_patch *klp_transition_patch;static int klp_target_state =KLP_UNDEFINED ; -static bool klp_forced = false; -/* * This work can be performed periodically to finish patching or unpatching any * "straggler" tasks which failed to transition in the first attempt.
@@ -137,10 +135,10 @@ static void klp_complete_transition(void) klp_target_state == KLP_PATCHED ? "patching" : "unpatching");/* -* klp_forced set implies unbounded increase of module's ref count if +* patch->forced set implies unbounded increase of module's ref count if * the module is disabled/enabled in a loop. */ -if (!klp_forced && klp_target_state ==KLP_UNPATCHED ) +if (!klp_transition_patch->forced && klp_target_state ==KLP_UNPATCHED )module_put (klp_transition_patch->mod); klp_target_state =KLP_UNDEFINED ;
@@ -620,6 +618,7 @@ void klp_send_signals(void)*/ void klp_force_transition (void ) { +struct klp_patch *patch;struct task_struct *g, *task;unsigned int cpu;
@@ -633,5 +632,6 @@ void klp_force_transition(void)for_each_possible_cpu (cpu)klp_update_patch_state (idle_task (cpu)); - klp_forced = true; +list_for_each_entry (patch, &klp_patches, list) + patch->forced = true; }
Signature · 1 line
2.13.7
Petr Mladek
maintainerPATCH v12 6/12 livepatch: Simplify API by removing registration step
In reply to Petr MladekThe possibility to re-enable a registered patch was useful for immediate patches where the livepatch module had to stay until the system reboot. The improved consistency model allows to achieve the same result by unloading and loading the livepatch module again.
Also we are going to add a feature called atomic replace. It will allow to create a patch that would replace all already registered patches. The aim is to handle dependent patches a more secure way. It will obsolete the stack of patches that helped to handle the dependencies so far. Then it might be unclear when a cumulative patch re-enabling is safe.
It would be complicated to support the many modes. Instead we could actually make the API and code easier.
This patch removes the two step public API. All the checks and init calls are moved from klp_register_patch() to klp_enabled_patch(). Also the patch is automatically freed, including the sysfs interface when the transition to the disabled state is completed.
As a result, there is newer a disabled patch on the top of the stack. Therefore we do not need to check the stack in __klp_enable_patch(). And we could simplify the check in __klp_disable_patch().
Also the API and logic is much easier. It is enough to call klp_enable_patch() in module_init() call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.
IMPORTANT: We only need to be really careful about when and where to call module_put(). It has to be called only when:
+ the reference was taken before
+ the module structures and code will not longer be accessed
Now, the disable operation is triggered from the sysfs interface. We clearly could not wait there until the interface is destroyed. Instead we need to call module_put() in the release callback of patch->kobj. It is safe because:
+ Patch could not longer get re-enabled from enabled_store().
+ kobjects are designed to be part of structures that are freed from the release callback. We just need to make sure that module_put() is the last call accessing the patch in the callback.
In theory, we could be more relaxed in klp_enable_patch() error paths because they are called from the module_init(). But better be on the safe side.
This patch does the following to keep the code sane:
+ patch->forced is replaced with patch->module_put and an inverted logic. Then we could call it in klp_enable_patch() error path even before the reference is taken.
+ try_module_get() is called before initializing patch->kobj. It makes it more symmetric with the moved module_put().
+ module_put() is the last action also in klp_free_patch_sync_end(). It makes it safe for an use outside module_init().
--- Documentation/livepatch/livepatch.txt | 121 +++++------- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 280 ++++++++++----------------- kernel/livepatch/core.h | 2 + kernel/livepatch/transition.c | 15 +- samples/livepatch/livepatch-callbacks-demo.c | 13 +- samples/livepatch/livepatch-sample.c | 13 +- samples/livepatch/livepatch-shadow-fix1.c | 14 +- samples/livepatch/livepatch-shadow-fix2.c | 14 +- 9 files changed, 157 insertions(+), 322 deletions(-)
Documentation/livepatch/livepatch.txt +44 -77
@@ -14,10 +14,8 @@ Table of Contents: 4.2. Metadata 4.3. Livepatch module handling 5. Livepatch life-cycle - 5.1. Registration - 5.2. Enabling - 5.3. Disabling - 5.4. Unregistration + 5.1. Enabling + 5.2. Disabling 6. Sysfs 7. Limitations
@@ -303,9 +301,8 @@ into three levels: The usual behavior is that the new functions will get used when the livepatch module is loaded. For this, the module init() function -has to register the patch (struct klp_patch) and enable it. See the -section "Livepatch life-cycle" below for more details about these -two operations. +has to enable the patch (struct klp_patch). See the section "Livepatch +life-cycle" below for more details. Module removal is only safe when there are no users of the underlying functions. This is the reason why the force feature permanently disables
@@ -319,96 +316,66 @@ forced it is guaranteed that no task sleeps or runs in the old code. 5. Livepatch life-cycle ======================= -Livepatching defines four basic operations that define the life cycle of each -live patch: registration, enabling, disabling and unregistration. There are -several reasons why it is done this way. +Livepatches get automatically enabled when the respective module is loaded. +On the other hand, the module can be removed only after the patch was +successfully disabled via the sysfs interface. -First, the patch is applied only when all patched symbols for already -loaded objects are found. The error handling is much easier if this -check is done before particular functions get redirected. -Second, it might take some time until the entire system is migrated with -the hybrid consistency model being used. The patch revert might block -the livepatch module removal for too long. Therefore it is useful to -revert the patch using a separate operation that might be called -explicitly. But it does not make sense to remove all information until -the livepatch module is really removed. - - -5.1. Registration ------------------ - -Each patch first has to be registered using klp_register_patch(). This makes -the patch known to the livepatch framework. Also it does some preliminary -computing and checks. - -In particular, the patch is added into the list of known patches. The -addresses of the patched functions are found according to their names. -The special relocations, mentioned in the section "New functions", are -applied. The relevant entries are created under -/sys/kernel/livepatch/<name>. The patch is rejected when any operation -fails. - - -5.2. Enabling +5.1. Enabling ------------- -Registered patches might be enabled either by calling klp_enable_patch() or -by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will -start using the new implementation of the patched functions at this stage. +Livepatch modules have to call klp_enable_patch() in module_init() callback. +This function is rather complex and might even fail in the early phase. -When a patch is enabled, livepatch enters into a transition state where -tasks are converging to the patched state. This is indicated by a value -of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks have -been patched, the 'transition' value changes to '0'. For more -information about this process, see the "Consistency model" section. +First, the addresses of the patched functions are found according to their +names. The special relocations, mentioned in the section "New functions", +are applied. The relevant entries are created under +/sys/kernel/livepatch/<name>. The patch is rejected when any above +operation fails. -If an original function is patched for the first time, a function -specific struct klp_ops is created and an universal ftrace handler is -registered. +Third, livepatch enters into a transition state where tasks are converging +to the patched state. If an original function is patched for the first +time, a function specific struct klp_ops is created and an universal +ftrace handler is registered[*]. This stage is indicated by a value of '1' +in /sys/kernel/livepatch/<name>/transition. For more information about +this process, see the "Consistency model" section. -Functions might be patched multiple times. The ftrace handler is registered -only once for the given function. Further patches just add an entry to the -list (see field `func_stack`) of the struct klp_ops. The last added -entry is chosen by the ftrace handler and becomes the active function -replacement. +Finally, once all tasks have been patched, the 'transition' value changes +to '0'. -Note that the patches might be enabled in a different order than they were -registered. +[*] Note that functions might be patched multiple times. The ftrace handler + is registered only once for a given function. Further patches just add + an entry to the list (see field `func_stack`) of the struct klp_ops. + The right implementation is selected by the ftrace handler, see + the "Consistency model" section. -5.3. Disabling +5.2. Disabling -------------- -Enabled patches might get disabled either by calling klp_disable_patch() or -by writing '0' to /sys/kernel/livepatch/<name>/enabled. At this stage -either the code from the previously enabled patch or even the original -code gets used. +Enabled patches might get disabled by writing '0' to +/sys/kernel/livepatch/<name>/enabled. -When a patch is disabled, livepatch enters into a transition state where -tasks are converging to the unpatched state. This is indicated by a -value of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks -have been unpatched, the 'transition' value changes to '0'. For more -information about this process, see the "Consistency model" section. +First, livepatch enters into a transition state where tasks are converging +to the unpatched state. The system starts using either the code from +the previously enabled patch or even the original one. This stage is +indicated by a value of '1' in /sys/kernel/livepatch/<name>/transition. +For more information about this process, see the "Consistency model" +section. -Here all the functions (struct klp_func) associated with the to-be-disabled +Second, once all tasks have been unpatched, the 'transition' value changes +to '0'. All the functions (struct klp_func) associated with the to-be-disabled patch are removed from the corresponding struct klp_ops. The ftrace handler is unregistered and the struct klp_ops is freed when the func_stack list becomes empty. -Patches must be disabled in exactly the reverse order in which they were -enabled. It makes the problem and the implementation much easier. - - -5.4. Unregistration -------------------- +Third, the sysfs interface is destroyed. -Disabled patches might be unregistered by calling klp_unregister_patch(). -This can be done only when the patch is disabled and the code is no longer -used. It must be called before the livepatch module gets unloaded. +Finally, the module can be removed if the transition was not forced and the +last sysfs entry has gone. -At this stage, all the relevant sys-fs entries are removed and the patch -is removed from the list of known patches. +Note that patches must be disabled in exactly the reverse order in which +they were enabled. It makes the problem and the implementation much easier. 6. Sysfs
include/linux/livepatch.h +2 -5
@@ -138,8 +138,8 @@ struct klp_object {* @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @enabled: the patch is enabled (but operation may be incomplete) -* @forced: was involved in a forced transition * @wait_free: wait until the patch is freed +* @module_put: module reference taken and patch not forced * @finish: for waiting till it is safe to remove the patch module */ struct klp_patch {
@@ -151,8 +151,8 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; -bool forced;bool wait_free; +bool module_put;struct completion finish; };
@@ -204,10 +204,7 @@ struct klp_patch { func->old_name || func->new_addr || func->old_sympos; \ func++) -int klp_register_patch (struct klp_patch *); -int klp_unregister_patch (struct klp_patch *);int klp_enable_patch (struct klp_patch *); -int klp_disable_patch (struct klp_patch *);void arch_klp_init_object_loaded (struct klp_patch *patch,struct klp_object *obj);
kernel/livepatch/core.c +98 -182
409 lines not on this page · raw message
kernel/livepatch/core.h +2 -0
@@ -7,6 +7,8 @@extern struct mutex klp_mutex;extern struct list_head klp_patches; +void klp_free_patch_nowait (struct klp_patch *patch); +static inline bool klp_is_object_loaded (struct klp_object *obj) {return !obj->name || obj->mod;
kernel/livepatch/transition.c +7 -8
@@ -134,13 +134,6 @@ static void klp_complete_transition(void)pr_notice ("'%s': %s complete\n" , klp_transition_patch->mod->name, klp_target_state ==KLP_PATCHED ?"patching" :"unpatching" ); -/* -* patch->forced set implies unbounded increase of module's ref count if -* the module is disabled/enabled in a loop. -*/ -if (!klp_transition_patch->forced && klp_target_state ==KLP_UNPATCHED ) -module_put (klp_transition_patch->mod); - klp_target_state =KLP_UNDEFINED ; klp_transition_patch =NULL ; }
@@ -357,6 +350,7 @@ void klp_try_complete_transition(void) {unsigned int cpu;struct task_struct *g, *task; +struct klp_patch *patch;bool complete = true;WARN_ON_ONCE (klp_target_state ==KLP_UNDEFINED );
@@ -405,7 +399,11 @@ void klp_try_complete_transition(void) }/* we're done, now cleanup the data structures */ + patch = klp_transition_patch;klp_complete_transition (); + +if (!patch->enabled) +klp_free_patch_nowait (patch); }/*
@@ -632,6 +630,7 @@ void klp_force_transition(void)for_each_possible_cpu (cpu)klp_update_patch_state (idle_task (cpu)); +/* Refuse unloading all livepatches. The code might be in use. */ list_for_each_entry (patch, &klp_patches, list) - patch->forced = true; + patch->module_put = false; }
samples/livepatch/livepatch-callbacks-demo.c +1 -12
@@ -184,22 +184,11 @@ static struct klp_patch patch = {static int livepatch_callbacks_demo_init (void ) { -int ret; - - ret =klp_register_patch (&patch); -if (ret) -return ret; - ret =klp_enable_patch (&patch); -if (ret) { -WARN_ON (klp_unregister_patch (&patch)); -return ret; - } -return 0 ; +return klp_enable_patch (&patch); }static void livepatch_callbacks_demo_exit (void ) { -WARN_ON (klp_unregister_patch (&patch)); }module_init (livepatch_callbacks_demo_init);
samples/livepatch/livepatch-sample.c +1 -12
@@ -66,22 +66,11 @@ static struct klp_patch patch = {static int livepatch_init (void ) { -int ret; - - ret =klp_register_patch (&patch); -if (ret) -return ret; - ret =klp_enable_patch (&patch); -if (ret) { -WARN_ON (klp_unregister_patch (&patch)); -return ret; - } -return 0 ; +return klp_enable_patch (&patch); }static void livepatch_exit (void ) { -WARN_ON (klp_unregister_patch (&patch)); }module_init (livepatch_init);
samples/livepatch/livepatch-shadow-fix1.c +1 -13
@@ -148,25 +148,13 @@ static struct klp_patch patch = {static int livepatch_shadow_fix1_init (void ) { -int ret; - - ret =klp_register_patch (&patch); -if (ret) -return ret; - ret =klp_enable_patch (&patch); -if (ret) { -WARN_ON (klp_unregister_patch (&patch)); -return ret; - } -return 0 ; +return klp_enable_patch (&patch); }static void livepatch_shadow_fix1_exit (void ) {/* Cleanup any existing SV_LEAK shadow variables */ klp_shadow_free_all (SV_LEAK , livepatch_fix1_dummy_leak_dtor); - -WARN_ON (klp_unregister_patch (&patch)); }module_init (livepatch_shadow_fix1_init);
samples/livepatch/livepatch-shadow-fix2.c +1 -13
@@ -125,25 +125,13 @@ static struct klp_patch patch = {static int livepatch_shadow_fix2_init (void ) { -int ret; - - ret =klp_register_patch (&patch); -if (ret) -return ret; - ret =klp_enable_patch (&patch); -if (ret) { -WARN_ON (klp_unregister_patch (&patch)); -return ret; - } -return 0 ; +return klp_enable_patch (&patch); }static void livepatch_shadow_fix2_exit (void ) {/* Cleanup any existing SV_COUNTER shadow variables */ klp_shadow_free_all (SV_COUNTER ,NULL ); - -WARN_ON (klp_unregister_patch (&patch)); }module_init (livepatch_shadow_fix2_init);
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 8 lines · Petr Mladek
The possibility to re-enable a registered patch was useful for immediate patches where the livepatch module had to stay until the system reboot. The improved consistency model allows to achieve the same result by unloading and loading the livepatch module again.
Also we are going to add a feature called atomic replace. It will allow to create a patch that would replace all already registered patches. The aim is to handle dependent patches a more secure way. It will obsolete
"in a more secure way", or "more securely" is maybe even better.
Quoted 5 lines
the stack of patches that helped to handle the dependencies so far. Then it might be unclear when a cumulative patch re-enabling is safe.
It would be complicated to support the many modes. Instead we could actually make the API and code easier.
s/easier/simpler/ ?
or "easier to understand" ?
Quoted 6 lines
This patch removes the two step public API. All the checks and init calls are moved from
klp_register_patch()toklp_enabled_patch(). Also the patch is automatically freed, including the sysfs interface when the transition to the disabled state is completed.As a result, there is newer a disabled patch on the top of the stack.
s/newer/never/
Quoted 7 lines
Therefore we do not need to check the stack in
__klp_enable_patch(). And we could simplify the check in__klp_disable_patch().Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.
I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Maybe we'd also find out that even patch->enabled member is not useful anymore in such case.
Quoted 5 lines
Documentation/livepatch/livepatch.txt +0
-0@@ -14,10 +14,8 @@ Table of Contents:
[...]
Quoted 15 lines
-5.2. Enabling
+5.1. Enabling
-------------Documentation/livepatch/livepatch.txt +2
-8-Registered patches might be enabled either by calling klp_enable_patch() or -by writing '1' to /sys/kernel/livepatch/<name>/enabled. The system will -start using the new implementation of the patched functions at this stage. +Livepatch modules have to call klp_enable_patch() in module_init() callback. +This function is rather complex and might even fail in the early phase. -When a patch is enabled, livepatch enters into a transition state where -tasks are converging to the patched state. This is indicated by a value -of '1' in /sys/kernel/livepatch/<name>/transition. Once all tasks have -been patched, the 'transition' value changes to '0'. For more -information about this process, see the "Consistency model" section.
Documentation/livepatch/livepatch.txt forced it is guaranteed that no task sleeps or runs in the old code. · line 336
Quoted 10 lines
Documentation/livepatch/livepatch.txt +6
-3+First, the addresses of the patched functions are found according to their +names. The special relocations, mentioned in the section "New functions", +are applied. The relevant entries are created under +/sys/kernel/livepatch/<name>. The patch is rejected when any above +operation fails. -If an original function is patched for the first time, a function -specific struct klp_ops is created and an universal ftrace handler is -registered. +Third, livepatch enters into a transition state where tasks are converging
s/Third/Second/ ?
[...]
Quoted 22 lines
Documentation/livepatch/livepatch.txt +8
-6@@ -655,116 +660,38 @@ static int klp_init_patch(struct klp_patch *patch) struct klp_object *obj; int ret; - if (!patch->objs) - return -EINVAL; - - mutex_lock(&klp_mutex); - patch->enabled = false; - patch->forced = false; + patch->module_put = false; INIT_LIST_HEAD(&patch->list); init_completion(&patch->finish); + if (!patch->objs) + return -EINVAL; + + /* + * A reference is taken on the patch module to prevent it from being + * unloaded. + */
Documentation/livepatch/livepatch.txt static int klp_init_patch(struct klp_patch *patch) · line 683
Quoted 10 lines
Documentation/livepatch/livepatch.txt +4
-1+ if (!try_module_get(patch->mod)) + return -ENODEV; + patch->module_put = true; + ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, klp_root_kobj, "%s", patch->mod->name); if (ret) { - mutex_unlock(&klp_mutex); return ret; }
{ } are not necessary after the change.
[...]
Quoted 10 lines
Documentation/livepatch/livepatch.txt +0
-3static int __klp_enable_patch(struct klp_patch *patch) { struct klp_object *obj;@@ -846,17 +740,8 @@ static int __klp_enable_patch(struct klp_patch *patch) if (WARN_ON(patch->enabled)) return -EINVAL; - /* enforce stacking: only the first disabled patch can be enabled */ - if (patch->list.prev != &klp_patches && - !list_prev_entry(patch, list)->enabled)
Documentation/livepatch/livepatch.txt static int __klp_enable_patch(struct klp_patch *patch) · line 744
Quoted 10 lines
Documentation/livepatch/livepatch.txt +2
-8- return -EBUSY; - - /* - * A reference is taken on the patch module to prevent it from being - * unloaded. - */ - if (!try_module_get(patch->mod)) - return -ENODEV; + if (!patch->kobj.state_initialized) + return -EINVAL;
I think the check is not needed here. __klp_enable_patch() is called right after klp_init_patch() in klp_enable_patch().
Quoted 2 lines
Documentation/livepatch/livepatch.txt +0
-0pr_notice("enabling patch '%s'\n", patch->mod->name);
[...]
Documentation/livepatch/livepatch.txt void klp_try_complete_transition(void) · line 407
Quoted 10 lines
Documentation/livepatch/livepatch.txt +4
-0@@ -405,7 +399,11 @@ void klp_try_complete_transition(void) } /* we're done, now cleanup the data structures */ + patch = klp_transition_patch; klp_complete_transition(); + + if (!patch->enabled) + klp_free_patch_nowait(patch); }
I'd welcome a comment here. I thought it was more logical to call klp_free_patch_nowait() in klp_complete_transition(). It's not possible though. klp_complete_transition() is also called from klp_cancel_transition() which has its own freeing in klp_enable_patch()'s error path.
Regards,
Miroslav
Petr Mladek
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 12 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Quoted 5 lines
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.
I though about this as well. I kept "enabled" because:
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
Quoted 4 lines
Quoted 4 lines
kernel/livepatch/core.c +0
-0@@ -846,17 +740,8 @@ static int __klp_enable_patch(struct klp_patch *patch)if (WARN_ON (patch->enabled))return -EINVAL ;
Quoted 16 lines
kernel/livepatch/core.c static int __klp_enable_patch(struct klp_patch *patch) · line 744
Quoted 13 lines
kernel/livepatch/core.c +2
-11-/* enforce stacking: only the first disabled patch can be enabled */ -if (patch->list.prev != &klp_patches && - !list_prev_entry (patch, list)->enabled) -return -EBUSY ; - -/* -* A reference is taken on the patch module to prevent it from being -* unloaded. -*/ -if (!try_module_get (patch->mod)) -return -ENODEV ; +if (!patch->kobj.state_initialized) +return -EINVAL ;I think the check is not needed here.
__klp_enable_patch()is called right afterklp_init_patch()inklp_enable_patch().
I would keep it. Someone might want to call this also from other location. Even we used to do it from enable_store() ;-)
Best Regards,
Petr
Miroslav Benes
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 16 lines · Petr Mladek
Quoted 15 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Quoted 8 lines
Quoted 2 lines
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.I though about this as well. I kept "enabled" because:
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
Well our sysfs interface is still in a testing phase as far as ABI is involved. Moreover, each live patch is bound to its base kernel by definition anyway. So we can change this without remorse, I think.
Quoted 6 lines
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
I still think it would be better to have a less confusing interface and it would outweigh the second remark.
Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
kernel/livepatch/core.c +0
-0@@ -846,17 +740,8 @@ static int __klp_enable_patch(struct klp_patch *patch)if (WARN_ON (patch->enabled))return -EINVAL ;Quoted 16 lines
kernel/livepatch/core.c static int __klp_enable_patch(struct klp_patch *patch) · line 744
Quoted 13 lines
kernel/livepatch/core.c +2
-11-/* enforce stacking: only the first disabled patch can be enabled */ -if (patch->list.prev != &klp_patches && - !list_prev_entry (patch, list)->enabled) -return -EBUSY ; - -/* -* A reference is taken on the patch module to prevent it from being -* unloaded. -*/ -if (!try_module_get (patch->mod)) -return -ENODEV ; +if (!patch->kobj.state_initialized) +return -EINVAL ;I think the check is not needed here.
__klp_enable_patch()is called right afterklp_init_patch()inklp_enable_patch().
Quoted 2 lines
I would keep it. Someone might want to call this also from other location. Even we used to do it from
enable_store();-)
Ok, I don't mind in the end.
Miroslav
Petr Mladek
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 38 lines · Miroslav Benes
Quoted 16 lines · Petr Mladek
Quoted 15 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Quoted 8 lines
Quoted 2 lines
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.I though about this as well. I kept "enabled" because:
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
Well our sysfs interface is still in a testing phase as far as ABI is involved. Moreover, each live patch is bound to its base kernel by definition anyway. So we can change this without remorse, I think.
Quoted 6 lines
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
Quoted 2 lines
I still think it would be better to have a less confusing interface and it would outweigh the second remark.
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
Best Regards,
Petr
Re: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 24 lines · Petr Mladek
Quoted 23 lines · Miroslav Benes
Quoted 21 lines · Petr Mladek
Quoted 12 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Quoted 5 lines
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.I though about this as well. I kept "enabled" because:
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
Well our sysfs interface is still in a testing phase as far as ABI is involved. Moreover, each live patch is bound to its base kernel by definition anyway. So we can change this without remorse, I think.
But it would break tooling, which is not kernel specific. I'm not sure whether it would be worth the headache. After all I think the livepatch sysfs interface is designed for tools, not humans.
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
I still think it would be better to have a less confusing interface and it would outweigh the second remark.
Quoted 7 lines
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
(Apologies if we've already talked about this. My brain is still mushy thanks to Spectre and friends.)
The amount of flexibility we allow is kind of crazy, considering how delicate of an operation live patching is. That reminds me that I should bring up my other favorite idea at LPC: require modules to be loaded before we "patch" them.
Signature · 1 line
Josh
Miroslav Benes
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 29 lines · Josh Poimboeuf
Quoted 28 lines · Petr Mladek
Quoted 27 lines · Miroslav Benes
Quoted 24 lines · Petr Mladek
Quoted 12 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Quoted 5 lines
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.I though about this as well. I kept "enabled" because:
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Well our sysfs interface is still in a testing phase as far as ABI is involved. Moreover, each live patch is bound to its base kernel by definition anyway. So we can change this without remorse, I think.
But it would break tooling, which is not kernel specific. I'm not sure whether it would be worth the headache. After all I think the livepatch sysfs interface is designed for tools, not humans.
You're right. It's probably not worth it. Oh well.
Quoted 18 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
I still think it would be better to have a less confusing interface and it would outweigh the second remark.
Quoted 7 lines
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
Quoted 7 lines
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
(Apologies if we've already talked about this. My brain is still mushy thanks to Spectre and friends.)
I think we talked about it last year in Prague and I think we convinced you that it was not a good idea (...not to allow disabling patches at all).
BUT! Empty 'nop' patch is a new idea and we may certainly discuss it.
Quoted 4 lines
The amount of flexibility we allow is kind of crazy, considering how delicate of an operation live patching is. That reminds me that I should bring up my other favorite idea at LPC: require modules to be loaded before we "patch" them.
We talked about this as well and if I remember correctly we came to a conclusion that it is all about a distribution and maintenance. We cannot ask customers to load modules they do not need just because we need to patch them. One cumulative patch is not that great in this case. I remember you had a crazy idea how to solve it, but I don't remember details. My notes from the event say...
- livepatch code complexity
- make it synchronous with respect to modules loading
- Josh's crazy idea
That's not much :D
So yes, we can talk about it and hopefully make proper notes this time.
Miroslav
Re: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 64 lines · Miroslav Benes
Quoted 29 lines · Josh Poimboeuf
Quoted 28 lines · Petr Mladek
Quoted 27 lines · Miroslav Benes
Quoted 24 lines · Petr Mladek
Quoted 12 lines · Miroslav Benes
Quoted 4 lines · Petr Mladek
Also the API and logic is much easier. It is enough to call
klp_enable_patch()inmodule_init()call. The patch patch can be disabled by writing '0' into /sys/kernel/livepatch/<patch>/enabled. Then the module can be removed once the transition finishes and sysfs interface is freed.I think it would be good to discuss our sysfs interface here as well.
Writing '1' to enabled attribute now makes sense only when you need to reverse an unpatching transition. Writing '0' means "disable" or a reversion again.
Quoted 5 lines
Wouldn't be better to split it to two different attributes? Something like "disable" and "reverse"? It could be more intuitive.
Maybe we'd also find out that even
patch->enabledmember is not useful anymore in such case.I though about this as well. I kept "enabled" because:
+ It keeps the public interface the same as before. Most people would not notice any change in the behavior except maybe that the interface disappears when the patch gets disabled.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Well our sysfs interface is still in a testing phase as far as ABI is involved. Moreover, each live patch is bound to its base kernel by definition anyway. So we can change this without remorse, I think.
But it would break tooling, which is not kernel specific. I'm not sure whether it would be worth the headache. After all I think the livepatch sysfs interface is designed for tools, not humans.
You're right. It's probably not worth it. Oh well.
Quoted 23 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
+ The reverse operation makes most sense when the transition cannot get finished. In theory, it might be problem to finish even the reversed one. People might want to reverse once again and force it. Then "reverse" file might be confusing. They might not know in which direction they do the reverse.
I still think it would be better to have a less confusing interface and it would outweigh the second remark.
Quoted 7 lines
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
Quoted 8 lines
Quoted 2 lines
(Apologies if we've already talked about this. My brain is still mushy thanks to Spectre and friends.)
I think we talked about it last year in Prague and I think we convinced you that it was not a good idea (...not to allow disabling patches at all).
BUT! Empty 'nop' patch is a new idea and we may certainly discuss it.
I definitely remember talking about it in Prague, but I don't remember any conclusions. My livepatch-related brain cache lines have been flushed thanks to the aforementioned CVEs and my rapidly advancing senility.
Quoted 9 lines
Quoted 4 lines
The amount of flexibility we allow is kind of crazy, considering how delicate of an operation live patching is. That reminds me that I should bring up my other favorite idea at LPC: require modules to be loaded before we "patch" them.
We talked about this as well and if I remember correctly we came to a conclusion that it is all about a distribution and maintenance. We cannot ask customers to load modules they do not need just because we need to patch them.
Fair enough.
Quoted 11 lines
One cumulative patch is not that great in this case. I remember you had a crazy idea how to solve it, but I don't remember details. My notes from the event say...
- livepatch code complexity
- make it synchronous with respect to modules loading
- Josh's crazy ideaThat's not much :D
So yes, we can talk about it and hopefully make proper notes this time.
Heh, better notes would be good, otherwise I'll just keep complaining about the same things every year :-) I'll try to remember what my crazy idea was, or maybe come up with some new ones to keep it fresh.
Signature · 1 line
Josh
Petr Mladek
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · Miroslav Benes
Quoted 16 lines · Josh Poimboeuf
Quoted 7 lines · Petr Mladek
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
(Apologies if we've already talked about this. My brain is still mushy thanks to Spectre and friends.)
Quoted 8 lines
Quoted 5 lines
I think we talked about it last year in Prague and I think we convinced you that it was not a good idea (...not to allow disabling patches at all).
BUT! Empty 'nop' patch is a new idea and we may certainly discuss it.
I definitely remember talking about it in Prague, but I don't remember any conclusions.
The revert operation allows to remove a livepatch stuck in the transition without forcing.
Also implementing empty cumulative patch might be tricky because of the callbacks. The current proposal is to call callbacks only from the new livepatch. It helps tp keep the interactions easy and under control. The way how to take over some change between an old and new patch depends on the particular functionality.
It would mean that the empty patch might need to be custom. Users probably would need to ask and wait for it.
Quoted 3 lines
My livepatch-related brain cache lines have been flushed thanks to the aforementioned CVEs and my rapidly advancing senility.
Uff, I am not the only one.
Quoted 20 lines
Quoted 9 lines
Quoted 4 lines
The amount of flexibility we allow is kind of crazy, considering how delicate of an operation live patching is. That reminds me that I should bring up my other favorite idea at LPC: require modules to be loaded before we "patch" them.
We talked about this as well and if I remember correctly we came to a conclusion that it is all about a distribution and maintenance. We cannot ask customers to load modules they do not need just because we need to patch them.
Fair enough.
Quoted 8 lines
One cumulative patch is not that great in this case. I remember you had a crazy idea how to solve it, but I don't remember details. My notes from the event say...
- livepatch code complexity
- make it synchronous with respect to modules loading
- Josh's crazy idea
Quoted 7 lines
Quoted 3 lines
That's not much :D
So yes, we can talk about it and hopefully make proper notes this time.
Heh, better notes would be good, otherwise I'll just keep complaining about the same things every year :-) I'll try to remember what my crazy idea was, or maybe come up with some new ones to keep it fresh.
If we do not want to force users to load all patched modules then we would need to create a livepatch-per-module. This just moves the complexity somewhere else.
One big problem would be how to keep the system consistent. You would need to solve races between loading modules and livepatches anyway.
For example, you could not load fixed/patched modules when the system is not fully patched yet. You would need to load the module and the related livepatch at the same time and follow the consistency model as we do now.
OK, there was the idea to refuse loading modules when livepatch transition is in progress. But it might not be acceptable, especially when the transition gets blocked infinitely and manual intervention would be needed.
I agree that the current solution adds complexity to the livepatching code but it is not that complicated. Races with loading modules and livepatches in parallel are solved by mod->klp_active flag. There are no other races because all other operations are done on code that is not actively used. One good thing is that everything is in one place and kernel has it under control.
I am open to discuss it. But we would need to come up with some clever solution.
Best Regards,
Petr
Re: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 25 lines · Petr Mladek
Quoted 24 lines · Josh Poimboeuf
Quoted 23 lines · Miroslav Benes
Quoted 9 lines · Josh Poimboeuf
Quoted 7 lines · Petr Mladek
OK, what about having just "disable" in sysfs. I agree that it makes much more sense than "enable" now.
It might be used also for the reverse operation the same way as "enable" was used before. I think that standalone "reverse" might be confusing when we allow to reverse the operation in both directions.
Quoted 7 lines
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
(Apologies if we've already talked about this. My brain is still mushy thanks to Spectre and friends.)
I think we talked about it last year in Prague and I think we convinced you that it was not a good idea (...not to allow disabling patches at all).
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
BUT! Empty 'nop' patch is a new idea and we may certainly discuss it.
I definitely remember talking about it in Prague, but I don't remember any conclusions.
The revert operation allows to remove a livepatch stuck in the transition without forcing.
True, though I question the real world value of that.
Quoted 5 lines
Also implementing empty cumulative patch might be tricky because of the callbacks. The current proposal is to call callbacks only from the new livepatch. It helps tp keep the interactions easy and under control. The way how to take over some change between an old and new patch depends on the particular functionality.
Presumably a 'no-op' patch would be special, in that it would call the un-patch callbacks.
I think the only *real* benefit of this proposal would be that history would be a straight line, with no backtracking. Similar to git rebase vs merge. You'd be able to tell what has been applied and reverted just by looking at what modules are loaded.
But, I now realize that in order for that to be the case, we'd have to disallow the unloading of replaced modules. But I think we're going to end up *allowing* the unloading of replaced modules, right?
So maybe it's not worth it. I'll drop it.
Quoted 9 lines
It would mean that the empty patch might need to be custom. Users probably would need to ask and wait for it.
Quoted 3 lines
My livepatch-related brain cache lines have been flushed thanks to the aforementioned CVEs and my rapidly advancing senility.
Uff, I am not the only one.
:-)
Quoted 55 lines
Quoted 20 lines
Quoted 9 lines
Quoted 4 lines
The amount of flexibility we allow is kind of crazy, considering how delicate of an operation live patching is. That reminds me that I should bring up my other favorite idea at LPC: require modules to be loaded before we "patch" them.
We talked about this as well and if I remember correctly we came to a conclusion that it is all about a distribution and maintenance. We cannot ask customers to load modules they do not need just because we need to patch them.
Fair enough.
Quoted 8 lines
One cumulative patch is not that great in this case. I remember you had a crazy idea how to solve it, but I don't remember details. My notes from the event say...
- livepatch code complexity
- make it synchronous with respect to modules loading
- Josh's crazy ideaQuoted 7 lines
Quoted 3 lines
That's not much :D
So yes, we can talk about it and hopefully make proper notes this time.
Heh, better notes would be good, otherwise I'll just keep complaining about the same things every year :-) I'll try to remember what my crazy idea was, or maybe come up with some new ones to keep it fresh.
If we do not want to force users to load all patched modules then we would need to create a livepatch-per-module. This just moves the complexity somewhere else.
One big problem would be how to keep the system consistent. You would need to solve races between loading modules and livepatches anyway.
For example, you could not load fixed/patched modules when the system is not fully patched yet. You would need to load the module and the related livepatch at the same time and follow the consistency model as we do now.
OK, there was the idea to refuse loading modules when livepatch transition is in progress. But it might not be acceptable, especially when the transition gets blocked infinitely and manual intervention would be needed.
I agree that the current solution adds complexity to the livepatching code but it is not that complicated. Races with loading modules and livepatches in parallel are solved by
mod->klp_activeflag. There are no other races because all other operations are done on code that is not actively used. One good thing is that everything is in one place and kernel has it under control.
Quoted 2 lines
I am open to discuss it. But we would need to come up with some clever solution.
Yeah, I think that's pretty much the crazy idea Miroslav mentioned. The patch would consist of several modules. The parent module would register the patch and patch vmlinux. Each child module would be associated with a to-be-patched module. The child modules could be loaded on demand, either by special klp code or by modprobe.
As you described, there would be some races to think about. However, it would also have some benefits.
I *hope* it would mean we could get rid of a lot of our ugly hacks, like
- klp symbols, klp relas
- preserving ELF data, PLT's, other horrible arch-specific things
- klp.arch..altinstructions, klp.arch..parainstructions
- manually calling apply_relocate_add()
However... we might still need some of those things for another reason:
to bypass exported symbol protections. It needs some more investigation.
Given this discussion, I'm thinking there wouldn't be much to discuss at LPC for this topic unless we had a prototype to look at (which I won't have time to do). So I may drop my talk in favor of giving more time for other more tangible discussions.
Signature · 1 line
Josh
Re: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
Quoted 4 lines · Josh Poimboeuf
Given this discussion, I'm thinking there wouldn't be much to discuss at LPC for this topic unless we had a prototype to look at (which I won't have time to do). So I may drop my talk in favor of giving more time for other more tangible discussions.
I dropped my talk to give us time for the other topics, and I somewhat arbitrarily added 5 minutes to each of the first 3 talks. Suggestions welcome.
Signature · 1 line
Josh
Petr Mladek
maintainerRe: [PATCH v12 06/12] livepatch: Simplify API by removing registration step
In reply to Josh PoimboeufQuoted 9 lines · Josh Poimboeuf
Quoted 8 lines · Petr Mladek
Quoted 7 lines · Josh Poimboeuf
Quoted 6 lines · Miroslav Benes
Quoted 5 lines · Josh Poimboeuf
As long as we're talking about radical changes... how about we just don't allow disabling patches at all? Instead a patch can be replaced with a 'revert' patch, or an empty 'nop' patch. That would make our code simpler and also ensure there's an audit trail.
Quoted 4 lines
Quoted 2 lines
The revert operation allows to remove a livepatch stuck in the transition without forcing.
True, though I question the real world value of that.
We ended in this situation few times with kGraft when a kthread was not annotated and migrated. We have not seen this with upstream livepatch code yet but we shipped first product with it only few months ago.
I would say that it is nice to have but it is not must to have.
Quoted 9 lines
Quoted 8 lines
One big problem would be how to keep the system consistent. You would need to solve races between loading modules and livepatches anyway.
For example, you could not load fixed/patched modules when the system is not fully patched yet. You would need to load the module and the related livepatch at the same time and follow the consistency model as we do now.
Quoted 5 lines
Yeah, I think that's pretty much the crazy idea Miroslav mentioned. The patch would consist of several modules. The parent module would register the patch and patch vmlinux. Each child module would be associated with a to-be-patched module. The child modules could be loaded on demand, either by special klp code or by modprobe.
Yup, something like this.
Quoted 6 lines
As you described, there would be some races to think about. However, it would also have some benefits.
I *hope* it would mean we could get rid of a lot of our ugly hacks, like
- klp symbols, klp relas
The access to external static symbols would still need so klp-specific relocations.
Quoted 3 lines
- preserving ELF data, PLT's, other horrible arch-specific things
- klp.arch..altinstructions, klp.arch..parainstructions
- manually callingapply_relocate_add()
Yup, these might be candidates to go.
Quoted 8 lines
However... we might still need some of those things for another reason:
to bypass exported symbol protections. It needs some more investigation.Given this discussion, I'm thinking there wouldn't be much to discuss at LPC for this topic unless we had a prototype to look at (which I won't have time to do). So I may drop my talk in favor of giving more time for other more tangible discussions.
Sounds reasonable. At least I would not be able to say much more about it without seeing a more detailed proposal and ideally a prototype code. That said, I definitely do not want to discourage you from playing with the idea.
Best Regards,
Petr
Petr Mladek
maintainerPATCH v12 7/12 livepatch: Use lists to manage patches, objects and functions
In reply to Petr MladekFrom: Jason Baron <[email protected]>
Currently klp_patch contains a pointer to a statically allocated array of struct klp_object and struct klp_objects contains a pointer to a statically allocated array of klp_func. In order to allow for the dynamic allocation of objects and functions, link klp_patch, klp_object, and klp_func together via linked lists. This allows us to more easily allocate new objects and functions, while having the iterator be a simple linked list walk.
The static structures are added to the lists early. It allows to add the dynamically allocated objects before klp_init_object() and klp_init_func() calls. Therefore it reduces the further changes to the code.
This patch does not change the existing behavior.
--- include/linux/livepatch.h | 19 +++++++++++++++++-- kernel/livepatch/core.c | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-)
include/linux/livepatch.h +17 -2
@@ -24,6 +24,7 @@#include <linux/module.h> #include <linux/ftrace.h> #include <linux/completion.h> +#include <linux/list.h> #if IS_ENABLED (CONFIG_LIVEPATCH )
@@ -42,6 +43,7 @@* can be found (optional) * @old_addr: the address of the function being patched * @kobj: kobject for sysfs resources +* @node: list node for klp_object func_list * @stack_node: list node for klp_ops func_stack list * @old_size: size of the old function * @new_size: size of the new function
@@ -79,6 +81,7 @@ struct klp_func {/* internal */ unsigned long old_addr;struct kobject kobj; +struct list_head node;struct list_head stack_node;unsigned long old_size, new_size;bool patched;
@@ -117,6 +120,8 @@ struct klp_callbacks {* @kobj: kobject for sysfs resources * @mod: kernel module associated with the patched object * (NULL for vmlinux) +* @func_list: dynamic list of the function entries +* @node: list node for klp_patch obj_list * @patched: the object's funcs have been added to the klp_ops list */ struct klp_object {
@@ -127,6 +132,8 @@ struct klp_object {/* internal */ struct kobject kobj; +struct list_head func_list; +struct list_head node;struct module *mod;bool patched; };
@@ -137,6 +144,7 @@ struct klp_object {* @objs: object entries for kernel objects to be patched * @list: list node for global list of registered patches * @kobj: kobject for sysfs resources +* @obj_list: dynamic list of the object entries * @enabled: the patch is enabled (but operation may be incomplete) * @wait_free: wait until the patch is freed * @module_put: module reference taken and patch not forced
@@ -150,6 +158,7 @@ struct klp_patch {/* internal */ struct list_head list;struct kobject kobj; +struct list_head obj_list;bool enabled;bool wait_free;bool module_put;
@@ -196,14 +205,20 @@ struct klp_patch { }#define KLP_OBJECT_END { } -#define klp_for_each_object (patch, obj) \ +#define klp_for_each_object_static (patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++) -#define klp_for_each_func (obj, func) \ +#define klp_for_each_object (patch, obj) \ + list_for_each_entry(obj, &patch->obj_list, node) + +#define klp_for_each_func_static (obj, func) \ for (func = obj->funcs; \ func->old_name || func->new_addr || func->old_sympos; \ func++) +#define klp_for_each_func (obj, func) \ + list_for_each_entry(func, &obj->func_list, node) +int klp_enable_patch (struct klp_patch *); void arch_klp_init_object_loaded(struct klp_patch *patch,
kernel/livepatch/core.c +16 -0
@@ -50,6 +50,21 @@ LIST_HEAD(klp_patches);static struct kobject *klp_root_kobj; +static void klp_init_lists (struct klp_patch *patch) +{ +struct klp_object *obj; +struct klp_func *func; + +INIT_LIST_HEAD (&patch->obj_list); +klp_for_each_object_static (patch, obj) { +list_add (&obj->node, &patch->obj_list); + +INIT_LIST_HEAD (&obj->func_list); +klp_for_each_func_static (obj, func) +list_add (&func->node, &obj->func_list); + } +} +static bool klp_is_module (struct klp_object *obj) {return obj->name;
@@ -664,6 +679,7 @@ static int klp_init_patch(struct klp_patch *patch) patch->module_put = false;INIT_LIST_HEAD (&patch->list);init_completion (&patch->finish); +klp_init_lists (patch);if (!patch->objs)return -EINVAL ;
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 07/12] livepatch: Use lists to manage patches, objects and functions
Quoted 47 lines
include/linux/livepatch.h +8
-2-#define klp_for_each_object (patch, obj) \ +#define klp_for_each_object_static (patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++) -#define klp_for_each_func (obj, func) \ +#define klp_for_each_object (patch, obj) \ + list_for_each_entry(obj, &patch->obj_list, node) + +#define klp_for_each_func_static (obj, func) \ for (func = obj->funcs; \ func->old_name || func->new_addr || func->old_sympos; \ func++) +#define klp_for_each_func (obj, func) \ + list_for_each_entry(func, &obj->func_list, node) +int klp_enable_patch (struct klp_patch *); void arch_klp_init_object_loaded(struct klp_patch *patch,kernel/livepatch/core.c +15
-0@@ -50,6 +50,21 @@ LIST_HEAD(klp_patches);static struct kobject *klp_root_kobj; +static void klp_init_lists (struct klp_patch *patch) +{ +struct klp_object *obj; +struct klp_func *func; + +INIT_LIST_HEAD (&patch->obj_list); +klp_for_each_object_static (patch, obj) { +list_add (&obj->node, &patch->obj_list); + +INIT_LIST_HEAD (&obj->func_list); +klp_for_each_func_static (obj, func) +list_add (&func->node, &obj->func_list); + } +} +static bool klp_is_module (struct klp_object *obj) {return obj->name;
kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 682
Quoted 4 lines
kernel/livepatch/core.c +1
-0@@ -664,6 +679,7 @@ static int klp_init_patch(struct klp_patch *patch) patch->module_put = false;INIT_LIST_HEAD (&patch->list);init_completion (&patch->finish); +klp_init_lists (patch);
This could explode easily if patch->objs is NULL. The check is just below.
kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 685
Quoted 2 lines
kernel/livepatch/core.c +0
-0if (!patch->objs)return -EINVAL ;
Here.
klp_init_lists() calls klp_for_each_object_static() which accesses obj->name without a check for obj. One could argue that it is a responsibility of the user not to do such a silly thing, but since the check is already there, could you move klp_init_lists() call after the check?
Regards,
Miroslav
Petr Mladek
maintainerRe: [PATCH v12 07/12] livepatch: Use lists to manage patches, objects and functions
Quoted 54 lines · Miroslav Benes
Quoted 47 lines
include/linux/livepatch.h +8
-2-#define klp_for_each_object (patch, obj) \ +#define klp_for_each_object_static (patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++) -#define klp_for_each_func (obj, func) \ +#define klp_for_each_object (patch, obj) \ + list_for_each_entry(obj, &patch->obj_list, node) + +#define klp_for_each_func_static (obj, func) \ for (func = obj->funcs; \ func->old_name || func->new_addr || func->old_sympos; \ func++) +#define klp_for_each_func (obj, func) \ + list_for_each_entry(func, &obj->func_list, node) +int klp_enable_patch (struct klp_patch *); void arch_klp_init_object_loaded(struct klp_patch *patch,kernel/livepatch/core.c +15
-0@@ -50,6 +50,21 @@ LIST_HEAD(klp_patches);static struct kobject *klp_root_kobj; +static void klp_init_lists (struct klp_patch *patch) +{ +struct klp_object *obj; +struct klp_func *func; + +INIT_LIST_HEAD (&patch->obj_list); +klp_for_each_object_static (patch, obj) { +list_add (&obj->node, &patch->obj_list); + +INIT_LIST_HEAD (&obj->func_list); +klp_for_each_func_static (obj, func) +list_add (&func->node, &obj->func_list); + } +} +static bool klp_is_module (struct klp_object *obj) {return obj->name;kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 682
Quoted 4 lines
kernel/livepatch/core.c +1
-0@@ -664,6 +679,7 @@ static int klp_init_patch(struct klp_patch *patch) patch->module_put = false;INIT_LIST_HEAD (&patch->list);init_completion (&patch->finish); +klp_init_lists (patch);This could explode easily if
patch->objsis NULL. The check is just below.
Quoted 7 lines
kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 685
Quoted 2 lines
kernel/livepatch/core.c +0
-0if (!patch->objs)return -EINVAL ;Here.
klp_init_lists()callsklp_for_each_object_static()which accessesobj->namewithout a check for obj.
Great catch!
Quoted 2 lines
since the check is already there, could you move
klp_init_lists()call after the check?
The same problem is with accessing obj->funcs byklp_for_each_func_static().
I have moved both checks into klp_init_lists(). I made sure that the lists were initialized to be usable with the free functions even in case of error.
Best Regards,
Petr
Petr Mladek
maintainerPATCH v12 8/12 livepatch: Add atomic replace
In reply to Petr MladekFrom: Jason Baron <[email protected]>
Sometimes we would like to revert a particular fix. Currently, this is not easy because we want to keep all other fixes active and we could revert only the last applied patch.
One solution would be to apply new patch that implemented all the reverted functions like in the original code. It would work as expected but there will be unnecessary redirections. In addition, it would also require knowing which functions need to be reverted at build time.
Another problem is when there are many patches that touch the same functions. There might be dependencies between patches that are not enforced on the kernel side. Also it might be pretty hard to actually prepare the patch and ensure compatibility with the other patches.
Atomic replace && cumulative patches:
A better solution would be to create cumulative patch and say that it replaces all older ones.
This patch adds a new "replace" flag to struct klp_patch. When it is enabled, a set of 'nop' klp_func will be dynamically created for all functions that are already being patched but that will no longer be modified by the new patch. They are used as a new target during the patch transition.
The idea is to handle Nops' structures like the static ones. When the dynamic structures are allocated, we initialize all values that are normally statically defined.
The only exception is "new_addr" in struct klp_func. It has to point to the original function and the address is known only when the object (module) is loaded. Note that we really need to set it. The address is used, for example, in klp_check_stack_func().
Nevertheless we still need to distinguish the dynamically allocated structures in some operations. For this, we add "nop" flag into struct klp_func and "dynamic" flag into struct klp_object. They need special handling in the following situations:
+ The structures are added into the lists of objects and functions immediately. In fact, the lists were created for this purpose.
+ The address of the original function is known only when the patched object (module) is loaded. Therefore it is copied later in klp_init_object_loaded().
+ The ftrace handler must not set PC to func->new_addr. It would cause infinite loop because the address points back to the beginning of the original function.
+ The various free() functions must free the structure itself.
Note that other ways to detect the dynamic structures are not considered safe. For example, even the statically defined struct klp_object might include empty funcs array. It might be there just to run some callbacks.
Special callbacks handling:
The callbacks from the replaced patches are _not_ called by intention. It would be pretty hard to define a reasonable semantic and implement it.
It might even be counter-productive. The new patch is cumulative. It is supposed to include most of the changes from older patches. In most cases, it will not want to call pre_unpatch() post_unpatch() callbacks from the replaced patches. It would disable/break things for no good reasons. Also it should be easier to handle various scenarios in a single script in the new patch than think about interactions caused by running many scripts from older patches. Not to say that the old scripts even would not expect to be called in this situation.
Removing replaced patches:
One nice effect of the cumulative patches is that the code from the older patches is no longer used. Therefore the replaced patches can be removed. It has several advantages:
+ Nops' structs will not longer be necessary and might be removed. This would save memory, restore performance (no ftrace handler), allow clear view on what is really patched.
+ Disabling the patch will cause using the original code everywhere. Therefore the livepatch callbacks could handle only one scenario. Note that the complication is already complex enough when the patch gets enabled. It is currently solved by calling callbacks only from the new cumulative patch.
+ The state is clean in both the sysfs interface and lsmod. The modules with the replaced livepatches might even get removed from the system.
Some people actually expected this behavior from the beginning. After all a cumulative patch is supposed to "completely" replace an existing one. It is like when a new version of an application replaces an older one.
This patch does the first step. It removes the replaced patches from the list of patches. It is safe. The consistency model ensures that they are not longer used. By other words, each process works only with the structures from klp_transition_patch.
The removal is done by a special function. It combines actions done by __disable_patch() and klp_complete_transition(). But it is a fast track without all the transaction-related stuff.
--- include/linux/livepatch.h | 6 ++ kernel/livepatch/core.c | 221 +++++++++++++++++++++++++++++++++++++++++- kernel/livepatch/core.h | 1 + kernel/livepatch/patch.c | 8 ++ kernel/livepatch/transition.c | 3 + 5 files changed, 236 insertions(+), 3 deletions(-)
include/linux/livepatch.h +6 -0
@@ -47,6 +47,7 @@* @stack_node: list node for klp_ops func_stack list * @old_size: size of the old function * @new_size: size of the new function +* @nop: temporary patch to use the original code again; dyn. allocated * @patched: the func has been added to the klp_ops list * @transition: the func is currently being applied or reverted *
@@ -84,6 +85,7 @@ struct klp_func {struct list_head node;struct list_head stack_node;unsigned long old_size, new_size; +bool nop;bool patched;bool transition; };
@@ -122,6 +124,7 @@ struct klp_callbacks {* (NULL for vmlinux) * @func_list: dynamic list of the function entries * @node: list node for klp_patch obj_list +* @dynamic: temporary object for nop functions; dynamically allocated * @patched: the object's funcs have been added to the klp_ops list */ struct klp_object {
@@ -135,6 +138,7 @@ struct klp_object {struct list_head func_list;struct list_head node;struct module *mod; +bool dynamic;bool patched; };
@@ -142,6 +146,7 @@ struct klp_object {* struct klp_patch - patch structure for live patching * @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched +* @replace: replace all already registered patches * @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
@@ -154,6 +159,7 @@ struct klp_patch {/* external */ struct module *mod;struct klp_object *objs; +bool replace;/* internal */ struct list_head list;
kernel/livepatch/core.c +218 -3
294 lines not on this page · raw message
kernel/livepatch/core.h +1 -0
@@ -8,6 +8,7 @@ extern struct mutex klp_mutex;extern struct list_head klp_patches;void klp_free_patch_nowait (struct klp_patch *patch); +void klp_discard_replaced_patches (struct klp_patch *new_patch);static inline bool klp_is_object_loaded (struct klp_object *obj) {
kernel/livepatch/patch.c +8 -0
@@ -118,7 +118,15 @@ static void notrace klp_ftrace_handler(unsigned long ip, } } +/* +* NOPs are used to replace existing patches with original code. +* Do nothing! Setting pc would cause an infinite loop. +*/ +if (func->nop) + goto unlock; +klp_arch_set_pc (regs, func->new_addr); + unlock:preempt_enable_notrace (); }
kernel/livepatch/transition.c +3 -0
@@ -85,6 +85,9 @@ static void klp_complete_transition(void) klp_transition_patch->mod->name, klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); +if (klp_transition_patch->replace && klp_target_state ==KLP_PATCHED ) +klp_discard_replaced_patches (klp_transition_patch); +if (klp_target_state ==KLP_UNPATCHED ) {/* * All tasks have transitioned to KLP_UNPATCHED so we can now
Signature · 1 line
2.13.7
Petr Mladek
maintainerPATCH v12 9/12 livepatch: Remove Nop structures when unused
In reply to Petr MladekReplaced patches are removed from the stack when the transition is finished. It means that Nop structures will never be needed again and can be removed. Why should we care?
+ Nop structures make false feeling that the function is patched even though the ftrace handler has no effect.
+ Ftrace handlers are not completely for free. They cause slowdown that might be visible in some workloads. The ftrace-related slowdown might actually be the reason why the function is not longer patched in the new cumulative patch. One would expect that cumulative patch would allow to solve these problems as well.
+ Cumulative patches are supposed to replace any earlier version of the patch. The amount of NOPs depends on which version was replaced. This multiplies the amount of scenarios that might happen.
One might say that NOPs are innocent. But there are even optimized NOP instructions for different processor, for example, see arch/x86/kernel/alternative.c. And klp_ftrace_handler() is much more complicated.
+ It sounds natural to clean up a mess that is not longer needed. It could only be worse if we do not do it.
This patch allows to unpatch and free the dynamic structures independently when the transition finishes.
The free part is a bit tricky because kobject free callbacks are called asynchronously. We could not wait for them easily. Fortunately, we do not have to. Any further access can be avoided by removing them from the dynamic lists.
--- include/linux/livepatch.h | 6 ++++ kernel/livepatch/core.c | 72 ++++++++++++++++++++++++++++++++++++++----- kernel/livepatch/core.h | 2 +- kernel/livepatch/patch.c | 31 ++++++++++++++++--- kernel/livepatch/patch.h | 1 + kernel/livepatch/transition.c | 2 +- 6 files changed, 99 insertions(+), 15 deletions(-)
include/linux/livepatch.h +6 -0
@@ -214,6 +214,9 @@ struct klp_patch {#define klp_for_each_object_static (patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++) +#define klp_for_each_object_safe (patch, obj, tmp_obj) \ + list_for_each_entry_safe(obj, tmp_obj, &patch->obj_list, node) +#define klp_for_each_object (patch, obj) \ list_for_each_entry(obj, &patch->obj_list, node)
@@ -222,6 +225,9 @@ struct klp_patch { func->old_name || func->new_addr || func->old_sympos; \ func++) +#define klp_for_each_func_safe (obj, func, tmp_func) \ + list_for_each_entry_safe(func, tmp_func, &obj->func_list, node) +#define klp_for_each_func (obj, func) \ list_for_each_entry(func, &obj->func_list, node)
kernel/livepatch/core.c +64 -8
106 lines not on this page · raw message
kernel/livepatch/core.h +1 -1
@@ -8,7 +8,7 @@ extern struct mutex klp_mutex;extern struct list_head klp_patches;void klp_free_patch_nowait (struct klp_patch *patch); -void klp_discard_replaced_patches (struct klp_patch *new_patch); +void klp_discard_replaced_stuff (struct klp_patch *new_patch);static inline bool klp_is_object_loaded (struct klp_object *obj) {
kernel/livepatch/patch.c +26 -5
@@ -244,15 +244,26 @@ static int klp_patch_func(struct klp_func *func)return ret; } -void klp_unpatch_object (struct klp_object *obj) +static void __klp_unpatch_object (struct klp_object *obj,bool unpatch_all) {struct klp_func *func; -klp_for_each_func (obj, func) +klp_for_each_func (obj, func) { +if (!unpatch_all && !func->nop) +continue ; +if (func->patched)klp_unpatch_func (func); + } - obj->patched = false; +if (unpatch_all || obj->dynamic) + obj->patched = false; +} + + +void klp_unpatch_object (struct klp_object *obj) +{ +__klp_unpatch_object (obj, true); }int klp_patch_object (struct klp_object *obj)
@@ -275,11 +286,21 @@ int klp_patch_object(struct klp_object *obj)return 0 ; } -void klp_unpatch_objects (struct klp_patch *patch) +static void __klp_unpatch_objects (struct klp_patch *patch,bool unpatch_all) {struct klp_object *obj;klp_for_each_object (patch, obj)if (obj->patched) -klp_unpatch_object (obj); +__klp_unpatch_object (obj, unpatch_all); +} + +void klp_unpatch_objects (struct klp_patch *patch) +{ +__klp_unpatch_objects (patch, true); +} + +void klp_unpatch_objects_dynamic (struct klp_patch *patch) +{ +__klp_unpatch_objects (patch, false); }
kernel/livepatch/patch.h +1 -0
@@ -30,5 +30,6 @@ struct klp_ops *klp_find_ops(unsigned long old_addr);int klp_patch_object (struct klp_object *obj);void klp_unpatch_object (struct klp_object *obj);void klp_unpatch_objects (struct klp_patch *patch); +void klp_unpatch_objects_dynamic (struct klp_patch *patch);#endif /* _LIVEPATCH_PATCH_H */
kernel/livepatch/transition.c +1 -1
@@ -86,7 +86,7 @@ static void klp_complete_transition(void) klp_target_state == KLP_PATCHED ? "patching" : "unpatching");if (klp_transition_patch->replace && klp_target_state ==KLP_PATCHED ) -klp_discard_replaced_patches (klp_transition_patch); +klp_discard_replaced_stuff (klp_transition_patch);if (klp_target_state ==KLP_UNPATCHED ) {/*
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 09/12] livepatch: Remove Nop structures when unused
Quoted 2 lines
+void
klp_free_objects_dynamic(struct klp_patch *patch)
should be static.
kernel/livepatch/core.c static void klp_free_objects(struct klp_patch *patch) · line 703
Quoted 4 lines
kernel/livepatch/core.c +4
-0+{ +__klp_free_objects (patch, false); +} +
Miroslav
Petr Mladek
maintainerPATCH v12 10/12 livepatch: Atomic replace and cumulative patches documentation
In reply to Petr MladekUser documentation for the atomic replace feature. It makes it easier to maintain livepatches using so-called cumulative patches.
--- Documentation/livepatch/cumulative-patches.txt | 105 +++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Documentation/livepatch/cumulative-patches.txt
Documentation/livepatch/cumulative-patches.txt new file +105 -0
@@ -0,0 +1,105 @@ +=================================== +Atomic Replace & Cumulative Patches +=================================== + +There might be dependencies between livepatches. If multiple patches need +to do different changes to the same function(s) then we need to define +an order in which the patches will be installed. And function implementations +from any newer livepatch must be done on top of the older ones. + +This might become a maintenance nightmare. Especially if anyone would want +to remove a patch that is in the middle of the stack. + +An elegant solution comes with the feature called "Atomic Replace". It allows +to create so called "Cumulative Patches". They include all wanted changes +from all older livepatches and completely replace them in one transition. + +Usage +----- + +The atomic replace can be enabled by setting "replace" flag in struct klp_patch, +for example: + + static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, + .replace = true, + }; + +Such a patch is added on top of the livepatch stack when registered. It can +be enabled even when some earlier patches have not been enabled yet. + +All processes are then migrated to use the code only from the new patch. +Once the transition is finished, all older patches are removed from the stack +of patches. Even the older not-enabled patches mentioned above. They can +even be unregistered and the related modules unloaded. + +Ftrace handlers are transparently removed from functions that are no +longer modified by the new cumulative patch. + +As a result, the livepatch authors might maintain sources only for one +cumulative patch. It helps to keep the patch consistent while adding or +removing various fixes or features. + +Users could keep only the last patch installed on the system after +the transition to has finished. It helps to clearly see what code is +actually in use. Also the livepatch might then be seen as a "normal" +module that modifies the kernel behavior. The only difference is that +it can be updated at runtime without breaking its functionality. + + +Features +-------- + +The atomic replace allows: + + + Atomically revert some functions in a previous patch while + upgrading other functions. + + + Remove eventual performance impact caused by core redirection + for functions that are no longer patched. + + + Decrease user confusion about stacking order and what patches are + currently in effect. + + +Limitations: +------------ + + + Replaced patches can no longer be enabled. But if the transition + to the cumulative patch was not forced, the kernel modules with + the older livepatches can be removed and eventually added again. + + A good practice is to set .replace flag in any released livepatch. + Then re-adding an older livepatch is equivalent to downgrading + to that patch. This is safe as long as the livepatches do _not_ do + extra modifications in (un)patching callbacks or in the module_init() + or module_exit() functions, see below. + + + + Only the (un)patching callbacks from the _new_ cumulative livepatch are + executed. Any callbacks from the replaced patches are ignored. + + By other words, the cumulative patch is responsible for doing any actions + that are necessary to properly replace any older patch. + + As a result, it might be dangerous to replace newer cumulative patches by + older ones. The old livepatches might not provide the necessary callbacks. + + This might be seen as a limitation in some scenarios. But it makes the life + easier in many others. Only the new cumulative livepatch knows what + fixes/features are added/removed and what special actions are necessary + for a smooth transition. + + In each case, it would be a nightmare to think about the order of + the various callbacks and their interactions if the callbacks from all + enabled patches were called. + + + + There is no special handling of shadow variables. Livepatch authors + must create their own rules how to pass them from one cumulative + patch to the other. Especially they should not blindly remove them + in module_exit() functions. + + A good practice might be to remove shadow variables in the post-unpatch + callback. It is called only when the livepatch is properly disabled.
Signature · 1 line
2.13.7
Miroslav Benes
maintainerRe: [PATCH v12 10/12] livepatch: Atomic replace and cumulative patches documentation
Quoted 2 lines · Petr Mladek
User documentation for the atomic replace feature. It makes it easier to maintain livepatches using so-called cumulative patches.
I think the documentation should be updated due to API changes.
Quoted 32 lines
Signed-off-by Petr Mladek <[email protected]>--- Documentation/livepatch/cumulative-patches.txt | 105 +++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Documentation/livepatch/cumulative-patches.txtDocumentation/livepatch/cumulative-patches.txt new file +20
-0@@ -0,0 +1,105 @@ +=================================== +Atomic Replace & Cumulative Patches +=================================== + +There might be dependencies between livepatches. If multiple patches need +to do different changes to the same function(s) then we need to define +an order in which the patches will be installed. And function implementations +from any newer livepatch must be done on top of the older ones. + +This might become a maintenance nightmare. Especially if anyone would want +to remove a patch that is in the middle of the stack. + +An elegant solution comes with the feature called "Atomic Replace". It allows +to create so called "Cumulative Patches". They include all wanted changes +from all older livepatches and completely replace them in one transition. + +Usage +----- + +The atomic replace can be enabled by setting "replace" flag in struct klp_patch,
Documentation/livepatch/cumulative-patches.txt line 30
Quoted 10 lines
Documentation/livepatch/cumulative-patches.txt new file +10
-0+for example: + + static struct klp_patch patch = { + .mod = THIS_MODULE, + .objs = objs, + .replace = true, + }; + +Such a patch is added on top of the livepatch stack when registered. It can +be enabled even when some earlier patches have not been enabled yet.
Here.
Documentation/livepatch/cumulative-patches.txt line 35
Quoted 4 lines
Documentation/livepatch/cumulative-patches.txt new file +4
-0+All processes are then migrated to use the code only from the new patch. +Once the transition is finished, all older patches are removed from the stack +of patches. Even the older not-enabled patches mentioned above. They can +even be unregistered and the related modules unloaded.
Here.
Quoted 25 lines
Documentation/livepatch/cumulative-patches.txt new file +25
-0+Ftrace handlers are transparently removed from functions that are no +longer modified by the new cumulative patch. + +As a result, the livepatch authors might maintain sources only for one +cumulative patch. It helps to keep the patch consistent while adding or +removing various fixes or features. + +Users could keep only the last patch installed on the system after +the transition to has finished. It helps to clearly see what code is +actually in use. Also the livepatch might then be seen as a "normal" +module that modifies the kernel behavior. The only difference is that +it can be updated at runtime without breaking its functionality. + + +Features +-------- + +The atomic replace allows: + + + Atomically revert some functions in a previous patch while + upgrading other functions. + + + Remove eventual performance impact caused by core redirection + for functions that are no longer patched. +
Documentation/livepatch/cumulative-patches.txt line 71
Quoted 10 lines
Documentation/livepatch/cumulative-patches.txt new file +10
-0+ + Decrease user confusion about stacking order and what patches are + currently in effect. + + +Limitations: +------------ + + + Replaced patches can no longer be enabled. But if the transition + to the cumulative patch was not forced, the kernel modules with + the older livepatches can be removed and eventually added again.
I'd rewrite even this.
Documentation/livepatch/cumulative-patches.txt line 84
Quoted 12 lines
Documentation/livepatch/cumulative-patches.txt new file +12
-0+ A good practice is to set .replace flag in any released livepatch. + Then re-adding an older livepatch is equivalent to downgrading + to that patch. This is safe as long as the livepatches do _not_ do + extra modifications in (un)patching callbacks or in the module_init() + or module_exit() functions, see below. + + + + Only the (un)patching callbacks from the _new_ cumulative livepatch are + executed. Any callbacks from the replaced patches are ignored. + + By other words, the cumulative patch is responsible for doing any actions + that are necessary to properly replace any older patch.
s/By other words/In other words/
Documentation/livepatch/cumulative-patches.txt line 96
Quoted 11 lines
Documentation/livepatch/cumulative-patches.txt new file +11
-0+ As a result, it might be dangerous to replace newer cumulative patches by + older ones. The old livepatches might not provide the necessary callbacks. + + This might be seen as a limitation in some scenarios. But it makes the life + easier in many others. Only the new cumulative livepatch knows what + fixes/features are added/removed and what special actions are necessary + for a smooth transition. + + In each case, it would be a nightmare to think about the order of + the various callbacks and their interactions if the callbacks from all + enabled patches were called.
s/In each case/In any case/ ?
Documentation/livepatch/cumulative-patches.txt line 105
Quoted 7 lines
Documentation/livepatch/cumulative-patches.txt new file +7
-0+ + There is no special handling of shadow variables. Livepatch authors + must create their own rules how to pass them from one cumulative + patch to the other. Especially they should not blindly remove them + in module_exit() functions. + + A good practice might be to remove shadow variables in the post-unpatch + callback. It is called only when the livepatch is properly disabled.
Miroslav
Petr Mladek
maintainerPATCH v12 11/12 livepatch: Remove ordering and refuse loading conflicting patches
In reply to Petr MladekThe atomic replace and cumulative patches were introduced as a more secure way to handle dependent patches. They simplify the logic:
+ Any new cumulative patch is supposed to take over shadow variables and changes made by callbacks from previous livepatches.
+ All replaced patches are discarded and the modules can be unloaded. As a result, there is only one scenario when a cumulative livepatch gets disabled.
The different handling of "normal" and cumulative patches might cause confusion. It would make sense to keep only one mode. On the other hand, it would be rude to enforce using the cumulative livepatches even for trivial and independent (hot) fixes.
This patch removes the stack of patches. The list of enabled patches is still needed but the ordering is not longer enforced.
Note that it is not possible to catch all possible dependencies. It is the responsibility of the livepatch authors to decide.
Nevertheless this patch prevents having two patches for the same function enabled at the same time after the transition finishes. It might help to catch obvious mistakes. But more importantly, we do not need to handle situation when a patch in the middle of the function stack (ops->func_stack) is being removed.
--- Documentation/livepatch/livepatch.txt | 30 +++++++++++-------- kernel/livepatch/core.c | 56 +++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 18 deletions(-)
Documentation/livepatch/livepatch.txt +17 -13
@@ -141,9 +141,9 @@ without HAVE_RELIABLE_STACKTRACE are not considered fully supported by the kernel livepatching. The /sys/kernel/livepatch/<patch>/transition file shows whether a patch -is in transition. Only a single patch (the topmost patch on the stack) -can be in transition at a given time. A patch can remain in transition -indefinitely, if any of the tasks are stuck in the initial patch state. +is in transition. Only a single patch can be in transition at a given +time. A patch can remain in transition indefinitely, if any of the tasks +are stuck in the initial patch state. A transition can be reversed and effectively canceled by writing the opposite value to the /sys/kernel/livepatch/<patch>/enabled file while
@@ -327,9 +327,10 @@ successfully disabled via the sysfs interface. Livepatch modules have to call klp_enable_patch() in module_init() callback. This function is rather complex and might even fail in the early phase. -First, the addresses of the patched functions are found according to their -names. The special relocations, mentioned in the section "New functions", -are applied. The relevant entries are created under +First, possible conflicts are checked for non-cummulative patches with +disabled replace flag. The addresses of the patched functions are found +according to their names. The special relocations, mentioned in the section +"New functions", are applied. The relevant entries are created under /sys/kernel/livepatch/<name>. The patch is rejected when any above operation fails.
@@ -343,11 +344,11 @@ this process, see the "Consistency model" section. Finally, once all tasks have been patched, the 'transition' value changes to '0'. -[*] Note that functions might be patched multiple times. The ftrace handler - is registered only once for a given function. Further patches just add - an entry to the list (see field `func_stack`) of the struct klp_ops. - The right implementation is selected by the ftrace handler, see - the "Consistency model" section. +[*] Note that two patches might modify the same function during the transition + to a new cumulative patch. The ftrace handler is registered only once + for a given function. The new patch just adds an entry to the list + (see field `func_stack`) of the struct klp_ops. The right implementation + is selected by the ftrace handler, see the "Consistency model" section. 5.2. Disabling
@@ -374,8 +375,11 @@ Third, the sysfs interface is destroyed. Finally, the module can be removed if the transition was not forced and the last sysfs entry has gone. -Note that patches must be disabled in exactly the reverse order in which -they were enabled. It makes the problem and the implementation much easier. +Note that any patch dependencies have to be handled by the atomic replace +and cumulative patches, see Documentation/livepatch/cumulative-patches.txt. +Therefore there is usually only one patch enabled on the system. There is +still possibility to have more trivial and independent livepatches enabled +at the same time. These can be enabled and disabled in any order. 6. Sysfs
kernel/livepatch/core.c +51 -5
82 lines not on this page · raw message
Signature · 1 line
2.13.7
Petr Mladek
maintainerPATCH v12 12/12 selftests/livepatch: introduce tests
In reply to Petr MladekFrom: Joe Lawrence <[email protected]>
Add a few livepatch modules and simple target modules that the included regression suite can run tests against:
- basic livepatching (multiple patches, atomic replace)
- pre/post (un)patch callbacks
- shadow variable API
--- Documentation/livepatch/callbacks.txt | 489 +---------------- MAINTAINERS | 1 + lib/Kconfig.debug | 21 + lib/Makefile | 2 + lib/livepatch/Makefile | 15 + lib/livepatch/test_klp_atomic_replace.c | 53 ++ lib/livepatch/test_klp_callbacks_busy.c | 43 ++ lib/livepatch/test_klp_callbacks_demo.c | 109 ++++ lib/livepatch/test_klp_callbacks_demo2.c | 89 ++++ lib/livepatch/test_klp_callbacks_mod.c | 24 + lib/livepatch/test_klp_livepatch.c | 47 ++ lib/livepatch/test_klp_shadow_vars.c | 236 +++++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/livepatch/Makefile | 8 + tools/testing/selftests/livepatch/README | 43 ++ tools/testing/selftests/livepatch/config | 1 + tools/testing/selftests/livepatch/functions.sh | 203 +++++++ .../testing/selftests/livepatch/test-callbacks.sh | 587 +++++++++++++++++++++ .../testing/selftests/livepatch/test-livepatch.sh | 168 ++++++ .../selftests/livepatch/test-shadow-vars.sh | 60 +++ 20 files changed, 1716 insertions(+), 484 deletions(-) create mode 100644 lib/livepatch/Makefile create mode 100644 lib/livepatch/test_klp_atomic_replace.c create mode 100644 lib/livepatch/test_klp_callbacks_busy.c create mode 100644 lib/livepatch/test_klp_callbacks_demo.c create mode 100644 lib/livepatch/test_klp_callbacks_demo2.c create mode 100644 lib/livepatch/test_klp_callbacks_mod.c create mode 100644 lib/livepatch/test_klp_livepatch.c create mode 100644 lib/livepatch/test_klp_shadow_vars.c create mode 100644 tools/testing/selftests/livepatch/Makefile create mode 100644 tools/testing/selftests/livepatch/README create mode 100644 tools/testing/selftests/livepatch/config create mode 100644 tools/testing/selftests/livepatch/functions.sh create mode 100755 tools/testing/selftests/livepatch/test-callbacks.sh create mode 100755 tools/testing/selftests/livepatch/test-livepatch.sh create mode 100755 tools/testing/selftests/livepatch/test-shadow-vars.sh
Documentation/livepatch/callbacks.txt +5 -484
@@ -118,488 +118,9 @@ similar change to their hw_features value. (Client functions of the value may need to be updated accordingly.) -Test cases -========== - -What follows is not an exhaustive test suite of every possible livepatch -pre/post-(un)patch combination, but a selection that demonstrates a few -important concepts. Each test case uses the kernel modules located in -the samples/livepatch/ and assumes that no livepatches are loaded at the -beginning of the test. - - -Test 1 ------- - -Test a combination of loading a kernel module and a livepatch that -patches a function in the first module. (Un)load the target module -before the livepatch module: - -- load target module -- load livepatch -- disable livepatch -- unload target module -- unload livepatch - -First load a target module: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 34.475708] livepatch_callbacks_mod: livepatch_callbacks_mod_init - -On livepatch enable, before the livepatch transition starts, pre-patch -callbacks are executed for vmlinux and livepatch_callbacks_mod (those -klp_objects currently loaded). After klp_objects are patched according -to the klp_patch, their post-patch callbacks run and the transition -completes: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 36.503719] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 36.504213] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 36.504238] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 36.504721] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 36.505849] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 37.727133] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 37.727232] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 37.727860] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 37.728792] livepatch: 'livepatch_callbacks_demo': patching complete - -Similarly, on livepatch disable, pre-patch callbacks run before the -unpatching transition starts. klp_objects are reverted, post-patch -callbacks execute and the transition completes: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 38.510209] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 38.510234] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 38.510982] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 38.512209] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 39.711132] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 39.711210] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 39.711779] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 39.712735] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 42.534183] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - - -Test 2 ------- - -This test is similar to the previous test, but (un)load the livepatch -module before the target kernel module. This tests the livepatch core's -module_coming handler: - -- load livepatch -- load target module -- disable livepatch -- unload livepatch -- unload target module - - -On livepatch enable, only pre/post-patch callbacks are executed for -currently loaded klp_objects, in this case, vmlinux: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 44.553328] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 44.553997] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 44.554049] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 44.554845] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 45.727128] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 45.727212] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 45.727961] livepatch: 'livepatch_callbacks_demo': patching complete - -When a targeted module is subsequently loaded, only its pre/post-patch -callbacks are executed: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 46.560845] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod' - [ 46.561988] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 46.563452] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 46.565495] livepatch_callbacks_mod: livepatch_callbacks_mod_init - -On livepatch disable, all currently loaded klp_objects' (vmlinux and -livepatch_callbacks_mod) pre/post-unpatch callbacks are executed: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 48.568885] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 48.568910] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 48.569441] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 48.570502] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 49.759091] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 49.759171] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 49.759742] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 49.760690] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 52.592283] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - - -Test 3 ------- - -Test loading the livepatch after a targeted kernel module, then unload -the kernel module before disabling the livepatch. This tests the -livepatch core's module_going handler: - -- load target module -- load livepatch -- unload target module -- disable livepatch -- unload livepatch - -First load a target module, then the livepatch: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 54.607948] livepatch_callbacks_mod: livepatch_callbacks_mod_init - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 56.613919] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 56.614411] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 56.614436] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 56.614818] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 56.615656] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 57.759070] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 57.759147] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 57.759621] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state - [ 57.760307] livepatch: 'livepatch_callbacks_demo': patching complete - -When a target module is unloaded, the livepatch is only reverted from -that klp_object (livepatch_callbacks_mod). As such, only its pre and -post-unpatch callbacks are executed when this occurs: - - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 58.623409] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - [ 58.623903] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - [ 58.624658] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod' - [ 58.625305] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - -When the livepatch is disabled, pre and post-unpatch callbacks are run -for the remaining klp_object, vmlinux: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 60.638420] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 60.638444] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 60.638996] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 61.727088] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 61.727165] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 61.727985] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - - -Test 4 ------- - -This test is similar to the previous test, however the livepatch is -loaded first. This tests the livepatch core's module_coming and -module_going handlers: - -- load livepatch -- load target module -- unload target module -- disable livepatch -- unload livepatch - -First load the livepatch: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 64.661552] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 64.662147] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 64.662175] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 64.662850] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 65.695056] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 65.695147] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 65.695561] livepatch: 'livepatch_callbacks_demo': patching complete - -When a targeted kernel module is subsequently loaded, only its -pre/post-patch callbacks are executed: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 66.669196] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod' - [ 66.669882] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 66.670744] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 66.672873] livepatch_callbacks_mod: livepatch_callbacks_mod_init - -When the target module is unloaded, the livepatch is only reverted from -the livepatch_callbacks_mod klp_object. As such, only pre and -post-unpatch callbacks are executed when this occurs: - - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 68.680065] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - [ 68.680688] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - [ 68.681452] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod' - [ 68.682094] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 70.689225] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 70.689256] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 70.689882] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 71.711080] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 71.711481] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 71.711988] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - - -Test 5 ------- - -A simple test of loading a livepatch without one of its patch target -klp_objects ever loaded (livepatch_callbacks_mod): - -- load livepatch -- disable livepatch -- unload livepatch - -Load the livepatch: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 74.711081] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 74.711595] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 74.711639] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 74.712272] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 75.743137] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 75.743219] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 75.743867] livepatch: 'livepatch_callbacks_demo': patching complete - -As expected, only pre/post-(un)patch handlers are executed for vmlinux: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 76.716254] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 76.716278] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 76.716666] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 77.727089] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 77.727194] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 77.727907] livepatch: 'livepatch_callbacks_demo': unpatching complete +Other Examples +============== - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - - -Test 6 ------- - -Test a scenario where a vmlinux pre-patch callback returns a non-zero -status (ie, failure): - -- load target module -- load livepatch -ENODEV -- unload target module - -First load a target module: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 80.740520] livepatch_callbacks_mod: livepatch_callbacks_mod_init - -Load the livepatch module, setting its 'pre_patch_ret' value to -19 -(-ENODEV). When its vmlinux pre-patch callback executed, this status -code will propagate back to the module-loading subsystem. The result is -that the insmod command refuses to load the livepatch module: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko pre_patch_ret=-19 - [ 82.747326] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 82.747743] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 82.747767] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 82.748237] livepatch: pre-patch callback failed for object 'vmlinux' - [ 82.748637] livepatch: failed to enable patch 'livepatch_callbacks_demo' - [ 82.749059] livepatch: 'livepatch_callbacks_demo': canceling transition, going to unpatch - [ 82.749060] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 82.749868] livepatch: 'livepatch_callbacks_demo': unpatching complete - [ 82.765809] insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-demo.ko: No such device - - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 84.774238] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - - -Test 7 ------- - -Similar to the previous test, setup a livepatch such that its vmlinux -pre-patch callback returns success. However, when a targeted kernel -module is later loaded, have the livepatch return a failing status code: - -- load livepatch -- setup -ENODEV -- load target module -- disable livepatch -- unload livepatch - -Load the livepatch, notice vmlinux pre-patch callback succeeds: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 86.787845] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 86.788325] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 86.788427] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 86.788821] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 87.711069] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 87.711143] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 87.711886] livepatch: 'livepatch_callbacks_demo': patching complete - -Set a trap so subsequent pre-patch callbacks to this livepatch will -return -ENODEV: - - % echo -19 > /sys/module/livepatch_callbacks_demo/parameters/pre_patch_ret - -The livepatch pre-patch callback for subsequently loaded target modules -will return failure, so the module loader refuses to load the kernel -module. Notice that no post-patch or pre/post-unpatch callbacks are -executed for this klp_object: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 90.796976] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod' - [ 90.797834] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 90.798900] livepatch: pre-patch callback failed for object 'livepatch_callbacks_mod' - [ 90.799652] livepatch: patch 'livepatch_callbacks_demo' failed for module 'livepatch_callbacks_mod', refusing to load module 'livepatch_callbacks_mod' - [ 90.819737] insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-mod.ko: No such device - -However, pre/post-unpatch callbacks run for the vmlinux klp_object: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 92.823547] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 92.823573] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 92.824331] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 93.727128] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 93.727327] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 93.727861] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - - -Test 8 ------- - -Test loading multiple targeted kernel modules. This test-case is -mainly for comparing with the next test-case. - -- load busy target module (0s sleep), -- load livepatch -- load target module -- unload target module -- disable livepatch -- unload livepatch -- unload busy target module - - -Load a target "busy" kernel module which kicks off a worker function -that immediately exits: - - % insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=0 - [ 96.910107] livepatch_callbacks_busymod: livepatch_callbacks_mod_init - [ 96.910600] livepatch_callbacks_busymod: busymod_work_func, sleeping 0 seconds ... - [ 96.913024] livepatch_callbacks_busymod: busymod_work_func exit - -Proceed with loading the livepatch and another ordinary target module, -notice that the post-patch callbacks are executed and the transition -completes quickly: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 98.917892] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 98.918426] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 98.918453] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 98.918955] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 98.923835] livepatch: 'livepatch_callbacks_demo': starting patching transition - [ 99.743104] livepatch: 'livepatch_callbacks_demo': completing patching transition - [ 99.743156] livepatch_callbacks_demo: post_patch_callback: vmlinux - [ 99.743679] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 99.744616] livepatch: 'livepatch_callbacks_demo': patching complete - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 100.930955] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod' - [ 100.931668] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 100.932645] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 100.934125] livepatch_callbacks_mod: livepatch_callbacks_mod_init - - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 102.942805] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - [ 102.943640] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - [ 102.944585] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod' - [ 102.945455] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 104.953815] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition - [ 104.953838] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux - [ 104.954431] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 104.955426] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 106.719073] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 106.722633] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 106.723282] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 106.724279] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - % rmmod samples/livepatch/livepatch-callbacks-busymod.ko - [ 108.975660] livepatch_callbacks_busymod: livepatch_callbacks_mod_exit - - -Test 9 ------- - -A similar test as the previous one, but force the "busy" kernel module -to do longer work. - -The livepatching core will refuse to patch a task that is currently -executing a to-be-patched function -- the consistency model stalls the -current patch transition until this safety-check is met. Test a -scenario where one of a livepatch's target klp_objects sits on such a -function for a long time. Meanwhile, load and unload other target -kernel modules while the livepatch transition is in progress. - -- load busy target module (30s sleep) -- load livepatch -- load target module -- unload target module -- disable livepatch -- unload livepatch -- unload busy target module - - -Load the "busy" kernel module, this time make it do 30 seconds worth of -work: - - % insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=30 - [ 110.993362] livepatch_callbacks_busymod: livepatch_callbacks_mod_init - [ 110.994059] livepatch_callbacks_busymod: busymod_work_func, sleeping 30 seconds ... - -Meanwhile, the livepatch is loaded. Notice that the patch transition -does not complete as the targeted "busy" module is sitting on a -to-be-patched function: - - % insmod samples/livepatch/livepatch-callbacks-demo.ko - [ 113.000309] livepatch: enabling patch 'livepatch_callbacks_demo' - [ 113.000764] livepatch: 'livepatch_callbacks_demo': initializing patching transition - [ 113.000791] livepatch_callbacks_demo: pre_patch_callback: vmlinux - [ 113.001289] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 113.005208] livepatch: 'livepatch_callbacks_demo': starting patching transition - -Load a second target module (this one is an ordinary idle kernel -module). Note that *no* post-patch callbacks will be executed while the -livepatch is still in transition: - - % insmod samples/livepatch/livepatch-callbacks-mod.ko - [ 115.012740] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod' - [ 115.013406] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init - [ 115.015315] livepatch_callbacks_mod: livepatch_callbacks_mod_init - -Request an unload of the simple kernel module. The patch is still -transitioning, so its pre-unpatch callbacks are skipped: - - % rmmod samples/livepatch/livepatch-callbacks-mod.ko - [ 117.022626] livepatch_callbacks_mod: livepatch_callbacks_mod_exit - [ 117.023376] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod' - [ 117.024533] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away - -Finally the livepatch is disabled. Since none of the patch's -klp_object's post-patch callbacks executed, the remaining klp_object's -pre-unpatch callbacks are skipped: - - % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled - [ 119.035408] livepatch: 'livepatch_callbacks_demo': reversing transition from patching to unpatching - [ 119.035485] livepatch: 'livepatch_callbacks_demo': starting unpatching transition - [ 119.711166] livepatch: 'livepatch_callbacks_demo': completing unpatching transition - [ 119.714179] livepatch_callbacks_demo: post_unpatch_callback: vmlinux - [ 119.714653] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state - [ 119.715437] livepatch: 'livepatch_callbacks_demo': unpatching complete - - % rmmod samples/livepatch/livepatch-callbacks-demo.ko - % rmmod samples/livepatch/livepatch-callbacks-busymod.ko - [ 141.279111] livepatch_callbacks_busymod: busymod_work_func exit - [ 141.279760] livepatch_callbacks_busymod: livepatch_callbacks_mod_exit +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.
MAINTAINERS +1 -0
@@ -8506,6 +8506,7 @@ F: arch/x86/kernel/livepatch.c F: Documentation/livepatch/ F: Documentation/ABI/testing/sysfs-kernel-livepatch F: samples/livepatch/ +F: tools/testing/selftests/livepatch/ L: [email protected] T: git git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching.git
lib/Kconfig.debug +21 -0
@@ -1965,6 +1965,27 @@ config TEST_DEBUG_VIRTUAL If unsure, say N. +config TEST_LIVEPATCH + tristate "Test livepatching" + default n + depends on LIVEPATCH + depends on m + help + Test kernel livepatching features for correctness. The tests will + load test modules that will be livepatched in various scenarios. + + To run all the livepatching tests: + + make -C tools/testing/selftests TARGETS=livepatch run_tests + + Alternatively, individual tests may be invoked: + + tools/testing/selftests/livepatch/test-callbacks.sh + tools/testing/selftests/livepatch/test-livepatch.sh + tools/testing/selftests/livepatch/test-shadow-vars.sh + + If unsure, say N. + endif # RUNTIME_TESTING_MENU config MEMTEST
lib/Makefile +2 -0
@@ -72,6 +72,8 @@ obj-$(CONFIG_TEST_PARMAN) += test_parman.o obj-$(CONFIG_TEST_KMOD) += test_kmod.o obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o +obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/ + ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG CFLAGS_kobject_uevent.o += -DDEBUG
lib/livepatch/Makefile new file +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# 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 + +# Target modules to be livepatched require CC_FLAGS_FTRACE +CFLAGS_test_klp_callbacks_busy.o += $(CC_FLAGS_FTRACE) +CFLAGS_test_klp_callbacks_mod.o += $(CC_FLAGS_FTRACE)
lib/livepatch/test_klp_atomic_replace.c new file +53 -0
53 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_busy.c new file +43 -0
43 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_demo.c new file +109 -0
109 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_demo2.c new file +89 -0
89 lines not on this page · raw message
lib/livepatch/test_klp_callbacks_mod.c new file +24 -0
@@ -0,0 +1,24 @@ +// 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_livepatch.c new file +47 -0
47 lines not on this page · raw message
lib/livepatch/test_klp_shadow_vars.c new file +236 -0
236 lines not on this page · raw message
tools/testing/selftests/Makefile +1 -0
@@ -18,6 +18,7 @@ TARGETS += ipc TARGETS += kcmp TARGETS += kvm TARGETS += lib +TARGETS += livepatch TARGETS += membarrier TARGETS += memfd TARGETS += memory-hotplug
tools/testing/selftests/livepatch/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := \ + test-livepatch.sh \ + test-callbacks.sh \ + test-shadow-vars.sh + +include ../lib.mk
tools/testing/selftests/livepatch/README new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/livepatch/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_TEST_LIVEPATCH=m
tools/testing/selftests/livepatch/functions.sh new file +203 -0
203 lines not on this page · raw message
tools/testing/selftests/livepatch/test-callbacks.sh new file +587 -0
587 lines not on this page · raw message
tools/testing/selftests/livepatch/test-livepatch.sh new file +168 -0
168 lines not on this page · raw message
tools/testing/selftests/livepatch/test-shadow-vars.sh new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.13.7
Miroslav Benes
maintainerQuoted 8 lines · Petr Mladek
livepatch: Atomic replace feature
The atomic replace allows to create cumulative patches. They are useful when you maintain many livepatches and want to remove one that is lower on the stack. In addition it is very useful when more patches touch the same function and there are dependencies between them.
Quoted 7 lines
This version does another big refactoring based on feedback against v11[*]. In particular, it removes the registration step, changes the API and handling of livepatch dependencies. The aim is to keep the number of possible variants on a sane level. It helps the keep the feature "easy" to use and maintain.
Hi,
I've started to review the patch set. Running selftests with lockdep enabled gives me...
======================================================
WARNING: possible circular locking dependency detected 4.17.0-rc1-klp_replace_v12-117114-gfedb3eba611d #218 Tainted: G K
------------------------------------------------------
kworker/1:1/49 is trying to acquire lock:00000000bb88dc17 (kn->count#186){++++}, at: kernfs_remove+0x23/0x40
but task is already holding lock:
0000000073632424 (klp_mutex){+.+.}, at: klp_transition_work_fn+0x17/0x40
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (klp_mutex){+.+.}:
lock_acquire+0xd4/0x220
__mutex_lock+0x75/0x920
mutex_lock_nested+0x1b/0x20
enabled_store+0x47/0x150
kobj_attr_store+0x12/0x20
sysfs_kf_write+0x4a/0x60
kernfs_fop_write+0x123/0x1b0
__vfs_write+0x2b/0x150
vfs_write+0xc7/0x1c0
ksys_write+0x49/0xa0
__x64_sys_write+0x1a/0x20
do_syscall_64+0x62/0x1b0
entry_SYSCALL_64_after_hwframe+0x49/0xbe-> #0 (kn->count#186){++++}:
__lock_acquire+0xe9d/0x1240
lock_acquire+0xd4/0x220
__kernfs_remove+0x23c/0x2c0
kernfs_remove+0x23/0x40
sysfs_remove_dir+0x51/0x60
kobject_del+0x18/0x50
kobject_cleanup+0x4b/0x180
kobject_put+0x2a/0x50
__klp_free_patch+0x5b/0x60
klp_free_patch_nowait+0x12/0x30
klp_try_complete_transition+0x13e/0x180
klp_transition_work_fn+0x26/0x40
process_one_work+0x1d8/0x5d0
worker_thread+0x4d/0x3d0
kthread+0x113/0x150
ret_from_fork+0x3a/0x50other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(klp_mutex);
lock(kn->count#186);
lock(klp_mutex); lock(kn->count#186);
*** DEADLOCK ***
3 locks held by kworker/1:1/49: #0: 00000000654f4e5a ((wq_completion)"events"){+.+.}, at:
process_one_work+0x153/0x5d0 #1: 000000003c1dc846 ((klp_transition_work).work){+.+.}, at:
process_one_work+0x153/0x5d0 #2: 0000000073632424 (klp_mutex){+.+.}, at: klp_transition_work_fn+0x17/0x40
stack backtrace:
CPU: 1 PID: 49 Comm: kworker/1:1 Tainted: G K
4.17.0-rc1-klp_replace_v12-117114-gfedb3eba611d #218
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.0.0-prebuilt.qemu-project.org 04/01/2014
Workqueue: events klp_transition_work_fn
Call Trace:
dump_stack+0x81/0xb8
print_circular_bug.isra.39+0x200/0x20e
check_prev_add.constprop.47+0x725/0x740
? print_shortest_lock_dependencies+0x1c0/0x1c0
__lock_acquire+0xe9d/0x1240
lock_acquire+0xd4/0x220
? kernfs_remove+0x23/0x40
__kernfs_remove+0x23c/0x2c0
? kernfs_remove+0x23/0x40
kernfs_remove+0x23/0x40
sysfs_remove_dir+0x51/0x60
kobject_del+0x18/0x50
kobject_cleanup+0x4b/0x180
kobject_put+0x2a/0x50
__klp_free_patch+0x5b/0x60
klp_free_patch_nowait+0x12/0x30
klp_try_complete_transition+0x13e/0x180
klp_transition_work_fn+0x26/0x40
process_one_work+0x1d8/0x5d0
? process_one_work+0x153/0x5d0
worker_thread+0x4d/0x3d0
? trace_hardirqs_on+0xd/0x10
kthread+0x113/0x150
? process_one_work+0x5d0/0x5d0
? kthread_delayed_work_timer_fn+0x90/0x90
? kthread_delayed_work_timer_fn+0x90/0x90
ret_from_fork+0x3a/0x50I think it could be related to registration removal and API changes. One thread writes to sysfs and wants to take klp_mutex there (CPU#1), the other holds klp_mutex in a transition period and calls klp_free_patch() to remove the sysfs infrastructure.
Regards,
Miroslav
Petr Mladek
maintainerQuoted 74 lines · Miroslav Benes
Quoted 8 lines · Petr Mladek
livepatch: Atomic replace feature
The atomic replace allows to create cumulative patches. They are useful when you maintain many livepatches and want to remove one that is lower on the stack. In addition it is very useful when more patches touch the same function and there are dependencies between them.
Quoted 7 lines
This version does another big refactoring based on feedback against v11[*]. In particular, it removes the registration step, changes the API and handling of livepatch dependencies. The aim is to keep the number of possible variants on a sane level. It helps the keep the feature "easy" to use and maintain.
Hi,
I've started to review the patch set. Running selftests with lockdep enabled gives me...
======================================================
WARNING: possible circular locking dependency detected 4.17.0-rc1-klp_replace_v12-117114-gfedb3eba611d #218 Tainted: G K
------------------------------------------------------
kworker/1:1/49 is trying to acquire lock:00000000bb88dc17(kn->count#186){++++}, at:kernfs_remove+0x23/0x40but task is already holding lock:
0000000073632424 (klp_mutex){+.+.}, at:klp_transition_work_fn+0x17/0x40which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (klp_mutex){+.+.}:
lock_acquire+0xd4/0x220 __mutex_lock+0x75/0x920 mutex_lock_nested+0x1b/0x20 enabled_store+0x47/0x150 kobj_attr_store+0x12/0x20 sysfs_kf_write+0x4a/0x60 kernfs_fop_write+0x123/0x1b0 __vfs_write+0x2b/0x150 vfs_write+0xc7/0x1c0 ksys_write+0x49/0xa0 __x64_sys_write+0x1a/0x20 do_syscall_64+0x62/0x1b0 entry_SYSCALL_64_after_hwframe+0x49/0xbe-> #0 (kn->count#186){++++}:
__lock_acquire+0xe9d/0x1240 lock_acquire+0xd4/0x220 __kernfs_remove+0x23c/0x2c0 kernfs_remove+0x23/0x40 sysfs_remove_dir+0x51/0x60 kobject_del+0x18/0x50 kobject_cleanup+0x4b/0x180 kobject_put+0x2a/0x50 __klp_free_patch+0x5b/0x60 klp_free_patch_nowait+0x12/0x30 klp_try_complete_transition+0x13e/0x180 klp_transition_work_fn+0x26/0x40 process_one_work+0x1d8/0x5d0 worker_thread+0x4d/0x3d0 kthread+0x113/0x150 ret_from_fork+0x3a/0x50other info that might help us debug this:
Quoted 8 lines
Possible unsafe locking scenario:CPU0 CPU1 ---- ---- lock(klp_mutex); lock(kn->count#186); lock(klp_mutex);
lock(kn->count#186);
Sigh, I overestimated the power of kobjects. I thought that this must have been a false positive but it was not.
1. kernfs_fop_write() ignores kobj->kref. It takes care only of its own reference count, see kernfs_get_active().
2. kobj_put() takes care only of kobj->kref. The following code is called when the reference count reaches zero:
+ kobj_put()
+ kref_put()
+ kobject_release()
+ kobject_cleanup()
+ kobject_del()
+ sysfs_remove_dir()
+ kernfs_remove()
+ __kernfs_remove().
+ kernfs_drain()
, where kernfs_drain() waits until all opened files are closed.
Now, we call kobj_put() under klp_mutex() when the sysfs interface still exists. Files can be opened for writing. As a result:
+ enabled_store() might wait for klp_mutex
+ kernfs_drain() would wait for enabled_store() with klp_mutex() taken.
I have reproduced this with some extra sleeps.
I am going to work on another solution.
Best Regards,
Petr