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 freezeable(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;
44 unsigned int wakeup = 0;
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) || !freezeable(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 if (!task_is_stopped_or_traced(p) &&
70 !freezer_should_skip(p))
72 } while_each_thread(g, p);
73 read_unlock(&tasklist_lock);
76 wq_busy = freeze_workqueues_busy();
80 if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
84 if (!todo || time_after(jiffies, end_time))
88 * We need to retry, but first give the freezing tasks some
89 * time to enter the regrigerator.
94 do_gettimeofday(&end);
95 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
96 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
97 elapsed_csecs = elapsed_csecs64;
100 /* This does not unfreeze processes that are already frozen
101 * (we have slightly ugly calling convention in that respect,
102 * and caller must call thaw_processes() if something fails),
103 * but it cleans up leftover PF_FREEZE requests.
107 printk(KERN_ERR "Freezing of %s aborted\n",
108 sig_only ? "user space " : "tasks ");
112 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
113 "(%d tasks refusing to freeze, wq_busy=%d):\n",
114 elapsed_csecs / 100, elapsed_csecs % 100,
115 todo - wq_busy, wq_busy);
119 read_lock(&tasklist_lock);
120 do_each_thread(g, p) {
122 if (freezing(p) && !freezer_should_skip(p) &&
127 } while_each_thread(g, p);
128 read_unlock(&tasklist_lock);
130 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
131 elapsed_csecs % 100);
134 return todo ? -EBUSY : 0;
138 * freeze_processes - tell processes to enter the refrigerator
140 int freeze_processes(void)
144 printk("Freezing user space processes ... ");
145 error = try_to_freeze_tasks(true);
150 printk("Freezing remaining freezable tasks ... ");
151 error = try_to_freeze_tasks(false);
156 oom_killer_disable();
164 static void thaw_tasks(bool nosig_only)
166 struct task_struct *g, *p;
168 read_lock(&tasklist_lock);
169 do_each_thread(g, p) {
173 if (nosig_only && should_send_signal(p))
176 if (cgroup_freezing_or_frozen(p))
180 } while_each_thread(g, p);
181 read_unlock(&tasklist_lock);
184 void thaw_processes(void)
188 printk("Restarting tasks ... ");