Skip to content

Commit 8cfe400

Browse files
htejunrjwysocki
authored andcommitted
Freezer: Fix a race during freezing of TASK_STOPPED tasks
After calling freeze_task(), try_to_freeze_tasks() see whether the task is stopped or traced and if so, considers it to be frozen; however, nothing guarantees that either the task being frozen sees TIF_FREEZE or the freezer sees TASK_STOPPED -> TASK_RUNNING transition. The task being frozen may wake up and not see TIF_FREEZE while the freezer fails to notice the transition and believes the task is still stopped. This patch fixes the race by making freeze_task() always go through fake_signal_wake_up() for applicable tasks. The function goes through the target task's scheduler lock and thus guarantees that either the target sees TIF_FREEZE or try_to_freeze_task() sees TASK_RUNNING. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
1 parent 133f112 commit 8cfe400

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎kernel/freezer.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ bool freeze_task(struct task_struct *p, bool sig_only)
104104
}
105105

106106
if (should_send_signal(p)) {
107-
if (!signal_pending(p))
108-
fake_signal_wake_up(p);
107+
fake_signal_wake_up(p);
108+
/*
109+
* fake_signal_wake_up() goes through p's scheduler
110+
* lock and guarantees that TASK_STOPPED/TRACED ->
111+
* TASK_RUNNING transition can't race with task state
112+
* testing in try_to_freeze_tasks().
113+
*/
109114
} else if (sig_only) {
110115
return false;
111116
} else {

‎kernel/power/process.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ static int try_to_freeze_tasks(bool sig_only)
6464
* perturb a task in TASK_STOPPED or TASK_TRACED.
6565
* It is "frozen enough". If the task does wake
6666
* up, it will immediately call try_to_freeze.
67+
*
68+
* Because freeze_task() goes through p's
69+
* scheduler lock after setting TIF_FREEZE, it's
70+
* guaranteed that either we see TASK_RUNNING or
71+
* try_to_stop() after schedule() in ptrace/signal
72+
* stop sees TIF_FREEZE.
6773
*/
6874
if (!task_is_stopped_or_traced(p) &&
6975
!freezer_should_skip(p))

0 commit comments

Comments
 (0)