blob: e6e2739190b5f483715d54a1a0b29d805c07b439 [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
Tejun Heo58a69cb2011-02-16 09:25:31 +010025static inline int freezable(struct task_struct * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Oleg Nesterov1065d132007-05-08 00:24:01 -070027 if ((p == current) ||
Tejun Heoa5850422011-11-21 12:32:23 -080028 (p->flags & PF_NOFREEZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return 0;
30 return 1;
31}
32
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020033static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080036 unsigned long end_time;
37 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020038 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070039 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070040 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070041 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020042 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070043
44 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070045
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080046 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020047
48 if (!sig_only)
49 freeze_workqueues_begin();
50
Tejun Heobe404f02009-10-08 22:47:30 +020051 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080052 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 read_lock(&tasklist_lock);
54 do_each_thread(g, p) {
Tejun Heo58a69cb2011-02-16 09:25:31 +010055 if (frozen(p) || !freezable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080057
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020058 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070059 continue;
60
Roland McGrath13b1c3d2008-03-03 20:22:05 -080061 /*
62 * Now that we've done set_freeze_flag, don't
63 * perturb a task in TASK_STOPPED or TASK_TRACED.
64 * It is "frozen enough". If the task does wake
65 * up, it will immediately call try_to_freeze.
Tejun Heo8cfe4002010-11-26 23:07:27 +010066 *
67 * Because freeze_task() goes through p's
68 * scheduler lock after setting TIF_FREEZE, it's
69 * guaranteed that either we see TASK_RUNNING or
70 * try_to_stop() after schedule() in ptrace/signal
71 * stop sees TIF_FREEZE.
Roland McGrath13b1c3d2008-03-03 20:22:05 -080072 */
73 if (!task_is_stopped_or_traced(p) &&
74 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070075 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 } while_each_thread(g, p);
77 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020078
79 if (!sig_only) {
80 wq_busy = freeze_workqueues_busy();
81 todo += wq_busy;
82 }
83
Tejun Heobe404f02009-10-08 22:47:30 +020084 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080085 break;
Tejun Heobe404f02009-10-08 22:47:30 +020086
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010087 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020088 wakeup = true;
89 break;
90 }
91
Tejun Heobe404f02009-10-08 22:47:30 +020092 /*
93 * We need to retry, but first give the freezing tasks some
94 * time to enter the regrigerator.
95 */
96 msleep(10);
97 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070098
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070099 do_gettimeofday(&end);
100 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
101 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
102 elapsed_csecs = elapsed_csecs64;
103
Pavel Machek6161b2c2005-09-03 15:57:05 -0700104 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800105 /* This does not unfreeze processes that are already frozen
106 * (we have slightly ugly calling convention in that respect,
107 * and caller must call thaw_processes() if something fails),
108 * but it cleans up leftover PF_FREEZE requests.
109 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800110 printk("\n");
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +0200111 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200112 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +0200113 wakeup ? "aborted" : "failed",
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200114 elapsed_csecs / 100, elapsed_csecs % 100,
115 todo - wq_busy, wq_busy);
116
117 thaw_workqueues();
118
Pavel Machek6161b2c2005-09-03 15:57:05 -0700119 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800120 do_each_thread(g, p) {
Tejun Heo69074832011-11-21 12:32:24 -0800121 if (!wakeup && !freezer_should_skip(p) &&
122 freezing(p) && !frozen(p))
Xiaotian Feng4f598452010-03-10 22:59:13 +0100123 sched_show_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700124 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800125 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700126 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700127 } else {
128 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
129 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700130 }
131
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700132 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800133}
134
135/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200136 * freeze_processes - Signal user space processes to enter the refrigerator.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800137 */
138int freeze_processes(void)
139{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700140 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800141
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700142 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200143 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200144 if (!error) {
145 printk("done.");
146 oom_killer_disable();
147 }
148 printk("\n");
149 BUG_ON(in_atomic());
150
151 return error;
152}
153
154/**
155 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
156 */
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 ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200162 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200163 if (!error)
164 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700165
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700166 printk("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200167 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700168
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700169 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800172void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct task_struct *g, *p;
175
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800176 oom_killer_enable();
177
178 printk("Restarting tasks ... ");
179
180 thaw_workqueues();
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800183 do_each_thread(g, p) {
Tejun Heo58a69cb2011-02-16 09:25:31 +0100184 if (!freezable(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800185 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800186
Matt Helsley5a7aadf2010-03-26 23:51:44 +0100187 if (cgroup_freezing_or_frozen(p))
Matt Helsley5a069152008-10-18 20:27:22 -0700188 continue;
189
Tejun Heoa5be2d02011-11-21 12:32:23 -0800190 __thaw_task(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800191 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800195 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197