blob: 6c8c925d2793e8a8ca458a4f203033aac700d420 [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>
Arve Hjønnevåg677f1042008-10-14 16:02:39 -070019#include <linux/wakelock.h>
Bryan Huntsmand074fa22011-11-16 13:52:50 -080020#include "power.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Timeout for stopping processes
24 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080025#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Tejun Heo58a69cb2011-02-16 09:25:31 +010027static inline int freezable(struct task_struct * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Oleg Nesterov1065d132007-05-08 00:24:01 -070029 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070031 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return 0;
33 return 1;
34}
35
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020036static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080039 unsigned long end_time;
40 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020041 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070042 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070043 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070044 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020045 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070046
47 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070048
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080049 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020050
51 if (!sig_only)
52 freeze_workqueues_begin();
53
Tejun Heobe404f02009-10-08 22:47:30 +020054 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080055 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 read_lock(&tasklist_lock);
57 do_each_thread(g, p) {
Tejun Heo58a69cb2011-02-16 09:25:31 +010058 if (frozen(p) || !freezable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080060
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020061 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070062 continue;
63
Roland McGrath13b1c3d2008-03-03 20:22:05 -080064 /*
65 * Now that we've done set_freeze_flag, don't
66 * perturb a task in TASK_STOPPED or TASK_TRACED.
67 * It is "frozen enough". If the task does wake
68 * up, it will immediately call try_to_freeze.
Tejun Heo8cfe4002010-11-26 23:07:27 +010069 *
70 * Because freeze_task() goes through p's
71 * scheduler lock after setting TIF_FREEZE, it's
72 * guaranteed that either we see TASK_RUNNING or
73 * try_to_stop() after schedule() in ptrace/signal
74 * stop sees TIF_FREEZE.
Roland McGrath13b1c3d2008-03-03 20:22:05 -080075 */
76 if (!task_is_stopped_or_traced(p) &&
77 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070078 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 } while_each_thread(g, p);
80 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020081
82 if (!sig_only) {
83 wq_busy = freeze_workqueues_busy();
84 todo += wq_busy;
85 }
86
Arve Hjønnevåg677f1042008-10-14 16:02:39 -070087 if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
88 wakeup = 1;
89 break;
90 }
Tejun Heobe404f02009-10-08 22:47:30 +020091 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080092 break;
Tejun Heobe404f02009-10-08 22:47:30 +020093
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010094 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020095 wakeup = true;
96 break;
97 }
98
Tejun Heobe404f02009-10-08 22:47:30 +020099 /*
100 * We need to retry, but first give the freezing tasks some
101 * time to enter the regrigerator.
102 */
103 msleep(10);
104 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700105
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700106 do_gettimeofday(&end);
107 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
108 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
109 elapsed_csecs = elapsed_csecs64;
110
Pavel Machek6161b2c2005-09-03 15:57:05 -0700111 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800112 /* This does not unfreeze processes that are already frozen
113 * (we have slightly ugly calling convention in that respect,
114 * and caller must call thaw_processes() if something fails),
115 * but it cleans up leftover PF_FREEZE requests.
116 */
Arve Hjønnevåg677f1042008-10-14 16:02:39 -0700117 if(wakeup) {
118 printk("\n");
119 printk(KERN_ERR "Freezing of %s aborted\n",
120 sig_only ? "user space " : "tasks ");
121 }
122 else {
123 printk("\n");
124 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
125 "(%d tasks refusing to freeze, wq_busy=%d):\n",
126 elapsed_csecs / 100, elapsed_csecs % 100,
127 todo - wq_busy, wq_busy);
128 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200129 thaw_workqueues();
130
Pavel Machek6161b2c2005-09-03 15:57:05 -0700131 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800132 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700133 task_lock(p);
Arve Hjønnevåg01e13542009-12-01 20:54:37 -0800134 if (freezing(p) && !freezer_should_skip(p) &&
135 elapsed_csecs > 100)
Xiaotian Feng4f598452010-03-10 22:59:13 +0100136 sched_show_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700137 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700138 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800139 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700140 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700141 } else {
142 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
143 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700144 }
145
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700146 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800147}
148
149/**
150 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800151 */
152int freeze_processes(void)
153{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700154 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800155
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700156 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200157 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700158 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700159 goto Exit;
160 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800161
Bryan Huntsmand074fa22011-11-16 13:52:50 -0800162 error = suspend_sys_sync_wait();
163 if (error)
164 goto Exit;
165
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700166 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200167 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700168 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700169 goto Exit;
170 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700171
172 oom_killer_disable();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700173 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700175 printk("\n");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700176
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700177 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200180static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct task_struct *g, *p;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800185 do_each_thread(g, p) {
Tejun Heo58a69cb2011-02-16 09:25:31 +0100186 if (!freezable(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800187 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800188
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200189 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800190 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Matt Helsley5a7aadf2010-03-26 23:51:44 +0100192 if (cgroup_freezing_or_frozen(p))
Matt Helsley5a069152008-10-18 20:27:22 -0700193 continue;
194
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700195 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800196 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800198}
199
200void thaw_processes(void)
201{
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700202 oom_killer_enable();
203
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800204 printk("Restarting tasks ... ");
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200205 thaw_workqueues();
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200206 thaw_tasks(true);
207 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800209 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211