OSDN Git Service

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