2 * drivers/power/process.c - Functions for starting/stopping processes on
5 * Originally from swsusp.
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>
24 * Timeout for stopping processes
26 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
28 static int try_to_freeze_tasks(bool user_only)
30 struct task_struct *g, *p;
31 unsigned long end_time;
34 struct timeval start, end;
36 unsigned int elapsed_msecs;
38 int sleep_usecs = USEC_PER_MSEC;
39 #ifdef CONFIG_PM_SLEEP
40 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
43 do_gettimeofday(&start);
45 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
48 freeze_workqueues_begin();
52 read_lock(&tasklist_lock);
53 for_each_process_thread(g, p) {
54 if (p == current || !freeze_task(p))
57 if (!freezer_should_skip(p))
60 read_unlock(&tasklist_lock);
63 wq_busy = freeze_workqueues_busy();
67 if (!todo || time_after(jiffies, end_time))
70 if (pm_wakeup_pending()) {
71 #ifdef CONFIG_PM_SLEEP
72 pm_get_active_wakeup_sources(suspend_abort,
73 MAX_SUSPEND_ABORT_LEN);
74 log_suspend_abort_reason(suspend_abort);
81 * We need to retry, but first give the freezing tasks some
82 * time to enter the refrigerator. Start with an initial
83 * 1 ms sleep followed by exponential backoff until 8 ms.
85 usleep_range(sleep_usecs / 2, sleep_usecs);
86 if (sleep_usecs < 8 * USEC_PER_MSEC)
90 do_gettimeofday(&end);
91 elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
92 do_div(elapsed_msecs64, NSEC_PER_MSEC);
93 elapsed_msecs = elapsed_msecs64;
97 pr_err("Freezing of tasks aborted after %d.%03d seconds",
98 elapsed_msecs / 1000, elapsed_msecs % 1000);
101 pr_err("Freezing of tasks failed after %d.%03d seconds"
102 " (%d tasks refusing to freeze, wq_busy=%d):\n",
103 elapsed_msecs / 1000, elapsed_msecs % 1000,
104 todo - wq_busy, wq_busy);
106 read_lock(&tasklist_lock);
107 for_each_process_thread(g, p) {
108 if (p != current && !freezer_should_skip(p)
109 && freezing(p) && !frozen(p))
112 read_unlock(&tasklist_lock);
114 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
115 elapsed_msecs % 1000);
118 return todo ? -EBUSY : 0;
122 * freeze_processes - Signal user space processes to enter the refrigerator.
123 * The current thread will not be frozen. The same process that calls
124 * freeze_processes must later call thaw_processes.
126 * On success, returns 0. On failure, -errno and system is fully thawed.
128 int freeze_processes(void)
132 error = __usermodehelper_disable(UMH_FREEZING);
136 /* Make sure this task doesn't get frozen */
137 current->flags |= PF_SUSPEND_TASK;
140 atomic_inc(&system_freezing_cnt);
143 pr_info("Freezing user space processes ... ");
145 error = try_to_freeze_tasks(true);
147 __usermodehelper_set_disable_depth(UMH_DISABLED);
154 * Now that the whole userspace is frozen we need to disbale
155 * the OOM killer to disallow any further interference with
158 if (!error && !oom_killer_disable())
167 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
169 * On success, returns 0. On failure, -errno and only the kernel threads are
170 * thawed, so as to give a chance to the caller to do additional cleanups
171 * (if any) before thawing the userspace tasks. So, it is the responsibility
172 * of the caller to thaw the userspace tasks, when the time is right.
174 int freeze_kernel_threads(void)
178 pr_info("Freezing remaining freezable tasks ... ");
180 pm_nosig_freezing = true;
181 error = try_to_freeze_tasks(false);
189 thaw_kernel_threads();
193 void thaw_processes(void)
195 struct task_struct *g, *p;
196 struct task_struct *curr = current;
198 trace_suspend_resume(TPS("thaw_processes"), 0, true);
200 atomic_dec(&system_freezing_cnt);
202 pm_nosig_freezing = false;
206 pr_info("Restarting tasks ... ");
208 __usermodehelper_set_disable_depth(UMH_FREEZING);
211 read_lock(&tasklist_lock);
212 for_each_process_thread(g, p) {
213 /* No other threads should have PF_SUSPEND_TASK set */
214 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
217 read_unlock(&tasklist_lock);
219 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
220 curr->flags &= ~PF_SUSPEND_TASK;
222 usermodehelper_enable();
226 trace_suspend_resume(TPS("thaw_processes"), 0, false);
229 void thaw_kernel_threads(void)
231 struct task_struct *g, *p;
233 pm_nosig_freezing = false;
234 pr_info("Restarting kernel threads ... ");
238 read_lock(&tasklist_lock);
239 for_each_process_thread(g, p) {
240 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
243 read_unlock(&tasklist_lock);