blob: 8ea24ded1dab1bd2d9ec3fcbd7cd848a685b3a53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/interrupt.h>
Alexey Dobriyan1a8670a2009-09-21 17:03:09 -070012#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/suspend.h>
14#include <linux/module.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080015#include <linux/syscalls.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080016#include <linux/freezer.h>
Tejun Heobe404f02009-10-08 22:47:30 +020017#include <linux/delay.h>
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020018#include <linux/workqueue.h>
Rafael J. Wysocki1e732032012-03-28 23:30:21 +020019#include <linux/kmod.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070020#include <trace/events/power.h>
Peter Zijlstraba155182017-09-07 11:13:38 +020021#include <linux/cpuset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Peter Zijlstraba155182017-09-07 11:13:38 +020023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Timeout for stopping processes
25 */
Li Fei957d1282013-02-01 08:56:03 +000026unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Tejun Heo839e3402011-11-21 12:32:26 -080028static int try_to_freeze_tasks(bool user_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080031 unsigned long end_time;
32 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020033 bool wq_busy = false;
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050034 ktime_t start, end, elapsed;
Colin Cross18ad0c62013-05-06 23:50:10 +000035 unsigned int elapsed_msecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020036 bool wakeup = false;
Colin Cross18ad0c62013-05-06 23:50:10 +000037 int sleep_usecs = USEC_PER_MSEC;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070038
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050039 start = ktime_get_boottime();
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070040
Li Fei957d1282013-02-01 08:56:03 +000041 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020042
Tejun Heo839e3402011-11-21 12:32:26 -080043 if (!user_only)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020044 freeze_workqueues_begin();
45
Tejun Heobe404f02009-10-08 22:47:30 +020046 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080047 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020049 for_each_process_thread(g, p) {
Tejun Heo839e3402011-11-21 12:32:26 -080050 if (p == current || !freeze_task(p))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070051 continue;
52
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +020053 if (!freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070054 todo++;
Michal Hockoa28e7852014-10-21 09:27:15 +020055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020057
Tejun Heo839e3402011-11-21 12:32:26 -080058 if (!user_only) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020059 wq_busy = freeze_workqueues_busy();
60 todo += wq_busy;
61 }
62
Tejun Heobe404f02009-10-08 22:47:30 +020063 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080064 break;
Tejun Heobe404f02009-10-08 22:47:30 +020065
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010066 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020067 wakeup = true;
68 break;
69 }
70
Tejun Heobe404f02009-10-08 22:47:30 +020071 /*
72 * We need to retry, but first give the freezing tasks some
Colin Cross18ad0c62013-05-06 23:50:10 +000073 * time to enter the refrigerator. Start with an initial
74 * 1 ms sleep followed by exponential backoff until 8 ms.
Tejun Heobe404f02009-10-08 22:47:30 +020075 */
Colin Cross18ad0c62013-05-06 23:50:10 +000076 usleep_range(sleep_usecs / 2, sleep_usecs);
77 if (sleep_usecs < 8 * USEC_PER_MSEC)
78 sleep_usecs *= 2;
Tejun Heobe404f02009-10-08 22:47:30 +020079 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070080
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050081 end = ktime_get_boottime();
82 elapsed = ktime_sub(end, start);
83 elapsed_msecs = ktime_to_ms(elapsed);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070084
Pavel Machek6161b2c2005-09-03 15:57:05 -070085 if (todo) {
Michal Hocko35536ae2015-02-11 15:26:18 -080086 pr_cont("\n");
87 pr_err("Freezing of tasks %s after %d.%03d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020088 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020089 wakeup ? "aborted" : "failed",
Colin Cross18ad0c62013-05-06 23:50:10 +000090 elapsed_msecs / 1000, elapsed_msecs % 1000,
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020091 todo - wq_busy, wq_busy);
92
Roger Lu7b776af2016-07-01 11:05:02 +080093 if (wq_busy)
94 show_workqueue_state();
95
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010096 if (!wakeup) {
97 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020098 for_each_process_thread(g, p) {
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010099 if (p != current && !freezer_should_skip(p)
100 && freezing(p) && !frozen(p))
101 sched_show_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200102 }
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +0100103 read_unlock(&tasklist_lock);
104 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700105 } else {
Michal Hocko35536ae2015-02-11 15:26:18 -0800106 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
Colin Cross18ad0c62013-05-06 23:50:10 +0000107 elapsed_msecs % 1000);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700108 }
109
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700110 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800111}
112
113/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200114 * freeze_processes - Signal user space processes to enter the refrigerator.
Colin Cross2b44c4d2013-07-24 17:41:33 -0700115 * The current thread will not be frozen. The same process that calls
116 * freeze_processes must later call thaw_processes.
Tejun Heo03afed82011-11-21 12:32:24 -0800117 *
118 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800119 */
120int freeze_processes(void)
121{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700122 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800123
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200124 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200125 if (error)
126 return error;
127
Colin Cross2b44c4d2013-07-24 17:41:33 -0700128 /* Make sure this task doesn't get frozen */
129 current->flags |= PF_SUSPEND_TASK;
130
Tejun Heoa3201222011-11-21 12:32:25 -0800131 if (!pm_freezing)
132 atomic_inc(&system_freezing_cnt);
133
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200134 pm_wakeup_clear();
Michal Hocko35536ae2015-02-11 15:26:18 -0800135 pr_info("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800136 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200137 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200138 if (!error) {
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200139 __usermodehelper_set_disable_depth(UMH_DISABLED);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800140 pr_cont("done.");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200141 }
Michal Hocko35536ae2015-02-11 15:26:18 -0800142 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200143 BUG_ON(in_atomic());
144
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800145 /*
146 * Now that the whole userspace is frozen we need to disbale
147 * the OOM killer to disallow any further interference with
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700148 * killable tasks. There is no guarantee oom victims will
149 * ever reach a point they go away we have to wait with a timeout.
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800150 */
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700151 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800152 error = -EBUSY;
153
Tejun Heo03afed82011-11-21 12:32:24 -0800154 if (error)
155 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200156 return error;
157}
158
159/**
160 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800161 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100162 * On success, returns 0. On failure, -errno and only the kernel threads are
163 * thawed, so as to give a chance to the caller to do additional cleanups
164 * (if any) before thawing the userspace tasks. So, it is the responsibility
165 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200166 */
167int freeze_kernel_threads(void)
168{
169 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800170
Michal Hocko35536ae2015-02-11 15:26:18 -0800171 pr_info("Freezing remaining freezable tasks ... ");
172
Tejun Heoa3201222011-11-21 12:32:25 -0800173 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200174 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200175 if (!error)
Michal Hocko35536ae2015-02-11 15:26:18 -0800176 pr_cont("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700177
Michal Hocko35536ae2015-02-11 15:26:18 -0800178 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200179 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700180
Tejun Heo03afed82011-11-21 12:32:24 -0800181 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100182 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700183 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800186void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct task_struct *g, *p;
Colin Cross2b44c4d2013-07-24 17:41:33 -0700189 struct task_struct *curr = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700191 trace_suspend_resume(TPS("thaw_processes"), 0, true);
Tejun Heoa3201222011-11-21 12:32:25 -0800192 if (pm_freezing)
193 atomic_dec(&system_freezing_cnt);
194 pm_freezing = false;
195 pm_nosig_freezing = false;
196
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800197 oom_killer_enable();
198
Michal Hocko35536ae2015-02-11 15:26:18 -0800199 pr_info("Restarting tasks ... ");
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800200
Takashi Iwai4320f6b2014-07-15 08:51:27 +0200201 __usermodehelper_set_disable_depth(UMH_FREEZING);
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800202 thaw_workqueues();
203
Peter Zijlstraba155182017-09-07 11:13:38 +0200204 cpuset_wait_for_hotplug();
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200207 for_each_process_thread(g, p) {
Colin Cross2b44c4d2013-07-24 17:41:33 -0700208 /* No other threads should have PF_SUSPEND_TASK set */
209 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
Tejun Heoa5be2d02011-11-21 12:32:23 -0800210 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800213
Colin Cross2b44c4d2013-07-24 17:41:33 -0700214 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
215 curr->flags &= ~PF_SUSPEND_TASK;
216
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200217 usermodehelper_enable();
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800220 pr_cont("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700221 trace_suspend_resume(TPS("thaw_processes"), 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100224void thaw_kernel_threads(void)
225{
226 struct task_struct *g, *p;
227
228 pm_nosig_freezing = false;
Michal Hocko35536ae2015-02-11 15:26:18 -0800229 pr_info("Restarting kernel threads ... ");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100230
231 thaw_workqueues();
232
233 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200234 for_each_process_thread(g, p) {
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100235 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
236 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200237 }
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100238 read_unlock(&tasklist_lock);
239
240 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800241 pr_cont("done.\n");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100242}