blob: 02e490e311ebad49b60fcf7b1eeba0dcd1d68cc3 [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
34/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070035void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 /* Hmm, should we be allowed to suspend when there are realtime
38 processes around? */
39 long save;
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070040
41 task_lock(current);
42 if (freezing(current)) {
43 frozen_process(current);
44 task_unlock(current);
45 } else {
46 task_unlock(current);
47 return;
48 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 spin_lock_irq(&current->sighand->siglock);
53 recalc_sigpending(); /* We sent fake signal, clean it up */
54 spin_unlock_irq(&current->sighand->siglock);
55
Oleg Nesterov433ecb42007-05-06 14:50:40 -070056 for (;;) {
57 set_current_state(TASK_UNINTERRUPTIBLE);
58 if (!frozen(current))
59 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 pr_debug("%s left refrigerator\n", current->comm);
63 current->state = save;
64}
65
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080066static inline void freeze_process(struct task_struct *p)
67{
68 unsigned long flags;
69
70 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080071 rmb();
72 if (!frozen(p)) {
73 if (p->state == TASK_STOPPED)
74 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080075
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080076 freeze(p);
77 spin_lock_irqsave(&p->sighand->siglock, flags);
78 signal_wake_up(p, p->state == TASK_STOPPED);
79 spin_unlock_irqrestore(&p->sighand->siglock, flags);
80 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080081 }
82}
83
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070084static void cancel_freezing(struct task_struct *p)
85{
86 unsigned long flags;
87
88 if (freezing(p)) {
89 pr_debug(" clean up: %s\n", p->comm);
90 do_not_freeze(p);
91 spin_lock_irqsave(&p->sighand->siglock, flags);
92 recalc_sigpending_tsk(p);
93 spin_unlock_irqrestore(&p->sighand->siglock, flags);
94 }
95}
96
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080097static inline int is_user_space(struct task_struct *p)
98{
99 return p->mm && !(p->flags & PF_BORROWED_MM);
100}
101
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800102static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800105 unsigned long end_time;
106 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700107
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800108 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800110 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 read_lock(&tasklist_lock);
112 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!freezeable(p))
114 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800115
Pavel Machek1322ad412005-07-07 17:56:45 -0700116 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800118
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -0800119 if (p->state == TASK_TRACED && frozen(p->parent)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700120 cancel_freezing(p);
121 continue;
122 }
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800123 if (is_user_space(p)) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800124 if (!freeze_user_space)
125 continue;
126
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800127 /* Freeze the task unless there is a vfork
128 * completion pending
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800129 */
130 if (!p->vfork_done)
131 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800132 } else {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800133 if (freeze_user_space)
134 continue;
135
136 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800137 }
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800138 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } while_each_thread(g, p);
140 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800141 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800142 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800143 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800144 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700145
Pavel Machek6161b2c2005-09-03 15:57:05 -0700146 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800147 /* This does not unfreeze processes that are already frozen
148 * (we have slightly ugly calling convention in that respect,
149 * and caller must call thaw_processes() if something fails),
150 * but it cleans up leftover PF_FREEZE requests.
151 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800152 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800153 printk(KERN_ERR "Stopping %s timed out after %d seconds "
154 "(%d tasks refusing to freeze):\n",
155 freeze_user_space ? "user space processes" :
156 "kernel threads",
157 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700158 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800159 do_each_thread(g, p) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800160 if (is_user_space(p) == !freeze_user_space)
161 continue;
162
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700163 task_lock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800164 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800165 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800166
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700167 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700168 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800169 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700170 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700171 }
172
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800173 return todo;
174}
175
176/**
177 * freeze_processes - tell processes to enter the refrigerator
178 *
179 * Returns 0 on success, or the number of processes that didn't freeze,
180 * although they were told to.
181 */
182int freeze_processes(void)
183{
184 unsigned int nr_unfrozen;
185
186 printk("Stopping tasks ... ");
187 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
188 if (nr_unfrozen)
189 return nr_unfrozen;
190
191 sys_sync();
192 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
193 if (nr_unfrozen)
194 return nr_unfrozen;
195
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800196 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 BUG_ON(in_atomic());
198 return 0;
199}
200
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800201static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct task_struct *g, *p;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800206 do_each_thread(g, p) {
207 if (!freezeable(p))
208 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800209
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800210 if (is_user_space(p) == !thaw_user_space)
211 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800213 if (!thaw_process(p))
214 printk(KERN_WARNING " Strange, %s not stopped\n",
215 p->comm );
216 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800218}
219
220void thaw_processes(void)
221{
222 printk("Restarting tasks ... ");
223 thaw_tasks(FREEZER_KERNEL_THREADS);
224 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800226 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229EXPORT_SYMBOL(refrigerator);