temp revert rk change
[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/wakelock.h>
20
21 /* 
22  * Timeout for stopping processes
23  */
24 #define TIMEOUT (20 * HZ)
25
26 static inline int freezeable(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         unsigned int wakeup = 0;
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) || !freezeable(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                         if (!task_is_stopped_or_traced(p) &&
70                             !freezer_should_skip(p))
71                                 todo++;
72                 } while_each_thread(g, p);
73                 read_unlock(&tasklist_lock);
74
75                 if (!sig_only) {
76                         wq_busy = freeze_workqueues_busy();
77                         todo += wq_busy;
78                 }
79
80                 if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
81                         wakeup = 1;
82                         break;
83                 }
84                 if (!todo || time_after(jiffies, end_time))
85                         break;
86
87                 /*
88                  * We need to retry, but first give the freezing tasks some
89                  * time to enter the regrigerator.
90                  */
91                 msleep(10);
92         }
93
94         do_gettimeofday(&end);
95         elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
96         do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
97         elapsed_csecs = elapsed_csecs64;
98
99         if (todo) {
100                 /* This does not unfreeze processes that are already frozen
101                  * (we have slightly ugly calling convention in that respect,
102                  * and caller must call thaw_processes() if something fails),
103                  * but it cleans up leftover PF_FREEZE requests.
104                  */
105                 if(wakeup) {
106                         printk("\n");
107                         printk(KERN_ERR "Freezing of %s aborted\n",
108                                         sig_only ? "user space " : "tasks ");
109                 }
110                 else {
111                         printk("\n");
112                         printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
113                                "(%d tasks refusing to freeze, wq_busy=%d):\n",
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 (freezing(p) && !freezer_should_skip(p) &&
123                                 elapsed_csecs > 100)
124                                 sched_show_task(p);
125                         cancel_freezing(p);
126                         task_unlock(p);
127                 } while_each_thread(g, p);
128                 read_unlock(&tasklist_lock);
129         } else {
130                 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
131                         elapsed_csecs % 100);
132         }
133
134         return todo ? -EBUSY : 0;
135 }
136
137 /**
138  *      freeze_processes - tell processes to enter the refrigerator
139  */
140 int freeze_processes(void)
141 {
142         int error;
143
144         printk("Freezing user space processes ... ");
145         error = try_to_freeze_tasks(true);
146         if (error)
147                 goto Exit;
148         printk("done.\n");
149
150         printk("Freezing remaining freezable tasks ... ");
151         error = try_to_freeze_tasks(false);
152         if (error)
153                 goto Exit;
154         printk("done.");
155
156         oom_killer_disable();
157  Exit:
158         BUG_ON(in_atomic());
159         printk("\n");
160
161         return error;
162 }
163
164 static void thaw_tasks(bool nosig_only)
165 {
166         struct task_struct *g, *p;
167
168         read_lock(&tasklist_lock);
169         do_each_thread(g, p) {
170                 if (!freezeable(p))
171                         continue;
172
173                 if (nosig_only && should_send_signal(p))
174                         continue;
175
176                 if (cgroup_freezing_or_frozen(p))
177                         continue;
178
179                 thaw_process(p);
180         } while_each_thread(g, p);
181         read_unlock(&tasklist_lock);
182 }
183
184 void thaw_processes(void)
185 {
186         oom_killer_enable();
187
188         printk("Restarting tasks ... ");
189         thaw_workqueues();
190         thaw_tasks(true);
191         thaw_tasks(false);
192         schedule();
193         printk("done.\n");
194 }
195