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>
21 * Timeout for stopping processes
23 #define TIMEOUT (20 * HZ)
25 static int try_to_freeze_tasks(bool user_only)
27 struct task_struct *g, *p;
28 unsigned long end_time;
31 struct timeval start, end;
33 unsigned int elapsed_csecs;
36 do_gettimeofday(&start);
38 end_time = jiffies + TIMEOUT;
41 freeze_workqueues_begin();
45 read_lock(&tasklist_lock);
46 do_each_thread(g, p) {
47 if (p == current || !freeze_task(p))
51 * Now that we've done set_freeze_flag, don't
52 * perturb a task in TASK_STOPPED or TASK_TRACED.
53 * It is "frozen enough". If the task does wake
54 * up, it will immediately call try_to_freeze.
56 * Because freeze_task() goes through p's scheduler lock, it's
57 * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING
58 * transition can't race with task state testing here.
60 if (!task_is_stopped_or_traced(p) &&
61 !freezer_should_skip(p))
63 } while_each_thread(g, p);
64 read_unlock(&tasklist_lock);
67 wq_busy = freeze_workqueues_busy();
71 if (!todo || time_after(jiffies, end_time))
74 if (pm_wakeup_pending()) {
80 * We need to retry, but first give the freezing tasks some
81 * time to enter the regrigerator.
86 do_gettimeofday(&end);
87 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
88 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
89 elapsed_csecs = elapsed_csecs64;
93 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
94 "(%d tasks refusing to freeze, wq_busy=%d):\n",
95 wakeup ? "aborted" : "failed",
96 elapsed_csecs / 100, elapsed_csecs % 100,
97 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);
109 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
110 elapsed_csecs % 100);
113 return todo ? -EBUSY : 0;
117 * freeze_processes - Signal user space processes to enter the refrigerator.
119 * On success, returns 0. On failure, -errno and system is fully thawed.
121 int freeze_processes(void)
126 atomic_inc(&system_freezing_cnt);
128 printk("Freezing user space processes ... ");
130 error = try_to_freeze_tasks(true);
133 oom_killer_disable();
144 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
146 * On success, returns 0. On failure, -errno and only the kernel threads are
147 * thawed, so as to give a chance to the caller to do additional cleanups
148 * (if any) before thawing the userspace tasks. So, it is the responsibility
149 * of the caller to thaw the userspace tasks, when the time is right.
151 int freeze_kernel_threads(void)
155 printk("Freezing remaining freezable tasks ... ");
156 pm_nosig_freezing = true;
157 error = try_to_freeze_tasks(false);
165 thaw_kernel_threads();
169 void thaw_processes(void)
171 struct task_struct *g, *p;
174 atomic_dec(&system_freezing_cnt);
176 pm_nosig_freezing = false;
180 printk("Restarting tasks ... ");
184 read_lock(&tasklist_lock);
185 do_each_thread(g, p) {
187 } while_each_thread(g, p);
188 read_unlock(&tasklist_lock);
194 void thaw_kernel_threads(void)
196 struct task_struct *g, *p;
198 pm_nosig_freezing = false;
199 printk("Restarting kernel threads ... ");
203 read_lock(&tasklist_lock);
204 do_each_thread(g, p) {
205 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
207 } while_each_thread(g, p);
208 read_unlock(&tasklist_lock);