blob: f1d0b345c9ba86a24ac48ee29cc4cb6f800b1c94 [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>
12#include <linux/suspend.h>
13#include <linux/module.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080014#include <linux/syscalls.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080015#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * Timeout for stopping processes
19 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080020#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080022#define FREEZER_KERNEL_THREADS 0
23#define FREEZER_USER_SPACE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static 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
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070034/*
35 * freezing is complete, mark current process as frozen
36 */
37static inline void frozen_process(void)
38{
39 if (!unlikely(current->flags & PF_NOFREEZE)) {
40 current->flags |= PF_FROZEN;
41 wmb();
42 }
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070043 clear_freeze_flag(current);
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070044}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070047void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 /* Hmm, should we be allowed to suspend when there are realtime
50 processes around? */
51 long save;
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070052
53 task_lock(current);
54 if (freezing(current)) {
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070055 frozen_process();
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070056 task_unlock(current);
57 } else {
58 task_unlock(current);
59 return;
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 spin_lock_irq(&current->sighand->siglock);
65 recalc_sigpending(); /* We sent fake signal, clean it up */
66 spin_unlock_irq(&current->sighand->siglock);
67
Oleg Nesterov433ecb42007-05-06 14:50:40 -070068 for (;;) {
69 set_current_state(TASK_UNINTERRUPTIBLE);
70 if (!frozen(current))
71 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 pr_debug("%s left refrigerator\n", current->comm);
Rafael J. Wysockif4a3a7d2007-07-19 01:47:33 -070075 __set_current_state(save);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Roland McGrath13b1c3d2008-03-03 20:22:05 -080078static void fake_signal_wake_up(struct task_struct *p)
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080079{
80 unsigned long flags;
81
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070082 spin_lock_irqsave(&p->sighand->siglock, flags);
Roland McGrath13b1c3d2008-03-03 20:22:05 -080083 signal_wake_up(p, 0);
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070084 spin_unlock_irqrestore(&p->sighand->siglock, flags);
85}
86
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070087static int has_mm(struct task_struct *p)
88{
89 return (p->mm && !(p->flags & PF_BORROWED_MM));
90}
91
92/**
93 * freeze_task - send a freeze request to given task
94 * @p: task to send the request to
95 * @with_mm_only: if set, the request will only be sent if the task has its
96 * own mm
97 * Return value: 0, if @with_mm_only is set and the task has no mm of its
98 * own or the task is frozen, 1, otherwise
99 *
100 * The freeze request is sent by seting the tasks's TIF_FREEZE flag and
101 * either sending a fake signal to it or waking it up, depending on whether
102 * or not it has its own mm (ie. it is a user land task). If @with_mm_only
103 * is set and the task has no mm of its own (ie. it is a kernel thread),
104 * its TIF_FREEZE flag should not be set.
105 *
106 * The task_lock() is necessary to prevent races with exit_mm() or
107 * use_mm()/unuse_mm() from occuring.
108 */
109static int freeze_task(struct task_struct *p, int with_mm_only)
110{
111 int ret = 1;
112
113 task_lock(p);
114 if (freezing(p)) {
115 if (has_mm(p)) {
116 if (!signal_pending(p))
Roland McGrath13b1c3d2008-03-03 20:22:05 -0800117 fake_signal_wake_up(p);
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700118 } else {
119 if (with_mm_only)
120 ret = 0;
121 else
122 wake_up_state(p, TASK_INTERRUPTIBLE);
123 }
124 } else {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -0800125 rmb();
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700126 if (frozen(p)) {
127 ret = 0;
128 } else {
129 if (has_mm(p)) {
130 set_freeze_flag(p);
Roland McGrath13b1c3d2008-03-03 20:22:05 -0800131 fake_signal_wake_up(p);
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700132 } else {
133 if (with_mm_only) {
134 ret = 0;
135 } else {
136 set_freeze_flag(p);
137 wake_up_state(p, TASK_INTERRUPTIBLE);
138 }
139 }
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -0800140 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800141 }
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700142 task_unlock(p);
143 return ret;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800144}
145
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700146static void cancel_freezing(struct task_struct *p)
147{
148 unsigned long flags;
149
150 if (freezing(p)) {
151 pr_debug(" clean up: %s\n", p->comm);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700152 clear_freeze_flag(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700153 spin_lock_irqsave(&p->sighand->siglock, flags);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700154 recalc_sigpending_and_wake(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700155 spin_unlock_irqrestore(&p->sighand->siglock, flags);
156 }
157}
158
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700159static int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800162 unsigned long end_time;
163 unsigned int todo;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700164 struct timeval start, end;
165 s64 elapsed_csecs64;
166 unsigned int elapsed_csecs;
167
168 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700169
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800170 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800172 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 read_lock(&tasklist_lock);
174 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700175 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800177
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700178 if (!freeze_task(p, freeze_user_space))
179 continue;
180
Roland McGrath13b1c3d2008-03-03 20:22:05 -0800181 /*
182 * Now that we've done set_freeze_flag, don't
183 * perturb a task in TASK_STOPPED or TASK_TRACED.
184 * It is "frozen enough". If the task does wake
185 * up, it will immediately call try_to_freeze.
186 */
187 if (!task_is_stopped_or_traced(p) &&
188 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700189 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 } while_each_thread(g, p);
191 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800192 yield(); /* Yield is okay here */
Rafael J. Wysockic2cf7d82007-07-19 01:47:35 -0700193 if (time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800194 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800195 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700196
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700197 do_gettimeofday(&end);
198 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
199 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
200 elapsed_csecs = elapsed_csecs64;
201
Pavel Machek6161b2c2005-09-03 15:57:05 -0700202 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800203 /* This does not unfreeze processes that are already frozen
204 * (we have slightly ugly calling convention in that respect,
205 * and caller must call thaw_processes() if something fails),
206 * but it cleans up leftover PF_FREEZE requests.
207 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800208 printk("\n");
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700209 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800210 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700211 elapsed_csecs / 100, elapsed_csecs % 100, todo);
Andrew Morton328616e2007-07-19 01:47:26 -0700212 show_state();
Pavel Machek6161b2c2005-09-03 15:57:05 -0700213 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800214 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700215 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700216 if (freezing(p) && !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800217 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700218 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700219 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800220 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700221 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700222 } else {
223 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
224 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700225 }
226
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700227 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800228}
229
230/**
231 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800232 */
233int freeze_processes(void)
234{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700235 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800236
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700237 printk("Freezing user space processes ... ");
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700238 error = try_to_freeze_tasks(FREEZER_USER_SPACE);
239 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700240 goto Exit;
241 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800242
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700243 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700244 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
245 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700246 goto Exit;
247 printk("done.");
248 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700250 printk("\n");
251 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800254static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct task_struct *g, *p;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800259 do_each_thread(g, p) {
260 if (!freezeable(p))
261 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800262
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700263 if (!p->mm == thaw_user_space)
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700266 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800267 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800269}
270
271void thaw_processes(void)
272{
273 printk("Restarting tasks ... ");
274 thaw_tasks(FREEZER_KERNEL_THREADS);
275 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800277 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280EXPORT_SYMBOL(refrigerator);