Skip to contentSkip to conversation

livepatch: add locking to force and signal functions

PATCH Superseded Miroslav Benes 2 participants 3 messages Dec 20, 2017 Live patching

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Miroslav Benes

author maintainer
#0 lore raw

PATCH 1/1 livepatch: add locking to force and signal functions

klp_send_signals() and klp_force_transition() do not acquire klp_mutex, because it seemed to be superfluous. A potential race in klp_send_signals() was harmless and there was nothing in klp_force_transition() which needed to be synchronized. That changed with the addition of klp_forced variable during the review process.

There is a small window now, when klp_complete_transition() does not see klp_forced set to true while all tasks have been already transitioned to the target state. module_put() is called and the module can be removed.

Acquire klp_mutex to prevent it. Do the same in klp_send_signals() just to be sure. There is no real downside to that.

---
 kernel/livepatch/transition.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/livepatch/transition.c +8 -0
index be5bfa533ee8..3f932ff607cd 100644
@@ -625,6 +625,8 @@ void klp_send_signals(void)
 
 	pr_notice("signaling remaining tasks\n");
 
+	mutex_lock(&klp_mutex);
+
 	read_lock(&tasklist_lock);
 	for_each_process_thread(g, task) {
 		if (!klp_patch_pending(task))
@@ -653,6 +655,8 @@ void klp_send_signals(void)
 		}
 	}
 	read_unlock(&tasklist_lock);
+
+	mutex_unlock(&klp_mutex);
 }
 
 /*
@@ -671,6 +675,8 @@ void klp_force_transition(void)
 
 	pr_warn("forcing remaining tasks to the patched state\n");
 
+	mutex_lock(&klp_mutex);
+
 	read_lock(&tasklist_lock);
 	for_each_process_thread(g, task)
 		klp_update_patch_state(task);
@@ -680,4 +686,6 @@ void klp_force_transition(void)
 		klp_update_patch_state(idle_task(cpu));
 
 	klp_forced = true;
+
+	mutex_unlock(&klp_mutex);
 }
Signature · 1 line
2.15.1
maintainer
#1 lore raw
Quoted 33 lines · Miroslav Benes

klp_send_signals() and klp_force_transition() do not acquire klp_mutex, because it seemed to be superfluous. A potential race in klp_send_signals() was harmless and there was nothing in klp_force_transition() which needed to be synchronized. That changed with the addition of klp_forced variable during the review process.

There is a small window now, when klp_complete_transition() does not see klp_forced set to true while all tasks have been already transitioned to the target state. module_put() is called and the module can be removed.

Acquire klp_mutex to prevent it. Do the same in klp_send_signals() just to be sure. There is no real downside to that.

---
 kernel/livepatch/transition.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/livepatch/transition.c +2 -0
index be5bfa533ee8..3f932ff607cd 100644
@@ -625,6 +625,8 @@ void klp_send_signals(void)
 
 	pr_notice("signaling remaining tasks\n");
 
+	mutex_lock(&klp_mutex);
+
 	read_lock(&tasklist_lock);
 	for_each_process_thread(g, task) {
 		if (!klp_patch_pending(task))

kernel/livepatch/transition.c void klp_send_signals(void) · line 659

Quoted 5 lines
kernel/livepatch/transition.c +2 -0
@@ -653,6 +655,8 @@ void klp_send_signals(void)
 		}
 	}
 	read_unlock(&tasklist_lock);
+
+	mutex_unlock(&klp_mutex);

It would be cleaner if the lock guarded also the check:

	if (patch != klp_transition_patch)
		return -EINVAL;

in signal_store(). Then we could remove also the comment above this check.

Same is true also for the force part stuff.

Best Regards,
Petr

PS: I am sorry that I hand waved the proposed solution when we spoke about it yeasterday. I should have looked into the code.

author maintainer
#2 lore raw
Quoted 42 lines · Petr Mladek
Quoted 33 lines · Miroslav Benes

klp_send_signals() and klp_force_transition() do not acquire klp_mutex, because it seemed to be superfluous. A potential race in klp_send_signals() was harmless and there was nothing in klp_force_transition() which needed to be synchronized. That changed with the addition of klp_forced variable during the review process.

There is a small window now, when klp_complete_transition() does not see klp_forced set to true while all tasks have been already transitioned to the target state. module_put() is called and the module can be removed.

Acquire klp_mutex to prevent it. Do the same in klp_send_signals() just to be sure. There is no real downside to that.

---
 kernel/livepatch/transition.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/livepatch/transition.c +2 -0
index be5bfa533ee8..3f932ff607cd 100644
@@ -625,6 +625,8 @@ void klp_send_signals(void)
 
 	pr_notice("signaling remaining tasks\n");
 
+	mutex_lock(&klp_mutex);
+
 	read_lock(&tasklist_lock);
 	for_each_process_thread(g, task) {
 		if (!klp_patch_pending(task))

kernel/livepatch/transition.c void klp_send_signals(void) · line 659

Quoted 5 lines
kernel/livepatch/transition.c +2 -0
@@ -653,6 +655,8 @@ void klp_send_signals(void)
 		}
 	}
 	read_unlock(&tasklist_lock);
+
+	mutex_unlock(&klp_mutex);

It would be cleaner if the lock guarded also the check:

Quoted 7 lines
	if (patch != klp_transition_patch)
		return -EINVAL;

in signal_store(). Then we could remove also the comment above this check.

Same is true also for the force part stuff.

And I even left obsolete comments in sysfs callbacks. Sigh. v2 is inevitable...

Miroslav