Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.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/kmod.h>
20 #include <linux/wakeup_reason.h>
21 /* 
22  * Timeout for stopping processes
23  */
24 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
25
26 static int try_to_freeze_tasks(bool user_only)
27 {
28         struct task_struct *g, *p;
29         unsigned long end_time;
30         unsigned int todo;
31         bool wq_busy = false;
32         struct timeval start, end;
33         u64 elapsed_msecs64;
34         unsigned int elapsed_msecs;
35         bool wakeup = false;
36         int sleep_usecs = USEC_PER_MSEC;
37         char suspend_abort[MAX_SUSPEND_ABORT_LEN];
38
39         do_gettimeofday(&start);
40
41         end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
42
43         if (!user_only)
44                 freeze_workqueues_begin();
45
46         while (true) {
47                 todo = 0;
48                 read_lock(&tasklist_lock);
49                 do_each_thread(g, p) {
50                         if (p == current || !freeze_task(p))
51                                 continue;
52
53                         if (!freezer_should_skip(p))
54                                 todo++;
55                 } while_each_thread(g, p);
56                 read_unlock(&tasklist_lock);
57
58                 if (!user_only) {
59                         wq_busy = freeze_workqueues_busy();
60                         todo += wq_busy;
61                 }
62
63                 if (!todo || time_after(jiffies, end_time))
64                         break;
65
66                 if (pm_wakeup_pending()) {
67                         pm_get_active_wakeup_sources(suspend_abort,
68                                 MAX_SUSPEND_ABORT_LEN);
69                         log_suspend_abort_reason(suspend_abort);
70                         wakeup = true;
71                         break;
72                 }
73
74                 /*
75                  * We need to retry, but first give the freezing tasks some
76                  * time to enter the refrigerator.  Start with an initial
77                  * 1 ms sleep followed by exponential backoff until 8 ms.
78                  */
79                 usleep_range(sleep_usecs / 2, sleep_usecs);
80                 if (sleep_usecs < 8 * USEC_PER_MSEC)
81                         sleep_usecs *= 2;
82         }
83
84         do_gettimeofday(&end);
85         elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
86         do_div(elapsed_msecs64, NSEC_PER_MSEC);
87         elapsed_msecs = elapsed_msecs64;
88
89         if (wakeup) {
90                 printk("\n");
91                 printk(KERN_ERR "Freezing of tasks aborted after %d.%03d seconds",
92                        elapsed_msecs / 1000, elapsed_msecs % 1000);
93         } else if (todo) {
94                 printk("\n");
95                 printk(KERN_ERR "Freezing of tasks failed after %d.%03d seconds"
96                        " (%d tasks refusing to freeze, wq_busy=%d):\n",
97                        elapsed_msecs / 1000, elapsed_msecs % 1000,
98                        todo - wq_busy, wq_busy);
99
100                 read_lock(&tasklist_lock);
101                 do_each_thread(g, p) {
102                         if (p != current && !freezer_should_skip(p)
103                             && freezing(p) && !frozen(p))
104                                 sched_show_task(p);
105                 } while_each_thread(g, p);
106                 read_unlock(&tasklist_lock);
107         } else {
108                 printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
109                         elapsed_msecs % 1000);
110         }
111
112         return todo ? -EBUSY : 0;
113 }
114
115 /*
116  * Returns true if all freezable tasks (except for current) are frozen already
117  */
118 static bool check_frozen_processes(void)
119 {
120         struct task_struct *g, *p;
121         bool ret = true;
122
123         read_lock(&tasklist_lock);
124         for_each_process_thread(g, p) {
125                 if (p != current && !freezer_should_skip(p) &&
126                     !frozen(p)) {
127                         ret = false;
128                         goto done;
129                 }
130         }
131 done:
132         read_unlock(&tasklist_lock);
133
134         return ret;
135 }
136
137 /**
138  * freeze_processes - Signal user space processes to enter the refrigerator.
139  *
140  * On success, returns 0.  On failure, -errno and system is fully thawed.
141  */
142 int freeze_processes(void)
143 {
144         int error;
145         int oom_kills_saved;
146
147         error = __usermodehelper_disable(UMH_FREEZING);
148         if (error)
149                 return error;
150
151         if (!pm_freezing)
152                 atomic_inc(&system_freezing_cnt);
153
154         printk("Freezing user space processes ... ");
155         pm_freezing = true;
156         oom_kills_saved = oom_kills_count();
157         error = try_to_freeze_tasks(true);
158         if (!error) {
159                 __usermodehelper_set_disable_depth(UMH_DISABLED);
160                 oom_killer_disable();
161
162                 /*
163                  * There might have been an OOM kill while we were
164                  * freezing tasks and the killed task might be still
165                  * on the way out so we have to double check for race.
166                  */
167                 if (oom_kills_count() != oom_kills_saved &&
168                                 !check_frozen_processes()) {
169                         __usermodehelper_set_disable_depth(UMH_ENABLED);
170                         printk("OOM in progress.");
171                         error = -EBUSY;
172                         goto done;
173                 }
174                 printk("done.");
175         }
176 done:
177         printk("\n");
178         BUG_ON(in_atomic());
179
180         if (error)
181                 thaw_processes();
182         return error;
183 }
184
185 /**
186  * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
187  *
188  * On success, returns 0.  On failure, -errno and only the kernel threads are
189  * thawed, so as to give a chance to the caller to do additional cleanups
190  * (if any) before thawing the userspace tasks. So, it is the responsibility
191  * of the caller to thaw the userspace tasks, when the time is right.
192  */
193 int freeze_kernel_threads(void)
194 {
195         int error;
196
197         printk("Freezing remaining freezable tasks ... ");
198         pm_nosig_freezing = true;
199         error = try_to_freeze_tasks(false);
200         if (!error)
201                 printk("done.");
202
203         printk("\n");
204         BUG_ON(in_atomic());
205
206         if (error)
207                 thaw_kernel_threads();
208         return error;
209 }
210
211 void thaw_processes(void)
212 {
213         struct task_struct *g, *p;
214
215         if (pm_freezing)
216                 atomic_dec(&system_freezing_cnt);
217         pm_freezing = false;
218         pm_nosig_freezing = false;
219
220         oom_killer_enable();
221
222         printk("Restarting tasks ... ");
223
224         __usermodehelper_set_disable_depth(UMH_FREEZING);
225         thaw_workqueues();
226
227         read_lock(&tasklist_lock);
228         do_each_thread(g, p) {
229                 __thaw_task(p);
230         } while_each_thread(g, p);
231         read_unlock(&tasklist_lock);
232
233         usermodehelper_enable();
234
235         schedule();
236         printk("done.\n");
237 }
238
239 void thaw_kernel_threads(void)
240 {
241         struct task_struct *g, *p;
242
243         pm_nosig_freezing = false;
244         printk("Restarting kernel threads ... ");
245
246         thaw_workqueues();
247
248         read_lock(&tasklist_lock);
249         do_each_thread(g, p) {
250                 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
251                         __thaw_task(p);
252         } while_each_thread(g, p);
253         read_unlock(&tasklist_lock);
254
255         schedule();
256         printk("done.\n");
257 }