freezer: remove racy clear_freeze_flag() and set PF_NOFREEZE on dead tasks
[cascardo/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/oom.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19
20 /* 
21  * Timeout for stopping processes
22  */
23 #define TIMEOUT (20 * HZ)
24
25 static inline int freezable(struct task_struct * p)
26 {
27         if ((p == current) ||
28             (p->flags & PF_NOFREEZE))
29                 return 0;
30         return 1;
31 }
32
33 static int try_to_freeze_tasks(bool sig_only)
34 {
35         struct task_struct *g, *p;
36         unsigned long end_time;
37         unsigned int todo;
38         bool wq_busy = false;
39         struct timeval start, end;
40         u64 elapsed_csecs64;
41         unsigned int elapsed_csecs;
42         bool wakeup = false;
43
44         do_gettimeofday(&start);
45
46         end_time = jiffies + TIMEOUT;
47
48         if (!sig_only)
49                 freeze_workqueues_begin();
50
51         while (true) {
52                 todo = 0;
53                 read_lock(&tasklist_lock);
54                 do_each_thread(g, p) {
55                         if (frozen(p) || !freezable(p))
56                                 continue;
57
58                         if (!freeze_task(p, sig_only))
59                                 continue;
60
61                         /*
62                          * Now that we've done set_freeze_flag, don't
63                          * perturb a task in TASK_STOPPED or TASK_TRACED.
64                          * It is "frozen enough".  If the task does wake
65                          * up, it will immediately call try_to_freeze.
66                          *
67                          * Because freeze_task() goes through p's
68                          * scheduler lock after setting TIF_FREEZE, it's
69                          * guaranteed that either we see TASK_RUNNING or
70                          * try_to_stop() after schedule() in ptrace/signal
71                          * stop sees TIF_FREEZE.
72                          */
73                         if (!task_is_stopped_or_traced(p) &&
74                             !freezer_should_skip(p))
75                                 todo++;
76                 } while_each_thread(g, p);
77                 read_unlock(&tasklist_lock);
78
79                 if (!sig_only) {
80                         wq_busy = freeze_workqueues_busy();
81                         todo += wq_busy;
82                 }
83
84                 if (!todo || time_after(jiffies, end_time))
85                         break;
86
87                 if (pm_wakeup_pending()) {
88                         wakeup = true;
89                         break;
90                 }
91
92                 /*
93                  * We need to retry, but first give the freezing tasks some
94                  * time to enter the regrigerator.
95                  */
96                 msleep(10);
97         }
98
99         do_gettimeofday(&end);
100         elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
101         do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
102         elapsed_csecs = elapsed_csecs64;
103
104         if (todo) {
105                 /* This does not unfreeze processes that are already frozen
106                  * (we have slightly ugly calling convention in that respect,
107                  * and caller must call thaw_processes() if something fails),
108                  * but it cleans up leftover PF_FREEZE requests.
109                  */
110                 printk("\n");
111                 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
112                        "(%d tasks refusing to freeze, wq_busy=%d):\n",
113                        wakeup ? "aborted" : "failed",
114                        elapsed_csecs / 100, elapsed_csecs % 100,
115                        todo - wq_busy, wq_busy);
116
117                 thaw_workqueues();
118
119                 read_lock(&tasklist_lock);
120                 do_each_thread(g, p) {
121                         task_lock(p);
122                         if (!wakeup && freezing(p) && !freezer_should_skip(p))
123                                 sched_show_task(p);
124                         cancel_freezing(p);
125                         task_unlock(p);
126                 } while_each_thread(g, p);
127                 read_unlock(&tasklist_lock);
128         } else {
129                 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
130                         elapsed_csecs % 100);
131         }
132
133         return todo ? -EBUSY : 0;
134 }
135
136 /**
137  * freeze_processes - Signal user space processes to enter the refrigerator.
138  */
139 int freeze_processes(void)
140 {
141         int error;
142
143         printk("Freezing user space processes ... ");
144         error = try_to_freeze_tasks(true);
145         if (!error) {
146                 printk("done.");
147                 oom_killer_disable();
148         }
149         printk("\n");
150         BUG_ON(in_atomic());
151
152         return error;
153 }
154
155 /**
156  * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
157  */
158 int freeze_kernel_threads(void)
159 {
160         int error;
161
162         printk("Freezing remaining freezable tasks ... ");
163         error = try_to_freeze_tasks(false);
164         if (!error)
165                 printk("done.");
166
167         printk("\n");
168         BUG_ON(in_atomic());
169
170         return error;
171 }
172
173 static void thaw_tasks(bool nosig_only)
174 {
175         struct task_struct *g, *p;
176
177         read_lock(&tasklist_lock);
178         do_each_thread(g, p) {
179                 if (!freezable(p))
180                         continue;
181
182                 if (nosig_only && should_send_signal(p))
183                         continue;
184
185                 if (cgroup_freezing_or_frozen(p))
186                         continue;
187
188                 __thaw_task(p);
189         } while_each_thread(g, p);
190         read_unlock(&tasklist_lock);
191 }
192
193 void thaw_processes(void)
194 {
195         oom_killer_enable();
196
197         printk("Restarting tasks ... ");
198         thaw_workqueues();
199         thaw_tasks(true);
200         thaw_tasks(false);
201         schedule();
202         printk("done.\n");
203 }
204