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