blob: 2fba066e125fa960b484b362d0fd6895f2e103a0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Timeout for stopping processes
24 */
Li Fei957d1282013-02-01 08:56:03 +000025unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Tejun Heo839e3402011-11-21 12:32:26 -080027static int try_to_freeze_tasks(bool user_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080030 unsigned long end_time;
31 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020032 bool wq_busy = false;
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050033 ktime_t start, end, elapsed;
Colin Cross18ad0c62013-05-06 23:50:10 +000034 unsigned int elapsed_msecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020035 bool wakeup = false;
Colin Cross18ad0c62013-05-06 23:50:10 +000036 int sleep_usecs = USEC_PER_MSEC;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070037
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050038 start = ktime_get_boottime();
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070039
Li Fei957d1282013-02-01 08:56:03 +000040 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020041
Tejun Heo839e3402011-11-21 12:32:26 -080042 if (!user_only)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020043 freeze_workqueues_begin();
44
Tejun Heobe404f02009-10-08 22:47:30 +020045 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080046 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020048 for_each_process_thread(g, p) {
Tejun Heo839e3402011-11-21 12:32:26 -080049 if (p == current || !freeze_task(p))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070050 continue;
51
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +020052 if (!freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070053 todo++;
Michal Hockoa28e7852014-10-21 09:27:15 +020054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020056
Tejun Heo839e3402011-11-21 12:32:26 -080057 if (!user_only) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020058 wq_busy = freeze_workqueues_busy();
59 todo += wq_busy;
60 }
61
Tejun Heobe404f02009-10-08 22:47:30 +020062 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080063 break;
Tejun Heobe404f02009-10-08 22:47:30 +020064
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010065 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020066 wakeup = true;
67 break;
68 }
69
Tejun Heobe404f02009-10-08 22:47:30 +020070 /*
71 * We need to retry, but first give the freezing tasks some
Colin Cross18ad0c62013-05-06 23:50:10 +000072 * time to enter the refrigerator. Start with an initial
73 * 1 ms sleep followed by exponential backoff until 8 ms.
Tejun Heobe404f02009-10-08 22:47:30 +020074 */
Colin Cross18ad0c62013-05-06 23:50:10 +000075 usleep_range(sleep_usecs / 2, sleep_usecs);
76 if (sleep_usecs < 8 * USEC_PER_MSEC)
77 sleep_usecs *= 2;
Tejun Heobe404f02009-10-08 22:47:30 +020078 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070079
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050080 end = ktime_get_boottime();
81 elapsed = ktime_sub(end, start);
82 elapsed_msecs = ktime_to_ms(elapsed);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070083
Pavel Machek6161b2c2005-09-03 15:57:05 -070084 if (todo) {
Michal Hocko35536ae2015-02-11 15:26:18 -080085 pr_cont("\n");
86 pr_err("Freezing of tasks %s after %d.%03d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020087 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020088 wakeup ? "aborted" : "failed",
Colin Cross18ad0c62013-05-06 23:50:10 +000089 elapsed_msecs / 1000, elapsed_msecs % 1000,
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020090 todo - wq_busy, wq_busy);
91
Roger Lu7b776af2016-07-01 11:05:02 +080092 if (wq_busy)
93 show_workqueue_state();
94
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010095 if (!wakeup) {
96 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020097 for_each_process_thread(g, p) {
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010098 if (p != current && !freezer_should_skip(p)
99 && freezing(p) && !frozen(p))
100 sched_show_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200101 }
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +0100102 read_unlock(&tasklist_lock);
103 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700104 } else {
Michal Hocko35536ae2015-02-11 15:26:18 -0800105 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
Colin Cross18ad0c62013-05-06 23:50:10 +0000106 elapsed_msecs % 1000);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700107 }
108
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700109 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800110}
111
112/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200113 * freeze_processes - Signal user space processes to enter the refrigerator.
Colin Cross2b44c4d2013-07-24 17:41:33 -0700114 * The current thread will not be frozen. The same process that calls
115 * freeze_processes must later call thaw_processes.
Tejun Heo03afed82011-11-21 12:32:24 -0800116 *
117 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800118 */
119int freeze_processes(void)
120{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700121 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800122
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200123 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200124 if (error)
125 return error;
126
Colin Cross2b44c4d2013-07-24 17:41:33 -0700127 /* Make sure this task doesn't get frozen */
128 current->flags |= PF_SUSPEND_TASK;
129
Tejun Heoa3201222011-11-21 12:32:25 -0800130 if (!pm_freezing)
131 atomic_inc(&system_freezing_cnt);
132
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200133 pm_wakeup_clear();
Michal Hocko35536ae2015-02-11 15:26:18 -0800134 pr_info("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800135 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200136 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200137 if (!error) {
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200138 __usermodehelper_set_disable_depth(UMH_DISABLED);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800139 pr_cont("done.");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200140 }
Michal Hocko35536ae2015-02-11 15:26:18 -0800141 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200142 BUG_ON(in_atomic());
143
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800144 /*
145 * Now that the whole userspace is frozen we need to disbale
146 * the OOM killer to disallow any further interference with
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700147 * killable tasks. There is no guarantee oom victims will
148 * ever reach a point they go away we have to wait with a timeout.
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800149 */
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700150 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800151 error = -EBUSY;
152
Tejun Heo03afed82011-11-21 12:32:24 -0800153 if (error)
154 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200155 return error;
156}
157
158/**
159 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800160 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100161 * On success, returns 0. On failure, -errno and only the kernel threads are
162 * thawed, so as to give a chance to the caller to do additional cleanups
163 * (if any) before thawing the userspace tasks. So, it is the responsibility
164 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200165 */
166int freeze_kernel_threads(void)
167{
168 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800169
Michal Hocko35536ae2015-02-11 15:26:18 -0800170 pr_info("Freezing remaining freezable tasks ... ");
171
Tejun Heoa3201222011-11-21 12:32:25 -0800172 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200173 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200174 if (!error)
Michal Hocko35536ae2015-02-11 15:26:18 -0800175 pr_cont("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700176
Michal Hocko35536ae2015-02-11 15:26:18 -0800177 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200178 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700179
Tejun Heo03afed82011-11-21 12:32:24 -0800180 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100181 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700182 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800185void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct task_struct *g, *p;
Colin Cross2b44c4d2013-07-24 17:41:33 -0700188 struct task_struct *curr = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700190 trace_suspend_resume(TPS("thaw_processes"), 0, true);
Tejun Heoa3201222011-11-21 12:32:25 -0800191 if (pm_freezing)
192 atomic_dec(&system_freezing_cnt);
193 pm_freezing = false;
194 pm_nosig_freezing = false;
195
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800196 oom_killer_enable();
197
Michal Hocko35536ae2015-02-11 15:26:18 -0800198 pr_info("Restarting tasks ... ");
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800199
Takashi Iwai4320f6b2014-07-15 08:51:27 +0200200 __usermodehelper_set_disable_depth(UMH_FREEZING);
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800201 thaw_workqueues();
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200204 for_each_process_thread(g, p) {
Colin Cross2b44c4d2013-07-24 17:41:33 -0700205 /* No other threads should have PF_SUSPEND_TASK set */
206 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
Tejun Heoa5be2d02011-11-21 12:32:23 -0800207 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800210
Colin Cross2b44c4d2013-07-24 17:41:33 -0700211 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
212 curr->flags &= ~PF_SUSPEND_TASK;
213
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200214 usermodehelper_enable();
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800217 pr_cont("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700218 trace_suspend_resume(TPS("thaw_processes"), 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100221void thaw_kernel_threads(void)
222{
223 struct task_struct *g, *p;
224
225 pm_nosig_freezing = false;
Michal Hocko35536ae2015-02-11 15:26:18 -0800226 pr_info("Restarting kernel threads ... ");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100227
228 thaw_workqueues();
229
230 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200231 for_each_process_thread(g, p) {
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100232 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
233 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200234 }
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100235 read_unlock(&tasklist_lock);
236
237 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800238 pr_cont("done.\n");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100239}