blob: 278946aecaf0d24b46b0736506de8ca3aafdc873 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static inline int freezeable(struct task_struct * p)
23{
Oleg Nesterov1065d132007-05-08 00:24:01 -070024 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070026 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return 0;
28 return 1;
29}
30
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070031/*
32 * freezing is complete, mark current process as frozen
33 */
34static inline void frozen_process(void)
35{
36 if (!unlikely(current->flags & PF_NOFREEZE)) {
37 current->flags |= PF_FROZEN;
38 wmb();
39 }
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070040 clear_freeze_flag(current);
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070041}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070044void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 /* Hmm, should we be allowed to suspend when there are realtime
47 processes around? */
48 long save;
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070049
50 task_lock(current);
51 if (freezing(current)) {
Gautham R Shenoy88f18ba2007-05-23 13:57:29 -070052 frozen_process();
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070053 task_unlock(current);
54 } else {
55 task_unlock(current);
56 return;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 spin_lock_irq(&current->sighand->siglock);
62 recalc_sigpending(); /* We sent fake signal, clean it up */
63 spin_unlock_irq(&current->sighand->siglock);
64
Oleg Nesterov433ecb42007-05-06 14:50:40 -070065 for (;;) {
66 set_current_state(TASK_UNINTERRUPTIBLE);
67 if (!frozen(current))
68 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 pr_debug("%s left refrigerator\n", current->comm);
Rafael J. Wysockif4a3a7d2007-07-19 01:47:33 -070072 __set_current_state(save);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Roland McGrath13b1c3d2008-03-03 20:22:05 -080075static void fake_signal_wake_up(struct task_struct *p)
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080076{
77 unsigned long flags;
78
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070079 spin_lock_irqsave(&p->sighand->siglock, flags);
Roland McGrath13b1c3d2008-03-03 20:22:05 -080080 signal_wake_up(p, 0);
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070081 spin_unlock_irqrestore(&p->sighand->siglock, flags);
82}
83
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020084static inline bool should_send_signal(struct task_struct *p)
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070085{
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020086 return !(p->flags & PF_FREEZER_NOSIG);
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070087}
88
89/**
90 * freeze_task - send a freeze request to given task
91 * @p: task to send the request to
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020092 * @sig_only: if set, the request will only be sent if the task has the
93 * PF_FREEZER_NOSIG flag unset
94 * Return value: 'false', if @sig_only is set and the task has
95 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070096 *
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020097 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070098 * either sending a fake signal to it or waking it up, depending on whether
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020099 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
100 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
101 * TIF_FREEZE flag will not be set.
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700102 */
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200103static bool freeze_task(struct task_struct *p, bool sig_only)
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700104{
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200105 /*
106 * We first check if the task is freezing and next if it has already
107 * been frozen to avoid the race with frozen_process() which first marks
108 * the task as frozen and next clears its TIF_FREEZE.
109 */
110 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -0800111 rmb();
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200112 if (frozen(p))
113 return false;
114
115 if (!sig_only || should_send_signal(p))
116 set_freeze_flag(p);
117 else
118 return false;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800119 }
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200120
121 if (should_send_signal(p)) {
122 if (!signal_pending(p))
123 fake_signal_wake_up(p);
124 } else if (sig_only) {
125 return false;
126 } else {
127 wake_up_state(p, TASK_INTERRUPTIBLE);
128 }
129
130 return true;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800131}
132
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700133static void cancel_freezing(struct task_struct *p)
134{
135 unsigned long flags;
136
137 if (freezing(p)) {
138 pr_debug(" clean up: %s\n", p->comm);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700139 clear_freeze_flag(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700140 spin_lock_irqsave(&p->sighand->siglock, flags);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700141 recalc_sigpending_and_wake(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700142 spin_unlock_irqrestore(&p->sighand->siglock, flags);
143 }
144}
145
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200146static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800149 unsigned long end_time;
150 unsigned int todo;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700151 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -0700152 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700153 unsigned int elapsed_csecs;
154
155 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700156
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800157 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800159 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 read_lock(&tasklist_lock);
161 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700162 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800164
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200165 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -0700166 continue;
167
Roland McGrath13b1c3d2008-03-03 20:22:05 -0800168 /*
169 * Now that we've done set_freeze_flag, don't
170 * perturb a task in TASK_STOPPED or TASK_TRACED.
171 * It is "frozen enough". If the task does wake
172 * up, it will immediately call try_to_freeze.
173 */
174 if (!task_is_stopped_or_traced(p) &&
175 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700176 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 } while_each_thread(g, p);
178 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800179 yield(); /* Yield is okay here */
Rafael J. Wysockic2cf7d82007-07-19 01:47:35 -0700180 if (time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800181 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800182 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700183
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700184 do_gettimeofday(&end);
185 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
186 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
187 elapsed_csecs = elapsed_csecs64;
188
Pavel Machek6161b2c2005-09-03 15:57:05 -0700189 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800190 /* This does not unfreeze processes that are already frozen
191 * (we have slightly ugly calling convention in that respect,
192 * and caller must call thaw_processes() if something fails),
193 * but it cleans up leftover PF_FREEZE requests.
194 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800195 printk("\n");
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700196 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800197 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700198 elapsed_csecs / 100, elapsed_csecs % 100, todo);
Andrew Morton328616e2007-07-19 01:47:26 -0700199 show_state();
Pavel Machek6161b2c2005-09-03 15:57:05 -0700200 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800201 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700202 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700203 if (freezing(p) && !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800204 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700205 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700206 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800207 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700208 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700209 } else {
210 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
211 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700212 }
213
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700214 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800215}
216
217/**
218 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800219 */
220int freeze_processes(void)
221{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700222 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800223
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700224 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200225 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700226 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700227 goto Exit;
228 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800229
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700230 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200231 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700232 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700233 goto Exit;
234 printk("done.");
235 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700237 printk("\n");
238 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200241static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct task_struct *g, *p;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800246 do_each_thread(g, p) {
247 if (!freezeable(p))
248 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800249
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200250 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800251 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700253 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800254 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800256}
257
258void thaw_processes(void)
259{
260 printk("Restarting tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200261 thaw_tasks(true);
262 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800264 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267EXPORT_SYMBOL(refrigerator);