• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
27  * Timeout for stopping processes
28  */
29 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
30 
try_to_freeze_tasks(bool user_only)31 static int try_to_freeze_tasks(bool user_only)
32 {
33 	struct task_struct *g, *p;
34 	unsigned long end_time;
35 	unsigned int todo;
36 	bool wq_busy = false;
37 	ktime_t start, end, elapsed;
38 	unsigned int elapsed_msecs;
39 	bool wakeup = false;
40 	int sleep_usecs = USEC_PER_MSEC;
41 
42 	start = ktime_get_boottime();
43 
44 	end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
45 
46 	if (!user_only)
47 		freeze_workqueues_begin();
48 
49 	while (true) {
50 		todo = 0;
51 		read_lock(&tasklist_lock);
52 		for_each_process_thread(g, p) {
53 			if (p == current || !freeze_task(p))
54 				continue;
55 
56 			if (!freezer_should_skip(p))
57 				todo++;
58 		}
59 		read_unlock(&tasklist_lock);
60 
61 		if (!user_only) {
62 			wq_busy = freeze_workqueues_busy();
63 			todo += wq_busy;
64 		}
65 
66 		if (!todo || time_after(jiffies, end_time))
67 			break;
68 
69 		if (pm_wakeup_pending()) {
70 			wakeup = true;
71 			break;
72 		}
73 
74 		/*
75 		 * We need to retry, but first give the freezing tasks some
76 		 * time to enter the refrigerator.  Start with an initial
77 		 * 1 ms sleep followed by exponential backoff until 8 ms.
78 		 */
79 		usleep_range(sleep_usecs / 2, sleep_usecs);
80 		if (sleep_usecs < 8 * USEC_PER_MSEC)
81 			sleep_usecs *= 2;
82 	}
83 
84 	end = ktime_get_boottime();
85 	elapsed = ktime_sub(end, start);
86 	elapsed_msecs = ktime_to_ms(elapsed);
87 
88 	if (wakeup) {
89 		pr_cont("\n");
90 		pr_err("Freezing of tasks aborted after %d.%03d seconds",
91 		       elapsed_msecs / 1000, elapsed_msecs % 1000);
92 	} else if (todo) {
93 		pr_cont("\n");
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_workqueue_state();
101 
102 		if (pm_debug_messages_on) {
103 			read_lock(&tasklist_lock);
104 			for_each_process_thread(g, p) {
105 				if (p != current && !freezer_should_skip(p)
106 				    && freezing(p) && !frozen(p))
107 					sched_show_task(p);
108 			}
109 			read_unlock(&tasklist_lock);
110 		}
111 	} else {
112 		pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
113 			elapsed_msecs % 1000);
114 	}
115 
116 	return todo ? -EBUSY : 0;
117 }
118 
119 /**
120  * freeze_processes - Signal user space processes to enter the refrigerator.
121  * The current thread will not be frozen.  The same process that calls
122  * freeze_processes must later call thaw_processes.
123  *
124  * On success, returns 0.  On failure, -errno and system is fully thawed.
125  */
freeze_processes(void)126 int freeze_processes(void)
127 {
128 	int error;
129 
130 	error = __usermodehelper_disable(UMH_FREEZING);
131 	if (error)
132 		return error;
133 
134 	/* Make sure this task doesn't get frozen */
135 	current->flags |= PF_SUSPEND_TASK;
136 
137 	if (!pm_freezing)
138 		atomic_inc(&system_freezing_cnt);
139 
140 	pm_wakeup_clear(0);
141 	pr_info("Freezing user space processes ... ");
142 	pm_freezing = true;
143 	error = try_to_freeze_tasks(true);
144 	if (!error) {
145 		__usermodehelper_set_disable_depth(UMH_DISABLED);
146 		pr_cont("done.");
147 	}
148 	pr_cont("\n");
149 	BUG_ON(in_atomic());
150 
151 	/*
152 	 * Now that the whole userspace is frozen we need to disbale
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 	pr_info("Freezing remaining freezable tasks ... ");
178 
179 	pm_nosig_freezing = true;
180 	error = try_to_freeze_tasks(false);
181 	if (!error)
182 		pr_cont("done.");
183 
184 	pr_cont("\n");
185 	BUG_ON(in_atomic());
186 
187 	if (error)
188 		thaw_kernel_threads();
189 	return error;
190 }
191 
thaw_processes(void)192 void thaw_processes(void)
193 {
194 	struct task_struct *g, *p;
195 	struct task_struct *curr = current;
196 
197 	trace_suspend_resume(TPS("thaw_processes"), 0, true);
198 	if (pm_freezing)
199 		atomic_dec(&system_freezing_cnt);
200 	pm_freezing = false;
201 	pm_nosig_freezing = false;
202 
203 	oom_killer_enable();
204 
205 	pr_info("Restarting tasks ... ");
206 
207 	__usermodehelper_set_disable_depth(UMH_FREEZING);
208 	thaw_workqueues();
209 
210 	cpuset_wait_for_hotplug();
211 
212 	read_lock(&tasklist_lock);
213 	for_each_process_thread(g, p) {
214 		/* No other threads should have PF_SUSPEND_TASK set */
215 		WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
216 		__thaw_task(p);
217 	}
218 	read_unlock(&tasklist_lock);
219 
220 	WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
221 	curr->flags &= ~PF_SUSPEND_TASK;
222 
223 	usermodehelper_enable();
224 
225 	schedule();
226 	pr_cont("done.\n");
227 	trace_suspend_resume(TPS("thaw_processes"), 0, false);
228 }
229 
thaw_kernel_threads(void)230 void thaw_kernel_threads(void)
231 {
232 	struct task_struct *g, *p;
233 
234 	pm_nosig_freezing = false;
235 	pr_info("Restarting kernel threads ... ");
236 
237 	thaw_workqueues();
238 
239 	read_lock(&tasklist_lock);
240 	for_each_process_thread(g, p) {
241 		if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
242 			__thaw_task(p);
243 	}
244 	read_unlock(&tasklist_lock);
245 
246 	schedule();
247 	pr_cont("done.\n");
248 }
249