blob: 2cea2658e9852bf922b65638fb32f20cc33aa5a8 [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 Machek1322ad42005-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. Wysocki49b12d42007-05-23 13:57:26 -0700123 if (freeze_user_space && !is_user_space(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700124 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800125
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700126 freeze_process(p);
127 if (!freezer_should_skip(p))
128 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } while_each_thread(g, p);
130 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800131 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800132 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800133 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800134 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700135
Pavel Machek6161b2c2005-09-03 15:57:05 -0700136 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800137 /* This does not unfreeze processes that are already frozen
138 * (we have slightly ugly calling convention in that respect,
139 * and caller must call thaw_processes() if something fails),
140 * but it cleans up leftover PF_FREEZE requests.
141 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800142 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800143 printk(KERN_ERR "Stopping %s timed out after %d seconds "
144 "(%d tasks refusing to freeze):\n",
145 freeze_user_space ? "user space processes" :
146 "kernel threads",
147 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700148 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800149 do_each_thread(g, p) {
Rafael J. Wysocki49b12d42007-05-23 13:57:26 -0700150 if (freeze_user_space && !is_user_space(p))
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800151 continue;
152
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700153 task_lock(p);
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700154 if (freezeable(p) && !frozen(p) &&
155 !freezer_should_skip(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. Wysocki33e1c282007-05-23 13:57:24 -0700159 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800160 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700161 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700162 }
163
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800164 return todo;
165}
166
167/**
168 * freeze_processes - tell processes to enter the refrigerator
169 *
170 * Returns 0 on success, or the number of processes that didn't freeze,
171 * although they were told to.
172 */
173int freeze_processes(void)
174{
175 unsigned int nr_unfrozen;
176
177 printk("Stopping tasks ... ");
178 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
179 if (nr_unfrozen)
180 return nr_unfrozen;
181
182 sys_sync();
183 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
184 if (nr_unfrozen)
185 return nr_unfrozen;
186
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800187 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 BUG_ON(in_atomic());
189 return 0;
190}
191
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800192static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct task_struct *g, *p;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800197 do_each_thread(g, p) {
198 if (!freezeable(p))
199 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800200
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800201 if (is_user_space(p) == !thaw_user_space)
202 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700204 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800205 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800207}
208
209void thaw_processes(void)
210{
211 printk("Restarting tasks ... ");
212 thaw_tasks(FREEZER_KERNEL_THREADS);
213 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800215 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218EXPORT_SYMBOL(refrigerator);