swsusp: do not use page flags
[linux-2.6/mini2440.git] / kernel / power / process.c
blob0eb5c420e8edfdba4fe04adea60608e0be6357be
1 /*
2 * drivers/power/process.c - Functions for starting/stopping processes on
3 * suspend transitions.
5 * Originally from swsusp.
6 */
9 #undef DEBUG
11 #include <linux/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
18 /*
19 * Timeout for stopping processes
21 #define TIMEOUT (20 * HZ)
23 #define FREEZER_KERNEL_THREADS 0
24 #define FREEZER_USER_SPACE 1
26 static inline int freezeable(struct task_struct * p)
28 if ((p == current) ||
29 (p->flags & PF_NOFREEZE) ||
30 (p->exit_state == EXIT_ZOMBIE) ||
31 (p->exit_state == EXIT_DEAD))
32 return 0;
33 return 1;
36 /* Refrigerator is place where frozen processes are stored :-). */
37 void refrigerator(void)
39 /* Hmm, should we be allowed to suspend when there are realtime
40 processes around? */
41 long save;
42 save = current->state;
43 pr_debug("%s entered refrigerator\n", current->comm);
45 frozen_process(current);
46 spin_lock_irq(&current->sighand->siglock);
47 recalc_sigpending(); /* We sent fake signal, clean it up */
48 spin_unlock_irq(&current->sighand->siglock);
50 for (;;) {
51 set_current_state(TASK_UNINTERRUPTIBLE);
52 if (!frozen(current))
53 break;
54 schedule();
56 pr_debug("%s left refrigerator\n", current->comm);
57 current->state = save;
60 static inline void freeze_process(struct task_struct *p)
62 unsigned long flags;
64 if (!freezing(p)) {
65 rmb();
66 if (!frozen(p)) {
67 if (p->state == TASK_STOPPED)
68 force_sig_specific(SIGSTOP, p);
70 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);
78 static void cancel_freezing(struct task_struct *p)
80 unsigned long flags;
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);
91 static inline int is_user_space(struct task_struct *p)
93 return p->mm && !(p->flags & PF_BORROWED_MM);
96 static unsigned int try_to_freeze_tasks(int freeze_user_space)
98 struct task_struct *g, *p;
99 unsigned long end_time;
100 unsigned int todo;
102 end_time = jiffies + TIMEOUT;
103 do {
104 todo = 0;
105 read_lock(&tasklist_lock);
106 do_each_thread(g, p) {
107 if (!freezeable(p))
108 continue;
110 if (frozen(p))
111 continue;
113 if (p->state == TASK_TRACED && frozen(p->parent)) {
114 cancel_freezing(p);
115 continue;
117 if (is_user_space(p)) {
118 if (!freeze_user_space)
119 continue;
121 /* Freeze the task unless there is a vfork
122 * completion pending
124 if (!p->vfork_done)
125 freeze_process(p);
126 } else {
127 if (freeze_user_space)
128 continue;
130 freeze_process(p);
132 todo++;
133 } while_each_thread(g, p);
134 read_unlock(&tasklist_lock);
135 yield(); /* Yield is okay here */
136 if (todo && time_after(jiffies, end_time))
137 break;
138 } while (todo);
140 if (todo) {
141 /* 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.
146 printk("\n");
147 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);
152 read_lock(&tasklist_lock);
153 do_each_thread(g, p) {
154 if (is_user_space(p) == !freeze_user_space)
155 continue;
157 if (freezeable(p) && !frozen(p))
158 printk(KERN_ERR " %s\n", p->comm);
160 cancel_freezing(p);
161 } while_each_thread(g, p);
162 read_unlock(&tasklist_lock);
165 return todo;
169 * freeze_processes - tell processes to enter the refrigerator
171 * Returns 0 on success, or the number of processes that didn't freeze,
172 * although they were told to.
174 int freeze_processes(void)
176 unsigned int nr_unfrozen;
178 printk("Stopping tasks ... ");
179 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
180 if (nr_unfrozen)
181 return nr_unfrozen;
183 sys_sync();
184 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
185 if (nr_unfrozen)
186 return nr_unfrozen;
188 printk("done.\n");
189 BUG_ON(in_atomic());
190 return 0;
193 static void thaw_tasks(int thaw_user_space)
195 struct task_struct *g, *p;
197 read_lock(&tasklist_lock);
198 do_each_thread(g, p) {
199 if (!freezeable(p))
200 continue;
202 if (is_user_space(p) == !thaw_user_space)
203 continue;
205 if (!thaw_process(p))
206 printk(KERN_WARNING " Strange, %s not stopped\n",
207 p->comm );
208 } while_each_thread(g, p);
209 read_unlock(&tasklist_lock);
212 void thaw_processes(void)
214 printk("Restarting tasks ... ");
215 thaw_tasks(FREEZER_KERNEL_THREADS);
216 thaw_tasks(FREEZER_USER_SPACE);
217 schedule();
218 printk("done.\n");
221 EXPORT_SYMBOL(refrigerator);