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 (todo) {
91 pr_err("Freezing %s %s after %d.%03d seconds "
92 "(%d tasks refusing to freeze, wq_busy=%d):\n", what,
93 wakeup ? "aborted" : "failed",
94 elapsed_msecs / 1000, elapsed_msecs % 1000,
95 todo - wq_busy, wq_busy);
96
97 if (wq_busy)
98 show_freezable_workqueues();
99
100 if (!wakeup || pm_debug_messages_on) {
101 read_lock(&tasklist_lock);
102 for_each_process_thread(g, p) {
103 if (p != current && freezing(p) && !frozen(p)) {
104 sched_show_task(p);
105 if (!wakeup)
106 trace_android_vh_try_to_freeze_todo_unfrozen(p);
107 }
108 }
109 read_unlock(&tasklist_lock);
110 }
111
112 if (!wakeup)
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 read_lock(&tasklist_lock);
206 for_each_process_thread(g, p) {
207 /* No other threads should have PF_SUSPEND_TASK set */
208 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
209 __thaw_task(p);
210 }
211 read_unlock(&tasklist_lock);
212
213 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
214 curr->flags &= ~PF_SUSPEND_TASK;
215
216 usermodehelper_enable();
217
218 schedule();
219 pr_cont("done.\n");
220 trace_suspend_resume(TPS("thaw_processes"), 0, false);
221 }
222
thaw_kernel_threads(void)223 void thaw_kernel_threads(void)
224 {
225 struct task_struct *g, *p;
226
227 pm_nosig_freezing = false;
228 pr_info("Restarting kernel threads ... ");
229
230 thaw_workqueues();
231
232 read_lock(&tasklist_lock);
233 for_each_process_thread(g, p) {
234 if (p->flags & PF_KTHREAD)
235 __thaw_task(p);
236 }
237 read_unlock(&tasklist_lock);
238
239 schedule();
240 pr_cont("done.\n");
241 }
242