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