blob: 6d566bf7085c0b4fd7803a14cbaabef07269198e [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
11#include <linux/smp_lock.h>
12#include <linux/interrupt.h>
13#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/*
19 * Timeout for stopping processes
20 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080021#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080023#define FREEZER_KERNEL_THREADS 0
24#define FREEZER_USER_SPACE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static inline int freezeable(struct task_struct * p)
27{
28 if ((p == current) ||
29 (p->flags & PF_NOFREEZE) ||
30 (p->exit_state == EXIT_ZOMBIE) ||
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080031 (p->exit_state == EXIT_DEAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return 0;
33 return 1;
34}
35
36/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070037void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 /* Hmm, should we be allowed to suspend when there are realtime
40 processes around? */
41 long save;
42 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070045 frozen_process(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 spin_lock_irq(&current->sighand->siglock);
47 recalc_sigpending(); /* We sent fake signal, clean it up */
48 spin_unlock_irq(&current->sighand->siglock);
49
Pavel Machek2a23b5d2005-09-03 15:56:53 -070050 while (frozen(current)) {
51 current->state = TASK_UNINTERRUPTIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 pr_debug("%s left refrigerator\n", current->comm);
55 current->state = save;
56}
57
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080058static inline void freeze_process(struct task_struct *p)
59{
60 unsigned long flags;
61
62 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080063 rmb();
64 if (!frozen(p)) {
65 if (p->state == TASK_STOPPED)
66 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080067
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080068 freeze(p);
69 spin_lock_irqsave(&p->sighand->siglock, flags);
70 signal_wake_up(p, p->state == TASK_STOPPED);
71 spin_unlock_irqrestore(&p->sighand->siglock, flags);
72 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080073 }
74}
75
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070076static void cancel_freezing(struct task_struct *p)
77{
78 unsigned long flags;
79
80 if (freezing(p)) {
81 pr_debug(" clean up: %s\n", p->comm);
82 do_not_freeze(p);
83 spin_lock_irqsave(&p->sighand->siglock, flags);
84 recalc_sigpending_tsk(p);
85 spin_unlock_irqrestore(&p->sighand->siglock, flags);
86 }
87}
88
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080089static inline int is_user_space(struct task_struct *p)
90{
91 return p->mm && !(p->flags & PF_BORROWED_MM);
92}
93
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080094static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080097 unsigned long end_time;
98 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070099
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800100 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800102 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 read_lock(&tasklist_lock);
104 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!freezeable(p))
106 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800107
Pavel Machek1322ad412005-07-07 17:56:45 -0700108 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800110
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -0800111 if (p->state == TASK_TRACED && frozen(p->parent)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700112 cancel_freezing(p);
113 continue;
114 }
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800115 if (is_user_space(p)) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800116 if (!freeze_user_space)
117 continue;
118
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800119 /* Freeze the task unless there is a vfork
120 * completion pending
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800121 */
122 if (!p->vfork_done)
123 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800124 } else {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800125 if (freeze_user_space)
126 continue;
127
128 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800129 }
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800130 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } while_each_thread(g, p);
132 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800133 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800134 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800135 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800136 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700137
Pavel Machek6161b2c2005-09-03 15:57:05 -0700138 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800139 /* This does not unfreeze processes that are already frozen
140 * (we have slightly ugly calling convention in that respect,
141 * and caller must call thaw_processes() if something fails),
142 * but it cleans up leftover PF_FREEZE requests.
143 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800144 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800145 printk(KERN_ERR "Stopping %s timed out after %d seconds "
146 "(%d tasks refusing to freeze):\n",
147 freeze_user_space ? "user space processes" :
148 "kernel threads",
149 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700150 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800151 do_each_thread(g, p) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800152 if (is_user_space(p) == !freeze_user_space)
153 continue;
154
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800155 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800156 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800157
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700158 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800159 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700160 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700161 }
162
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800163 return todo;
164}
165
166/**
167 * freeze_processes - tell processes to enter the refrigerator
168 *
169 * Returns 0 on success, or the number of processes that didn't freeze,
170 * although they were told to.
171 */
172int freeze_processes(void)
173{
174 unsigned int nr_unfrozen;
175
176 printk("Stopping tasks ... ");
177 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
178 if (nr_unfrozen)
179 return nr_unfrozen;
180
181 sys_sync();
182 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
183 if (nr_unfrozen)
184 return nr_unfrozen;
185
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800186 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 BUG_ON(in_atomic());
188 return 0;
189}
190
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800191static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct task_struct *g, *p;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800196 do_each_thread(g, p) {
197 if (!freezeable(p))
198 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800199
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800200 if (is_user_space(p) == !thaw_user_space)
201 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800203 if (!thaw_process(p))
204 printk(KERN_WARNING " Strange, %s not stopped\n",
205 p->comm );
206 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800208}
209
210void thaw_processes(void)
211{
212 printk("Restarting tasks ... ");
213 thaw_tasks(FREEZER_KERNEL_THREADS);
214 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800216 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219EXPORT_SYMBOL(refrigerator);