blob: 9b5301c73b8331ad66779ca6f78cb8dcbe6b13f8 [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
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070078static void freeze_task(struct task_struct *p)
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080079{
80 unsigned long flags;
81
82 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080083 rmb();
84 if (!frozen(p)) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070085 set_freeze_flag(p);
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080086 if (p->state == TASK_STOPPED)
87 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080088 spin_lock_irqsave(&p->sighand->siglock, flags);
89 signal_wake_up(p, p->state == TASK_STOPPED);
90 spin_unlock_irqrestore(&p->sighand->siglock, flags);
91 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080092 }
93}
94
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070095static void cancel_freezing(struct task_struct *p)
96{
97 unsigned long flags;
98
99 if (freezing(p)) {
100 pr_debug(" clean up: %s\n", p->comm);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700101 clear_freeze_flag(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700102 spin_lock_irqsave(&p->sighand->siglock, flags);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700103 recalc_sigpending_and_wake(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700104 spin_unlock_irqrestore(&p->sighand->siglock, flags);
105 }
106}
107
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800108static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800111 unsigned long end_time;
112 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700113
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800114 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800116 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 read_lock(&tasklist_lock);
118 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700119 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800121
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700122 if (freeze_user_space) {
123 if (p->state == TASK_TRACED &&
124 frozen(p->parent)) {
125 cancel_freezing(p);
126 continue;
127 }
128 /*
129 * Kernel threads should not have TIF_FREEZE set
130 * at this point, so we must ensure that either
131 * p->mm is not NULL *and* PF_BORROWED_MM is
132 * unset, or TIF_FRREZE is left unset.
133 * The task_lock() is necessary to prevent races
134 * with exit_mm() or use_mm()/unuse_mm() from
135 * occuring.
136 */
137 task_lock(p);
138 if (!p->mm || (p->flags & PF_BORROWED_MM)) {
139 task_unlock(p);
140 continue;
141 }
142 freeze_task(p);
143 task_unlock(p);
144 } else {
145 freeze_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700146 }
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700147 if (!freezer_should_skip(p))
148 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 } while_each_thread(g, p);
150 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800151 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800152 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800153 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800154 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700155
Pavel Machek6161b2c2005-09-03 15:57:05 -0700156 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800157 /* This does not unfreeze processes that are already frozen
158 * (we have slightly ugly calling convention in that respect,
159 * and caller must call thaw_processes() if something fails),
160 * but it cleans up leftover PF_FREEZE requests.
161 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800162 printk("\n");
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700163 printk(KERN_ERR "Freezing of %s timed out after %d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800164 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700165 freeze_user_space ? "user space " : "tasks ",
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800166 TIMEOUT / HZ, todo);
Andrew Morton328616e2007-07-19 01:47:26 -0700167 show_state();
Pavel Machek6161b2c2005-09-03 15:57:05 -0700168 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800169 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700170 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700171 if (freezing(p) && !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800172 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700173 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700174 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800175 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700176 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700177 }
178
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800179 return todo;
180}
181
182/**
183 * freeze_processes - tell processes to enter the refrigerator
184 *
185 * Returns 0 on success, or the number of processes that didn't freeze,
186 * although they were told to.
187 */
188int freeze_processes(void)
189{
190 unsigned int nr_unfrozen;
191
192 printk("Stopping tasks ... ");
193 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
194 if (nr_unfrozen)
195 return nr_unfrozen;
196
197 sys_sync();
198 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
199 if (nr_unfrozen)
200 return nr_unfrozen;
201
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800202 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 BUG_ON(in_atomic());
204 return 0;
205}
206
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800207static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct task_struct *g, *p;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800212 do_each_thread(g, p) {
213 if (!freezeable(p))
214 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800215
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700216 if (!p->mm == thaw_user_space)
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800217 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700219 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800220 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800222}
223
224void thaw_processes(void)
225{
226 printk("Restarting tasks ... ");
227 thaw_tasks(FREEZER_KERNEL_THREADS);
228 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800230 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233EXPORT_SYMBOL(refrigerator);