aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/livepatch/core.c
diff options
context:
space:
mode:
authorJosh Poimboeuf <jpoimboe@redhat.com>2017-03-06 12:20:29 -0500
committerJiri Kosina <jkosina@suse.cz>2017-03-08 03:38:43 -0500
commit3ec24776bfd09668079df7dca0c0136d80820ab4 (patch)
treeb9b1a65ff9a91a0f724d033f60645d2a78df47a3 /kernel/livepatch/core.c
parent7c23b330011690705613a66a8239d2ca64a41d4d (diff)
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. Finally, we need to be very careful about possible races between klp_unregister_patch(), kobject_put() functions and operations on the related sysfs files. kobject_put(&patch->kobj) must be called without klp_mutex. Otherwise, it might be blocked by enabled_store() that needs the mutex as well. In addition, enabled_store() must check if the patch was not unregisted in the meantime. There is no need to do the same for other kobject_put() callsites at the moment. Their sysfs operations neither take the lock nor they access any data that might be freed in the meantime. There was an attempt to use kobjects the right way and prevent these races by design. But it made the patch definition more complicated and opened another can of worms. See https://lkml.kernel.org/r/1464018848-4303-1-git-send-email-pmladek@suse.com [Thanks to Petr Mladek for improving the commit message.] Signed-off-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'kernel/livepatch/core.c')
-rw-r--r--kernel/livepatch/core.c80
1 files changed, 50 insertions, 30 deletions
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 3dc3c9049690..6844c1213df8 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -29,6 +29,7 @@
29#include <linux/livepatch.h> 29#include <linux/livepatch.h>
30#include <linux/elf.h> 30#include <linux/elf.h>
31#include <linux/moduleloader.h> 31#include <linux/moduleloader.h>
32#include <linux/completion.h>
32#include <asm/cacheflush.h> 33#include <asm/cacheflush.h>
33#include "patch.h" 34#include "patch.h"
34#include "transition.h" 35#include "transition.h"
@@ -354,6 +355,18 @@ static int __klp_enable_patch(struct klp_patch *patch)
354 !list_prev_entry(patch, list)->enabled) 355 !list_prev_entry(patch, list)->enabled)
355 return -EBUSY; 356 return -EBUSY;
356 357
358 /*
359 * A reference is taken on the patch module to prevent it from being
360 * unloaded.
361 *
362 * Note: For immediate (no consistency model) patches we don't allow
363 * patch modules to unload since there is no safe/sane method to
364 * determine if a thread is still running in the patched code contained
365 * in the patch module once the ftrace registration is successful.
366 */
367 if (!try_module_get(patch->mod))
368 return -ENODEV;
369
357 pr_notice("enabling patch '%s'\n", patch->mod->name); 370 pr_notice("enabling patch '%s'\n", patch->mod->name);
358 371
359 klp_init_transition(patch, KLP_PATCHED); 372 klp_init_transition(patch, KLP_PATCHED);
@@ -442,6 +455,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
442 455
443 mutex_lock(&klp_mutex); 456 mutex_lock(&klp_mutex);
444 457
458 if (!klp_is_patch_registered(patch)) {
459 /*
460 * Module with the patch could either disappear meanwhile or is
461 * not properly initialized yet.
462 */
463 ret = -EINVAL;
464 goto err;
465 }
466
445 if (patch->enabled == enabled) { 467 if (patch->enabled == enabled) {
446 /* already in requested state */ 468 /* already in requested state */
447 ret = -EINVAL; 469 ret = -EINVAL;
@@ -498,10 +520,10 @@ static struct attribute *klp_patch_attrs[] = {
498 520
499static void klp_kobj_release_patch(struct kobject *kobj) 521static void klp_kobj_release_patch(struct kobject *kobj)
500{ 522{
501 /* 523 struct klp_patch *patch;
502 * Once we have a consistency model we'll need to module_put() the 524
503 * patch module here. See klp_register_patch() for more details. 525 patch = container_of(kobj, struct klp_patch, kobj);
504 */ 526 complete(&patch->finish);
505} 527}
506 528
507static struct kobj_type klp_ktype_patch = { 529static struct kobj_type klp_ktype_patch = {
@@ -572,7 +594,6 @@ static void klp_free_patch(struct klp_patch *patch)
572 klp_free_objects_limited(patch, NULL); 594 klp_free_objects_limited(patch, NULL);
573 if (!list_empty(&patch->list)) 595 if (!list_empty(&patch->list))
574 list_del(&patch->list); 596 list_del(&patch->list);
575 kobject_put(&patch->kobj);
576} 597}
577 598
578static int klp_init_func(struct klp_object *obj, struct klp_func *func) 599static int klp_init_func(struct klp_object *obj, struct klp_func *func)
@@ -695,11 +716,14 @@ static int klp_init_patch(struct klp_patch *patch)
695 mutex_lock(&klp_mutex); 716 mutex_lock(&klp_mutex);
696 717
697 patch->enabled = false; 718 patch->enabled = false;
719 init_completion(&patch->finish);
698 720
699 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, 721 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
700 klp_root_kobj, "%s", patch->mod->name); 722 klp_root_kobj, "%s", patch->mod->name);
701 if (ret) 723 if (ret) {
702 goto unlock; 724 mutex_unlock(&klp_mutex);
725 return ret;
726 }
703 727
704 klp_for_each_object(patch, obj) { 728 klp_for_each_object(patch, obj) {
705 ret = klp_init_object(patch, obj); 729 ret = klp_init_object(patch, obj);
@@ -715,9 +739,12 @@ static int klp_init_patch(struct klp_patch *patch)
715 739
716free: 740free:
717 klp_free_objects_limited(patch, obj); 741 klp_free_objects_limited(patch, obj);
718 kobject_put(&patch->kobj); 742
719unlock:
720 mutex_unlock(&klp_mutex); 743 mutex_unlock(&klp_mutex);
744
745 kobject_put(&patch->kobj);
746 wait_for_completion(&patch->finish);
747
721 return ret; 748 return ret;
722} 749}
723 750
@@ -731,23 +758,29 @@ unlock:
731 */ 758 */
732int klp_unregister_patch(struct klp_patch *patch) 759int klp_unregister_patch(struct klp_patch *patch)
733{ 760{
734 int ret = 0; 761 int ret;
735 762
736 mutex_lock(&klp_mutex); 763 mutex_lock(&klp_mutex);
737 764
738 if (!klp_is_patch_registered(patch)) { 765 if (!klp_is_patch_registered(patch)) {
739 ret = -EINVAL; 766 ret = -EINVAL;
740 goto out; 767 goto err;
741 } 768 }
742 769
743 if (patch->enabled) { 770 if (patch->enabled) {
744 ret = -EBUSY; 771 ret = -EBUSY;
745 goto out; 772 goto err;
746 } 773 }
747 774
748 klp_free_patch(patch); 775 klp_free_patch(patch);
749 776
750out: 777 mutex_unlock(&klp_mutex);
778
779 kobject_put(&patch->kobj);
780 wait_for_completion(&patch->finish);
781
782 return 0;
783err:
751 mutex_unlock(&klp_mutex); 784 mutex_unlock(&klp_mutex);
752 return ret; 785 return ret;
753} 786}
@@ -760,12 +793,13 @@ EXPORT_SYMBOL_GPL(klp_unregister_patch);
760 * Initializes the data structure associated with the patch and 793 * Initializes the data structure associated with the patch and
761 * creates the sysfs interface. 794 * creates the sysfs interface.
762 * 795 *
796 * There is no need to take the reference on the patch module here. It is done
797 * later when the patch is enabled.
798 *
763 * Return: 0 on success, otherwise error 799 * Return: 0 on success, otherwise error
764 */ 800 */
765int klp_register_patch(struct klp_patch *patch) 801int klp_register_patch(struct klp_patch *patch)
766{ 802{
767 int ret;
768
769 if (!patch || !patch->mod) 803 if (!patch || !patch->mod)
770 return -EINVAL; 804 return -EINVAL;
771 805
@@ -788,21 +822,7 @@ int klp_register_patch(struct klp_patch *patch)
788 return -ENOSYS; 822 return -ENOSYS;
789 } 823 }
790 824
791 /* 825 return klp_init_patch(patch);
792 * A reference is taken on the patch module to prevent it from being
793 * unloaded. Right now, we don't allow patch modules to unload since
794 * there is currently no method to determine if a thread is still
795 * running in the patched code contained in the patch module once
796 * the ftrace registration is successful.
797 */
798 if (!try_module_get(patch->mod))
799 return -ENODEV;
800
801 ret = klp_init_patch(patch);
802 if (ret)
803 module_put(patch->mod);
804
805 return ret;
806} 826}
807EXPORT_SYMBOL_GPL(klp_register_patch); 827EXPORT_SYMBOL_GPL(klp_register_patch);
808 828