livepatch: allow removal of a disabled patch
Subsystems (2)
Miroslav Benes
maintainerPATCH v2 1/1 livepatch: allow removal of a disabled patch
Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code.
The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe.
We can safely let the patch module go after that.
Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit 942e443127e9 ("module: Fix mod->mkobj.kobj potentially freed too early").
Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm.
With this change a call to try_module_get() is moved to __klp_enable_patch from klp_register_patch to make module reference counting symmetric (module_put() is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled.
Also all kobject_put(&patch->kobj) calls are moved outside of klp_mutex lock protection to prevent a deadlock situation when klp_unregister_patch is called and sysfs directories are removed. There is no need to do the same for other kobject_put() callsites as we currently do not have their sysfs counterparts.
---
Based on Josh's v2 of the consistency model.
Documentation/livepatch/livepatch.txt | 29 ++++--------- include/linux/livepatch.h | 3 ++ kernel/livepatch/core.c | 80 ++++++++++++++++++++++------------- kernel/livepatch/transition.c | 12 +++++- samples/livepatch/livepatch-sample.c | 1 - 5 files changed, 72 insertions(+), 53 deletions(-)
Documentation/livepatch/livepatch.txt +9 -20
@@ -272,8 +272,15 @@ section "Livepatch life-cycle" below for more details about these two operations. Module removal is only safe when there are no users of the underlying -functions. The immediate consistency model is not able to detect this; -therefore livepatch modules cannot be removed. See "Limitations" below. +functions. The immediate consistency model is not able to detect this. The +code just redirects the functions at the very beginning and it does not +check if the functions are in use. In other words, it knows when the +functions get called but it does not know when the functions return. +Therefore it cannot be decided when the livepatch module can be safely +removed. This is solved by a hybrid consistency model. When the system is +transitioned to a new patch state (patched/unpatched) it is guaranteed that +no task sleeps or runs in the old code. + 5. Livepatch life-cycle =======================
@@ -444,24 +451,6 @@ See Documentation/ABI/testing/sysfs-kernel-livepatch for more details. There is work in progress to remove this limitation. - + Livepatch modules can not be removed. - - The current implementation just redirects the functions at the very - beginning. It does not check if the functions are in use. In other - words, it knows when the functions get called but it does not - know when the functions return. Therefore it can not decide when - the livepatch module can be safely removed. - - This will get most likely solved once a more complex consistency model - is supported. The idea is that a safe state for patching should also - mean a safe state for removing the patch. - - Note that the patch itself might get disabled by writing zero - to /sys/kernel/livepatch/<patch>/enabled. It causes that the new - code will not longer get called. But it does not guarantee - that anyone is not sleeping anywhere in the new code. - - + Livepatch works reliably only when the dynamic ftrace is located at the very beginning of the function.
include/linux/livepatch.h +3 -0
@@ -23,6 +23,7 @@#include <linux/module.h> #include <linux/ftrace.h> +#include <linux/completion.h> #if IS_ENABLED (CONFIG_LIVEPATCH )
@@ -114,6 +115,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) +* @finish: for waiting till it is safe to remove the patch module */ struct klp_patch {/* external */
@@ -125,6 +127,7 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; +struct completion finish; };#define klp_for_each_object (patch, obj) \
kernel/livepatch/core.c +50 -30
161 lines not on this page · raw message
kernel/livepatch/transition.c +10 -2
@@ -197,13 +197,21 @@ void klp_complete_transition(void)struct klp_func *func;struct task_struct *g, *task;unsigned int cpu; +bool is_immediate = false;if (klp_transition_patch->immediate) goto done; -klp_for_each_object (klp_transition_patch, obj) -klp_for_each_func (obj, func) +klp_for_each_object (klp_transition_patch, obj) { +klp_for_each_func (obj, func) { func->transition = false; +if (func->immediate) + is_immediate = true; + } + } + +if (klp_target_state ==KLP_UNPATCHED && !is_immediate) +module_put (klp_transition_patch->mod);read_lock (&tasklist_lock);for_each_process_thread (g, task) {
samples/livepatch/livepatch-sample.c +0 -1
@@ -82,7 +82,6 @@ static int livepatch_init(void)static void livepatch_exit (void ) { -WARN_ON (klp_disable_patch (&patch));WARN_ON (klp_unregister_patch (&patch)); }
Signature · 1 line
2.8.3
Quoted 302 lines · Miroslav Benes
Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code.
The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe.
We can safely let the patch module go after that.
Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit
942e443127e9("module: Fixmod->mkobj.kobjpotentially freed too early").Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm.
With this change a call to
try_module_get()is moved to__klp_enable_patchfromklp_register_patchto make module reference counting symmetric (module_put()is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled.Also all
kobject_put(&patch->kobj)calls are moved outside ofklp_mutexlock protection to prevent a deadlock situation whenklp_unregister_patchis called and sysfs directories are removed. There is no need to do the same for otherkobject_put()callsites as we currently do not have their sysfs counterparts.Signed-off-by Miroslav Benes <[email protected]>---
Based on Josh's v2 of the consistency model.Documentation/livepatch/livepatch.txt | 29 ++++--------- include/linux/livepatch.h | 3 ++ kernel/livepatch/core.c | 80 ++++++++++++++++++++++------------- kernel/livepatch/transition.c | 12 +++++- samples/livepatch/livepatch-sample.c | 1 - 5 files changed, 72 insertions(+), 53 deletions(-)Documentation/livepatch/livepatch.txt +9
-20@@ -272,8 +272,15 @@ section "Livepatch life-cycle" below for more details about these two operations. Module removal is only safe when there are no users of the underlying -functions. The immediate consistency model is not able to detect this; -therefore livepatch modules cannot be removed. See "Limitations" below. +functions. The immediate consistency model is not able to detect this. The +code just redirects the functions at the very beginning and it does not +check if the functions are in use. In other words, it knows when the +functions get called but it does not know when the functions return. +Therefore it cannot be decided when the livepatch module can be safely +removed. This is solved by a hybrid consistency model. When the system is +transitioned to a new patch state (patched/unpatched) it is guaranteed that +no task sleeps or runs in the old code. + 5. Livepatch life-cycle =======================@@ -444,24 +451,6 @@ See Documentation/ABI/testing/sysfs-kernel-livepatch for more details. There is work in progress to remove this limitation. - + Livepatch modules can not be removed. - - The current implementation just redirects the functions at the very - beginning. It does not check if the functions are in use. In other - words, it knows when the functions get called but it does not - know when the functions return. Therefore it can not decide when - the livepatch module can be safely removed. - - This will get most likely solved once a more complex consistency model - is supported. The idea is that a safe state for patching should also - mean a safe state for removing the patch. - - Note that the patch itself might get disabled by writing zero - to /sys/kernel/livepatch/<patch>/enabled. It causes that the new - code will not longer get called. But it does not guarantee - that anyone is not sleeping anywhere in the new code. - - + Livepatch works reliably only when the dynamic ftrace is located at the very beginning of the function.include/linux/livepatch.h +3
-0@@ -23,6 +23,7 @@#include <linux/module.h> #include <linux/ftrace.h> +#include <linux/completion.h> #if IS_ENABLED (CONFIG_LIVEPATCH )@@ -114,6 +115,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) +* @finish: for waiting till it is safe to remove the patch module */ struct klp_patch {/* external */ @@ -125,6 +127,7 @@ struct klp_patch {struct list_head list;struct kobject kobj;bool enabled; +struct completion finish; };#define klp_for_each_object (patch, obj) \kernel/livepatch/core.c +50
-30@@ -29,6 +29,7 @@#include <linux/livepatch.h> #include <linux/elf.h> #include <linux/moduleloader.h> +#include <linux/completion.h> #include <asm/cacheflush.h> #include "patch.h" #include "transition.h" @@ -371,6 +372,18 @@ static int __klp_enable_patch(struct klp_patch *patch) !list_prev_entry(patch, list)->enabled)return -EBUSY ; +/* +* A reference is taken on the patch module to prevent it from being +* unloaded. +* +* Note: For immediate (no consistency model) patches we don't allow +* patch modules to unload since there is no safe/sane method to +* determine if a thread is still running in the patched code contained +* in the patch module once the ftrace registration is successful. +*/ +if (!try_module_get (patch->mod)) +return -ENODEV ; +pr_notice_once ("tainting kernel with TAINT_LIVEPATCH\n" );add_taint (TAINT_LIVEPATCH ,LOCKDEP_STILL_OK );@@ -459,6 +472,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,mutex_lock (&klp_mutex); +if (!klp_is_patch_registered (patch)) { +/* +* Module with the patch could either disappear meanwhile or is +* not properly initialized yet. +*/ + ret = -EINVAL ; + goto err; + } +if (patch->enabled == val) {/* already in requested state */ ret = -EINVAL ;@@ -515,10 +537,10 @@ static struct attribute *klp_patch_attrs[] = {static void klp_kobj_release_patch (struct kobject *kobj) { -/* -* Once we have a consistency model we'll need to module_put() the -* patch module here. See klp_register_patch() for more details. -*/ +struct klp_patch *patch; + + patch = container_of(kobj, struct klp_patch, kobj); +complete (&patch->finish); }static struct kobj_type klp_ktype_patch = {@@ -589,7 +611,6 @@ static void klp_free_patch(struct klp_patch *patch)klp_free_objects_limited (patch,NULL );if (!list_empty (&patch->list))list_del (&patch->list); -kobject_put (&patch->kobj); }static int klp_init_func (struct klp_object *obj,struct klp_func *func)@@ -700,11 +721,14 @@ static int klp_init_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); patch->enabled = false; +init_completion (&patch->finish); ret =kobject_init_and_add (&patch->kobj, &klp_ktype_patch, klp_root_kobj,"%s" , patch->mod->name); -if (ret) - goto unlock; +if (ret) { +mutex_unlock (&klp_mutex); +return ret; + }klp_for_each_object (patch, obj) { ret =klp_init_object (patch, obj);@@ -720,9 +744,12 @@ static int klp_init_patch(struct klp_patch *patch) free:klp_free_objects_limited (patch, obj); -kobject_put (&patch->kobj); -unlock: +mutex_unlock (&klp_mutex); + +kobject_put (&patch->kobj); +wait_for_completion (&patch->finish); +return ret; }@@ -736,23 +763,29 @@ static int klp_init_patch(struct klp_patch *patch)*/ int klp_unregister_patch (struct klp_patch *patch) { -int ret =0 ; +int ret;mutex_lock (&klp_mutex);if (!klp_is_patch_registered (patch)) { ret = -EINVAL ; - goto out; + goto err; }if (patch->enabled) { ret = -EBUSY ; - goto out; + goto err; }klp_free_patch (patch); -out: +mutex_unlock (&klp_mutex); + +kobject_put (&patch->kobj); +wait_for_completion (&patch->finish); + +return 0 ; +err:mutex_unlock (&klp_mutex);return ret; }@@ -765,12 +798,13 @@ EXPORT_SYMBOL_GPL(klp_unregister_patch);* Initializes the data structure associated with the patch and * creates the sysfs interface. * +* There is no need to take the reference on the patch module here. It is done +* later when the patch is enabled. +* * Return: 0 on success, otherwise error */ int klp_register_patch (struct klp_patch *patch) { -int ret; -if (!patch || !patch->mod)return -EINVAL ;@@ -783,21 +817,7 @@ int klp_register_patch(struct klp_patch *patch)if (!klp_initialized ())return -ENODEV ; -/* -* A reference is taken on the patch module to prevent it from being -* unloaded. Right now, we don't allow patch modules to unload since -* there is currently no method to determine if a thread is still -* running in the patched code contained in the patch module once -* the ftrace registration is successful. -*/ -if (!try_module_get (patch->mod)) -return -ENODEV ; - - ret =klp_init_patch (patch); -if (ret) -module_put (patch->mod); - -return ret; +return klp_init_patch (patch); }EXPORT_SYMBOL_GPL (klp_register_patch);kernel/livepatch/transition.c +1
-0@@ -197,13 +197,21 @@ void klp_complete_transition(void)struct klp_func *func;struct task_struct *g, *task;unsigned int cpu; +bool is_immediate = false;
kernel/livepatch/transition.c void klp_complete_transition(void) · line 209
Quoted 10 lines
kernel/livepatch/transition.c +4
-2if (klp_transition_patch->immediate) goto done; -klp_for_each_object (klp_transition_patch, obj) -klp_for_each_func (obj, func) +klp_for_each_object (klp_transition_patch, obj) { +klp_for_each_func (obj, func) { func->transition = false; +if (func->immediate) + is_immediate = true;
Once an immediate function is found you could return from this function since releasing a reference from the livepatch module is no longer possible.
--chris
Quoted 27 lines
kernel/livepatch/transition.c +5
-0+ } + } + +if (klp_target_state ==KLP_UNPATCHED && !is_immediate) +module_put (klp_transition_patch->mod);read_lock (&tasklist_lock);for_each_process_thread (g, task) {samples/livepatch/livepatch-sample.c +0
-1@@ -82,7 +82,6 @@ static int livepatch_init(void)static void livepatch_exit (void ) { -WARN_ON (klp_disable_patch (&patch));WARN_ON (klp_unregister_patch (&patch)); }Signature · 6 lines
2.8.3 -- To unsubscribe from this list: send the line "unsubscribe live-patching" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Miroslav Benes
maintainerQuoted 15 lines · Christopher Arges
Quoted 14 lines · Miroslav Benes
kernel/livepatch/transition.c +1
-0@@ -197,13 +197,21 @@ void klp_complete_transition(void)struct klp_func *func;struct task_struct *g, *task;unsigned int cpu; +bool is_immediate = false;if (klp_transition_patch->immediate) goto done;
Quoted 10 lines
kernel/livepatch/transition.c void klp_complete_transition(void) · line 209
Quoted 7 lines
kernel/livepatch/transition.c +4
-2-klp_for_each_object (klp_transition_patch, obj) -klp_for_each_func (obj, func) +klp_for_each_object (klp_transition_patch, obj) { +klp_for_each_func (obj, func) { func->transition = false; +if (func->immediate) + is_immediate = true;Once an immediate function is found you could return from this function since releasing a reference from the livepatch module is no longer possible.
That is true, but I think we should tidy up after the patching process even in this case. That is clear all func->transition, task->patch_state and task->TIF_PATCH_PENDING flags. These all could be set if patch->immediate is false and one of the func->immediate is true.
Regards,
Miroslav
Petr Mladek
maintainerQuoted 73 lines · Miroslav Benes
Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code.
The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe.
We can safely let the patch module go after that.
Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit
942e443127e9("module: Fixmod->mkobj.kobjpotentially freed too early").Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm.
With this change a call to
try_module_get()is moved to__klp_enable_patchfromklp_register_patchto make module reference counting symmetric (module_put()is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled.Also all
kobject_put(&patch->kobj)calls are moved outside ofklp_mutexlock protection to prevent a deadlock situation whenklp_unregister_patchis called and sysfs directories are removed. There is no need to do the same for otherkobject_put()callsites as we currently do not have their sysfs counterparts.Signed-off-by Miroslav Benes <[email protected]>---
Based on Josh's v2 of the consistency model.kernel/livepatch/core.c +14
-2@@ -459,6 +472,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,mutex_lock (&klp_mutex); +if (!klp_is_patch_registered (patch)) { +/* +* Module with the patch could either disappear meanwhile or is +* not properly initialized yet. +*/ + ret = -EINVAL ; + goto err; + } +if (patch->enabled == val) {/* already in requested state */ ret = -EINVAL ;@@ -700,11 +721,14 @@ static int klp_init_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); patch->enabled = false; +init_completion (&patch->finish); ret =kobject_init_and_add (&patch->kobj, &klp_ktype_patch, klp_root_kobj,"%s" , patch->mod->name); -if (ret) - goto unlock; +if (ret) { +mutex_unlock (&klp_mutex); +return ret; + }klp_for_each_object (patch, obj) { ret =klp_init_object (patch, obj);
kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 749
Quoted 8 lines
kernel/livepatch/core.c +2
-2@@ -720,9 +744,12 @@ static int klp_init_patch(struct klp_patch *patch) free:klp_free_objects_limited (patch, obj); -kobject_put (&patch->kobj); -unlock: +mutex_unlock (&klp_mutex); +
Just for record. The sysfs entry exists but patch->list is not initialized in this error path. Therefore we could write into
/sys/.../livepatch_foo/enable
and call enabled_store(). It is safe because enabled_store() calls klp_is_patch_registered() and it does not need patch->list for this check. Kudos for the klp_is_patch_registered() implementation.
I write this because it is not obvious and it took me some time to verify that we are on the safe side.
Well, I would feel more comfortable if we initialize patch->list above.
Quoted 11 lines
kernel/livepatch/core.c +3
-0+kobject_put (&patch->kobj); +wait_for_completion (&patch->finish); +return ret; }samples/livepatch/livepatch-sample.c +0
-0
samples/livepatch/livepatch-sample.c static int livepatch_init(void) · line 85
Quoted 5 lines
samples/livepatch/livepatch-sample.c +0
-1@@ -82,7 +82,6 @@ static int livepatch_init(void)static void livepatch_exit (void ) { -WARN_ON (klp_disable_patch (&patch));WARN_ON (klp_unregister_patch (&patch));
Just for record. I was curious if the following race:
CPU0 CPU1
rmmod livepatch_foo
livepatch_exit()
echo 1> /sys/.../livepatch_foo/enable
__klp_enable_patch()
lock(&klp_mutex)
...
patch->enabled = true;
...
unlock(&klp_mutex)
klp_unregister_patch()
lock(&klp_mutex);
if (patch->enabled)
ret = -EBUSY;
unlock(&klp_mutex);Fortunately, this is not possible. livepatch_exit() is called when the module is in MODULE_STATE_GOING. It means that try_module_get() will fail and therefore __klp_enable_patch() will fail.
All in all, the patch looks good to me. I am fine with the completion. In fact, I like it.
Best Regards,
Petr
+++ Petr Mladek [07/06/16 11:36 +0200]:
Quoted 92 lines · Miroslav Benes
Quoted 73 lines
Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code.
The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe.
We can safely let the patch module go after that.
Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit
942e443127e9("module: Fixmod->mkobj.kobjpotentially freed too early").Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm.
With this change a call to
try_module_get()is moved to__klp_enable_patchfromklp_register_patchto make module reference counting symmetric (module_put()is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled.Also all
kobject_put(&patch->kobj)calls are moved outside ofklp_mutexlock protection to prevent a deadlock situation whenklp_unregister_patchis called and sysfs directories are removed. There is no need to do the same for otherkobject_put()callsites as we currently do not have their sysfs counterparts.Signed-off-by Miroslav Benes <[email protected]>---
Based on Josh's v2 of the consistency model.kernel/livepatch/core.c +14
-2@@ -459,6 +472,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,mutex_lock (&klp_mutex); +if (!klp_is_patch_registered (patch)) { +/* +* Module with the patch could either disappear meanwhile or is +* not properly initialized yet. +*/ + ret = -EINVAL ; + goto err; + } +if (patch->enabled == val) {/* already in requested state */ ret = -EINVAL ;@@ -700,11 +721,14 @@ static int klp_init_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); patch->enabled = false; +init_completion (&patch->finish); ret =kobject_init_and_add (&patch->kobj, &klp_ktype_patch, klp_root_kobj,"%s" , patch->mod->name); -if (ret) - goto unlock; +if (ret) { +mutex_unlock (&klp_mutex); +return ret; + }klp_for_each_object (patch, obj) { ret =klp_init_object (patch, obj);kernel/livepatch/core.c static int klp_init_patch(struct klp_patch *patch) · line 749
Quoted 8 lines
kernel/livepatch/core.c +2
-2@@ -720,9 +744,12 @@ static int klp_init_patch(struct klp_patch *patch) free:klp_free_objects_limited (patch, obj); -kobject_put (&patch->kobj); -unlock: +mutex_unlock (&klp_mutex); +Just for record. The sysfs entry exists but
patch->listis not initialized in this error path. Therefore we could write into
/sys/.../livepatch_foo/enableand call
enabled_store(). It is safe becauseenabled_store()callsklp_is_patch_registered()and it does not needpatch->listfor this check. Kudos for theklp_is_patch_registered()implementation.
Quoted 5 lines
I write this because it is not obvious and it took me some time to verify that we are on the safe side.
Well, I would feel more comfortable if we initialize
patch->listabove.
Hi Petr,
I'm a bit unclear on this, can you clarify what you mean by initialize patch->list? I don't think any extra initialization is needed here to be able use a patch->list node with an existing list (klp_patches), since this field is embedded and the klp_patches list header is already statically initialized with LIST_HEAD.
Jessica
Petr Mladek
maintainerQuoted 99 lines · Jessica Yu
+++ Petr Mladek [07/06/16 11:36 +0200]:
Quoted 92 lines · Miroslav Benes
Quoted 80 lines
Currently we do not allow patch module to unload since there is no method to determine if a task is still running in the patched code.
The consistency model gives us the way because when the unpatching finishes we know that all tasks were marked as safe to call an original function. Thus every new call to the function calls the original code and at the same time no task can be somewhere in the patched code, because it had to leave that code to be marked as safe.
We can safely let the patch module go after that.
Completion is used for synchronization between module removal and sysfs infrastructure in a similar way to commit
942e443127e9("module: Fixmod->mkobj.kobjpotentially freed too early").Note that we still do not allow the removal for immediate model, that is no consistency model. The module refcount may increase in this case if somebody disables and enables the patch several times. This should not cause any harm.
With this change a call to
try_module_get()is moved to__klp_enable_patchfromklp_register_patchto make module reference counting symmetric (module_put()is in a patch disable path) and to allow to take a new reference to a disabled module when being enabled.Also all
kobject_put(&patch->kobj)calls are moved outside ofklp_mutexlock protection to prevent a deadlock situation whenklp_unregister_patchis called and sysfs directories are removed. There is no need to do the same for otherkobject_put()callsites as we currently do not have their sysfs counterparts.Signed-off-by Miroslav Benes <[email protected]>---
Based on Josh's v2 of the consistency model.kernel/livepatch/core.c +14
-2@@ -459,6 +472,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,mutex_lock (&klp_mutex); +if (!klp_is_patch_registered (patch)) { +/* +* Module with the patch could either disappear meanwhile or is +* not properly initialized yet. +*/ + ret = -EINVAL ; + goto err; + } +if (patch->enabled == val) {/* already in requested state */ ret = -EINVAL ;@@ -700,11 +721,14 @@ static int klp_init_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); patch->enabled = false; +init_completion (&patch->finish); ret =kobject_init_and_add (&patch->kobj, &klp_ktype_patch, klp_root_kobj,"%s" , patch->mod->name); -if (ret) - goto unlock; +if (ret) { +mutex_unlock (&klp_mutex); +return ret; + }klp_for_each_object (patch, obj) { ret =klp_init_object (patch, obj);@@ -720,9 +744,12 @@ static int klp_init_patch(struct klp_patch *patch)free:
klp_free_objects_limited(patch, obj);kernel/livepatch/core.c +1
-2-kobject_put (&patch->kobj); -unlock: +mutex_unlock(&klp_mutex);Quoted 1 line
+
Just for record. The sysfs entry exists but
patch->listis not initialized in this error path. Therefore we could write into
/sys/.../livepatch_foo/enableand call
enabled_store(). It is safe becauseenabled_store()callsklp_is_patch_registered()and it does not needpatch->listfor this check. Kudos for theklp_is_patch_registered()implementation.Quoted 5 lines
I write this because it is not obvious and it took me some time to verify that we are on the safe side.
Well, I would feel more comfortable if we initialize
patch->listabove.
Quoted 7 lines
Hi Petr,
I'm a bit unclear on this, can you clarify what you mean by initialize
patch->list? I don't think any extra initialization is needed here to be able use apatch->listnode with an existing list (klp_patches), since this field is embedded and theklp_patcheslist header is already statically initialized withLIST_HEAD.
I meant to call
INIT_LIST_HEAD(&patch->list);
around the existing initialization:
patch->enabled = false; init_completion(&patch->finish);
The list is zeroed by default. This is true at least in the livepatch_sample.c. It means that it is not a valid list head without an extra initialization.
Also note that we do a check for list_empty(&patch->list) in klp_free_patch(). This check is useless if we do not initialize the list.
Best Regards,
Petr