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/moduleparam.h>
16 #include <linux/syscalls.h>
17 #include <linux/freezer.h>
18 #include <linux/delay.h>
19 #include <linux/workqueue.h>
22 * Timeout for stopping processes
24 int freeze_task_timeout = 20;
25 EXPORT_SYMBOL(freeze_task_timeout);
26 module_param(freeze_task_timeout, int, S_IRUGO | S_IWUSR);
28 #define TIMEOUT (freeze_task_timeout * HZ)
30 static inline int freezeable(struct task_struct * p)
33 (p->flags & PF_NOFREEZE) ||
39 static int try_to_freeze_tasks(bool sig_only)
41 struct task_struct *g, *p;
42 unsigned long end_time;
45 struct timeval start, end;
47 unsigned int elapsed_csecs;
49 do_gettimeofday(&start);
51 end_time = jiffies + TIMEOUT;
54 freeze_workqueues_begin();
58 read_lock(&tasklist_lock);
59 do_each_thread(g, p) {
60 if (frozen(p) || !freezeable(p))
63 if (!freeze_task(p, sig_only))
67 * Now that we've done set_freeze_flag, don't
68 * perturb a task in TASK_STOPPED or TASK_TRACED.
69 * It is "frozen enough". If the task does wake
70 * up, it will immediately call try_to_freeze.
72 if (!task_is_stopped_or_traced(p) &&
73 !freezer_should_skip(p))
75 } while_each_thread(g, p);
76 read_unlock(&tasklist_lock);
79 wq_busy = freeze_workqueues_busy();
83 if (!todo || time_after(jiffies, end_time))
87 * We need to retry, but first give the freezing tasks some
88 * time to enter the regrigerator.
93 do_gettimeofday(&end);
94 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
95 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
96 elapsed_csecs = elapsed_csecs64;
99 /* This does not unfreeze processes that are already frozen
100 * (we have slightly ugly calling convention in that respect,
101 * and caller must call thaw_processes() if something fails),
102 * but it cleans up leftover PF_FREEZE requests.
105 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
106 "(%d tasks refusing to freeze, wq_busy=%d):\n",
107 elapsed_csecs / 100, elapsed_csecs % 100,
108 todo - wq_busy, wq_busy);
112 read_lock(&tasklist_lock);
113 do_each_thread(g, p) {
115 if (freezing(p) && !freezer_should_skip(p))
119 } while_each_thread(g, p);
120 read_unlock(&tasklist_lock);
122 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
123 elapsed_csecs % 100);
126 return todo ? -EBUSY : 0;
130 * freeze_processes - tell processes to enter the refrigerator
132 int freeze_processes(void)
136 printk("Freezing user space processes ... ");
137 error = try_to_freeze_tasks(true);
142 printk("Freezing remaining freezable tasks ... ");
143 error = try_to_freeze_tasks(false);
148 oom_killer_disable();
156 static void thaw_tasks(bool nosig_only)
158 struct task_struct *g, *p;
160 read_lock(&tasklist_lock);
161 do_each_thread(g, p) {
165 if (nosig_only && should_send_signal(p))
168 if (cgroup_freezing_or_frozen(p))
172 } while_each_thread(g, p);
173 read_unlock(&tasklist_lock);
176 void thaw_processes(void)
180 printk("Restarting tasks ... ");