blob: 87da817f9e132204add38dc73685962081de9dd8 [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 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080024#define TIMEOUT (20 * HZ)
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;
David Howellsf0af5662008-07-23 21:28:44 -070033 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070034 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020035 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070036
37 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070038
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080039 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020040
Tejun Heo839e3402011-11-21 12:32:26 -080041 if (!user_only)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020042 freeze_workqueues_begin();
43
Tejun Heobe404f02009-10-08 22:47:30 +020044 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080045 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 read_lock(&tasklist_lock);
47 do_each_thread(g, p) {
Tejun Heo839e3402011-11-21 12:32:26 -080048 if (p == current || !freeze_task(p))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070049 continue;
50
Roland McGrath13b1c3d2008-03-03 20:22:05 -080051 /*
52 * Now that we've done set_freeze_flag, don't
53 * perturb a task in TASK_STOPPED or TASK_TRACED.
54 * It is "frozen enough". If the task does wake
55 * up, it will immediately call try_to_freeze.
Tejun Heo8cfe4002010-11-26 23:07:27 +010056 *
Marcos Paulo de Souza37f08be2012-02-21 23:57:47 +010057 * Because freeze_task() goes through p's scheduler lock, it's
58 * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING
59 * transition can't race with task state testing here.
Roland McGrath13b1c3d2008-03-03 20:22:05 -080060 */
61 if (!task_is_stopped_or_traced(p) &&
62 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070063 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 } while_each_thread(g, p);
65 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020066
Tejun Heo839e3402011-11-21 12:32:26 -080067 if (!user_only) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020068 wq_busy = freeze_workqueues_busy();
69 todo += wq_busy;
70 }
71
Tejun Heobe404f02009-10-08 22:47:30 +020072 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080073 break;
Tejun Heobe404f02009-10-08 22:47:30 +020074
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010075 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020076 wakeup = true;
77 break;
78 }
79
Tejun Heobe404f02009-10-08 22:47:30 +020080 /*
81 * We need to retry, but first give the freezing tasks some
Sedat Dilek5834ec32012-08-23 02:47:13 +020082 * time to enter the refrigerator.
Tejun Heobe404f02009-10-08 22:47:30 +020083 */
84 msleep(10);
85 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070086
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070087 do_gettimeofday(&end);
88 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
89 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
90 elapsed_csecs = elapsed_csecs64;
91
Pavel Machek6161b2c2005-09-03 15:57:05 -070092 if (todo) {
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080093 printk("\n");
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020094 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020095 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020096 wakeup ? "aborted" : "failed",
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020097 elapsed_csecs / 100, elapsed_csecs % 100,
98 todo - wq_busy, wq_busy);
99
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +0100100 if (!wakeup) {
101 read_lock(&tasklist_lock);
102 do_each_thread(g, p) {
103 if (p != current && !freezer_should_skip(p)
104 && freezing(p) && !frozen(p))
105 sched_show_task(p);
106 } while_each_thread(g, p);
107 read_unlock(&tasklist_lock);
108 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700109 } else {
110 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
111 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700112 }
113
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700114 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800115}
116
117/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200118 * freeze_processes - Signal user space processes to enter the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800119 *
120 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800121 */
122int freeze_processes(void)
123{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700124 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800125
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200126 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200127 if (error)
128 return error;
129
Tejun Heoa3201222011-11-21 12:32:25 -0800130 if (!pm_freezing)
131 atomic_inc(&system_freezing_cnt);
132
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700133 printk("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800134 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200135 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200136 if (!error) {
137 printk("done.");
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200138 __usermodehelper_set_disable_depth(UMH_DISABLED);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200139 oom_killer_disable();
140 }
141 printk("\n");
142 BUG_ON(in_atomic());
143
Tejun Heo03afed82011-11-21 12:32:24 -0800144 if (error)
145 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200146 return error;
147}
148
149/**
150 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800151 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100152 * On success, returns 0. On failure, -errno and only the kernel threads are
153 * thawed, so as to give a chance to the caller to do additional cleanups
154 * (if any) before thawing the userspace tasks. So, it is the responsibility
155 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200156 */
157int freeze_kernel_threads(void)
158{
159 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800160
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700161 printk("Freezing remaining freezable tasks ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800162 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200163 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200164 if (!error)
165 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700166
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700167 printk("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200168 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700169
Tejun Heo03afed82011-11-21 12:32:24 -0800170 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100171 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700172 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800175void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct task_struct *g, *p;
178
Tejun Heoa3201222011-11-21 12:32:25 -0800179 if (pm_freezing)
180 atomic_dec(&system_freezing_cnt);
181 pm_freezing = false;
182 pm_nosig_freezing = false;
183
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800184 oom_killer_enable();
185
186 printk("Restarting tasks ... ");
187
188 thaw_workqueues();
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800191 do_each_thread(g, p) {
Tejun Heoa5be2d02011-11-21 12:32:23 -0800192 __thaw_task(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800193 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800195
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200196 usermodehelper_enable();
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800199 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100202void thaw_kernel_threads(void)
203{
204 struct task_struct *g, *p;
205
206 pm_nosig_freezing = false;
207 printk("Restarting kernel threads ... ");
208
209 thaw_workqueues();
210
211 read_lock(&tasklist_lock);
212 do_each_thread(g, p) {
213 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
214 __thaw_task(p);
215 } while_each_thread(g, p);
216 read_unlock(&tasklist_lock);
217
218 schedule();
219 printk("done.\n");
220}