Skip to content

Commit 6800728

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Don't block the removal of patches loaded after a forced transition
module_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 give the impression 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 be confusing. 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. Signed-off-by: Petr Mladek <[email protected]> Acked-by: Joe Lawrence <[email protected]> Acked-by: Miroslav Benes <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 0430f78 commit 6800728

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

include/linux/livepatch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct klp_object {
143143
* @kobj: kobject for sysfs resources
144144
* @kobj_added: @kobj has been added and needs freeing
145145
* @enabled: the patch is enabled (but operation may be incomplete)
146+
* @forced: was involved in a forced transition
146147
* @finish: for waiting till it is safe to remove the patch module
147148
*/
148149
struct klp_patch {
@@ -155,6 +156,7 @@ struct klp_patch {
155156
struct kobject kobj;
156157
bool kobj_added;
157158
bool enabled;
159+
bool forced;
158160
struct completion finish;
159161
};
160162

kernel/livepatch/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
*/
4646
DEFINE_MUTEX(klp_mutex);
4747

48-
static LIST_HEAD(klp_patches);
48+
/* Registered patches */
49+
LIST_HEAD(klp_patches);
4950

5051
static struct kobject *klp_root_kobj;
5152

@@ -659,6 +660,7 @@ static int klp_init_patch_early(struct klp_patch *patch)
659660
INIT_LIST_HEAD(&patch->list);
660661
patch->kobj_added = false;
661662
patch->enabled = false;
663+
patch->forced = false;
662664
init_completion(&patch->finish);
663665

664666
klp_for_each_object(patch, obj) {

kernel/livepatch/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/livepatch.h>
66

77
extern struct mutex klp_mutex;
8+
extern struct list_head klp_patches;
89

910
static inline bool klp_is_object_loaded(struct klp_object *obj)
1011
{

kernel/livepatch/transition.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ struct klp_patch *klp_transition_patch;
3333

3434
static int klp_target_state = KLP_UNDEFINED;
3535

36-
static bool klp_forced = false;
37-
3836
/*
3937
* This work can be performed periodically to finish patching or unpatching any
4038
* "straggler" tasks which failed to transition in the first attempt.
@@ -137,10 +135,10 @@ static void klp_complete_transition(void)
137135
klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
138136

139137
/*
140-
* klp_forced set implies unbounded increase of module's ref count if
138+
* patch->forced set implies unbounded increase of module's ref count if
141139
* the module is disabled/enabled in a loop.
142140
*/
143-
if (!klp_forced && klp_target_state == KLP_UNPATCHED)
141+
if (!klp_transition_patch->forced && klp_target_state == KLP_UNPATCHED)
144142
module_put(klp_transition_patch->mod);
145143

146144
klp_target_state = KLP_UNDEFINED;
@@ -620,6 +618,7 @@ void klp_send_signals(void)
620618
*/
621619
void klp_force_transition(void)
622620
{
621+
struct klp_patch *patch;
623622
struct task_struct *g, *task;
624623
unsigned int cpu;
625624

@@ -633,5 +632,6 @@ void klp_force_transition(void)
633632
for_each_possible_cpu(cpu)
634633
klp_update_patch_state(idle_task(cpu));
635634

636-
klp_forced = true;
635+
list_for_each_entry(patch, &klp_patches, list)
636+
patch->forced = true;
637637
}

0 commit comments

Comments
 (0)