]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/power/process.c
Merge branch 'slab-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/christoph/vm
[mirror_ubuntu-zesty-kernel.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 }
0c1eecfb 43 clear_freeze_flag(current);
88f18ba0
GS
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 74 pr_debug("%s left refrigerator\n", current->comm);
f4a3a7d6 75 __set_current_state(save);
1da177e4
LT
76}
77
d5d8c597 78static void fake_signal_wake_up(struct task_struct *p, int resume)
02aaeb9b
RW
79{
80 unsigned long flags;
81
d5d8c597
RW
82 spin_lock_irqsave(&p->sighand->siglock, flags);
83 signal_wake_up(p, resume);
84 spin_unlock_irqrestore(&p->sighand->siglock, flags);
85}
86
87static void send_fake_signal(struct task_struct *p)
88{
d2f60e5f 89 if (task_is_stopped(p))
d5d8c597 90 force_sig_specific(SIGSTOP, p);
d2f60e5f 91 fake_signal_wake_up(p, task_is_stopped(p));
d5d8c597
RW
92}
93
94static 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 */
116static 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 {
8a102eed 132 rmb();
d5d8c597
RW
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 }
8a102eed 147 }
02aaeb9b 148 }
d5d8c597
RW
149 task_unlock(p);
150 return ret;
02aaeb9b
RW
151}
152
a7ef7878
RW
153static 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);
0c1eecfb 159 clear_freeze_flag(p);
a7ef7878 160 spin_lock_irqsave(&p->sighand->siglock, flags);
7bb44ade 161 recalc_sigpending_and_wake(p);
a7ef7878
RW
162 spin_unlock_irqrestore(&p->sighand->siglock, flags);
163 }
164}
165
e7cd8a72 166static int try_to_freeze_tasks(int freeze_user_space)
1da177e4 167{
1da177e4 168 struct task_struct *g, *p;
11b2ce2b
RW
169 unsigned long end_time;
170 unsigned int todo;
438e2ce6
RW
171 struct timeval start, end;
172 s64 elapsed_csecs64;
173 unsigned int elapsed_csecs;
174
175 do_gettimeofday(&start);
3e1d1d28 176
11b2ce2b 177 end_time = jiffies + TIMEOUT;
1da177e4 178 do {
11b2ce2b 179 todo = 0;
1da177e4
LT
180 read_lock(&tasklist_lock);
181 do_each_thread(g, p) {
0c1eecfb 182 if (frozen(p) || !freezeable(p))
1da177e4 183 continue;
11b2ce2b 184
d2f60e5f 185 if (task_is_traced(p) && frozen(p->parent)) {
d5d8c597
RW
186 cancel_freezing(p);
187 continue;
a7ef7878 188 }
d5d8c597
RW
189
190 if (!freeze_task(p, freeze_user_space))
191 continue;
192
ba96a0c8
RW
193 if (!freezer_should_skip(p))
194 todo++;
1da177e4
LT
195 } while_each_thread(g, p);
196 read_unlock(&tasklist_lock);
197 yield(); /* Yield is okay here */
c2cf7d87 198 if (time_after(jiffies, end_time))
6161b2ce 199 break;
11b2ce2b 200 } while (todo);
3e1d1d28 201
438e2ce6
RW
202 do_gettimeofday(&end);
203 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
204 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
205 elapsed_csecs = elapsed_csecs64;
206
6161b2ce 207 if (todo) {
11b2ce2b
RW
208 /* This does not unfreeze processes that are already frozen
209 * (we have slightly ugly calling convention in that respect,
210 * and caller must call thaw_processes() if something fails),
211 * but it cleans up leftover PF_FREEZE requests.
212 */
14b5b7cf 213 printk("\n");
438e2ce6 214 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
11b2ce2b 215 "(%d tasks refusing to freeze):\n",
438e2ce6 216 elapsed_csecs / 100, elapsed_csecs % 100, todo);
328616e3 217 show_state();
6161b2ce 218 read_lock(&tasklist_lock);
02aaeb9b 219 do_each_thread(g, p) {
33e1c288 220 task_lock(p);
0c1eecfb 221 if (freezing(p) && !freezer_should_skip(p))
14b5b7cf 222 printk(KERN_ERR " %s\n", p->comm);
a7ef7878 223 cancel_freezing(p);
33e1c288 224 task_unlock(p);
02aaeb9b 225 } while_each_thread(g, p);
6161b2ce 226 read_unlock(&tasklist_lock);
438e2ce6
RW
227 } else {
228 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
229 elapsed_csecs % 100);
6161b2ce
PM
230 }
231
e7cd8a72 232 return todo ? -EBUSY : 0;
11b2ce2b
RW
233}
234
235/**
236 * freeze_processes - tell processes to enter the refrigerator
11b2ce2b
RW
237 */
238int freeze_processes(void)
239{
e7cd8a72 240 int error;
11b2ce2b 241
b842ee57 242 printk("Freezing user space processes ... ");
e7cd8a72
RW
243 error = try_to_freeze_tasks(FREEZER_USER_SPACE);
244 if (error)
b842ee57
RW
245 goto Exit;
246 printk("done.\n");
11b2ce2b 247
b842ee57 248 printk("Freezing remaining freezable tasks ... ");
e7cd8a72
RW
249 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
250 if (error)
b842ee57
RW
251 goto Exit;
252 printk("done.");
253 Exit:
1da177e4 254 BUG_ON(in_atomic());
b842ee57
RW
255 printk("\n");
256 return error;
1da177e4
LT
257}
258
a9b6f562 259static void thaw_tasks(int thaw_user_space)
1da177e4
LT
260{
261 struct task_struct *g, *p;
262
1da177e4 263 read_lock(&tasklist_lock);
a9b6f562
RW
264 do_each_thread(g, p) {
265 if (!freezeable(p))
266 continue;
ff39593a 267
0c1eecfb 268 if (!p->mm == thaw_user_space)
a9b6f562 269 continue;
1da177e4 270
ba96a0c8 271 thaw_process(p);
a9b6f562 272 } while_each_thread(g, p);
1da177e4 273 read_unlock(&tasklist_lock);
a9b6f562
RW
274}
275
276void thaw_processes(void)
277{
278 printk("Restarting tasks ... ");
279 thaw_tasks(FREEZER_KERNEL_THREADS);
280 thaw_tasks(FREEZER_USER_SPACE);
1da177e4 281 schedule();
14b5b7cf 282 printk("done.\n");
1da177e4
LT
283}
284
285EXPORT_SYMBOL(refrigerator);