blob: c7209f060eeb7c8672cf8f07ba9c83ac6c9460ac [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010015#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010016#include <linux/sched/task.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080017#include <linux/syscalls.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080018#include <linux/freezer.h>
Tejun Heobe404f02009-10-08 22:47:30 +020019#include <linux/delay.h>
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020020#include <linux/workqueue.h>
Rafael J. Wysocki1e732032012-03-28 23:30:21 +020021#include <linux/kmod.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070022#include <trace/events/power.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Timeout for stopping processes
26 */
Li Fei957d1282013-02-01 08:56:03 +000027unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tejun Heo839e3402011-11-21 12:32:26 -080029static int try_to_freeze_tasks(bool user_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080032 unsigned long end_time;
33 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020034 bool wq_busy = false;
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050035 ktime_t start, end, elapsed;
Colin Cross18ad0c62013-05-06 23:50:10 +000036 unsigned int elapsed_msecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020037 bool wakeup = false;
Colin Cross18ad0c62013-05-06 23:50:10 +000038 int sleep_usecs = USEC_PER_MSEC;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070039
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050040 start = ktime_get_boottime();
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070041
Li Fei957d1282013-02-01 08:56:03 +000042 end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020043
Tejun Heo839e3402011-11-21 12:32:26 -080044 if (!user_only)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020045 freeze_workqueues_begin();
46
Tejun Heobe404f02009-10-08 22:47:30 +020047 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080048 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020050 for_each_process_thread(g, p) {
Tejun Heo839e3402011-11-21 12:32:26 -080051 if (p == current || !freeze_task(p))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070052 continue;
53
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +020054 if (!freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070055 todo++;
Michal Hockoa28e7852014-10-21 09:27:15 +020056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020058
Tejun Heo839e3402011-11-21 12:32:26 -080059 if (!user_only) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020060 wq_busy = freeze_workqueues_busy();
61 todo += wq_busy;
62 }
63
Tejun Heobe404f02009-10-08 22:47:30 +020064 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080065 break;
Tejun Heobe404f02009-10-08 22:47:30 +020066
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010067 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020068 wakeup = true;
69 break;
70 }
71
Tejun Heobe404f02009-10-08 22:47:30 +020072 /*
73 * We need to retry, but first give the freezing tasks some
Colin Cross18ad0c62013-05-06 23:50:10 +000074 * time to enter the refrigerator. Start with an initial
75 * 1 ms sleep followed by exponential backoff until 8 ms.
Tejun Heobe404f02009-10-08 22:47:30 +020076 */
Colin Cross18ad0c62013-05-06 23:50:10 +000077 usleep_range(sleep_usecs / 2, sleep_usecs);
78 if (sleep_usecs < 8 * USEC_PER_MSEC)
79 sleep_usecs *= 2;
Tejun Heobe404f02009-10-08 22:47:30 +020080 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070081
Abhilash Jindalf7b382b2016-01-31 14:29:01 -050082 end = ktime_get_boottime();
83 elapsed = ktime_sub(end, start);
84 elapsed_msecs = ktime_to_ms(elapsed);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070085
Pavel Machek6161b2c2005-09-03 15:57:05 -070086 if (todo) {
Michal Hocko35536ae2015-02-11 15:26:18 -080087 pr_cont("\n");
88 pr_err("Freezing of tasks %s after %d.%03d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020089 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020090 wakeup ? "aborted" : "failed",
Colin Cross18ad0c62013-05-06 23:50:10 +000091 elapsed_msecs / 1000, elapsed_msecs % 1000,
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020092 todo - wq_busy, wq_busy);
93
Roger Lu7b776af2016-07-01 11:05:02 +080094 if (wq_busy)
95 show_workqueue_state();
96
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010097 if (!wakeup) {
98 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +020099 for_each_process_thread(g, p) {
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +0100100 if (p != current && !freezer_should_skip(p)
101 && freezing(p) && !frozen(p))
102 sched_show_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200103 }
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +0100104 read_unlock(&tasklist_lock);
105 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700106 } else {
Michal Hocko35536ae2015-02-11 15:26:18 -0800107 pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
Colin Cross18ad0c62013-05-06 23:50:10 +0000108 elapsed_msecs % 1000);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700109 }
110
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700111 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800112}
113
114/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200115 * freeze_processes - Signal user space processes to enter the refrigerator.
Colin Cross2b44c4d2013-07-24 17:41:33 -0700116 * The current thread will not be frozen. The same process that calls
117 * freeze_processes must later call thaw_processes.
Tejun Heo03afed82011-11-21 12:32:24 -0800118 *
119 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800120 */
121int freeze_processes(void)
122{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700123 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800124
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200125 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200126 if (error)
127 return error;
128
Colin Cross2b44c4d2013-07-24 17:41:33 -0700129 /* Make sure this task doesn't get frozen */
130 current->flags |= PF_SUSPEND_TASK;
131
Tejun Heoa3201222011-11-21 12:32:25 -0800132 if (!pm_freezing)
133 atomic_inc(&system_freezing_cnt);
134
Rafael J. Wysocki068765b2014-09-01 13:47:49 +0200135 pm_wakeup_clear();
Michal Hocko35536ae2015-02-11 15:26:18 -0800136 pr_info("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800137 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200138 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200139 if (!error) {
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200140 __usermodehelper_set_disable_depth(UMH_DISABLED);
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800141 pr_cont("done.");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200142 }
Michal Hocko35536ae2015-02-11 15:26:18 -0800143 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200144 BUG_ON(in_atomic());
145
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800146 /*
147 * Now that the whole userspace is frozen we need to disbale
148 * the OOM killer to disallow any further interference with
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700149 * killable tasks. There is no guarantee oom victims will
150 * ever reach a point they go away we have to wait with a timeout.
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800151 */
Michal Hocko7d2e7a22016-10-07 16:59:00 -0700152 if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
Michal Hockoc32b3cb2015-02-11 15:26:24 -0800153 error = -EBUSY;
154
Tejun Heo03afed82011-11-21 12:32:24 -0800155 if (error)
156 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200157 return error;
158}
159
160/**
161 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800162 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100163 * On success, returns 0. On failure, -errno and only the kernel threads are
164 * thawed, so as to give a chance to the caller to do additional cleanups
165 * (if any) before thawing the userspace tasks. So, it is the responsibility
166 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200167 */
168int freeze_kernel_threads(void)
169{
170 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800171
Michal Hocko35536ae2015-02-11 15:26:18 -0800172 pr_info("Freezing remaining freezable tasks ... ");
173
Tejun Heoa3201222011-11-21 12:32:25 -0800174 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200175 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200176 if (!error)
Michal Hocko35536ae2015-02-11 15:26:18 -0800177 pr_cont("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700178
Michal Hocko35536ae2015-02-11 15:26:18 -0800179 pr_cont("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200180 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700181
Tejun Heo03afed82011-11-21 12:32:24 -0800182 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100183 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700184 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800187void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct task_struct *g, *p;
Colin Cross2b44c4d2013-07-24 17:41:33 -0700190 struct task_struct *curr = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700192 trace_suspend_resume(TPS("thaw_processes"), 0, true);
Tejun Heoa3201222011-11-21 12:32:25 -0800193 if (pm_freezing)
194 atomic_dec(&system_freezing_cnt);
195 pm_freezing = false;
196 pm_nosig_freezing = false;
197
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800198 oom_killer_enable();
199
Michal Hocko35536ae2015-02-11 15:26:18 -0800200 pr_info("Restarting tasks ... ");
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800201
Takashi Iwai4320f6b2014-07-15 08:51:27 +0200202 __usermodehelper_set_disable_depth(UMH_FREEZING);
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800203 thaw_workqueues();
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200206 for_each_process_thread(g, p) {
Colin Cross2b44c4d2013-07-24 17:41:33 -0700207 /* No other threads should have PF_SUSPEND_TASK set */
208 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
Tejun Heoa5be2d02011-11-21 12:32:23 -0800209 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800212
Colin Cross2b44c4d2013-07-24 17:41:33 -0700213 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
214 curr->flags &= ~PF_SUSPEND_TASK;
215
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200216 usermodehelper_enable();
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800219 pr_cont("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700220 trace_suspend_resume(TPS("thaw_processes"), 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100223void thaw_kernel_threads(void)
224{
225 struct task_struct *g, *p;
226
227 pm_nosig_freezing = false;
Michal Hocko35536ae2015-02-11 15:26:18 -0800228 pr_info("Restarting kernel threads ... ");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100229
230 thaw_workqueues();
231
232 read_lock(&tasklist_lock);
Michal Hockoa28e7852014-10-21 09:27:15 +0200233 for_each_process_thread(g, p) {
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100234 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
235 __thaw_task(p);
Michal Hockoa28e7852014-10-21 09:27:15 +0200236 }
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100237 read_unlock(&tasklist_lock);
238
239 schedule();
Michal Hocko35536ae2015-02-11 15:26:18 -0800240 pr_cont("done.\n");
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100241}