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