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 if (pm_debug_messages_on) {
112 read_lock(&tasklist_lock);
113 for_each_process_thread(g, p) {
114 if (p != current && !freezer_should_skip(p)
115 && freezing(p) && !frozen(p))
116 sched_show_task(p);
117 }
118 read_unlock(&tasklist_lock);
119 }
120 } else {
121 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
122 elapsed_msecs % 1000);
123 }
124
125 return todo ? -EBUSY : 0;
126 }
127
128 /**
129 * freeze_processes - Signal user space processes to enter the refrigerator.
130 * The current thread will not be frozen. The same process that calls
131 * freeze_processes must later call thaw_processes.
132 *
133 * On success, returns 0. On failure, -errno and system is fully thawed.
134 */
freeze_processes(void)135 int freeze_processes(void)
136 {
137 int error;
138
139 error = __usermodehelper_disable(UMH_FREEZING);
140 if (error)
141 return error;
142
143 /* Make sure this task doesn't get frozen */
144 current->flags |= PF_SUSPEND_TASK;
145
146 if (!pm_freezing)
147 atomic_inc(&system_freezing_cnt);
148
149 pm_wakeup_clear(true);
150 pr_info("Freezing user space processes ... ");
151 pm_freezing = true;
152 error = try_to_freeze_tasks(true);
153 if (!error) {
154 __usermodehelper_set_disable_depth(UMH_DISABLED);
155 pr_cont("done.");
156 }
157 pr_cont("\n");
158 BUG_ON(in_atomic());
159
160 /*
161 * Now that the whole userspace is frozen we need to disbale
162 * the OOM killer to disallow any further interference with
163 * killable tasks. There is no guarantee oom victims will
164 * ever reach a point they go away we have to wait with a timeout.
165 */
166 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
167 error = -EBUSY;
168
169 if (error)
170 thaw_processes();
171 return error;
172 }
173
174 /**
175 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
176 *
177 * On success, returns 0. On failure, -errno and only the kernel threads are
178 * thawed, so as to give a chance to the caller to do additional cleanups
179 * (if any) before thawing the userspace tasks. So, it is the responsibility
180 * of the caller to thaw the userspace tasks, when the time is right.
181 */
freeze_kernel_threads(void)182 int freeze_kernel_threads(void)
183 {
184 int error;
185
186 pr_info("Freezing remaining freezable tasks ... ");
187
188 pm_nosig_freezing = true;
189 error = try_to_freeze_tasks(false);
190 if (!error)
191 pr_cont("done.");
192
193 pr_cont("\n");
194 BUG_ON(in_atomic());
195
196 if (error)
197 thaw_kernel_threads();
198 return error;
199 }
200
thaw_processes(void)201 void thaw_processes(void)
202 {
203 struct task_struct *g, *p;
204 struct task_struct *curr = current;
205
206 trace_suspend_resume(TPS("thaw_processes"), 0, true);
207 if (pm_freezing)
208 atomic_dec(&system_freezing_cnt);
209 pm_freezing = false;
210 pm_nosig_freezing = false;
211
212 oom_killer_enable();
213
214 pr_info("Restarting tasks ... ");
215
216 __usermodehelper_set_disable_depth(UMH_FREEZING);
217 thaw_workqueues();
218
219 cpuset_wait_for_hotplug();
220
221 read_lock(&tasklist_lock);
222 for_each_process_thread(g, p) {
223 /* No other threads should have PF_SUSPEND_TASK set */
224 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
225 __thaw_task(p);
226 }
227 read_unlock(&tasklist_lock);
228
229 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
230 curr->flags &= ~PF_SUSPEND_TASK;
231
232 usermodehelper_enable();
233
234 schedule();
235 pr_cont("done.\n");
236 trace_suspend_resume(TPS("thaw_processes"), 0, false);
237 }
238
thaw_kernel_threads(void)239 void thaw_kernel_threads(void)
240 {
241 struct task_struct *g, *p;
242
243 pm_nosig_freezing = false;
244 pr_info("Restarting kernel threads ... ");
245
246 thaw_workqueues();
247
248 read_lock(&tasklist_lock);
249 for_each_process_thread(g, p) {
250 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
251 __thaw_task(p);
252 }
253 read_unlock(&tasklist_lock);
254
255 schedule();
256 pr_cont("done.\n");
257 }
258