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 inline int freezable(struct task_struct * p)
28 (p->flags & PF_NOFREEZE))
33 static int try_to_freeze_tasks(bool sig_only)
35 struct task_struct *g, *p;
36 unsigned long end_time;
39 struct timeval start, end;
41 unsigned int elapsed_csecs;
44 do_gettimeofday(&start);
46 end_time = jiffies + TIMEOUT;
49 freeze_workqueues_begin();
53 read_lock(&tasklist_lock);
54 do_each_thread(g, p) {
55 if (frozen(p) || !freezable(p))
58 if (!freeze_task(p, sig_only))
62 * Now that we've done set_freeze_flag, don't
63 * perturb a task in TASK_STOPPED or TASK_TRACED.
64 * It is "frozen enough". If the task does wake
65 * up, it will immediately call try_to_freeze.
67 * Because freeze_task() goes through p's
68 * scheduler lock after setting TIF_FREEZE, it's
69 * guaranteed that either we see TASK_RUNNING or
70 * try_to_stop() after schedule() in ptrace/signal
71 * stop sees TIF_FREEZE.
73 if (!task_is_stopped_or_traced(p) &&
74 !freezer_should_skip(p))
76 } while_each_thread(g, p);
77 read_unlock(&tasklist_lock);
80 wq_busy = freeze_workqueues_busy();
84 if (!todo || time_after(jiffies, end_time))
87 if (pm_wakeup_pending()) {
93 * We need to retry, but first give the freezing tasks some
94 * time to enter the regrigerator.
99 do_gettimeofday(&end);
100 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
101 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
102 elapsed_csecs = elapsed_csecs64;
105 /* This does not unfreeze processes that are already frozen
106 * (we have slightly ugly calling convention in that respect,
107 * and caller must call thaw_processes() if something fails),
108 * but it cleans up leftover PF_FREEZE requests.
111 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
112 "(%d tasks refusing to freeze, wq_busy=%d):\n",
113 wakeup ? "aborted" : "failed",
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 (!wakeup && freezing(p) && !freezer_should_skip(p))
126 } while_each_thread(g, p);
127 read_unlock(&tasklist_lock);
129 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
130 elapsed_csecs % 100);
133 return todo ? -EBUSY : 0;
137 * freeze_processes - Signal user space processes to enter the refrigerator.
139 int freeze_processes(void)
143 printk("Freezing user space processes ... ");
144 error = try_to_freeze_tasks(true);
147 oom_killer_disable();
156 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
158 int freeze_kernel_threads(void)
162 printk("Freezing remaining freezable tasks ... ");
163 error = try_to_freeze_tasks(false);
173 static void thaw_tasks(bool nosig_only)
175 struct task_struct *g, *p;
177 read_lock(&tasklist_lock);
178 do_each_thread(g, p) {
182 if (nosig_only && should_send_signal(p))
185 if (cgroup_freezing_or_frozen(p))
189 } while_each_thread(g, p);
190 read_unlock(&tasklist_lock);
193 void thaw_processes(void)
197 printk("Restarting tasks ... ");