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 <linux/wakeup_reason.h>
22 * Timeout for stopping processes
24 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
26 static int try_to_freeze_tasks(bool user_only)
28 struct task_struct *g, *p;
29 unsigned long end_time;
32 struct timeval start, end;
34 unsigned int elapsed_msecs;
36 int sleep_usecs = USEC_PER_MSEC;
37 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
39 do_gettimeofday(&start);
41 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
44 freeze_workqueues_begin();
48 read_lock(&tasklist_lock);
49 do_each_thread(g, p) {
50 if (p == current || !freeze_task(p))
53 if (!freezer_should_skip(p))
55 } while_each_thread(g, p);
56 read_unlock(&tasklist_lock);
59 wq_busy = freeze_workqueues_busy();
63 if (!todo || time_after(jiffies, end_time))
66 if (pm_wakeup_pending()) {
67 pm_get_active_wakeup_sources(suspend_abort,
68 MAX_SUSPEND_ABORT_LEN);
69 log_suspend_abort_reason(suspend_abort);
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.
79 usleep_range(sleep_usecs / 2, sleep_usecs);
80 if (sleep_usecs < 8 * USEC_PER_MSEC)
84 do_gettimeofday(&end);
85 elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
86 do_div(elapsed_msecs64, NSEC_PER_MSEC);
87 elapsed_msecs = elapsed_msecs64;
91 printk(KERN_ERR "Freezing of tasks aborted after %d.%03d seconds",
92 elapsed_msecs / 1000, elapsed_msecs % 1000);
95 printk(KERN_ERR "Freezing of tasks failed after %d.%03d seconds"
96 " (%d tasks refusing to freeze, wq_busy=%d):\n",
97 elapsed_msecs / 1000, elapsed_msecs % 1000,
98 todo - wq_busy, wq_busy);
100 read_lock(&tasklist_lock);
101 do_each_thread(g, p) {
102 if (p != current && !freezer_should_skip(p)
103 && freezing(p) && !frozen(p))
105 } while_each_thread(g, p);
106 read_unlock(&tasklist_lock);
108 printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
109 elapsed_msecs % 1000);
112 return todo ? -EBUSY : 0;
116 * Returns true if all freezable tasks (except for current) are frozen already
118 static bool check_frozen_processes(void)
120 struct task_struct *g, *p;
123 read_lock(&tasklist_lock);
124 for_each_process_thread(g, p) {
125 if (p != current && !freezer_should_skip(p) &&
132 read_unlock(&tasklist_lock);
138 * freeze_processes - Signal user space processes to enter the refrigerator.
140 * On success, returns 0. On failure, -errno and system is fully thawed.
142 int freeze_processes(void)
147 error = __usermodehelper_disable(UMH_FREEZING);
152 atomic_inc(&system_freezing_cnt);
154 printk("Freezing user space processes ... ");
156 oom_kills_saved = oom_kills_count();
157 error = try_to_freeze_tasks(true);
159 __usermodehelper_set_disable_depth(UMH_DISABLED);
160 oom_killer_disable();
163 * There might have been an OOM kill while we were
164 * freezing tasks and the killed task might be still
165 * on the way out so we have to double check for race.
167 if (oom_kills_count() != oom_kills_saved &&
168 !check_frozen_processes()) {
169 __usermodehelper_set_disable_depth(UMH_ENABLED);
170 printk("OOM in progress.");
186 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
188 * On success, returns 0. On failure, -errno and only the kernel threads are
189 * thawed, so as to give a chance to the caller to do additional cleanups
190 * (if any) before thawing the userspace tasks. So, it is the responsibility
191 * of the caller to thaw the userspace tasks, when the time is right.
193 int freeze_kernel_threads(void)
197 printk("Freezing remaining freezable tasks ... ");
198 pm_nosig_freezing = true;
199 error = try_to_freeze_tasks(false);
207 thaw_kernel_threads();
211 void thaw_processes(void)
213 struct task_struct *g, *p;
216 atomic_dec(&system_freezing_cnt);
218 pm_nosig_freezing = false;
222 printk("Restarting tasks ... ");
224 __usermodehelper_set_disable_depth(UMH_FREEZING);
227 read_lock(&tasklist_lock);
228 do_each_thread(g, p) {
230 } while_each_thread(g, p);
231 read_unlock(&tasklist_lock);
233 usermodehelper_enable();
239 void thaw_kernel_threads(void)
241 struct task_struct *g, *p;
243 pm_nosig_freezing = false;
244 printk("Restarting kernel threads ... ");
248 read_lock(&tasklist_lock);
249 do_each_thread(g, p) {
250 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
252 } while_each_thread(g, p);
253 read_unlock(&tasklist_lock);