blob: 179529dc38190f8860f02ac4cc2f3def1df88895 [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{
Oleg Nesterov1065d132007-05-08 00:24:01 -070028 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070030 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32 return 1;
33}
34
35/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070036void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 /* Hmm, should we be allowed to suspend when there are realtime
39 processes around? */
40 long save;
41 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070044 frozen_process(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 spin_lock_irq(&current->sighand->siglock);
46 recalc_sigpending(); /* We sent fake signal, clean it up */
47 spin_unlock_irq(&current->sighand->siglock);
48
Oleg Nesterov433ecb42007-05-06 14:50:40 -070049 for (;;) {
50 set_current_state(TASK_UNINTERRUPTIBLE);
51 if (!frozen(current))
52 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 pr_debug("%s left refrigerator\n", current->comm);
56 current->state = save;
57}
58
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080059static inline void freeze_process(struct task_struct *p)
60{
61 unsigned long flags;
62
63 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080064 rmb();
65 if (!frozen(p)) {
66 if (p->state == TASK_STOPPED)
67 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080068
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080069 freeze(p);
70 spin_lock_irqsave(&p->sighand->siglock, flags);
71 signal_wake_up(p, p->state == TASK_STOPPED);
72 spin_unlock_irqrestore(&p->sighand->siglock, flags);
73 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080074 }
75}
76
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070077static void cancel_freezing(struct task_struct *p)
78{
79 unsigned long flags;
80
81 if (freezing(p)) {
82 pr_debug(" clean up: %s\n", p->comm);
83 do_not_freeze(p);
84 spin_lock_irqsave(&p->sighand->siglock, flags);
85 recalc_sigpending_tsk(p);
86 spin_unlock_irqrestore(&p->sighand->siglock, flags);
87 }
88}
89
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080090static inline int is_user_space(struct task_struct *p)
91{
92 return p->mm && !(p->flags & PF_BORROWED_MM);
93}
94
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080095static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080098 unsigned long end_time;
99 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700100
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800101 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800103 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 read_lock(&tasklist_lock);
105 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!freezeable(p))
107 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800108
Pavel Machek1322ad42005-07-07 17:56:45 -0700109 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800111
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -0800112 if (p->state == TASK_TRACED && frozen(p->parent)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700113 cancel_freezing(p);
114 continue;
115 }
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800116 if (is_user_space(p)) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800117 if (!freeze_user_space)
118 continue;
119
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800120 /* Freeze the task unless there is a vfork
121 * completion pending
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800122 */
123 if (!p->vfork_done)
124 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800125 } else {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800126 if (freeze_user_space)
127 continue;
128
129 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800130 }
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800131 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } while_each_thread(g, p);
133 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800134 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800135 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800136 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800137 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700138
Pavel Machek6161b2c2005-09-03 15:57:05 -0700139 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800140 /* This does not unfreeze processes that are already frozen
141 * (we have slightly ugly calling convention in that respect,
142 * and caller must call thaw_processes() if something fails),
143 * but it cleans up leftover PF_FREEZE requests.
144 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800145 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800146 printk(KERN_ERR "Stopping %s timed out after %d seconds "
147 "(%d tasks refusing to freeze):\n",
148 freeze_user_space ? "user space processes" :
149 "kernel threads",
150 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700151 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800152 do_each_thread(g, p) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800153 if (is_user_space(p) == !freeze_user_space)
154 continue;
155
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800156 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800157 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800158
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700159 cancel_freezing(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. Wysockia9b6f562006-12-06 20:34:37 -0800204 if (!thaw_process(p))
205 printk(KERN_WARNING " Strange, %s not stopped\n",
206 p->comm );
207 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800209}
210
211void thaw_processes(void)
212{
213 printk("Restarting tasks ... ");
214 thaw_tasks(FREEZER_KERNEL_THREADS);
215 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800217 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220EXPORT_SYMBOL(refrigerator);