pm_trace displays the wrong time from the RTC
[deliverable/linux.git] / kernel / power / process.c
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
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>
16
17 /*
18 * Timeout for stopping processes
19 */
20 #define TIMEOUT (20 * HZ)
21
22 #define FREEZER_KERNEL_THREADS 0
23 #define FREEZER_USER_SPACE 1
24
25 static inline int freezeable(struct task_struct * p)
26 {
27 if ((p == current) ||
28 (p->flags & PF_NOFREEZE) ||
29 (p->exit_state != 0))
30 return 0;
31 return 1;
32 }
33
34 /*
35 * freezing is complete, mark current process as frozen
36 */
37 static inline void frozen_process(void)
38 {
39 if (!unlikely(current->flags & PF_NOFREEZE)) {
40 current->flags |= PF_FROZEN;
41 wmb();
42 }
43 clear_freeze_flag(current);
44 }
45
46 /* Refrigerator is place where frozen processes are stored :-). */
47 void refrigerator(void)
48 {
49 /* Hmm, should we be allowed to suspend when there are realtime
50 processes around? */
51 long save;
52
53 task_lock(current);
54 if (freezing(current)) {
55 frozen_process();
56 task_unlock(current);
57 } else {
58 task_unlock(current);
59 return;
60 }
61 save = current->state;
62 pr_debug("%s entered refrigerator\n", current->comm);
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
68 for (;;) {
69 set_current_state(TASK_UNINTERRUPTIBLE);
70 if (!frozen(current))
71 break;
72 schedule();
73 }
74 pr_debug("%s left refrigerator\n", current->comm);
75 __set_current_state(save);
76 }
77
78 static void fake_signal_wake_up(struct task_struct *p, int resume)
79 {
80 unsigned long flags;
81
82 spin_lock_irqsave(&p->sighand->siglock, flags);
83 signal_wake_up(p, resume);
84 spin_unlock_irqrestore(&p->sighand->siglock, flags);
85 }
86
87 static void send_fake_signal(struct task_struct *p)
88 {
89 if (p->state == TASK_STOPPED)
90 force_sig_specific(SIGSTOP, p);
91 fake_signal_wake_up(p, p->state == TASK_STOPPED);
92 }
93
94 static int has_mm(struct task_struct *p)
95 {
96 return (p->mm && !(p->flags & PF_BORROWED_MM));
97 }
98
99 /**
100 * freeze_task - send a freeze request to given task
101 * @p: task to send the request to
102 * @with_mm_only: if set, the request will only be sent if the task has its
103 * own mm
104 * Return value: 0, if @with_mm_only is set and the task has no mm of its
105 * own or the task is frozen, 1, otherwise
106 *
107 * The freeze request is sent by seting the tasks's TIF_FREEZE flag and
108 * either sending a fake signal to it or waking it up, depending on whether
109 * or not it has its own mm (ie. it is a user land task). If @with_mm_only
110 * is set and the task has no mm of its own (ie. it is a kernel thread),
111 * its TIF_FREEZE flag should not be set.
112 *
113 * The task_lock() is necessary to prevent races with exit_mm() or
114 * use_mm()/unuse_mm() from occuring.
115 */
116 static int freeze_task(struct task_struct *p, int with_mm_only)
117 {
118 int ret = 1;
119
120 task_lock(p);
121 if (freezing(p)) {
122 if (has_mm(p)) {
123 if (!signal_pending(p))
124 fake_signal_wake_up(p, 0);
125 } else {
126 if (with_mm_only)
127 ret = 0;
128 else
129 wake_up_state(p, TASK_INTERRUPTIBLE);
130 }
131 } else {
132 rmb();
133 if (frozen(p)) {
134 ret = 0;
135 } else {
136 if (has_mm(p)) {
137 set_freeze_flag(p);
138 send_fake_signal(p);
139 } else {
140 if (with_mm_only) {
141 ret = 0;
142 } else {
143 set_freeze_flag(p);
144 wake_up_state(p, TASK_INTERRUPTIBLE);
145 }
146 }
147 }
148 }
149 task_unlock(p);
150 return ret;
151 }
152
153 static void cancel_freezing(struct task_struct *p)
154 {
155 unsigned long flags;
156
157 if (freezing(p)) {
158 pr_debug(" clean up: %s\n", p->comm);
159 clear_freeze_flag(p);
160 spin_lock_irqsave(&p->sighand->siglock, flags);
161 recalc_sigpending_and_wake(p);
162 spin_unlock_irqrestore(&p->sighand->siglock, flags);
163 }
164 }
165
166 static int try_to_freeze_tasks(int freeze_user_space)
167 {
168 struct task_struct *g, *p;
169 unsigned long end_time;
170 unsigned int todo;
171
172 end_time = jiffies + TIMEOUT;
173 do {
174 todo = 0;
175 read_lock(&tasklist_lock);
176 do_each_thread(g, p) {
177 if (frozen(p) || !freezeable(p))
178 continue;
179
180 if (p->state == TASK_TRACED && frozen(p->parent)) {
181 cancel_freezing(p);
182 continue;
183 }
184
185 if (!freeze_task(p, freeze_user_space))
186 continue;
187
188 if (!freezer_should_skip(p))
189 todo++;
190 } while_each_thread(g, p);
191 read_unlock(&tasklist_lock);
192 yield(); /* Yield is okay here */
193 if (time_after(jiffies, end_time))
194 break;
195 } while (todo);
196
197 if (todo) {
198 /* This does not unfreeze processes that are already frozen
199 * (we have slightly ugly calling convention in that respect,
200 * and caller must call thaw_processes() if something fails),
201 * but it cleans up leftover PF_FREEZE requests.
202 */
203 printk("\n");
204 printk(KERN_ERR "Freezing of %s timed out after %d seconds "
205 "(%d tasks refusing to freeze):\n",
206 freeze_user_space ? "user space " : "tasks ",
207 TIMEOUT / HZ, todo);
208 show_state();
209 read_lock(&tasklist_lock);
210 do_each_thread(g, p) {
211 task_lock(p);
212 if (freezing(p) && !freezer_should_skip(p))
213 printk(KERN_ERR " %s\n", p->comm);
214 cancel_freezing(p);
215 task_unlock(p);
216 } while_each_thread(g, p);
217 read_unlock(&tasklist_lock);
218 }
219
220 return todo ? -EBUSY : 0;
221 }
222
223 /**
224 * freeze_processes - tell processes to enter the refrigerator
225 */
226 int freeze_processes(void)
227 {
228 int error;
229
230 printk("Stopping tasks ... ");
231 error = try_to_freeze_tasks(FREEZER_USER_SPACE);
232 if (error)
233 return error;
234
235 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
236 if (error)
237 return error;
238
239 printk("done.\n");
240 BUG_ON(in_atomic());
241 return 0;
242 }
243
244 static void thaw_tasks(int thaw_user_space)
245 {
246 struct task_struct *g, *p;
247
248 read_lock(&tasklist_lock);
249 do_each_thread(g, p) {
250 if (!freezeable(p))
251 continue;
252
253 if (!p->mm == thaw_user_space)
254 continue;
255
256 thaw_process(p);
257 } while_each_thread(g, p);
258 read_unlock(&tasklist_lock);
259 }
260
261 void thaw_processes(void)
262 {
263 printk("Restarting tasks ... ");
264 thaw_tasks(FREEZER_KERNEL_THREADS);
265 thaw_tasks(FREEZER_USER_SPACE);
266 schedule();
267 printk("done.\n");
268 }
269
270 EXPORT_SYMBOL(refrigerator);
This page took 0.043905 seconds and 5 git commands to generate.