Freezer: Fix a race during freezing of TASK_STOPPED tasks
authorTejun Heo <tj@kernel.org>
Fri, 26 Nov 2010 22:07:27 +0000 (23:07 +0100)
committerRafael J. Wysocki <rjw@sisk.pl>
Fri, 24 Dec 2010 14:02:40 +0000 (15:02 +0100)
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>
kernel/freezer.c
kernel/power/process.c

index bd1d42b17cb2f52f5156506ba25d75376102af7f..66ecd2ead215a9cb88ca8ae9f674e1c82766a066 100644 (file)
@@ -104,8 +104,13 @@ bool freeze_task(struct task_struct *p, bool sig_only)
        }
 
        if (should_send_signal(p)) {
-               if (!signal_pending(p))
-                       fake_signal_wake_up(p);
+               fake_signal_wake_up(p);
+               /*
+                * fake_signal_wake_up() goes through p's scheduler
+                * lock and guarantees that TASK_STOPPED/TRACED ->
+                * TASK_RUNNING transition can't race with task state
+                * testing in try_to_freeze_tasks().
+                */
        } else if (sig_only) {
                return false;
        } else {
index e50b4c1b2a0f7f8943acb041b5d8ab202c40d5cd..eb2c88a9e5623efcbcd16b6a6c90fd2e19497885 100644 (file)
@@ -64,6 +64,12 @@ static int try_to_freeze_tasks(bool sig_only)
                         * perturb a task in TASK_STOPPED or TASK_TRACED.
                         * It is "frozen enough".  If the task does wake
                         * up, it will immediately call try_to_freeze.
+                        *
+                        * Because freeze_task() goes through p's
+                        * scheduler lock after setting TIF_FREEZE, it's
+                        * guaranteed that either we see TASK_RUNNING or
+                        * try_to_stop() after schedule() in ptrace/signal
+                        * stop sees TIF_FREEZE.
                         */
                        if (!task_is_stopped_or_traced(p) &&
                            !freezer_should_skip(p))