blob: d31d638ab4c03ae94830189378610b89ebb1c2b4 [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 }
43 clear_tsk_thread_flag(current, TIF_FREEZE);
44}
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);
75 current->state = save;
76}
77
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080078static inline void freeze_process(struct task_struct *p)
79{
80 unsigned long flags;
81
82 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080083 rmb();
84 if (!frozen(p)) {
85 if (p->state == TASK_STOPPED)
86 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080087
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080088 freeze(p);
89 spin_lock_irqsave(&p->sighand->siglock, flags);
90 signal_wake_up(p, p->state == TASK_STOPPED);
91 spin_unlock_irqrestore(&p->sighand->siglock, flags);
92 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080093 }
94}
95
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070096static void cancel_freezing(struct task_struct *p)
97{
98 unsigned long flags;
99
100 if (freezing(p)) {
101 pr_debug(" clean up: %s\n", p->comm);
102 do_not_freeze(p);
103 spin_lock_irqsave(&p->sighand->siglock, flags);
104 recalc_sigpending_tsk(p);
105 spin_unlock_irqrestore(&p->sighand->siglock, flags);
106 }
107}
108
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800109static inline int is_user_space(struct task_struct *p)
110{
111 return p->mm && !(p->flags & PF_BORROWED_MM);
112}
113
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800114static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800117 unsigned long end_time;
118 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700119
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800120 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800122 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 read_lock(&tasklist_lock);
124 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!freezeable(p))
126 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800127
Pavel Machek1322ad412005-07-07 17:56:45 -0700128 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800130
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -0800131 if (p->state == TASK_TRACED && frozen(p->parent)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700132 cancel_freezing(p);
133 continue;
134 }
Rafael J. Wysocki49b12d42007-05-23 13:57:26 -0700135 if (freeze_user_space && !is_user_space(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700136 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800137
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700138 freeze_process(p);
139 if (!freezer_should_skip(p))
140 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 } while_each_thread(g, p);
142 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800143 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800144 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800145 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800146 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700147
Pavel Machek6161b2c2005-09-03 15:57:05 -0700148 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800149 /* This does not unfreeze processes that are already frozen
150 * (we have slightly ugly calling convention in that respect,
151 * and caller must call thaw_processes() if something fails),
152 * but it cleans up leftover PF_FREEZE requests.
153 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800154 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800155 printk(KERN_ERR "Stopping %s timed out after %d seconds "
156 "(%d tasks refusing to freeze):\n",
157 freeze_user_space ? "user space processes" :
158 "kernel threads",
159 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700160 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800161 do_each_thread(g, p) {
Rafael J. Wysocki49b12d42007-05-23 13:57:26 -0700162 if (freeze_user_space && !is_user_space(p))
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800163 continue;
164
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700165 task_lock(p);
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700166 if (freezeable(p) && !frozen(p) &&
167 !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800168 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800169
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700170 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700171 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800172 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700173 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700174 }
175
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800176 return todo;
177}
178
179/**
180 * freeze_processes - tell processes to enter the refrigerator
181 *
182 * Returns 0 on success, or the number of processes that didn't freeze,
183 * although they were told to.
184 */
185int freeze_processes(void)
186{
187 unsigned int nr_unfrozen;
188
189 printk("Stopping tasks ... ");
190 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
191 if (nr_unfrozen)
192 return nr_unfrozen;
193
194 sys_sync();
195 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
196 if (nr_unfrozen)
197 return nr_unfrozen;
198
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800199 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 BUG_ON(in_atomic());
201 return 0;
202}
203
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800204static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct task_struct *g, *p;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800209 do_each_thread(g, p) {
210 if (!freezeable(p))
211 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800212
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800213 if (is_user_space(p) == !thaw_user_space)
214 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700216 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800217 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800219}
220
221void thaw_processes(void)
222{
223 printk("Restarting tasks ... ");
224 thaw_tasks(FREEZER_KERNEL_THREADS);
225 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800227 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230EXPORT_SYMBOL(refrigerator);