ipmi: add pci remove handling
[deliverable/linux.git] / kernel / power / process.c
CommitLineData
1da177e4
LT
1/*
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>
02aaeb9b 15#include <linux/syscalls.h>
7dfb7103 16#include <linux/freezer.h>
1da177e4
LT
17
18/*
19 * Timeout for stopping processes
20 */
02aaeb9b 21#define TIMEOUT (20 * HZ)
1da177e4 22
a9b6f562
RW
23#define FREEZER_KERNEL_THREADS 0
24#define FREEZER_USER_SPACE 1
1da177e4
LT
25
26static inline int freezeable(struct task_struct * p)
27{
28 if ((p == current) ||
29 (p->flags & PF_NOFREEZE) ||
30 (p->exit_state == EXIT_ZOMBIE) ||
3df494a3 31 (p->exit_state == EXIT_DEAD))
1da177e4
LT
32 return 0;
33 return 1;
34}
35
36/* Refrigerator is place where frozen processes are stored :-). */
3e1d1d28 37void refrigerator(void)
1da177e4
LT
38{
39 /* Hmm, should we be allowed to suspend when there are realtime
40 processes around? */
41 long save;
42 save = current->state;
1da177e4 43 pr_debug("%s entered refrigerator\n", current->comm);
1da177e4 44
3e1d1d28 45 frozen_process(current);
1da177e4
LT
46 spin_lock_irq(&current->sighand->siglock);
47 recalc_sigpending(); /* We sent fake signal, clean it up */
48 spin_unlock_irq(&current->sighand->siglock);
49
433ecb4a
ON
50 for (;;) {
51 set_current_state(TASK_UNINTERRUPTIBLE);
52 if (!frozen(current))
53 break;
1da177e4 54 schedule();
2a23b5d1 55 }
1da177e4
LT
56 pr_debug("%s left refrigerator\n", current->comm);
57 current->state = save;
58}
59
02aaeb9b
RW
60static inline void freeze_process(struct task_struct *p)
61{
62 unsigned long flags;
63
64 if (!freezing(p)) {
8a102eed
RW
65 rmb();
66 if (!frozen(p)) {
67 if (p->state == TASK_STOPPED)
68 force_sig_specific(SIGSTOP, p);
3df494a3 69
8a102eed
RW
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);
74 }
02aaeb9b
RW
75 }
76}
77
a7ef7878
RW
78static 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
a9b6f562
RW
91static inline int is_user_space(struct task_struct *p)
92{
93 return p->mm && !(p->flags & PF_BORROWED_MM);
94}
95
11b2ce2b 96static unsigned int try_to_freeze_tasks(int freeze_user_space)
1da177e4 97{
1da177e4 98 struct task_struct *g, *p;
11b2ce2b
RW
99 unsigned long end_time;
100 unsigned int todo;
3e1d1d28 101
11b2ce2b 102 end_time = jiffies + TIMEOUT;
1da177e4 103 do {
11b2ce2b 104 todo = 0;
1da177e4
LT
105 read_lock(&tasklist_lock);
106 do_each_thread(g, p) {
1da177e4
LT
107 if (!freezeable(p))
108 continue;
11b2ce2b 109
1322ad41 110 if (frozen(p))
1da177e4 111 continue;
11b2ce2b 112
3df494a3 113 if (p->state == TASK_TRACED && frozen(p->parent)) {
a7ef7878
RW
114 cancel_freezing(p);
115 continue;
116 }
a9b6f562 117 if (is_user_space(p)) {
11b2ce2b
RW
118 if (!freeze_user_space)
119 continue;
120
a9b6f562
RW
121 /* Freeze the task unless there is a vfork
122 * completion pending
02aaeb9b
RW
123 */
124 if (!p->vfork_done)
125 freeze_process(p);
02aaeb9b 126 } else {
11b2ce2b
RW
127 if (freeze_user_space)
128 continue;
129
130 freeze_process(p);
02aaeb9b 131 }
11b2ce2b 132 todo++;
1da177e4
LT
133 } while_each_thread(g, p);
134 read_unlock(&tasklist_lock);
135 yield(); /* Yield is okay here */
11b2ce2b 136 if (todo && time_after(jiffies, end_time))
6161b2ce 137 break;
11b2ce2b 138 } while (todo);
3e1d1d28 139
6161b2ce 140 if (todo) {
11b2ce2b
RW
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.
145 */
14b5b7cf 146 printk("\n");
11b2ce2b
RW
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);
6161b2ce 152 read_lock(&tasklist_lock);
02aaeb9b 153 do_each_thread(g, p) {
11b2ce2b
RW
154 if (is_user_space(p) == !freeze_user_space)
155 continue;
156
02aaeb9b 157 if (freezeable(p) && !frozen(p))
14b5b7cf 158 printk(KERN_ERR " %s\n", p->comm);
11b2ce2b 159
a7ef7878 160 cancel_freezing(p);
02aaeb9b 161 } while_each_thread(g, p);
6161b2ce 162 read_unlock(&tasklist_lock);
6161b2ce
PM
163 }
164
11b2ce2b
RW
165 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
14b5b7cf 188 printk("done.\n");
1da177e4
LT
189 BUG_ON(in_atomic());
190 return 0;
191}
192
a9b6f562 193static void thaw_tasks(int thaw_user_space)
1da177e4
LT
194{
195 struct task_struct *g, *p;
196
1da177e4 197 read_lock(&tasklist_lock);
a9b6f562
RW
198 do_each_thread(g, p) {
199 if (!freezeable(p))
200 continue;
ff39593a 201
a9b6f562
RW
202 if (is_user_space(p) == !thaw_user_space)
203 continue;
1da177e4 204
a9b6f562
RW
205 if (!thaw_process(p))
206 printk(KERN_WARNING " Strange, %s not stopped\n",
207 p->comm );
208 } while_each_thread(g, p);
1da177e4 209 read_unlock(&tasklist_lock);
a9b6f562
RW
210}
211
212void thaw_processes(void)
213{
214 printk("Restarting tasks ... ");
215 thaw_tasks(FREEZER_KERNEL_THREADS);
216 thaw_tasks(FREEZER_USER_SPACE);
1da177e4 217 schedule();
14b5b7cf 218 printk("done.\n");
1da177e4
LT
219}
220
221EXPORT_SYMBOL(refrigerator);
This page took 0.217711 seconds and 5 git commands to generate.