• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * drivers/power/process.c - Functions for starting/stopping processes on
3  *                           suspend transitions.
4  *
5  * Originally from swsusp.
6  */
7 
8 
9 #undef DEBUG
10 
11 #include <linux/interrupt.h>
12 #include <linux/oom.h>
13 #include <linux/suspend.h>
14 #include <linux/module.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/wakeup_reason.h>
22 #include <linux/cpuset.h>
23 
24 /*
25  * Timeout for stopping processes
26  */
27 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
28 
try_to_freeze_tasks(bool user_only)29 static int try_to_freeze_tasks(bool user_only)
30 {
31 	struct task_struct *g, *p;
32 	unsigned long end_time;
33 	unsigned int todo;
34 	bool wq_busy = false;
35 	struct timeval start, end;
36 	u64 elapsed_msecs64;
37 	unsigned int elapsed_msecs;
38 	bool wakeup = false;
39 	int sleep_usecs = USEC_PER_MSEC;
40 #ifdef CONFIG_PM_SLEEP
41 	char suspend_abort[MAX_SUSPEND_ABORT_LEN];
42 #endif
43 
44 	do_gettimeofday(&start);
45 
46 	end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
47 
48 	if (!user_only)
49 		freeze_workqueues_begin();
50 
51 	while (true) {
52 		todo = 0;
53 		read_lock(&tasklist_lock);
54 		for_each_process_thread(g, p) {
55 			if (p == current || !freeze_task(p))
56 				continue;
57 
58 			if (!freezer_should_skip(p))
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 #ifdef CONFIG_PM_SLEEP
73 			pm_get_active_wakeup_sources(suspend_abort,
74 				MAX_SUSPEND_ABORT_LEN);
75 			log_suspend_abort_reason(suspend_abort);
76 #endif
77 			wakeup = true;
78 			break;
79 		}
80 
81 		/*
82 		 * We need to retry, but first give the freezing tasks some
83 		 * time to enter the refrigerator.  Start with an initial
84 		 * 1 ms sleep followed by exponential backoff until 8 ms.
85 		 */
86 		usleep_range(sleep_usecs / 2, sleep_usecs);
87 		if (sleep_usecs < 8 * USEC_PER_MSEC)
88 			sleep_usecs *= 2;
89 	}
90 
91 	do_gettimeofday(&end);
92 	elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
93 	do_div(elapsed_msecs64, NSEC_PER_MSEC);
94 	elapsed_msecs = elapsed_msecs64;
95 
96 	if (wakeup) {
97 		pr_cont("\n");
98 		pr_err("Freezing of tasks aborted after %d.%03d seconds",
99 		       elapsed_msecs / 1000, elapsed_msecs % 1000);
100 	} else if (todo) {
101 		pr_cont("\n");
102 		pr_err("Freezing of tasks failed after %d.%03d seconds"
103 		       " (%d tasks refusing to freeze, wq_busy=%d):\n",
104 		       elapsed_msecs / 1000, elapsed_msecs % 1000,
105 		       todo - wq_busy, wq_busy);
106 
107 			read_lock(&tasklist_lock);
108 			for_each_process_thread(g, p) {
109 				if (p != current && !freezer_should_skip(p)
110 				    && freezing(p) && !frozen(p))
111 					sched_show_task(p);
112 			}
113 			read_unlock(&tasklist_lock);
114 	} else {
115 		pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
116 			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 		atomic_inc(&system_freezing_cnt);
142 
143 	pm_wakeup_clear();
144 	pr_info("Freezing user space processes ... ");
145 	pm_freezing = true;
146 	error = try_to_freeze_tasks(true);
147 	if (!error) {
148 		__usermodehelper_set_disable_depth(UMH_DISABLED);
149 		pr_cont("done.");
150 	}
151 	pr_cont("\n");
152 	BUG_ON(in_atomic());
153 
154 	/*
155 	 * Now that the whole userspace is frozen we need to disbale
156 	 * the OOM killer to disallow any further interference with
157 	 * killable tasks.
158 	 */
159 	if (!error && !oom_killer_disable())
160 		error = -EBUSY;
161 
162 	if (error)
163 		thaw_processes();
164 	return error;
165 }
166 
167 /**
168  * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
169  *
170  * On success, returns 0.  On failure, -errno and only the kernel threads are
171  * thawed, so as to give a chance to the caller to do additional cleanups
172  * (if any) before thawing the userspace tasks. So, it is the responsibility
173  * of the caller to thaw the userspace tasks, when the time is right.
174  */
freeze_kernel_threads(void)175 int freeze_kernel_threads(void)
176 {
177 	int error;
178 
179 	pr_info("Freezing remaining freezable tasks ... ");
180 
181 	pm_nosig_freezing = true;
182 	error = try_to_freeze_tasks(false);
183 	if (!error)
184 		pr_cont("done.");
185 
186 	pr_cont("\n");
187 	BUG_ON(in_atomic());
188 
189 	if (error)
190 		thaw_kernel_threads();
191 	return error;
192 }
193 
thaw_processes(void)194 void thaw_processes(void)
195 {
196 	struct task_struct *g, *p;
197 	struct task_struct *curr = current;
198 
199 	trace_suspend_resume(TPS("thaw_processes"), 0, true);
200 	if (pm_freezing)
201 		atomic_dec(&system_freezing_cnt);
202 	pm_freezing = false;
203 	pm_nosig_freezing = false;
204 
205 	oom_killer_enable();
206 
207 	pr_info("Restarting tasks ... ");
208 
209 	__usermodehelper_set_disable_depth(UMH_FREEZING);
210 	thaw_workqueues();
211 
212 	cpuset_wait_for_hotplug();
213 
214 	read_lock(&tasklist_lock);
215 	for_each_process_thread(g, p) {
216 		/* No other threads should have PF_SUSPEND_TASK set */
217 		WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
218 		__thaw_task(p);
219 	}
220 	read_unlock(&tasklist_lock);
221 
222 	WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
223 	curr->flags &= ~PF_SUSPEND_TASK;
224 
225 	usermodehelper_enable();
226 
227 	schedule();
228 	pr_cont("done.\n");
229 	trace_suspend_resume(TPS("thaw_processes"), 0, false);
230 }
231 
thaw_kernel_threads(void)232 void thaw_kernel_threads(void)
233 {
234 	struct task_struct *g, *p;
235 
236 	pm_nosig_freezing = false;
237 	pr_info("Restarting kernel threads ... ");
238 
239 	thaw_workqueues();
240 
241 	read_lock(&tasklist_lock);
242 	for_each_process_thread(g, p) {
243 		if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
244 			__thaw_task(p);
245 	}
246 	read_unlock(&tasklist_lock);
247 
248 	schedule();
249 	pr_cont("done.\n");
250 }
251