Commit | Line | Data |
---|---|---|
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 | |
26 | static 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 | 37 | void 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(¤t->sighand->siglock); |
47 | recalc_sigpending(); /* We sent fake signal, clean it up */ | |
48 | spin_unlock_irq(¤t->sighand->siglock); | |
49 | ||
2a23b5d1 PM |
50 | while (frozen(current)) { |
51 | current->state = TASK_UNINTERRUPTIBLE; | |
1da177e4 | 52 | schedule(); |
2a23b5d1 | 53 | } |
1da177e4 LT |
54 | pr_debug("%s left refrigerator\n", current->comm); |
55 | current->state = save; | |
56 | } | |
57 | ||
02aaeb9b RW |
58 | static inline void freeze_process(struct task_struct *p) |
59 | { | |
60 | unsigned long flags; | |
61 | ||
62 | if (!freezing(p)) { | |
8a102eed RW |
63 | rmb(); |
64 | if (!frozen(p)) { | |
65 | if (p->state == TASK_STOPPED) | |
66 | force_sig_specific(SIGSTOP, p); | |
3df494a3 | 67 | |
8a102eed RW |
68 | freeze(p); |
69 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
70 | signal_wake_up(p, p->state == TASK_STOPPED); | |
71 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | |
72 | } | |
02aaeb9b RW |
73 | } |
74 | } | |
75 | ||
a7ef7878 RW |
76 | static void cancel_freezing(struct task_struct *p) |
77 | { | |
78 | unsigned long flags; | |
79 | ||
80 | if (freezing(p)) { | |
81 | pr_debug(" clean up: %s\n", p->comm); | |
82 | do_not_freeze(p); | |
83 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
84 | recalc_sigpending_tsk(p); | |
85 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | |
86 | } | |
87 | } | |
88 | ||
a9b6f562 RW |
89 | static inline int is_user_space(struct task_struct *p) |
90 | { | |
91 | return p->mm && !(p->flags & PF_BORROWED_MM); | |
92 | } | |
93 | ||
11b2ce2b | 94 | static unsigned int try_to_freeze_tasks(int freeze_user_space) |
1da177e4 | 95 | { |
1da177e4 | 96 | struct task_struct *g, *p; |
11b2ce2b RW |
97 | unsigned long end_time; |
98 | unsigned int todo; | |
3e1d1d28 | 99 | |
11b2ce2b | 100 | end_time = jiffies + TIMEOUT; |
1da177e4 | 101 | do { |
11b2ce2b | 102 | todo = 0; |
1da177e4 LT |
103 | read_lock(&tasklist_lock); |
104 | do_each_thread(g, p) { | |
1da177e4 LT |
105 | if (!freezeable(p)) |
106 | continue; | |
11b2ce2b | 107 | |
1322ad41 | 108 | if (frozen(p)) |
1da177e4 | 109 | continue; |
11b2ce2b | 110 | |
3df494a3 | 111 | if (p->state == TASK_TRACED && frozen(p->parent)) { |
a7ef7878 RW |
112 | cancel_freezing(p); |
113 | continue; | |
114 | } | |
a9b6f562 | 115 | if (is_user_space(p)) { |
11b2ce2b RW |
116 | if (!freeze_user_space) |
117 | continue; | |
118 | ||
a9b6f562 RW |
119 | /* Freeze the task unless there is a vfork |
120 | * completion pending | |
02aaeb9b RW |
121 | */ |
122 | if (!p->vfork_done) | |
123 | freeze_process(p); | |
02aaeb9b | 124 | } else { |
11b2ce2b RW |
125 | if (freeze_user_space) |
126 | continue; | |
127 | ||
128 | freeze_process(p); | |
02aaeb9b | 129 | } |
11b2ce2b | 130 | todo++; |
1da177e4 LT |
131 | } while_each_thread(g, p); |
132 | read_unlock(&tasklist_lock); | |
133 | yield(); /* Yield is okay here */ | |
11b2ce2b | 134 | if (todo && time_after(jiffies, end_time)) |
6161b2ce | 135 | break; |
11b2ce2b | 136 | } while (todo); |
3e1d1d28 | 137 | |
6161b2ce | 138 | if (todo) { |
11b2ce2b RW |
139 | /* This does not unfreeze processes that are already frozen |
140 | * (we have slightly ugly calling convention in that respect, | |
141 | * and caller must call thaw_processes() if something fails), | |
142 | * but it cleans up leftover PF_FREEZE requests. | |
143 | */ | |
14b5b7cf | 144 | printk("\n"); |
11b2ce2b RW |
145 | printk(KERN_ERR "Stopping %s timed out after %d seconds " |
146 | "(%d tasks refusing to freeze):\n", | |
147 | freeze_user_space ? "user space processes" : | |
148 | "kernel threads", | |
149 | TIMEOUT / HZ, todo); | |
6161b2ce | 150 | read_lock(&tasklist_lock); |
02aaeb9b | 151 | do_each_thread(g, p) { |
11b2ce2b RW |
152 | if (is_user_space(p) == !freeze_user_space) |
153 | continue; | |
154 | ||
02aaeb9b | 155 | if (freezeable(p) && !frozen(p)) |
14b5b7cf | 156 | printk(KERN_ERR " %s\n", p->comm); |
11b2ce2b | 157 | |
a7ef7878 | 158 | cancel_freezing(p); |
02aaeb9b | 159 | } while_each_thread(g, p); |
6161b2ce | 160 | read_unlock(&tasklist_lock); |
6161b2ce PM |
161 | } |
162 | ||
11b2ce2b RW |
163 | return todo; |
164 | } | |
165 | ||
166 | /** | |
167 | * freeze_processes - tell processes to enter the refrigerator | |
168 | * | |
169 | * Returns 0 on success, or the number of processes that didn't freeze, | |
170 | * although they were told to. | |
171 | */ | |
172 | int freeze_processes(void) | |
173 | { | |
174 | unsigned int nr_unfrozen; | |
175 | ||
176 | printk("Stopping tasks ... "); | |
177 | nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE); | |
178 | if (nr_unfrozen) | |
179 | return nr_unfrozen; | |
180 | ||
181 | sys_sync(); | |
182 | nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS); | |
183 | if (nr_unfrozen) | |
184 | return nr_unfrozen; | |
185 | ||
14b5b7cf | 186 | printk("done.\n"); |
1da177e4 LT |
187 | BUG_ON(in_atomic()); |
188 | return 0; | |
189 | } | |
190 | ||
a9b6f562 | 191 | static void thaw_tasks(int thaw_user_space) |
1da177e4 LT |
192 | { |
193 | struct task_struct *g, *p; | |
194 | ||
1da177e4 | 195 | read_lock(&tasklist_lock); |
a9b6f562 RW |
196 | do_each_thread(g, p) { |
197 | if (!freezeable(p)) | |
198 | continue; | |
ff39593a | 199 | |
a9b6f562 RW |
200 | if (is_user_space(p) == !thaw_user_space) |
201 | continue; | |
1da177e4 | 202 | |
a9b6f562 RW |
203 | if (!thaw_process(p)) |
204 | printk(KERN_WARNING " Strange, %s not stopped\n", | |
205 | p->comm ); | |
206 | } while_each_thread(g, p); | |
1da177e4 | 207 | read_unlock(&tasklist_lock); |
a9b6f562 RW |
208 | } |
209 | ||
210 | void thaw_processes(void) | |
211 | { | |
212 | printk("Restarting tasks ... "); | |
213 | thaw_tasks(FREEZER_KERNEL_THREADS); | |
214 | thaw_tasks(FREEZER_USER_SPACE); | |
1da177e4 | 215 | schedule(); |
14b5b7cf | 216 | printk("done.\n"); |
1da177e4 LT |
217 | } |
218 | ||
219 | EXPORT_SYMBOL(refrigerator); |