1 /*
2 * drivers/power/process.c - Functions for starting/stopping processes on
3 * suspend transitions.
4 *
5 * Originally from swsusp.
6 */
7
8
9 #undef DEBUG
10
11 #include <linux/interrupt.h>
12 #include <linux/oom.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19 #include <linux/kmod.h>
20 #include <trace/events/power.h>
21 #include <linux/wakeup_reason.h>
22 #include <linux/cpuset.h>
23
24 /*
25 * Timeout for stopping processes
26 */
27 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
28
try_to_freeze_tasks(bool user_only)29 static int try_to_freeze_tasks(bool user_only)
30 {
31 struct task_struct *g, *p;
32 unsigned long end_time;
33 unsigned int todo;
34 bool wq_busy = false;
35 ktime_t start, end, elapsed;
36 unsigned int elapsed_msecs;
37 bool wakeup = false;
38 int sleep_usecs = USEC_PER_MSEC;
39 #ifdef CONFIG_PM_SLEEP
40 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
41 #endif
42
43 start = ktime_get_boottime();
44
45 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
46
47 if (!user_only)
48 freeze_workqueues_begin();
49
50 while (true) {
51 todo = 0;
52 read_lock(&tasklist_lock);
53 for_each_process_thread(g, p) {
54 if (p == current || !freeze_task(p))
55 continue;
56
57 if (!freezer_should_skip(p))
58 todo++;
59 }
60 read_unlock(&tasklist_lock);
61
62 if (!user_only) {
63 wq_busy = freeze_workqueues_busy();
64 todo += wq_busy;
65 }
66
67 if (!todo || time_after(jiffies, end_time))
68 break;
69
70 if (pm_wakeup_pending()) {
71 #ifdef CONFIG_PM_SLEEP
72 pm_get_active_wakeup_sources(suspend_abort,
73 MAX_SUSPEND_ABORT_LEN);
74 log_suspend_abort_reason(suspend_abort);
75 #endif
76 wakeup = true;
77 break;
78 }
79
80 /*
81 * We need to retry, but first give the freezing tasks some
82 * time to enter the refrigerator. Start with an initial
83 * 1 ms sleep followed by exponential backoff until 8 ms.
84 */
85 usleep_range(sleep_usecs / 2, sleep_usecs);
86 if (sleep_usecs < 8 * USEC_PER_MSEC)
87 sleep_usecs *= 2;
88 }
89
90 end = ktime_get_boottime();
91 elapsed = ktime_sub(end, start);
92 elapsed_msecs = ktime_to_ms(elapsed);
93
94 if (wakeup) {
95 pr_cont("\n");
96 pr_err("Freezing of tasks aborted after %d.%03d seconds",
97 elapsed_msecs / 1000, elapsed_msecs % 1000);
98 } else if (todo) {
99 pr_cont("\n");
100 pr_err("Freezing of tasks failed after %d.%03d seconds"
101 " (%d tasks refusing to freeze, wq_busy=%d):\n",
102 elapsed_msecs / 1000, elapsed_msecs % 1000,
103 todo - wq_busy, wq_busy);
104
105 if (wq_busy)
106 show_workqueue_state();
107
108 read_lock(&tasklist_lock);
109 for_each_process_thread(g, p) {
110 if (p != current && !freezer_should_skip(p)
111 && freezing(p) && !frozen(p))
112 sched_show_task(p);
113 }
114 read_unlock(&tasklist_lock);
115 } else {
116 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
117 elapsed_msecs % 1000);
118 }
119
120 return todo ? -EBUSY : 0;
121 }
122
123 /**
124 * freeze_processes - Signal user space processes to enter the refrigerator.
125 * The current thread will not be frozen. The same process that calls
126 * freeze_processes must later call thaw_processes.
127 *
128 * On success, returns 0. On failure, -errno and system is fully thawed.
129 */
freeze_processes(void)130 int freeze_processes(void)
131 {
132 int error;
133
134 error = __usermodehelper_disable(UMH_FREEZING);
135 if (error)
136 return error;
137
138 /* Make sure this task doesn't get frozen */
139 current->flags |= PF_SUSPEND_TASK;
140
141 if (!pm_freezing)
142 atomic_inc(&system_freezing_cnt);
143
144 pm_wakeup_clear();
145 pr_info("Freezing user space processes ... ");
146 pm_freezing = true;
147 error = try_to_freeze_tasks(true);
148 if (!error) {
149 __usermodehelper_set_disable_depth(UMH_DISABLED);
150 pr_cont("done.");
151 }
152 pr_cont("\n");
153 BUG_ON(in_atomic());
154
155 /*
156 * Now that the whole userspace is frozen we need to disbale
157 * the OOM killer to disallow any further interference with
158 * killable tasks. There is no guarantee oom victims will
159 * ever reach a point they go away we have to wait with a timeout.
160 */
161 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
162 error = -EBUSY;
163
164 if (error)
165 thaw_processes();
166 return error;
167 }
168
169 /**
170 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
171 *
172 * On success, returns 0. On failure, -errno and only the kernel threads are
173 * thawed, so as to give a chance to the caller to do additional cleanups
174 * (if any) before thawing the userspace tasks. So, it is the responsibility
175 * of the caller to thaw the userspace tasks, when the time is right.
176 */
freeze_kernel_threads(void)177 int freeze_kernel_threads(void)
178 {
179 int error;
180
181 pr_info("Freezing remaining freezable tasks ... ");
182
183 pm_nosig_freezing = true;
184 error = try_to_freeze_tasks(false);
185 if (!error)
186 pr_cont("done.");
187
188 pr_cont("\n");
189 BUG_ON(in_atomic());
190
191 if (error)
192 thaw_kernel_threads();
193 return error;
194 }
195
thaw_processes(void)196 void thaw_processes(void)
197 {
198 struct task_struct *g, *p;
199 struct task_struct *curr = current;
200
201 trace_suspend_resume(TPS("thaw_processes"), 0, true);
202 if (pm_freezing)
203 atomic_dec(&system_freezing_cnt);
204 pm_freezing = false;
205 pm_nosig_freezing = false;
206
207 oom_killer_enable();
208
209 pr_info("Restarting tasks ... ");
210
211 __usermodehelper_set_disable_depth(UMH_FREEZING);
212 thaw_workqueues();
213
214 cpuset_wait_for_hotplug();
215
216 read_lock(&tasklist_lock);
217 for_each_process_thread(g, p) {
218 /* No other threads should have PF_SUSPEND_TASK set */
219 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
220 __thaw_task(p);
221 }
222 read_unlock(&tasklist_lock);
223
224 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
225 curr->flags &= ~PF_SUSPEND_TASK;
226
227 usermodehelper_enable();
228
229 schedule();
230 pr_cont("done.\n");
231 trace_suspend_resume(TPS("thaw_processes"), 0, false);
232 }
233
thaw_kernel_threads(void)234 void thaw_kernel_threads(void)
235 {
236 struct task_struct *g, *p;
237
238 pm_nosig_freezing = false;
239 pr_info("Restarting kernel threads ... ");
240
241 thaw_workqueues();
242
243 read_lock(&tasklist_lock);
244 for_each_process_thread(g, p) {
245 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
246 __thaw_task(p);
247 }
248 read_unlock(&tasklist_lock);
249
250 schedule();
251 pr_cont("done.\n");
252 }
253