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