History

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.]

Josh Poimboeuf committed 9 years ago

Commit 3ec24771 parent 7c23b335 files · +96 −53Browse files

Unified · Split

5 files
  1. Documentation/livepatch/livepatch.txt +9 −19
  2. include/linux/livepatch.h +3 −0
  3. kernel/livepatch/core.c +50 −30
  4. kernel/livepatch/transition.c +34 −3
  5. samples/livepatch/livepatch-sample.c +0 −1
Documentation/livepatch/livepatch.txt+9−19
@@ -316,8 +316,15 @@ section "Livepatch life-cycle" below for more details about these · 315 unmodified lines
316two operations.316two operations.
317 317
318Module removal is only safe when there are no users of the underlying318Module removal is only safe when there are no users of the underlying
319functions. The immediate consistency model is not able to detect this;319functions. The immediate consistency model is not able to detect this. The
320therefore livepatch modules cannot be removed. See "Limitations" below.320code just redirects the functions at the very beginning and it does not
321check if the functions are in use. In other words, it knows when the
322functions get called but it does not know when the functions return.
323Therefore it cannot be decided when the livepatch module can be safely
324removed. This is solved by a hybrid consistency model. When the system is
325transitioned to a new patch state (patched/unpatched) it is guaranteed that
326no task sleeps or runs in the old code.
327
321 328
3225. Livepatch life-cycle3295. Livepatch life-cycle
323=======================330=======================
@@ -469,23 +476,6 @@ The current Livepatch implementation has several limitations: · 145 unmodified lines
469 by "notrace".476 by "notrace".
470 477
471 478
472 + Livepatch modules can not be removed.
473
474 The current implementation just redirects the functions at the very
475 beginning. It does not check if the functions are in use. In other
476 words, it knows when the functions get called but it does not
477 know when the functions return. Therefore it can not decide when
478 the livepatch module can be safely removed.
479
480 This will get most likely solved once a more complex consistency model
481 is supported. The idea is that a safe state for patching should also
482 mean a safe state for removing the patch.
483
484 Note that the patch itself might get disabled by writing zero
485 to /sys/kernel/livepatch/<patch>/enabled. It causes that the new
486 code will not longer get called. But it does not guarantee
487 that anyone is not sleeping anywhere in the new code.
488
489 479
490 + Livepatch works reliably only when the dynamic ftrace is located at480 + Livepatch works reliably only when the dynamic ftrace is located at
491 the very beginning of the function.481 the very beginning of the function.
include/linux/livepatch.h+3−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
23 23
24#include <linux/module.h>24#include <linux/module.h>
25#include <linux/ftrace.h>25#include <linux/ftrace.h>
26#include <linux/completion.h>
26 27
27#if IS_ENABLED(CONFIG_LIVEPATCH)28#if IS_ENABLED(CONFIG_LIVEPATCH)
28 29
@@ -114,6 +115,7 @@ struct klp_object { · 85 unmodified lines
114 * @list: list node for global list of registered patches115 * @list: list node for global list of registered patches
115 * @kobj: kobject for sysfs resources116 * @kobj: kobject for sysfs resources
116 * @enabled: the patch is enabled (but operation may be incomplete)117 * @enabled: the patch is enabled (but operation may be incomplete)
118 * @finish: for waiting till it is safe to remove the patch module
117 */119 */
118struct klp_patch {120struct klp_patch {
119 /* external */121 /* external */
@@ -125,6 +127,7 @@ struct klp_patch { · 5 unmodified lines
125 struct list_head list;127 struct list_head list;
126 struct kobject kobj;128 struct kobject kobj;
127 bool enabled;129 bool enabled;
130 struct completion finish;
128};131};
129 132
130#define klp_for_each_object(patch, obj) \133#define klp_for_each_object(patch, obj) \
kernel/livepatch/core.c+50−30
@@ -29,6 +29,7 @@ · 28 unmodified lines
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) · 319 unmodified lines
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, · 82 unmodified lines
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[] = { · 50 unmodified lines
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() the524
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) · 64 unmodified lines
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) · 116 unmodified lines
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) · 9 unmodified lines
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: · 7 unmodified lines
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); · 6 unmodified lines
760 * Initializes the data structure associated with the patch and793 * 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 error799 * 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) · 16 unmodified lines
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
kernel/livepatch/transition.c+34−3
@@ -59,6 +59,7 @@ static void klp_complete_transition(void) · 58 unmodified lines
59 struct klp_func *func;59 struct klp_func *func;
60 struct task_struct *g, *task;60 struct task_struct *g, *task;
61 unsigned int cpu;61 unsigned int cpu;
62 bool immediate_func = false;
62 63
63 if (klp_target_state == KLP_UNPATCHED) {64 if (klp_target_state == KLP_UNPATCHED) {
64 /*65 /*
@@ -79,9 +80,16 @@ static void klp_complete_transition(void) · 14 unmodified lines
79 if (klp_transition_patch->immediate)80 if (klp_transition_patch->immediate)
80 goto done;81 goto done;
81 82
82 klp_for_each_object(klp_transition_patch, obj)83 klp_for_each_object(klp_transition_patch, obj) {
83 klp_for_each_func(obj, func)84 klp_for_each_func(obj, func) {
84 func->transition = false;85 func->transition = false;
86 if (func->immediate)
87 immediate_func = true;
88 }
89 }
90
91 if (klp_target_state == KLP_UNPATCHED && !immediate_func)
92 module_put(klp_transition_patch->mod);
85 93
86 /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */94 /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */
87 if (klp_target_state == KLP_PATCHED)95 if (klp_target_state == KLP_PATCHED)
@@ -113,8 +121,31 @@ done: · 25 unmodified lines
113 */121 */
114void klp_cancel_transition(void)122void klp_cancel_transition(void)
115{123{
116 klp_target_state = !klp_target_state;124 struct klp_patch *patch = klp_transition_patch;
125 struct klp_object *obj;
126 struct klp_func *func;
127 bool immediate_func = false;
128
129 if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED))
130 return;
131
132 klp_target_state = KLP_UNPATCHED;
117 klp_complete_transition();133 klp_complete_transition();
134
135 /*
136 * In the enable error path, even immediate patches can be safely
137 * removed because the transition hasn't been started yet.
138 *
139 * klp_complete_transition() doesn't have a module_put() for immediate
140 * patches, so do it here.
141 */
142 klp_for_each_object(patch, obj)
143 klp_for_each_func(obj, func)
144 if (func->immediate)
145 immediate_func = true;
146
147 if (patch->immediate || immediate_func)
148 module_put(patch->mod);
118}149}
119 150
120/*151/*
samples/livepatch/livepatch-sample.c+0−1
@@ -99,7 +99,6 @@ static int livepatch_init(void) · 98 unmodified lines
99 99
100static void livepatch_exit(void)100static void livepatch_exit(void)
101{101{
102 WARN_ON(klp_disable_patch(&patch));
103 WARN_ON(klp_unregister_patch(&patch));102 WARN_ON(klp_unregister_patch(&patch));
104}103}
105 104

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t