blob: d6d2a10320e03ef0795108fd147d161616f1bab5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * Timeout for stopping processes
22 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080023#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static inline int freezeable(struct task_struct * p)
26{
Oleg Nesterov1065d132007-05-08 00:24:01 -070027 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070029 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31 return 1;
32}
33
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020034static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080037 unsigned long end_time;
38 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020039 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070040 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070041 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070042 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020043 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070044
45 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070046
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080047 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020048
49 if (!sig_only)
50 freeze_workqueues_begin();
51
Tejun Heobe404f02009-10-08 22:47:30 +020052 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080053 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 read_lock(&tasklist_lock);
55 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070056 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080058
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020059 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070060 continue;
61
Roland McGrath13b1c3d2008-03-03 20:22:05 -080062 /*
63 * Now that we've done set_freeze_flag, don't
64 * perturb a task in TASK_STOPPED or TASK_TRACED.
65 * It is "frozen enough". If the task does wake
66 * up, it will immediately call try_to_freeze.
Tejun Heo8cfe4002010-11-26 23:07:27 +010067 *
68 * Because freeze_task() goes through p's
69 * scheduler lock after setting TIF_FREEZE, it's
70 * guaranteed that either we see TASK_RUNNING or
71 * try_to_stop() after schedule() in ptrace/signal
72 * stop sees TIF_FREEZE.
Roland McGrath13b1c3d2008-03-03 20:22:05 -080073 */
74 if (!task_is_stopped_or_traced(p) &&
75 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070076 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 } while_each_thread(g, p);
78 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020079
80 if (!sig_only) {
81 wq_busy = freeze_workqueues_busy();
82 todo += wq_busy;
83 }
84
Tejun Heobe404f02009-10-08 22:47:30 +020085 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080086 break;
Tejun Heobe404f02009-10-08 22:47:30 +020087
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010088 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020089 wakeup = true;
90 break;
91 }
92
Tejun Heobe404f02009-10-08 22:47:30 +020093 /*
94 * We need to retry, but first give the freezing tasks some
95 * time to enter the regrigerator.
96 */
97 msleep(10);
98 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070099
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700100 do_gettimeofday(&end);
101 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
102 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
103 elapsed_csecs = elapsed_csecs64;
104
Pavel Machek6161b2c2005-09-03 15:57:05 -0700105 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800106 /* This does not unfreeze processes that are already frozen
107 * (we have slightly ugly calling convention in that respect,
108 * and caller must call thaw_processes() if something fails),
109 * but it cleans up leftover PF_FREEZE requests.
110 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800111 printk("\n");
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +0200112 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200113 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +0200114 wakeup ? "aborted" : "failed",
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200115 elapsed_csecs / 100, elapsed_csecs % 100,
116 todo - wq_busy, wq_busy);
117
118 thaw_workqueues();
119
Pavel Machek6161b2c2005-09-03 15:57:05 -0700120 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800121 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700122 task_lock(p);
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +0200123 if (!wakeup && freezing(p) && !freezer_should_skip(p))
Xiaotian Feng4f598452010-03-10 22:59:13 +0100124 sched_show_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700125 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700126 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800127 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700128 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700129 } else {
130 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
131 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700132 }
133
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700134 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800135}
136
137/**
138 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800139 */
140int freeze_processes(void)
141{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700142 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800143
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700144 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200145 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700146 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700147 goto Exit;
148 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800149
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700150 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200151 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700152 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700153 goto Exit;
154 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700155
156 oom_killer_disable();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700157 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700159 printk("\n");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700160
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700161 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200164static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct task_struct *g, *p;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800169 do_each_thread(g, p) {
170 if (!freezeable(p))
171 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800172
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200173 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800174 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Matt Helsley5a7aadf2010-03-26 23:51:44 +0100176 if (cgroup_freezing_or_frozen(p))
Matt Helsley5a069152008-10-18 20:27:22 -0700177 continue;
178
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700179 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800180 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800182}
183
184void thaw_processes(void)
185{
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700186 oom_killer_enable();
187
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800188 printk("Restarting tasks ... ");
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200189 thaw_workqueues();
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200190 thaw_tasks(true);
191 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800193 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195