Skip to content

Commit 3ec2477

Browse files
jpoimboeJiri Kosina
authored and
Jiri Kosina
committed
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 942e443 ("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/[email protected] [Thanks to Petr Mladek for improving the commit message.] Signed-off-by: Miroslav Benes <[email protected]> Signed-off-by: Josh Poimboeuf <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Acked-by: Miroslav Benes <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 7c23b33 commit 3ec2477

File tree

5 files changed

+96
-53
lines changed

5 files changed

+96
-53
lines changed

Documentation/livepatch/livepatch.txt

+9-19
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,15 @@ section "Livepatch life-cycle" below for more details about these
316316
two operations.
317317

318318
Module removal is only safe when there are no users of the underlying
319-
functions. The immediate consistency model is not able to detect this;
320-
therefore livepatch modules cannot be removed. See "Limitations" below.
319+
functions. The immediate consistency model is not able to detect this. The
320+
code just redirects the functions at the very beginning and it does not
321+
check if the functions are in use. In other words, it knows when the
322+
functions get called but it does not know when the functions return.
323+
Therefore it cannot be decided when the livepatch module can be safely
324+
removed. This is solved by a hybrid consistency model. When the system is
325+
transitioned to a new patch state (patched/unpatched) it is guaranteed that
326+
no task sleeps or runs in the old code.
327+
321328

322329
5. Livepatch life-cycle
323330
=======================
@@ -469,23 +476,6 @@ The current Livepatch implementation has several limitations:
469476
by "notrace".
470477

471478

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-
489479

490480
+ Livepatch works reliably only when the dynamic ftrace is located at
491481
the very beginning of the function.

include/linux/livepatch.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <linux/module.h>
2525
#include <linux/ftrace.h>
26+
#include <linux/completion.h>
2627

2728
#if IS_ENABLED(CONFIG_LIVEPATCH)
2829

@@ -114,6 +115,7 @@ struct klp_object {
114115
* @list: list node for global list of registered patches
115116
* @kobj: kobject for sysfs resources
116117
* @enabled: the patch is enabled (but operation may be incomplete)
118+
* @finish: for waiting till it is safe to remove the patch module
117119
*/
118120
struct klp_patch {
119121
/* external */
@@ -125,6 +127,7 @@ struct klp_patch {
125127
struct list_head list;
126128
struct kobject kobj;
127129
bool enabled;
130+
struct completion finish;
128131
};
129132

130133
#define klp_for_each_object(patch, obj) \

kernel/livepatch/core.c

+50-30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/livepatch.h>
3030
#include <linux/elf.h>
3131
#include <linux/moduleloader.h>
32+
#include <linux/completion.h>
3233
#include <asm/cacheflush.h>
3334
#include "patch.h"
3435
#include "transition.h"
@@ -354,6 +355,18 @@ static int __klp_enable_patch(struct klp_patch *patch)
354355
!list_prev_entry(patch, list)->enabled)
355356
return -EBUSY;
356357

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+
357370
pr_notice("enabling patch '%s'\n", patch->mod->name);
358371

359372
klp_init_transition(patch, KLP_PATCHED);
@@ -442,6 +455,15 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
442455

443456
mutex_lock(&klp_mutex);
444457

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+
445467
if (patch->enabled == enabled) {
446468
/* already in requested state */
447469
ret = -EINVAL;
@@ -498,10 +520,10 @@ static struct attribute *klp_patch_attrs[] = {
498520

499521
static void klp_kobj_release_patch(struct kobject *kobj)
500522
{
501-
/*
502-
* Once we have a consistency model we'll need to module_put() the
503-
* patch module here. See klp_register_patch() for more details.
504-
*/
523+
struct klp_patch *patch;
524+
525+
patch = container_of(kobj, struct klp_patch, kobj);
526+
complete(&patch->finish);
505527
}
506528

507529
static struct kobj_type klp_ktype_patch = {
@@ -572,7 +594,6 @@ static void klp_free_patch(struct klp_patch *patch)
572594
klp_free_objects_limited(patch, NULL);
573595
if (!list_empty(&patch->list))
574596
list_del(&patch->list);
575-
kobject_put(&patch->kobj);
576597
}
577598

578599
static 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)
695716
mutex_lock(&klp_mutex);
696717

697718
patch->enabled = false;
719+
init_completion(&patch->finish);
698720

699721
ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
700722
klp_root_kobj, "%s", patch->mod->name);
701-
if (ret)
702-
goto unlock;
723+
if (ret) {
724+
mutex_unlock(&klp_mutex);
725+
return ret;
726+
}
703727

704728
klp_for_each_object(patch, obj) {
705729
ret = klp_init_object(patch, obj);
@@ -715,9 +739,12 @@ static int klp_init_patch(struct klp_patch *patch)
715739

716740
free:
717741
klp_free_objects_limited(patch, obj);
718-
kobject_put(&patch->kobj);
719-
unlock:
742+
720743
mutex_unlock(&klp_mutex);
744+
745+
kobject_put(&patch->kobj);
746+
wait_for_completion(&patch->finish);
747+
721748
return ret;
722749
}
723750

@@ -731,23 +758,29 @@ static int klp_init_patch(struct klp_patch *patch)
731758
*/
732759
int klp_unregister_patch(struct klp_patch *patch)
733760
{
734-
int ret = 0;
761+
int ret;
735762

736763
mutex_lock(&klp_mutex);
737764

738765
if (!klp_is_patch_registered(patch)) {
739766
ret = -EINVAL;
740-
goto out;
767+
goto err;
741768
}
742769

743770
if (patch->enabled) {
744771
ret = -EBUSY;
745-
goto out;
772+
goto err;
746773
}
747774

748775
klp_free_patch(patch);
749776

750-
out:
777+
mutex_unlock(&klp_mutex);
778+
779+
kobject_put(&patch->kobj);
780+
wait_for_completion(&patch->finish);
781+
782+
return 0;
783+
err:
751784
mutex_unlock(&klp_mutex);
752785
return ret;
753786
}
@@ -760,12 +793,13 @@ EXPORT_SYMBOL_GPL(klp_unregister_patch);
760793
* Initializes the data structure associated with the patch and
761794
* creates the sysfs interface.
762795
*
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+
*
763799
* Return: 0 on success, otherwise error
764800
*/
765801
int klp_register_patch(struct klp_patch *patch)
766802
{
767-
int ret;
768-
769803
if (!patch || !patch->mod)
770804
return -EINVAL;
771805

@@ -788,21 +822,7 @@ int klp_register_patch(struct klp_patch *patch)
788822
return -ENOSYS;
789823
}
790824

791-
/*
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;
825+
return klp_init_patch(patch);
806826
}
807827
EXPORT_SYMBOL_GPL(klp_register_patch);
808828

kernel/livepatch/transition.c

+34-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static void klp_complete_transition(void)
5959
struct klp_func *func;
6060
struct task_struct *g, *task;
6161
unsigned int cpu;
62+
bool immediate_func = false;
6263

6364
if (klp_target_state == KLP_UNPATCHED) {
6465
/*
@@ -79,9 +80,16 @@ static void klp_complete_transition(void)
7980
if (klp_transition_patch->immediate)
8081
goto done;
8182

82-
klp_for_each_object(klp_transition_patch, obj)
83-
klp_for_each_func(obj, func)
83+
klp_for_each_object(klp_transition_patch, obj) {
84+
klp_for_each_func(obj, func) {
8485
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);
8593

8694
/* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */
8795
if (klp_target_state == KLP_PATCHED)
@@ -113,8 +121,31 @@ static void klp_complete_transition(void)
113121
*/
114122
void klp_cancel_transition(void)
115123
{
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;
117133
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);
118149
}
119150

120151
/*

samples/livepatch/livepatch-sample.c

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ static int livepatch_init(void)
9999

100100
static void livepatch_exit(void)
101101
{
102-
WARN_ON(klp_disable_patch(&patch));
103102
WARN_ON(klp_unregister_patch(&patch));
104103
}
105104

0 commit comments

Comments
 (0)