]>
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 | ||
1da177e4 LT |
11 | #include <linux/interrupt.h> |
12 | #include <linux/suspend.h> | |
13 | #include <linux/module.h> | |
02aaeb9b | 14 | #include <linux/syscalls.h> |
7dfb7103 | 15 | #include <linux/freezer.h> |
1da177e4 LT |
16 | |
17 | /* | |
18 | * Timeout for stopping processes | |
19 | */ | |
02aaeb9b | 20 | #define TIMEOUT (20 * HZ) |
1da177e4 | 21 | |
a9b6f562 RW |
22 | #define FREEZER_KERNEL_THREADS 0 |
23 | #define FREEZER_USER_SPACE 1 | |
1da177e4 LT |
24 | |
25 | static inline int freezeable(struct task_struct * p) | |
26 | { | |
1065d130 | 27 | if ((p == current) || |
1da177e4 | 28 | (p->flags & PF_NOFREEZE) || |
1065d130 | 29 | (p->exit_state != 0)) |
1da177e4 LT |
30 | return 0; |
31 | return 1; | |
32 | } | |
33 | ||
88f18ba0 GS |
34 | /* |
35 | * freezing is complete, mark current process as frozen | |
36 | */ | |
37 | static inline void frozen_process(void) | |
38 | { | |
39 | if (!unlikely(current->flags & PF_NOFREEZE)) { | |
40 | current->flags |= PF_FROZEN; | |
41 | wmb(); | |
42 | } | |
0c1eecfb | 43 | clear_freeze_flag(current); |
88f18ba0 GS |
44 | } |
45 | ||
1da177e4 | 46 | /* Refrigerator is place where frozen processes are stored :-). */ |
3e1d1d28 | 47 | void refrigerator(void) |
1da177e4 LT |
48 | { |
49 | /* Hmm, should we be allowed to suspend when there are realtime | |
50 | processes around? */ | |
51 | long save; | |
33e1c288 RW |
52 | |
53 | task_lock(current); | |
54 | if (freezing(current)) { | |
88f18ba0 | 55 | frozen_process(); |
33e1c288 RW |
56 | task_unlock(current); |
57 | } else { | |
58 | task_unlock(current); | |
59 | return; | |
60 | } | |
1da177e4 | 61 | save = current->state; |
1da177e4 | 62 | pr_debug("%s entered refrigerator\n", current->comm); |
1da177e4 LT |
63 | |
64 | spin_lock_irq(¤t->sighand->siglock); | |
65 | recalc_sigpending(); /* We sent fake signal, clean it up */ | |
66 | spin_unlock_irq(¤t->sighand->siglock); | |
67 | ||
433ecb4a ON |
68 | for (;;) { |
69 | set_current_state(TASK_UNINTERRUPTIBLE); | |
70 | if (!frozen(current)) | |
71 | break; | |
1da177e4 | 72 | schedule(); |
2a23b5d1 | 73 | } |
1da177e4 | 74 | pr_debug("%s left refrigerator\n", current->comm); |
f4a3a7d6 | 75 | __set_current_state(save); |
1da177e4 LT |
76 | } |
77 | ||
0c1eecfb | 78 | static void freeze_task(struct task_struct *p) |
02aaeb9b RW |
79 | { |
80 | unsigned long flags; | |
81 | ||
82 | if (!freezing(p)) { | |
8a102eed RW |
83 | rmb(); |
84 | if (!frozen(p)) { | |
0c1eecfb | 85 | set_freeze_flag(p); |
8a102eed RW |
86 | if (p->state == TASK_STOPPED) |
87 | force_sig_specific(SIGSTOP, p); | |
8a102eed RW |
88 | spin_lock_irqsave(&p->sighand->siglock, flags); |
89 | signal_wake_up(p, p->state == TASK_STOPPED); | |
90 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | |
91 | } | |
02aaeb9b RW |
92 | } |
93 | } | |
94 | ||
a7ef7878 RW |
95 | static void cancel_freezing(struct task_struct *p) |
96 | { | |
97 | unsigned long flags; | |
98 | ||
99 | if (freezing(p)) { | |
100 | pr_debug(" clean up: %s\n", p->comm); | |
0c1eecfb | 101 | clear_freeze_flag(p); |
a7ef7878 | 102 | spin_lock_irqsave(&p->sighand->siglock, flags); |
7bb44ade | 103 | recalc_sigpending_and_wake(p); |
a7ef7878 RW |
104 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
105 | } | |
106 | } | |
107 | ||
11b2ce2b | 108 | static unsigned int try_to_freeze_tasks(int freeze_user_space) |
1da177e4 | 109 | { |
1da177e4 | 110 | struct task_struct *g, *p; |
11b2ce2b RW |
111 | unsigned long end_time; |
112 | unsigned int todo; | |
3e1d1d28 | 113 | |
11b2ce2b | 114 | end_time = jiffies + TIMEOUT; |
1da177e4 | 115 | do { |
11b2ce2b | 116 | todo = 0; |
1da177e4 LT |
117 | read_lock(&tasklist_lock); |
118 | do_each_thread(g, p) { | |
0c1eecfb | 119 | if (frozen(p) || !freezeable(p)) |
1da177e4 | 120 | continue; |
11b2ce2b | 121 | |
0c1eecfb RW |
122 | if (freeze_user_space) { |
123 | if (p->state == TASK_TRACED && | |
124 | frozen(p->parent)) { | |
125 | cancel_freezing(p); | |
126 | continue; | |
127 | } | |
128 | /* | |
129 | * Kernel threads should not have TIF_FREEZE set | |
130 | * at this point, so we must ensure that either | |
131 | * p->mm is not NULL *and* PF_BORROWED_MM is | |
132 | * unset, or TIF_FRREZE is left unset. | |
133 | * The task_lock() is necessary to prevent races | |
134 | * with exit_mm() or use_mm()/unuse_mm() from | |
135 | * occuring. | |
136 | */ | |
137 | task_lock(p); | |
138 | if (!p->mm || (p->flags & PF_BORROWED_MM)) { | |
139 | task_unlock(p); | |
140 | continue; | |
141 | } | |
142 | freeze_task(p); | |
143 | task_unlock(p); | |
144 | } else { | |
145 | freeze_task(p); | |
a7ef7878 | 146 | } |
ba96a0c8 RW |
147 | if (!freezer_should_skip(p)) |
148 | todo++; | |
1da177e4 LT |
149 | } while_each_thread(g, p); |
150 | read_unlock(&tasklist_lock); | |
151 | yield(); /* Yield is okay here */ | |
11b2ce2b | 152 | if (todo && time_after(jiffies, end_time)) |
6161b2ce | 153 | break; |
11b2ce2b | 154 | } while (todo); |
3e1d1d28 | 155 | |
6161b2ce | 156 | if (todo) { |
11b2ce2b RW |
157 | /* This does not unfreeze processes that are already frozen |
158 | * (we have slightly ugly calling convention in that respect, | |
159 | * and caller must call thaw_processes() if something fails), | |
160 | * but it cleans up leftover PF_FREEZE requests. | |
161 | */ | |
14b5b7cf | 162 | printk("\n"); |
0c1eecfb | 163 | printk(KERN_ERR "Freezing of %s timed out after %d seconds " |
11b2ce2b | 164 | "(%d tasks refusing to freeze):\n", |
0c1eecfb | 165 | freeze_user_space ? "user space " : "tasks ", |
11b2ce2b | 166 | TIMEOUT / HZ, todo); |
328616e3 | 167 | show_state(); |
6161b2ce | 168 | read_lock(&tasklist_lock); |
02aaeb9b | 169 | do_each_thread(g, p) { |
33e1c288 | 170 | task_lock(p); |
0c1eecfb | 171 | if (freezing(p) && !freezer_should_skip(p)) |
14b5b7cf | 172 | printk(KERN_ERR " %s\n", p->comm); |
a7ef7878 | 173 | cancel_freezing(p); |
33e1c288 | 174 | task_unlock(p); |
02aaeb9b | 175 | } while_each_thread(g, p); |
6161b2ce | 176 | read_unlock(&tasklist_lock); |
6161b2ce PM |
177 | } |
178 | ||
11b2ce2b RW |
179 | return todo; |
180 | } | |
181 | ||
182 | /** | |
183 | * freeze_processes - tell processes to enter the refrigerator | |
184 | * | |
185 | * Returns 0 on success, or the number of processes that didn't freeze, | |
186 | * although they were told to. | |
187 | */ | |
188 | int freeze_processes(void) | |
189 | { | |
190 | unsigned int nr_unfrozen; | |
191 | ||
192 | printk("Stopping tasks ... "); | |
193 | nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE); | |
194 | if (nr_unfrozen) | |
195 | return nr_unfrozen; | |
196 | ||
197 | sys_sync(); | |
198 | nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS); | |
199 | if (nr_unfrozen) | |
200 | return nr_unfrozen; | |
201 | ||
14b5b7cf | 202 | printk("done.\n"); |
1da177e4 LT |
203 | BUG_ON(in_atomic()); |
204 | return 0; | |
205 | } | |
206 | ||
a9b6f562 | 207 | static void thaw_tasks(int thaw_user_space) |
1da177e4 LT |
208 | { |
209 | struct task_struct *g, *p; | |
210 | ||
1da177e4 | 211 | read_lock(&tasklist_lock); |
a9b6f562 RW |
212 | do_each_thread(g, p) { |
213 | if (!freezeable(p)) | |
214 | continue; | |
ff39593a | 215 | |
0c1eecfb | 216 | if (!p->mm == thaw_user_space) |
a9b6f562 | 217 | continue; |
1da177e4 | 218 | |
ba96a0c8 | 219 | thaw_process(p); |
a9b6f562 | 220 | } while_each_thread(g, p); |
1da177e4 | 221 | read_unlock(&tasklist_lock); |
a9b6f562 RW |
222 | } |
223 | ||
224 | void thaw_processes(void) | |
225 | { | |
226 | printk("Restarting tasks ... "); | |
227 | thaw_tasks(FREEZER_KERNEL_THREADS); | |
228 | thaw_tasks(FREEZER_USER_SPACE); | |
1da177e4 | 229 | schedule(); |
14b5b7cf | 230 | printk("done.\n"); |
1da177e4 LT |
231 | } |
232 | ||
233 | EXPORT_SYMBOL(refrigerator); |