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