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