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/wakelock.h>
22 * Timeout for stopping processes
24 #define TIMEOUT (20 * HZ)
26 static inline int freezable(struct task_struct * p)
29 (p->flags & PF_NOFREEZE) ||
35 static int try_to_freeze_tasks(bool sig_only)
37 struct task_struct *g, *p;
38 unsigned long end_time;
41 struct timeval start, end;
43 unsigned int elapsed_csecs;
46 do_gettimeofday(&start);
48 end_time = jiffies + TIMEOUT;
51 freeze_workqueues_begin();
55 read_lock(&tasklist_lock);
56 do_each_thread(g, p) {
57 if (frozen(p) || !freezable(p))
60 if (!freeze_task(p, sig_only))
64 * Now that we've done set_freeze_flag, don't
65 * perturb a task in TASK_STOPPED or TASK_TRACED.
66 * It is "frozen enough". If the task does wake
67 * up, it will immediately call try_to_freeze.
69 * Because freeze_task() goes through p's
70 * scheduler lock after setting TIF_FREEZE, it's
71 * guaranteed that either we see TASK_RUNNING or
72 * try_to_stop() after schedule() in ptrace/signal
73 * stop sees TIF_FREEZE.
75 if (!task_is_stopped_or_traced(p) &&
76 !freezer_should_skip(p))
78 } while_each_thread(g, p);
79 read_unlock(&tasklist_lock);
82 wq_busy = freeze_workqueues_busy();
86 if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
90 if (!todo || time_after(jiffies, end_time))
93 if (pm_wakeup_pending()) {
99 * We need to retry, but first give the freezing tasks some
100 * time to enter the regrigerator.
105 do_gettimeofday(&end);
106 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
107 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
108 elapsed_csecs = elapsed_csecs64;
111 /* This does not unfreeze processes that are already frozen
112 * (we have slightly ugly calling convention in that respect,
113 * and caller must call thaw_processes() if something fails),
114 * but it cleans up leftover PF_FREEZE requests.
118 printk(KERN_ERR "Freezing of %s aborted\n",
119 sig_only ? "user space " : "tasks ");
123 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
124 "(%d tasks refusing to freeze, wq_busy=%d):\n",
125 elapsed_csecs / 100, elapsed_csecs % 100,
126 todo - wq_busy, wq_busy);
130 read_lock(&tasklist_lock);
131 do_each_thread(g, p) {
133 if (freezing(p) && !freezer_should_skip(p) &&
138 } while_each_thread(g, p);
139 read_unlock(&tasklist_lock);
141 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
142 elapsed_csecs % 100);
145 return todo ? -EBUSY : 0;
149 * freeze_processes - tell processes to enter the refrigerator
151 int freeze_processes(void)
155 printk("Freezing user space processes ... ");
156 error = try_to_freeze_tasks(true);
161 printk("Freezing remaining freezable tasks ... ");
162 error = try_to_freeze_tasks(false);
167 oom_killer_disable();
175 static void thaw_tasks(bool nosig_only)
177 struct task_struct *g, *p;
179 read_lock(&tasklist_lock);
180 do_each_thread(g, p) {
184 if (nosig_only && should_send_signal(p))
187 if (cgroup_freezing_or_frozen(p))
191 } while_each_thread(g, p);
192 read_unlock(&tasklist_lock);
195 void thaw_processes(void)
199 printk("Restarting tasks ... ");