blob: 06ec8869dbf1629f2c7cabc63f2edc96426ea87d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/*
22 * Timeout for stopping processes
23 */
Li Fei957d1282013-02-01 08:56:03 +000024unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Tejun Heo839e3402011-11-21 12:32:26 -080026static int try_to_freeze_tasks(bool user_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080029 unsigned long end_time;
30 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020031 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070032 struct timeval start, end;
Colin Cross18ad0c62013-05-06 23:50:10 +000033 u64 elapsed_msecs64;
34 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
38 do_gettimeofday(&start);
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);
48 do_each_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++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 } while_each_thread(g, p);
55 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
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070080 do_gettimeofday(&end);
Colin Cross18ad0c62013-05-06 23:50:10 +000081 elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
82 do_div(elapsed_msecs64, NSEC_PER_MSEC);
83 elapsed_msecs = elapsed_msecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070084
Pavel Machek6161b2c2005-09-03 15:57:05 -070085 if (todo) {
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080086 printk("\n");
Colin Cross18ad0c62013-05-06 23:50:10 +000087 printk(KERN_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
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010093 if (!wakeup) {
94 read_lock(&tasklist_lock);
95 do_each_thread(g, p) {
96 if (p != current && !freezer_should_skip(p)
97 && freezing(p) && !frozen(p))
98 sched_show_task(p);
99 } while_each_thread(g, p);
100 read_unlock(&tasklist_lock);
101 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700102 } else {
Colin Cross18ad0c62013-05-06 23:50:10 +0000103 printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
104 elapsed_msecs % 1000);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700105 }
106
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700107 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800108}
109
110/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200111 * freeze_processes - Signal user space processes to enter the refrigerator.
Colin Cross2b44c4d2013-07-24 17:41:33 -0700112 * The current thread will not be frozen. The same process that calls
113 * freeze_processes must later call thaw_processes.
Tejun Heo03afed82011-11-21 12:32:24 -0800114 *
115 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800116 */
117int freeze_processes(void)
118{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700119 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800120
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200121 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200122 if (error)
123 return error;
124
Colin Cross2b44c4d2013-07-24 17:41:33 -0700125 /* Make sure this task doesn't get frozen */
126 current->flags |= PF_SUSPEND_TASK;
127
Tejun Heoa3201222011-11-21 12:32:25 -0800128 if (!pm_freezing)
129 atomic_inc(&system_freezing_cnt);
130
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700131 printk("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800132 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200133 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200134 if (!error) {
135 printk("done.");
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200136 __usermodehelper_set_disable_depth(UMH_DISABLED);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200137 oom_killer_disable();
138 }
139 printk("\n");
140 BUG_ON(in_atomic());
141
Tejun Heo03afed82011-11-21 12:32:24 -0800142 if (error)
143 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200144 return error;
145}
146
147/**
148 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800149 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100150 * On success, returns 0. On failure, -errno and only the kernel threads are
151 * thawed, so as to give a chance to the caller to do additional cleanups
152 * (if any) before thawing the userspace tasks. So, it is the responsibility
153 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200154 */
155int freeze_kernel_threads(void)
156{
157 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800158
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700159 printk("Freezing remaining freezable tasks ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800160 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200161 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200162 if (!error)
163 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700164
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700165 printk("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200166 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700167
Tejun Heo03afed82011-11-21 12:32:24 -0800168 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100169 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700170 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800173void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct task_struct *g, *p;
Colin Cross2b44c4d2013-07-24 17:41:33 -0700176 struct task_struct *curr = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Tejun Heoa3201222011-11-21 12:32:25 -0800178 if (pm_freezing)
179 atomic_dec(&system_freezing_cnt);
180 pm_freezing = false;
181 pm_nosig_freezing = false;
182
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800183 oom_killer_enable();
184
185 printk("Restarting tasks ... ");
186
187 thaw_workqueues();
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800190 do_each_thread(g, p) {
Colin Cross2b44c4d2013-07-24 17:41:33 -0700191 /* No other threads should have PF_SUSPEND_TASK set */
192 WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
Tejun Heoa5be2d02011-11-21 12:32:23 -0800193 __thaw_task(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800194 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800196
Colin Cross2b44c4d2013-07-24 17:41:33 -0700197 WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
198 curr->flags &= ~PF_SUSPEND_TASK;
199
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200200 usermodehelper_enable();
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800203 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100206void thaw_kernel_threads(void)
207{
208 struct task_struct *g, *p;
209
210 pm_nosig_freezing = false;
211 printk("Restarting kernel threads ... ");
212
213 thaw_workqueues();
214
215 read_lock(&tasklist_lock);
216 do_each_thread(g, p) {
217 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
218 __thaw_task(p);
219 } while_each_thread(g, p);
220 read_unlock(&tasklist_lock);
221
222 schedule();
223 printk("done.\n");
224}