freezer: fix current->state restoration race in refrigerator()
authorTejun Heo <tj@kernel.org>
Mon, 21 Nov 2011 20:32:22 +0000 (12:32 -0800)
committerTejun Heo <tj@kernel.org>
Mon, 21 Nov 2011 20:32:22 +0000 (12:32 -0800)
refrigerator() saves current->state before entering frozen state and
restores it before returning using __set_current_state(); however,
this is racy, for example, please consider the following sequence.

set_current_state(TASK_INTERRUPTIBLE);
try_to_freeze();
if (kthread_should_stop())
break;
schedule();

If kthread_stop() races with ->state restoration, the restoration can
restore ->state to TASK_INTERRUPTIBLE after kthread_stop() sets it to
TASK_RUNNING but kthread_should_stop() may still see zero
->should_stop because there's no memory barrier between restoring
TASK_INTERRUPTIBLE and kthread_should_stop() test.

This isn't restricted to kthread_should_stop().  current->state is
often used in memory barrier based synchronization and silently
restoring it w/o mb breaks them.

Use set_current_state() instead.

Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/freezer.c

index 7be56c53439723bb1f95ff43b6cb620ef1524193..3f460104a9d6064d0db167f03092c9e1819ce81a 100644 (file)
@@ -58,7 +58,13 @@ void refrigerator(void)
        current->flags &= ~PF_FREEZING;
 
        pr_debug("%s left refrigerator\n", current->comm);
-       __set_current_state(save);
+
+       /*
+        * Restore saved task state before returning.  The mb'd version
+        * needs to be used; otherwise, it might silently break
+        * synchronization which depends on ordered task state change.
+        */
+       set_current_state(save);
 }
 EXPORT_SYMBOL(refrigerator);