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
try_to_freeze_tasks(bool user_only)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 #ifdef CONFIG_PM_SLEEP
38 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
39 #endif
40
41 do_gettimeofday(&start);
42
43 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
44
45 if (!user_only)
46 freeze_workqueues_begin();
47
48 while (true) {
49 todo = 0;
50 read_lock(&tasklist_lock);
51 do_each_thread(g, p) {
52 if (p == current || !freeze_task(p))
53 continue;
54
55 if (!freezer_should_skip(p))
56 todo++;
57 } while_each_thread(g, p);
58 read_unlock(&tasklist_lock);
59
60 if (!user_only) {
61 wq_busy = freeze_workqueues_busy();
62 todo += wq_busy;
63 }
64
65 if (!todo || time_after(jiffies, end_time))
66 break;
67
68 if (pm_wakeup_pending()) {
69 #ifdef CONFIG_PM_SLEEP
70 pm_get_active_wakeup_sources(suspend_abort,
71 MAX_SUSPEND_ABORT_LEN);
72 log_suspend_abort_reason(suspend_abort);
73 #endif
74 wakeup = true;
75 break;
76 }
77
78 /*
79 * We need to retry, but first give the freezing tasks some
80 * time to enter the refrigerator. Start with an initial
81 * 1 ms sleep followed by exponential backoff until 8 ms.
82 */
83 usleep_range(sleep_usecs / 2, sleep_usecs);
84 if (sleep_usecs < 8 * USEC_PER_MSEC)
85 sleep_usecs *= 2;
86 }
87
88 do_gettimeofday(&end);
89 elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
90 do_div(elapsed_msecs64, NSEC_PER_MSEC);
91 elapsed_msecs = elapsed_msecs64;
92
93 if (wakeup) {
94 printk("\n");
95 printk(KERN_ERR "Freezing of tasks aborted after %d.%03d seconds",
96 elapsed_msecs / 1000, elapsed_msecs % 1000);
97 } else if (todo) {
98 printk("\n");
99 printk(KERN_ERR "Freezing of tasks failed after %d.%03d seconds"
100 " (%d tasks refusing to freeze, wq_busy=%d):\n",
101 elapsed_msecs / 1000, elapsed_msecs % 1000,
102 todo - wq_busy, wq_busy);
103
104 read_lock(&tasklist_lock);
105 do_each_thread(g, p) {
106 if (p != current && !freezer_should_skip(p)
107 && freezing(p) && !frozen(p))
108 sched_show_task(p);
109 } while_each_thread(g, p);
110 read_unlock(&tasklist_lock);
111 } else {
112 printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
113 elapsed_msecs % 1000);
114 }
115
116 return todo ? -EBUSY : 0;
117 }
118
119 /**
120 * freeze_processes - Signal user space processes to enter the refrigerator.
121 *
122 * On success, returns 0. On failure, -errno and system is fully thawed.
123 */
freeze_processes(void)124 int freeze_processes(void)
125 {
126 int error;
127
128 error = __usermodehelper_disable(UMH_FREEZING);
129 if (error)
130 return error;
131
132 if (!pm_freezing)
133 atomic_inc(&system_freezing_cnt);
134
135 printk("Freezing user space processes ... ");
136 pm_freezing = true;
137 error = try_to_freeze_tasks(true);
138 if (!error) {
139 printk("done.");
140 __usermodehelper_set_disable_depth(UMH_DISABLED);
141 oom_killer_disable();
142 }
143 printk("\n");
144 BUG_ON(in_atomic());
145
146 if (error)
147 thaw_processes();
148 return error;
149 }
150
151 /**
152 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
153 *
154 * On success, returns 0. On failure, -errno and only the kernel threads are
155 * thawed, so as to give a chance to the caller to do additional cleanups
156 * (if any) before thawing the userspace tasks. So, it is the responsibility
157 * of the caller to thaw the userspace tasks, when the time is right.
158 */
freeze_kernel_threads(void)159 int freeze_kernel_threads(void)
160 {
161 int error;
162
163 printk("Freezing remaining freezable tasks ... ");
164 pm_nosig_freezing = true;
165 error = try_to_freeze_tasks(false);
166 if (!error)
167 printk("done.");
168
169 printk("\n");
170 BUG_ON(in_atomic());
171
172 if (error)
173 thaw_kernel_threads();
174 return error;
175 }
176
thaw_processes(void)177 void thaw_processes(void)
178 {
179 struct task_struct *g, *p;
180
181 if (pm_freezing)
182 atomic_dec(&system_freezing_cnt);
183 pm_freezing = false;
184 pm_nosig_freezing = false;
185
186 oom_killer_enable();
187
188 printk("Restarting tasks ... ");
189
190 thaw_workqueues();
191
192 read_lock(&tasklist_lock);
193 do_each_thread(g, p) {
194 __thaw_task(p);
195 } while_each_thread(g, p);
196 read_unlock(&tasklist_lock);
197
198 usermodehelper_enable();
199
200 schedule();
201 printk("done.\n");
202 }
203
thaw_kernel_threads(void)204 void thaw_kernel_threads(void)
205 {
206 struct task_struct *g, *p;
207
208 pm_nosig_freezing = false;
209 printk("Restarting kernel threads ... ");
210
211 thaw_workqueues();
212
213 read_lock(&tasklist_lock);
214 do_each_thread(g, p) {
215 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
216 __thaw_task(p);
217 } while_each_thread(g, p);
218 read_unlock(&tasklist_lock);
219
220 schedule();
221 printk("done.\n");
222 }
223