freezer: make freezing indicate freeze condition in effect
[linux-2.6.git] / kernel / power / process.c
blobe6e2739190b5f483715d54a1a0b29d805c07b439
1 /*
2 * drivers/power/process.c - Functions for starting/stopping processes on
3 * suspend transitions.
5 * Originally from swsusp.
6 */
9 #undef DEBUG
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>
20 /*
21 * Timeout for stopping processes
23 #define TIMEOUT (20 * HZ)
25 static inline int freezable(struct task_struct * p)
27 if ((p == current) ||
28 (p->flags & PF_NOFREEZE))
29 return 0;
30 return 1;
33 static int try_to_freeze_tasks(bool sig_only)
35 struct task_struct *g, *p;
36 unsigned long end_time;
37 unsigned int todo;
38 bool wq_busy = false;
39 struct timeval start, end;
40 u64 elapsed_csecs64;
41 unsigned int elapsed_csecs;
42 bool wakeup = false;
44 do_gettimeofday(&start);
46 end_time = jiffies + TIMEOUT;
48 if (!sig_only)
49 freeze_workqueues_begin();
51 while (true) {
52 todo = 0;
53 read_lock(&tasklist_lock);
54 do_each_thread(g, p) {
55 if (frozen(p) || !freezable(p))
56 continue;
58 if (!freeze_task(p, sig_only))
59 continue;
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))
75 todo++;
76 } while_each_thread(g, p);
77 read_unlock(&tasklist_lock);
79 if (!sig_only) {
80 wq_busy = freeze_workqueues_busy();
81 todo += wq_busy;
84 if (!todo || time_after(jiffies, end_time))
85 break;
87 if (pm_wakeup_pending()) {
88 wakeup = true;
89 break;
93 * We need to retry, but first give the freezing tasks some
94 * time to enter the regrigerator.
96 msleep(10);
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;
104 if (todo) {
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.
110 printk("\n");
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);
117 thaw_workqueues();
119 read_lock(&tasklist_lock);
120 do_each_thread(g, p) {
121 if (!wakeup && !freezer_should_skip(p) &&
122 freezing(p) && !frozen(p))
123 sched_show_task(p);
124 cancel_freezing(p);
125 } while_each_thread(g, p);
126 read_unlock(&tasklist_lock);
127 } else {
128 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
129 elapsed_csecs % 100);
132 return todo ? -EBUSY : 0;
136 * freeze_processes - Signal user space processes to enter the refrigerator.
138 int freeze_processes(void)
140 int error;
142 printk("Freezing user space processes ... ");
143 error = try_to_freeze_tasks(true);
144 if (!error) {
145 printk("done.");
146 oom_killer_disable();
148 printk("\n");
149 BUG_ON(in_atomic());
151 return error;
155 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
157 int freeze_kernel_threads(void)
159 int error;
161 printk("Freezing remaining freezable tasks ... ");
162 error = try_to_freeze_tasks(false);
163 if (!error)
164 printk("done.");
166 printk("\n");
167 BUG_ON(in_atomic());
169 return error;
172 void thaw_processes(void)
174 struct task_struct *g, *p;
176 oom_killer_enable();
178 printk("Restarting tasks ... ");
180 thaw_workqueues();
182 read_lock(&tasklist_lock);
183 do_each_thread(g, p) {
184 if (!freezable(p))
185 continue;
187 if (cgroup_freezing_or_frozen(p))
188 continue;
190 __thaw_task(p);
191 } while_each_thread(g, p);
192 read_unlock(&tasklist_lock);
194 schedule();
195 printk("done.\n");