Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / kernel / power / process.c
blob3d39843af3d162a7b10251bc1957f01ba2acbb98
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/suspend.h>
13 #include <linux/module.h>
14 #include <linux/syscalls.h>
15 #include <linux/freezer.h>
17 /*
18 * Timeout for stopping processes
20 #define TIMEOUT (20 * HZ)
22 #define FREEZER_KERNEL_THREADS 0
23 #define FREEZER_USER_SPACE 1
25 static inline int freezeable(struct task_struct * p)
27 if ((p == current) ||
28 (p->flags & PF_NOFREEZE) ||
29 (p->exit_state != 0))
30 return 0;
31 return 1;
35 * freezing is complete, mark current process as frozen
37 static inline void frozen_process(void)
39 if (!unlikely(current->flags & PF_NOFREEZE)) {
40 current->flags |= PF_FROZEN;
41 wmb();
43 clear_freeze_flag(current);
46 /* Refrigerator is place where frozen processes are stored :-). */
47 void refrigerator(void)
49 /* Hmm, should we be allowed to suspend when there are realtime
50 processes around? */
51 long save;
53 task_lock(current);
54 if (freezing(current)) {
55 frozen_process();
56 task_unlock(current);
57 } else {
58 task_unlock(current);
59 return;
61 save = current->state;
62 pr_debug("%s entered refrigerator\n", current->comm);
64 spin_lock_irq(&current->sighand->siglock);
65 recalc_sigpending(); /* We sent fake signal, clean it up */
66 spin_unlock_irq(&current->sighand->siglock);
68 for (;;) {
69 set_current_state(TASK_UNINTERRUPTIBLE);
70 if (!frozen(current))
71 break;
72 schedule();
74 pr_debug("%s left refrigerator\n", current->comm);
75 __set_current_state(save);
78 <<<<<<< HEAD:kernel/power/process.c
79 static void fake_signal_wake_up(struct task_struct *p, int resume)
80 =======
81 static void fake_signal_wake_up(struct task_struct *p)
82 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
84 unsigned long flags;
86 spin_lock_irqsave(&p->sighand->siglock, flags);
87 <<<<<<< HEAD:kernel/power/process.c
88 signal_wake_up(p, resume);
89 =======
90 signal_wake_up(p, 0);
91 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
92 spin_unlock_irqrestore(&p->sighand->siglock, flags);
95 <<<<<<< HEAD:kernel/power/process.c
96 static void send_fake_signal(struct task_struct *p)
98 if (task_is_stopped(p))
99 force_sig_specific(SIGSTOP, p);
100 fake_signal_wake_up(p, task_is_stopped(p));
103 =======
104 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
105 static int has_mm(struct task_struct *p)
107 return (p->mm && !(p->flags & PF_BORROWED_MM));
111 * freeze_task - send a freeze request to given task
112 * @p: task to send the request to
113 * @with_mm_only: if set, the request will only be sent if the task has its
114 * own mm
115 * Return value: 0, if @with_mm_only is set and the task has no mm of its
116 * own or the task is frozen, 1, otherwise
118 * The freeze request is sent by seting the tasks's TIF_FREEZE flag and
119 * either sending a fake signal to it or waking it up, depending on whether
120 * or not it has its own mm (ie. it is a user land task). If @with_mm_only
121 * is set and the task has no mm of its own (ie. it is a kernel thread),
122 * its TIF_FREEZE flag should not be set.
124 * The task_lock() is necessary to prevent races with exit_mm() or
125 * use_mm()/unuse_mm() from occuring.
127 static int freeze_task(struct task_struct *p, int with_mm_only)
129 int ret = 1;
131 task_lock(p);
132 if (freezing(p)) {
133 if (has_mm(p)) {
134 if (!signal_pending(p))
135 <<<<<<< HEAD:kernel/power/process.c
136 fake_signal_wake_up(p, 0);
137 =======
138 fake_signal_wake_up(p);
139 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
140 } else {
141 if (with_mm_only)
142 ret = 0;
143 else
144 wake_up_state(p, TASK_INTERRUPTIBLE);
146 } else {
147 rmb();
148 if (frozen(p)) {
149 ret = 0;
150 } else {
151 if (has_mm(p)) {
152 set_freeze_flag(p);
153 <<<<<<< HEAD:kernel/power/process.c
154 send_fake_signal(p);
155 =======
156 fake_signal_wake_up(p);
157 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
158 } else {
159 if (with_mm_only) {
160 ret = 0;
161 } else {
162 set_freeze_flag(p);
163 wake_up_state(p, TASK_INTERRUPTIBLE);
168 task_unlock(p);
169 return ret;
172 static void cancel_freezing(struct task_struct *p)
174 unsigned long flags;
176 if (freezing(p)) {
177 pr_debug(" clean up: %s\n", p->comm);
178 clear_freeze_flag(p);
179 spin_lock_irqsave(&p->sighand->siglock, flags);
180 recalc_sigpending_and_wake(p);
181 spin_unlock_irqrestore(&p->sighand->siglock, flags);
185 static int try_to_freeze_tasks(int freeze_user_space)
187 struct task_struct *g, *p;
188 unsigned long end_time;
189 unsigned int todo;
190 struct timeval start, end;
191 s64 elapsed_csecs64;
192 unsigned int elapsed_csecs;
194 do_gettimeofday(&start);
196 end_time = jiffies + TIMEOUT;
197 do {
198 todo = 0;
199 read_lock(&tasklist_lock);
200 do_each_thread(g, p) {
201 if (frozen(p) || !freezeable(p))
202 continue;
204 <<<<<<< HEAD:kernel/power/process.c
205 if (task_is_traced(p) && frozen(p->parent)) {
206 cancel_freezing(p);
207 continue;
210 =======
211 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
212 if (!freeze_task(p, freeze_user_space))
213 continue;
215 <<<<<<< HEAD:kernel/power/process.c
216 if (!freezer_should_skip(p))
217 =======
219 * Now that we've done set_freeze_flag, don't
220 * perturb a task in TASK_STOPPED or TASK_TRACED.
221 * It is "frozen enough". If the task does wake
222 * up, it will immediately call try_to_freeze.
224 if (!task_is_stopped_or_traced(p) &&
225 !freezer_should_skip(p))
226 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/power/process.c
227 todo++;
228 } while_each_thread(g, p);
229 read_unlock(&tasklist_lock);
230 yield(); /* Yield is okay here */
231 if (time_after(jiffies, end_time))
232 break;
233 } while (todo);
235 do_gettimeofday(&end);
236 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
237 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
238 elapsed_csecs = elapsed_csecs64;
240 if (todo) {
241 /* This does not unfreeze processes that are already frozen
242 * (we have slightly ugly calling convention in that respect,
243 * and caller must call thaw_processes() if something fails),
244 * but it cleans up leftover PF_FREEZE requests.
246 printk("\n");
247 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
248 "(%d tasks refusing to freeze):\n",
249 elapsed_csecs / 100, elapsed_csecs % 100, todo);
250 show_state();
251 read_lock(&tasklist_lock);
252 do_each_thread(g, p) {
253 task_lock(p);
254 if (freezing(p) && !freezer_should_skip(p))
255 printk(KERN_ERR " %s\n", p->comm);
256 cancel_freezing(p);
257 task_unlock(p);
258 } while_each_thread(g, p);
259 read_unlock(&tasklist_lock);
260 } else {
261 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
262 elapsed_csecs % 100);
265 return todo ? -EBUSY : 0;
269 * freeze_processes - tell processes to enter the refrigerator
271 int freeze_processes(void)
273 int error;
275 printk("Freezing user space processes ... ");
276 error = try_to_freeze_tasks(FREEZER_USER_SPACE);
277 if (error)
278 goto Exit;
279 printk("done.\n");
281 printk("Freezing remaining freezable tasks ... ");
282 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
283 if (error)
284 goto Exit;
285 printk("done.");
286 Exit:
287 BUG_ON(in_atomic());
288 printk("\n");
289 return error;
292 static void thaw_tasks(int thaw_user_space)
294 struct task_struct *g, *p;
296 read_lock(&tasklist_lock);
297 do_each_thread(g, p) {
298 if (!freezeable(p))
299 continue;
301 if (!p->mm == thaw_user_space)
302 continue;
304 thaw_process(p);
305 } while_each_thread(g, p);
306 read_unlock(&tasklist_lock);
309 void thaw_processes(void)
311 printk("Restarting tasks ... ");
312 thaw_tasks(FREEZER_KERNEL_THREADS);
313 thaw_tasks(FREEZER_USER_SPACE);
314 schedule();
315 printk("done.\n");
318 EXPORT_SYMBOL(refrigerator);