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 #include <linux/interrupt.h>
10 #include <linux/oom.h>
11 #include <linux/suspend.h>
12 #include <linux/module.h>
13 #include <linux/sched/debug.h>
14 #include <linux/sched/task.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/cpuset.h>
22
23 #include <trace/hooks/power.h>
24
25 /*
26 * Timeout for stopping processes
27 */
28 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
29
try_to_freeze_tasks(bool user_only)30 static int try_to_freeze_tasks(bool user_only)
31 {
32 const char *what = user_only ? "user space processes" :
33 "remaining freezable tasks";
34 struct task_struct *g, *p;
35 unsigned long end_time;
36 unsigned int todo;
37 bool wq_busy = false;
38 ktime_t start, end, elapsed;
39 unsigned int elapsed_msecs;
40 bool wakeup = false;
41 int sleep_usecs = USEC_PER_MSEC;
42
43 pr_info("Freezing %s\n", what);
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 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_err("Freezing of tasks aborted after %d.%03d seconds",
92 elapsed_msecs / 1000, elapsed_msecs % 1000);
93 } else if (todo) {
94 pr_err("Freezing of tasks failed after %d.%03d seconds"
95 " (%d tasks refusing to freeze, wq_busy=%d):\n",
96 elapsed_msecs / 1000, elapsed_msecs % 1000,
97 todo - wq_busy, wq_busy);
98
99 if (wq_busy)
100 show_all_workqueues();
101
102 if (pm_debug_messages_on) {
103 read_lock(&tasklist_lock);
104 for_each_process_thread(g, p) {
105 if (p != current && freezing(p) && !frozen(p)) {
106 sched_show_task(p);
107 trace_android_vh_try_to_freeze_todo_unfrozen(p);
108 }
109 }
110 read_unlock(&tasklist_lock);
111 }
112
113 trace_android_vh_try_to_freeze_todo(todo, elapsed_msecs, wq_busy);
114 } else {
115 pr_info("Freezing %s completed (elapsed %d.%03d seconds)\n",
116 what, elapsed_msecs / 1000, elapsed_msecs % 1000);
117 }
118
119 return todo ? -EBUSY : 0;
120 }
121
122 /**
123 * freeze_processes - Signal user space processes to enter the refrigerator.
124 * The current thread will not be frozen. The same process that calls
125 * freeze_processes must later call thaw_processes.
126 *
127 * On success, returns 0. On failure, -errno and system is fully thawed.
128 */
freeze_processes(void)129 int freeze_processes(void)
130 {
131 int error;
132
133 error = __usermodehelper_disable(UMH_FREEZING);
134 if (error)
135 return error;
136
137 /* Make sure this task doesn't get frozen */
138 current->flags |= PF_SUSPEND_TASK;
139
140 if (!pm_freezing)
141 static_branch_inc(&freezer_active);
142
143 pm_wakeup_clear(0);
144 pm_freezing = true;
145 error = try_to_freeze_tasks(true);
146 if (!error)
147 __usermodehelper_set_disable_depth(UMH_DISABLED);
148
149 BUG_ON(in_atomic());
150
151 /*
152 * Now that the whole userspace is frozen we need to disable
153 * the OOM killer to disallow any further interference with
154 * killable tasks. There is no guarantee oom victims will
155 * ever reach a point they go away we have to wait with a timeout.
156 */
157 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
158 error = -EBUSY;
159
160 if (error)
161 thaw_processes();
162 return error;
163 }
164
165 /**
166 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
167 *
168 * On success, returns 0. On failure, -errno and only the kernel threads are
169 * thawed, so as to give a chance to the caller to do additional cleanups
170 * (if any) before thawing the userspace tasks. So, it is the responsibility
171 * of the caller to thaw the userspace tasks, when the time is right.
172 */
freeze_kernel_threads(void)173 int freeze_kernel_threads(void)
174 {
175 int error;
176
177 pm_nosig_freezing = true;
178 error = try_to_freeze_tasks(false);
179
180 BUG_ON(in_atomic());
181
182 if (error)
183 thaw_kernel_threads();
184 return error;
185 }
186
thaw_processes(void)187 void thaw_processes(void)
188 {
189 struct task_struct *g, *p;
190 struct task_struct *curr = current;
191
192 trace_suspend_resume(TPS("thaw_processes"), 0, true);
193 if (pm_freezing)
194 static_branch_dec(&freezer_active);
195 pm_freezing = false;
196 pm_nosig_freezing = false;
197
198 oom_killer_enable();
199
200 pr_info("Restarting tasks ... ");
201
202 __usermodehelper_set_disable_depth(UMH_FREEZING);
203 thaw_workqueues();
204
205 cpuset_wait_for_hotplug();
206
207 read_lock(&tasklist_lock);
208 for_each_process_thread(g, p) {
209 /* No other threads should have PF_SUSPEND_TASK set */
210 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
211 __thaw_task(p);
212 }
213 read_unlock(&tasklist_lock);
214
215 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
216 curr->flags &= ~PF_SUSPEND_TASK;
217
218 usermodehelper_enable();
219
220 schedule();
221 pr_cont("done.\n");
222 trace_suspend_resume(TPS("thaw_processes"), 0, false);
223 }
224
thaw_kernel_threads(void)225 void thaw_kernel_threads(void)
226 {
227 struct task_struct *g, *p;
228
229 pm_nosig_freezing = false;
230 pr_info("Restarting kernel threads ... ");
231
232 thaw_workqueues();
233
234 read_lock(&tasklist_lock);
235 for_each_process_thread(g, p) {
236 if (p->flags & PF_KTHREAD)
237 __thaw_task(p);
238 }
239 read_unlock(&tasklist_lock);
240
241 schedule();
242 pr_cont("done.\n");
243 }
244