blob: b162b74611e475e611723aacb04c857f7638ab53 [file] [log] [blame]
Matt Helsley8174f152008-10-18 20:27:19 -07001/*
2 * kernel/freezer.c - Function to freeze a process
3 *
4 * Originally from kernel/power/process.c
5 */
6
7#include <linux/interrupt.h>
8#include <linux/suspend.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Matt Helsley8174f152008-10-18 20:27:19 -070010#include <linux/syscalls.h>
11#include <linux/freezer.h>
Tejun Heo8a32c442011-11-21 12:32:23 -080012#include <linux/kthread.h>
Matt Helsley8174f152008-10-18 20:27:19 -070013
Tejun Heoa3201222011-11-21 12:32:25 -080014/* total number of freezing conditions in effect */
15atomic_t system_freezing_cnt = ATOMIC_INIT(0);
16EXPORT_SYMBOL(system_freezing_cnt);
17
Pingfan Liu55f25032018-07-31 16:51:32 +080018/* indicate whether PM freezing is in effect, protected by
19 * system_transition_mutex
20 */
Tejun Heoa3201222011-11-21 12:32:25 -080021bool pm_freezing;
22bool pm_nosig_freezing;
23
Tejun Heo85fbd722013-12-18 07:07:32 -050024/*
25 * Temporary export for the deadlock workaround in ata_scsi_hotplug().
26 * Remove once the hack becomes unnecessary.
27 */
28EXPORT_SYMBOL_GPL(pm_freezing);
29
Tejun Heo0c9af092011-11-21 12:32:24 -080030/* protects freezing and frozen transitions */
31static DEFINE_SPINLOCK(freezer_lock);
Matt Helsley8174f152008-10-18 20:27:19 -070032
Tejun Heoa3201222011-11-21 12:32:25 -080033/**
34 * freezing_slow_path - slow path for testing whether a task needs to be frozen
35 * @p: task to be tested
36 *
37 * This function is called by freezing() if system_freezing_cnt isn't zero
38 * and tests whether @p needs to enter and stay in frozen state. Can be
39 * called under any context. The freezers are responsible for ensuring the
40 * target tasks see the updated state.
41 */
42bool freezing_slow_path(struct task_struct *p)
43{
Colin Cross2b44c4d2013-07-24 17:41:33 -070044 if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
Tejun Heoa3201222011-11-21 12:32:25 -080045 return false;
46
Michal Hockoa34c80a2016-07-28 15:45:16 -070047 if (test_tsk_thread_flag(p, TIF_MEMDIE))
Cong Wang51fae6da2014-10-21 09:27:12 +020048 return false;
49
Tejun Heoa3201222011-11-21 12:32:25 -080050 if (pm_nosig_freezing || cgroup_freezing(p))
51 return true;
52
Tejun Heo34b087e2011-11-23 09:28:17 -080053 if (pm_freezing && !(p->flags & PF_KTHREAD))
Tejun Heoa3201222011-11-21 12:32:25 -080054 return true;
55
56 return false;
57}
58EXPORT_SYMBOL(freezing_slow_path);
59
Matt Helsley8174f152008-10-18 20:27:19 -070060/* Refrigerator is place where frozen processes are stored :-). */
Tejun Heo8a32c442011-11-21 12:32:23 -080061bool __refrigerator(bool check_kthr_stop)
Matt Helsley8174f152008-10-18 20:27:19 -070062{
63 /* Hmm, should we be allowed to suspend when there are realtime
64 processes around? */
Tejun Heoa0acae02011-11-21 12:32:22 -080065 bool was_frozen = false;
Tejun Heo5ece3ea2011-11-21 12:32:26 -080066 long save = current->state;
Matt Helsley8174f152008-10-18 20:27:19 -070067
Matt Helsley8174f152008-10-18 20:27:19 -070068 pr_debug("%s entered refrigerator\n", current->comm);
69
Matt Helsley8174f152008-10-18 20:27:19 -070070 for (;;) {
71 set_current_state(TASK_UNINTERRUPTIBLE);
Tejun Heo5ece3ea2011-11-21 12:32:26 -080072
73 spin_lock_irq(&freezer_lock);
74 current->flags |= PF_FROZEN;
Tejun Heo69074832011-11-21 12:32:24 -080075 if (!freezing(current) ||
Tejun Heo8a32c442011-11-21 12:32:23 -080076 (check_kthr_stop && kthread_should_stop()))
Tejun Heo5ece3ea2011-11-21 12:32:26 -080077 current->flags &= ~PF_FROZEN;
78 spin_unlock_irq(&freezer_lock);
79
80 if (!(current->flags & PF_FROZEN))
Matt Helsley8174f152008-10-18 20:27:19 -070081 break;
Tejun Heoa0acae02011-11-21 12:32:22 -080082 was_frozen = true;
Matt Helsley8174f152008-10-18 20:27:19 -070083 schedule();
84 }
Thomas Gleixner6301cb92009-07-17 14:15:47 +020085
Matt Helsley8174f152008-10-18 20:27:19 -070086 pr_debug("%s left refrigerator\n", current->comm);
Tejun Heo50fb4f7f2011-11-21 12:32:22 -080087
88 /*
89 * Restore saved task state before returning. The mb'd version
90 * needs to be used; otherwise, it might silently break
91 * synchronization which depends on ordered task state change.
92 */
93 set_current_state(save);
Tejun Heoa0acae02011-11-21 12:32:22 -080094
95 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070096}
Tejun Heoa0acae02011-11-21 12:32:22 -080097EXPORT_SYMBOL(__refrigerator);
Matt Helsley8174f152008-10-18 20:27:19 -070098
99static void fake_signal_wake_up(struct task_struct *p)
100{
101 unsigned long flags;
102
Tejun Heo37ad8ac2011-11-21 12:32:26 -0800103 if (lock_task_sighand(p, &flags)) {
104 signal_wake_up(p, 0);
105 unlock_task_sighand(p, &flags);
106 }
Matt Helsley8174f152008-10-18 20:27:19 -0700107}
108
109/**
Tejun Heo839e3402011-11-21 12:32:26 -0800110 * freeze_task - send a freeze request to given task
111 * @p: task to send the request to
Matt Helsley8174f152008-10-18 20:27:19 -0700112 *
Marcos Paulo de Souza37f08be2012-02-21 23:57:47 +0100113 * If @p is freezing, the freeze request is sent either by sending a fake
114 * signal (if it's not a kernel thread) or waking it up (if it's a kernel
115 * thread).
Tejun Heo839e3402011-11-21 12:32:26 -0800116 *
117 * RETURNS:
118 * %false, if @p is not freezing or already frozen; %true, otherwise
Matt Helsley8174f152008-10-18 20:27:19 -0700119 */
Tejun Heo839e3402011-11-21 12:32:26 -0800120bool freeze_task(struct task_struct *p)
Matt Helsley8174f152008-10-18 20:27:19 -0700121{
Tejun Heo0c9af092011-11-21 12:32:24 -0800122 unsigned long flags;
Matt Helsley8174f152008-10-18 20:27:19 -0700123
Colin Cross613f5d12013-05-06 23:50:11 +0000124 /*
125 * This check can race with freezer_do_not_count, but worst case that
126 * will result in an extra wakeup being sent to the task. It does not
127 * race with freezer_count(), the barriers in freezer_count() and
128 * freezer_should_skip() ensure that either freezer_count() sees
129 * freezing == true in try_to_freeze() and freezes, or
130 * freezer_should_skip() sees !PF_FREEZE_SKIP and freezes the task
131 * normally.
132 */
133 if (freezer_should_skip(p))
134 return false;
135
Tejun Heo0c9af092011-11-21 12:32:24 -0800136 spin_lock_irqsave(&freezer_lock, flags);
Tejun Heoa3201222011-11-21 12:32:25 -0800137 if (!freezing(p) || frozen(p)) {
138 spin_unlock_irqrestore(&freezer_lock, flags);
139 return false;
140 }
Matt Helsley8174f152008-10-18 20:27:19 -0700141
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200142 if (!(p->flags & PF_KTHREAD))
Tejun Heo8cfe4002010-11-26 23:07:27 +0100143 fake_signal_wake_up(p);
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200144 else
Matt Helsley8174f152008-10-18 20:27:19 -0700145 wake_up_state(p, TASK_INTERRUPTIBLE);
Tejun Heoa3201222011-11-21 12:32:25 -0800146
Tejun Heo0c9af092011-11-21 12:32:24 -0800147 spin_unlock_irqrestore(&freezer_lock, flags);
Tejun Heoa3201222011-11-21 12:32:25 -0800148 return true;
Matt Helsley8174f152008-10-18 20:27:19 -0700149}
150
Tejun Heoa5be2d02011-11-21 12:32:23 -0800151void __thaw_task(struct task_struct *p)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700152{
Tejun Heo0c9af092011-11-21 12:32:24 -0800153 unsigned long flags;
Tejun Heoa5be2d02011-11-21 12:32:23 -0800154
Tejun Heo0c9af092011-11-21 12:32:24 -0800155 spin_lock_irqsave(&freezer_lock, flags);
Tejun Heo34b087e2011-11-23 09:28:17 -0800156 if (frozen(p))
Tejun Heoa5be2d02011-11-21 12:32:23 -0800157 wake_up_process(p);
Tejun Heo0c9af092011-11-21 12:32:24 -0800158 spin_unlock_irqrestore(&freezer_lock, flags);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700159}
Tejun Heo96ee6d82011-11-21 12:32:25 -0800160
161/**
Tejun Heo34b087e2011-11-23 09:28:17 -0800162 * set_freezable - make %current freezable
Tejun Heo96ee6d82011-11-21 12:32:25 -0800163 *
164 * Mark %current freezable and enter refrigerator if necessary.
165 */
Tejun Heo34b087e2011-11-23 09:28:17 -0800166bool set_freezable(void)
Tejun Heo96ee6d82011-11-21 12:32:25 -0800167{
168 might_sleep();
169
170 /*
171 * Modify flags while holding freezer_lock. This ensures the
172 * freezer notices that we aren't frozen yet or the freezing
173 * condition is visible to try_to_freeze() below.
174 */
175 spin_lock_irq(&freezer_lock);
176 current->flags &= ~PF_NOFREEZE;
Tejun Heo96ee6d82011-11-21 12:32:25 -0800177 spin_unlock_irq(&freezer_lock);
178
179 return try_to_freeze();
180}
Tejun Heo34b087e2011-11-23 09:28:17 -0800181EXPORT_SYMBOL(set_freezable);