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