Hibernation: prepare to enter the low power state
[linux-2.6-block.git] / kernel / power / process.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/power/process.c - Functions for starting/stopping processes on
3 * suspend transitions.
4 *
5 * Originally from swsusp.
6 */
7
8
9#undef DEBUG
10
1da177e4
LT
11#include <linux/interrupt.h>
12#include <linux/suspend.h>
13#include <linux/module.h>
02aaeb9b 14#include <linux/syscalls.h>
7dfb7103 15#include <linux/freezer.h>
1da177e4
LT
16
17/*
18 * Timeout for stopping processes
19 */
02aaeb9b 20#define TIMEOUT (20 * HZ)
1da177e4 21
a9b6f562
RW
22#define FREEZER_KERNEL_THREADS 0
23#define FREEZER_USER_SPACE 1
1da177e4
LT
24
25static inline int freezeable(struct task_struct * p)
26{
1065d130 27 if ((p == current) ||
1da177e4 28 (p->flags & PF_NOFREEZE) ||
1065d130 29 (p->exit_state != 0))
1da177e4
LT
30 return 0;
31 return 1;
32}
33
88f18ba0
GS
34/*
35 * freezing is complete, mark current process as frozen
36 */
37static inline void frozen_process(void)
38{
39 if (!unlikely(current->flags & PF_NOFREEZE)) {
40 current->flags |= PF_FROZEN;
41 wmb();
42 }
43 clear_tsk_thread_flag(current, TIF_FREEZE);
44}
45
1da177e4 46/* Refrigerator is place where frozen processes are stored :-). */
3e1d1d28 47void refrigerator(void)
1da177e4
LT
48{
49 /* Hmm, should we be allowed to suspend when there are realtime
50 processes around? */
51 long save;
33e1c288
RW
52
53 task_lock(current);
54 if (freezing(current)) {
88f18ba0 55 frozen_process();
33e1c288
RW
56 task_unlock(current);
57 } else {
58 task_unlock(current);
59 return;
60 }
1da177e4 61 save = current->state;
1da177e4 62 pr_debug("%s entered refrigerator\n", current->comm);
1da177e4
LT
63
64 spin_lock_irq(&current->sighand->siglock);
65 recalc_sigpending(); /* We sent fake signal, clean it up */
66 spin_unlock_irq(&current->sighand->siglock);
67
433ecb4a
ON
68 for (;;) {
69 set_current_state(TASK_UNINTERRUPTIBLE);
70 if (!frozen(current))
71 break;
1da177e4 72 schedule();
2a23b5d1 73 }
1da177e4
LT
74 pr_debug("%s left refrigerator\n", current->comm);
75 current->state = save;
76}
77
02aaeb9b
RW
78static inline void freeze_process(struct task_struct *p)
79{
80 unsigned long flags;
81
82 if (!freezing(p)) {
8a102eed
RW
83 rmb();
84 if (!frozen(p)) {
85 if (p->state == TASK_STOPPED)
86 force_sig_specific(SIGSTOP, p);
3df494a3 87
8a102eed
RW
88 freeze(p);
89 spin_lock_irqsave(&p->sighand->siglock, flags);
90 signal_wake_up(p, p->state == TASK_STOPPED);
91 spin_unlock_irqrestore(&p->sighand->siglock, flags);
92 }
02aaeb9b
RW
93 }
94}
95
a7ef7878
RW
96static void cancel_freezing(struct task_struct *p)
97{
98 unsigned long flags;
99
100 if (freezing(p)) {
101 pr_debug(" clean up: %s\n", p->comm);
102 do_not_freeze(p);
103 spin_lock_irqsave(&p->sighand->siglock, flags);
7bb44ade 104 recalc_sigpending_and_wake(p);
a7ef7878
RW
105 spin_unlock_irqrestore(&p->sighand->siglock, flags);
106 }
107}
108
a9b6f562
RW
109static inline int is_user_space(struct task_struct *p)
110{
111 return p->mm && !(p->flags & PF_BORROWED_MM);
112}
113
11b2ce2b 114static unsigned int try_to_freeze_tasks(int freeze_user_space)
1da177e4 115{
1da177e4 116 struct task_struct *g, *p;
11b2ce2b
RW
117 unsigned long end_time;
118 unsigned int todo;
3e1d1d28 119
11b2ce2b 120 end_time = jiffies + TIMEOUT;
1da177e4 121 do {
11b2ce2b 122 todo = 0;
1da177e4
LT
123 read_lock(&tasklist_lock);
124 do_each_thread(g, p) {
1da177e4
LT
125 if (!freezeable(p))
126 continue;
11b2ce2b 127
1322ad41 128 if (frozen(p))
1da177e4 129 continue;
11b2ce2b 130
3df494a3 131 if (p->state == TASK_TRACED && frozen(p->parent)) {
a7ef7878
RW
132 cancel_freezing(p);
133 continue;
134 }
49b12d4f 135 if (freeze_user_space && !is_user_space(p))
ba96a0c8
RW
136 continue;
137
138 freeze_process(p);
139 if (!freezer_should_skip(p))
140 todo++;
1da177e4
LT
141 } while_each_thread(g, p);
142 read_unlock(&tasklist_lock);
143 yield(); /* Yield is okay here */
11b2ce2b 144 if (todo && time_after(jiffies, end_time))
6161b2ce 145 break;
11b2ce2b 146 } while (todo);
3e1d1d28 147
6161b2ce 148 if (todo) {
11b2ce2b
RW
149 /* This does not unfreeze processes that are already frozen
150 * (we have slightly ugly calling convention in that respect,
151 * and caller must call thaw_processes() if something fails),
152 * but it cleans up leftover PF_FREEZE requests.
153 */
14b5b7cf 154 printk("\n");
11b2ce2b
RW
155 printk(KERN_ERR "Stopping %s timed out after %d seconds "
156 "(%d tasks refusing to freeze):\n",
157 freeze_user_space ? "user space processes" :
158 "kernel threads",
159 TIMEOUT / HZ, todo);
328616e3 160 show_state();
6161b2ce 161 read_lock(&tasklist_lock);
02aaeb9b 162 do_each_thread(g, p) {
49b12d4f 163 if (freeze_user_space && !is_user_space(p))
11b2ce2b
RW
164 continue;
165
33e1c288 166 task_lock(p);
ba96a0c8
RW
167 if (freezeable(p) && !frozen(p) &&
168 !freezer_should_skip(p))
14b5b7cf 169 printk(KERN_ERR " %s\n", p->comm);
11b2ce2b 170
a7ef7878 171 cancel_freezing(p);
33e1c288 172 task_unlock(p);
02aaeb9b 173 } while_each_thread(g, p);
6161b2ce 174 read_unlock(&tasklist_lock);
6161b2ce
PM
175 }
176
11b2ce2b
RW
177 return todo;
178}
179
180/**
181 * freeze_processes - tell processes to enter the refrigerator
182 *
183 * Returns 0 on success, or the number of processes that didn't freeze,
184 * although they were told to.
185 */
186int freeze_processes(void)
187{
188 unsigned int nr_unfrozen;
189
190 printk("Stopping tasks ... ");
191 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
192 if (nr_unfrozen)
193 return nr_unfrozen;
194
195 sys_sync();
196 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
197 if (nr_unfrozen)
198 return nr_unfrozen;
199
14b5b7cf 200 printk("done.\n");
1da177e4
LT
201 BUG_ON(in_atomic());
202 return 0;
203}
204
a9b6f562 205static void thaw_tasks(int thaw_user_space)
1da177e4
LT
206{
207 struct task_struct *g, *p;
208
1da177e4 209 read_lock(&tasklist_lock);
a9b6f562
RW
210 do_each_thread(g, p) {
211 if (!freezeable(p))
212 continue;
ff39593a 213
a9b6f562
RW
214 if (is_user_space(p) == !thaw_user_space)
215 continue;
1da177e4 216
ba96a0c8 217 thaw_process(p);
a9b6f562 218 } while_each_thread(g, p);
1da177e4 219 read_unlock(&tasklist_lock);
a9b6f562
RW
220}
221
222void thaw_processes(void)
223{
224 printk("Restarting tasks ... ");
225 thaw_tasks(FREEZER_KERNEL_THREADS);
226 thaw_tasks(FREEZER_USER_SPACE);
1da177e4 227 schedule();
14b5b7cf 228 printk("done.\n");
1da177e4
LT
229}
230
231EXPORT_SYMBOL(refrigerator);