blob: 0eb5c420e8edfdba4fe04adea60608e0be6357be [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
Oleg Nesterov433ecb42007-05-06 14:50:40 -070050 for (;;) {
51 set_current_state(TASK_UNINTERRUPTIBLE);
52 if (!frozen(current))
53 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 pr_debug("%s left refrigerator\n", current->comm);
57 current->state = save;
58}
59
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080060static inline void freeze_process(struct task_struct *p)
61{
62 unsigned long flags;
63
64 if (!freezing(p)) {
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080065 rmb();
66 if (!frozen(p)) {
67 if (p->state == TASK_STOPPED)
68 force_sig_specific(SIGSTOP, p);
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -080069
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080070 freeze(p);
71 spin_lock_irqsave(&p->sighand->siglock, flags);
72 signal_wake_up(p, p->state == TASK_STOPPED);
73 spin_unlock_irqrestore(&p->sighand->siglock, flags);
74 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080075 }
76}
77
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070078static void cancel_freezing(struct task_struct *p)
79{
80 unsigned long flags;
81
82 if (freezing(p)) {
83 pr_debug(" clean up: %s\n", p->comm);
84 do_not_freeze(p);
85 spin_lock_irqsave(&p->sighand->siglock, flags);
86 recalc_sigpending_tsk(p);
87 spin_unlock_irqrestore(&p->sighand->siglock, flags);
88 }
89}
90
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080091static inline int is_user_space(struct task_struct *p)
92{
93 return p->mm && !(p->flags & PF_BORROWED_MM);
94}
95
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080096static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080099 unsigned long end_time;
100 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700101
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800102 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800104 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 read_lock(&tasklist_lock);
106 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!freezeable(p))
108 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800109
Pavel Machek1322ad42005-07-07 17:56:45 -0700110 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800112
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -0800113 if (p->state == TASK_TRACED && frozen(p->parent)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700114 cancel_freezing(p);
115 continue;
116 }
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800117 if (is_user_space(p)) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800118 if (!freeze_user_space)
119 continue;
120
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800121 /* Freeze the task unless there is a vfork
122 * completion pending
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800123 */
124 if (!p->vfork_done)
125 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800126 } else {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800127 if (freeze_user_space)
128 continue;
129
130 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800131 }
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800132 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } while_each_thread(g, p);
134 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800135 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800136 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800137 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800138 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700139
Pavel Machek6161b2c2005-09-03 15:57:05 -0700140 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800141 /* This does not unfreeze processes that are already frozen
142 * (we have slightly ugly calling convention in that respect,
143 * and caller must call thaw_processes() if something fails),
144 * but it cleans up leftover PF_FREEZE requests.
145 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800146 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800147 printk(KERN_ERR "Stopping %s timed out after %d seconds "
148 "(%d tasks refusing to freeze):\n",
149 freeze_user_space ? "user space processes" :
150 "kernel threads",
151 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700152 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800153 do_each_thread(g, p) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800154 if (is_user_space(p) == !freeze_user_space)
155 continue;
156
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800157 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800158 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800159
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700160 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800161 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700162 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700163 }
164
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800165 return todo;
166}
167
168/**
169 * freeze_processes - tell processes to enter the refrigerator
170 *
171 * Returns 0 on success, or the number of processes that didn't freeze,
172 * although they were told to.
173 */
174int freeze_processes(void)
175{
176 unsigned int nr_unfrozen;
177
178 printk("Stopping tasks ... ");
179 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
180 if (nr_unfrozen)
181 return nr_unfrozen;
182
183 sys_sync();
184 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
185 if (nr_unfrozen)
186 return nr_unfrozen;
187
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800188 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 BUG_ON(in_atomic());
190 return 0;
191}
192
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800193static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct task_struct *g, *p;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800198 do_each_thread(g, p) {
199 if (!freezeable(p))
200 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800201
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800202 if (is_user_space(p) == !thaw_user_space)
203 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800205 if (!thaw_process(p))
206 printk(KERN_WARNING " Strange, %s not stopped\n",
207 p->comm );
208 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800210}
211
212void thaw_processes(void)
213{
214 printk("Restarting tasks ... ");
215 thaw_tasks(FREEZER_KERNEL_THREADS);
216 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800218 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221EXPORT_SYMBOL(refrigerator);