]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 | 11 | #include <linux/interrupt.h> |
1a8670a2 | 12 | #include <linux/oom.h> |
1da177e4 LT |
13 | #include <linux/suspend.h> |
14 | #include <linux/module.h> | |
b17b0153 | 15 | #include <linux/sched/debug.h> |
29930025 | 16 | #include <linux/sched/task.h> |
02aaeb9b | 17 | #include <linux/syscalls.h> |
7dfb7103 | 18 | #include <linux/freezer.h> |
be404f02 | 19 | #include <linux/delay.h> |
a0a1a5fd | 20 | #include <linux/workqueue.h> |
1e73203c | 21 | #include <linux/kmod.h> |
bb3632c6 | 22 | #include <trace/events/power.h> |
50e76632 | 23 | #include <linux/cpuset.h> |
1da177e4 | 24 | |
50e76632 | 25 | /* |
1da177e4 LT |
26 | * Timeout for stopping processes |
27 | */ | |
957d1282 | 28 | unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC; |
1da177e4 | 29 | |
839e3407 | 30 | static int try_to_freeze_tasks(bool user_only) |
1da177e4 | 31 | { |
1da177e4 | 32 | struct task_struct *g, *p; |
11b2ce2b RW |
33 | unsigned long end_time; |
34 | unsigned int todo; | |
a0a1a5fd | 35 | bool wq_busy = false; |
f7b382b9 | 36 | ktime_t start, end, elapsed; |
18ad0c62 | 37 | unsigned int elapsed_msecs; |
dbeeec5f | 38 | bool wakeup = false; |
18ad0c62 | 39 | int sleep_usecs = USEC_PER_MSEC; |
438e2ce6 | 40 | |
f7b382b9 | 41 | start = ktime_get_boottime(); |
3e1d1d28 | 42 | |
957d1282 | 43 | end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs); |
a0a1a5fd | 44 | |
839e3407 | 45 | if (!user_only) |
a0a1a5fd TH |
46 | freeze_workqueues_begin(); |
47 | ||
be404f02 | 48 | while (true) { |
11b2ce2b | 49 | todo = 0; |
1da177e4 | 50 | read_lock(&tasklist_lock); |
a28e785a | 51 | for_each_process_thread(g, p) { |
839e3407 | 52 | if (p == current || !freeze_task(p)) |
d5d8c597 RW |
53 | continue; |
54 | ||
5d8f72b5 | 55 | if (!freezer_should_skip(p)) |
ba96a0c8 | 56 | todo++; |
a28e785a | 57 | } |
1da177e4 | 58 | read_unlock(&tasklist_lock); |
a0a1a5fd | 59 | |
839e3407 | 60 | if (!user_only) { |
a0a1a5fd TH |
61 | wq_busy = freeze_workqueues_busy(); |
62 | todo += wq_busy; | |
63 | } | |
64 | ||
be404f02 | 65 | if (!todo || time_after(jiffies, end_time)) |
6161b2ce | 66 | break; |
be404f02 | 67 | |
a2867e08 | 68 | if (pm_wakeup_pending()) { |
dbeeec5f RW |
69 | wakeup = true; |
70 | break; | |
71 | } | |
72 | ||
be404f02 TH |
73 | /* |
74 | * We need to retry, but first give the freezing tasks some | |
18ad0c62 CC |
75 | * time to enter the refrigerator. Start with an initial |
76 | * 1 ms sleep followed by exponential backoff until 8 ms. | |
be404f02 | 77 | */ |
18ad0c62 CC |
78 | usleep_range(sleep_usecs / 2, sleep_usecs); |
79 | if (sleep_usecs < 8 * USEC_PER_MSEC) | |
80 | sleep_usecs *= 2; | |
be404f02 | 81 | } |
3e1d1d28 | 82 | |
f7b382b9 AJ |
83 | end = ktime_get_boottime(); |
84 | elapsed = ktime_sub(end, start); | |
85 | elapsed_msecs = ktime_to_ms(elapsed); | |
438e2ce6 | 86 | |
6161b2ce | 87 | if (todo) { |
35536ae1 MH |
88 | pr_cont("\n"); |
89 | pr_err("Freezing of tasks %s after %d.%03d seconds " | |
a0a1a5fd | 90 | "(%d tasks refusing to freeze, wq_busy=%d):\n", |
dbeeec5f | 91 | wakeup ? "aborted" : "failed", |
18ad0c62 | 92 | elapsed_msecs / 1000, elapsed_msecs % 1000, |
a0a1a5fd TH |
93 | todo - wq_busy, wq_busy); |
94 | ||
7b776af6 RL |
95 | if (wq_busy) |
96 | show_workqueue_state(); | |
97 | ||
6c83b481 RW |
98 | if (!wakeup) { |
99 | read_lock(&tasklist_lock); | |
a28e785a | 100 | for_each_process_thread(g, p) { |
6c83b481 RW |
101 | if (p != current && !freezer_should_skip(p) |
102 | && freezing(p) && !frozen(p)) | |
103 | sched_show_task(p); | |
a28e785a | 104 | } |
6c83b481 RW |
105 | read_unlock(&tasklist_lock); |
106 | } | |
438e2ce6 | 107 | } else { |
35536ae1 | 108 | pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000, |
18ad0c62 | 109 | elapsed_msecs % 1000); |
6161b2ce PM |
110 | } |
111 | ||
e7cd8a72 | 112 | return todo ? -EBUSY : 0; |
11b2ce2b RW |
113 | } |
114 | ||
115 | /** | |
2aede851 | 116 | * freeze_processes - Signal user space processes to enter the refrigerator. |
2b44c4db CC |
117 | * The current thread will not be frozen. The same process that calls |
118 | * freeze_processes must later call thaw_processes. | |
03afed8b TH |
119 | * |
120 | * On success, returns 0. On failure, -errno and system is fully thawed. | |
11b2ce2b RW |
121 | */ |
122 | int freeze_processes(void) | |
123 | { | |
e7cd8a72 | 124 | int error; |
11b2ce2b | 125 | |
247bc037 | 126 | error = __usermodehelper_disable(UMH_FREEZING); |
1e73203c RW |
127 | if (error) |
128 | return error; | |
129 | ||
2b44c4db CC |
130 | /* Make sure this task doesn't get frozen */ |
131 | current->flags |= PF_SUSPEND_TASK; | |
132 | ||
a3201227 TH |
133 | if (!pm_freezing) |
134 | atomic_inc(&system_freezing_cnt); | |
135 | ||
33e4f80e | 136 | pm_wakeup_clear(true); |
35536ae1 | 137 | pr_info("Freezing user space processes ... "); |
a3201227 | 138 | pm_freezing = true; |
ebb12db5 | 139 | error = try_to_freeze_tasks(true); |
2aede851 | 140 | if (!error) { |
247bc037 | 141 | __usermodehelper_set_disable_depth(UMH_DISABLED); |
c32b3cbe | 142 | pr_cont("done."); |
2aede851 | 143 | } |
35536ae1 | 144 | pr_cont("\n"); |
2aede851 RW |
145 | BUG_ON(in_atomic()); |
146 | ||
c32b3cbe MH |
147 | /* |
148 | * Now that the whole userspace is frozen we need to disbale | |
149 | * the OOM killer to disallow any further interference with | |
7d2e7a22 MH |
150 | * killable tasks. There is no guarantee oom victims will |
151 | * ever reach a point they go away we have to wait with a timeout. | |
c32b3cbe | 152 | */ |
7d2e7a22 | 153 | if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs))) |
c32b3cbe MH |
154 | error = -EBUSY; |
155 | ||
03afed8b TH |
156 | if (error) |
157 | thaw_processes(); | |
2aede851 RW |
158 | return error; |
159 | } | |
160 | ||
161 | /** | |
162 | * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator. | |
03afed8b | 163 | * |
379e0be8 SB |
164 | * On success, returns 0. On failure, -errno and only the kernel threads are |
165 | * thawed, so as to give a chance to the caller to do additional cleanups | |
166 | * (if any) before thawing the userspace tasks. So, it is the responsibility | |
167 | * of the caller to thaw the userspace tasks, when the time is right. | |
2aede851 RW |
168 | */ |
169 | int freeze_kernel_threads(void) | |
170 | { | |
171 | int error; | |
11b2ce2b | 172 | |
35536ae1 MH |
173 | pr_info("Freezing remaining freezable tasks ... "); |
174 | ||
a3201227 | 175 | pm_nosig_freezing = true; |
ebb12db5 | 176 | error = try_to_freeze_tasks(false); |
2aede851 | 177 | if (!error) |
35536ae1 | 178 | pr_cont("done."); |
7f33d49a | 179 | |
35536ae1 | 180 | pr_cont("\n"); |
2aede851 | 181 | BUG_ON(in_atomic()); |
7f33d49a | 182 | |
03afed8b | 183 | if (error) |
379e0be8 | 184 | thaw_kernel_threads(); |
b842ee57 | 185 | return error; |
1da177e4 LT |
186 | } |
187 | ||
6cd8dedc | 188 | void thaw_processes(void) |
1da177e4 LT |
189 | { |
190 | struct task_struct *g, *p; | |
2b44c4db | 191 | struct task_struct *curr = current; |
1da177e4 | 192 | |
bb3632c6 | 193 | trace_suspend_resume(TPS("thaw_processes"), 0, true); |
a3201227 TH |
194 | if (pm_freezing) |
195 | atomic_dec(&system_freezing_cnt); | |
196 | pm_freezing = false; | |
197 | pm_nosig_freezing = false; | |
198 | ||
6cd8dedc TH |
199 | oom_killer_enable(); |
200 | ||
35536ae1 | 201 | pr_info("Restarting tasks ... "); |
6cd8dedc | 202 | |
4320f6b1 | 203 | __usermodehelper_set_disable_depth(UMH_FREEZING); |
6cd8dedc TH |
204 | thaw_workqueues(); |
205 | ||
50e76632 PZ |
206 | cpuset_wait_for_hotplug(); |
207 | ||
1da177e4 | 208 | read_lock(&tasklist_lock); |
a28e785a | 209 | for_each_process_thread(g, p) { |
2b44c4db CC |
210 | /* No other threads should have PF_SUSPEND_TASK set */ |
211 | WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK)); | |
a5be2d0d | 212 | __thaw_task(p); |
a28e785a | 213 | } |
1da177e4 | 214 | read_unlock(&tasklist_lock); |
7f33d49a | 215 | |
2b44c4db CC |
216 | WARN_ON(!(curr->flags & PF_SUSPEND_TASK)); |
217 | curr->flags &= ~PF_SUSPEND_TASK; | |
218 | ||
1e73203c RW |
219 | usermodehelper_enable(); |
220 | ||
1da177e4 | 221 | schedule(); |
35536ae1 | 222 | pr_cont("done.\n"); |
bb3632c6 | 223 | trace_suspend_resume(TPS("thaw_processes"), 0, false); |
1da177e4 LT |
224 | } |
225 | ||
181e9bde RW |
226 | void thaw_kernel_threads(void) |
227 | { | |
228 | struct task_struct *g, *p; | |
229 | ||
230 | pm_nosig_freezing = false; | |
35536ae1 | 231 | pr_info("Restarting kernel threads ... "); |
181e9bde RW |
232 | |
233 | thaw_workqueues(); | |
234 | ||
235 | read_lock(&tasklist_lock); | |
a28e785a | 236 | for_each_process_thread(g, p) { |
181e9bde RW |
237 | if (p->flags & (PF_KTHREAD | PF_WQ_WORKER)) |
238 | __thaw_task(p); | |
a28e785a | 239 | } |
181e9bde RW |
240 | read_unlock(&tasklist_lock); |
241 | ||
242 | schedule(); | |
35536ae1 | 243 | pr_cont("done.\n"); |
181e9bde | 244 | } |