Skip to content

Commit 50fb4f7

Browse files
committed
freezer: fix current->state restoration race in refrigerator()
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>
1 parent 6fe4c6d commit 50fb4f7

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎kernel/freezer.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ void refrigerator(void)
5858
current->flags &= ~PF_FREEZING;
5959

6060
pr_debug("%s left refrigerator\n", current->comm);
61-
__set_current_state(save);
61+
62+
/*
63+
* Restore saved task state before returning. The mb'd version
64+
* needs to be used; otherwise, it might silently break
65+
* synchronization which depends on ordered task state change.
66+
*/
67+
set_current_state(save);
6268
}
6369
EXPORT_SYMBOL(refrigerator);
6470

0 commit comments

Comments
 (0)