2 * drivers/power/process.c - Functions for starting/stopping processes on
5 * Originally from swsusp.
11 #include <linux/interrupt.h>
12 #include <linux/suspend.h>
13 #include <linux/module.h>
14 #include <linux/syscalls.h>
15 #include <linux/freezer.h>
18 * Timeout for stopping processes
20 #define TIMEOUT (20 * HZ)
22 static inline int freezeable(struct task_struct
* p
)
25 (p
->flags
& PF_NOFREEZE
) ||
32 * freezing is complete, mark current process as frozen
34 static inline void frozen_process(void)
36 if (!unlikely(current
->flags
& PF_NOFREEZE
)) {
37 current
->flags
|= PF_FROZEN
;
40 clear_freeze_flag(current
);
43 /* Refrigerator is place where frozen processes are stored :-). */
44 void refrigerator(void)
46 /* Hmm, should we be allowed to suspend when there are realtime
51 if (freezing(current
)) {
58 save
= current
->state
;
59 pr_debug("%s entered refrigerator\n", current
->comm
);
61 spin_lock_irq(¤t
->sighand
->siglock
);
62 recalc_sigpending(); /* We sent fake signal, clean it up */
63 spin_unlock_irq(¤t
->sighand
->siglock
);
66 set_current_state(TASK_UNINTERRUPTIBLE
);
71 pr_debug("%s left refrigerator\n", current
->comm
);
72 __set_current_state(save
);
75 static void fake_signal_wake_up(struct task_struct
*p
)
79 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
81 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
84 static inline bool should_send_signal(struct task_struct
*p
)
86 return !(p
->flags
& PF_FREEZER_NOSIG
);
90 * freeze_task - send a freeze request to given task
91 * @p: task to send the request to
92 * @sig_only: if set, the request will only be sent if the task has the
93 * PF_FREEZER_NOSIG flag unset
94 * Return value: 'false', if @sig_only is set and the task has
95 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
97 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
98 * either sending a fake signal to it or waking it up, depending on whether
99 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
100 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
101 * TIF_FREEZE flag will not be set.
103 static bool freeze_task(struct task_struct
*p
, bool sig_only
)
106 * We first check if the task is freezing and next if it has already
107 * been frozen to avoid the race with frozen_process() which first marks
108 * the task as frozen and next clears its TIF_FREEZE.
115 if (!sig_only
|| should_send_signal(p
))
121 if (should_send_signal(p
)) {
122 if (!signal_pending(p
))
123 fake_signal_wake_up(p
);
124 } else if (sig_only
) {
127 wake_up_state(p
, TASK_INTERRUPTIBLE
);
133 static void cancel_freezing(struct task_struct
*p
)
138 pr_debug(" clean up: %s\n", p
->comm
);
139 clear_freeze_flag(p
);
140 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
141 recalc_sigpending_and_wake(p
);
142 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
146 static int try_to_freeze_tasks(bool sig_only
)
148 struct task_struct
*g
, *p
;
149 unsigned long end_time
;
151 struct timeval start
, end
;
153 unsigned int elapsed_csecs
;
155 do_gettimeofday(&start
);
157 end_time
= jiffies
+ TIMEOUT
;
160 read_lock(&tasklist_lock
);
161 do_each_thread(g
, p
) {
162 if (frozen(p
) || !freezeable(p
))
165 if (!freeze_task(p
, sig_only
))
169 * Now that we've done set_freeze_flag, don't
170 * perturb a task in TASK_STOPPED or TASK_TRACED.
171 * It is "frozen enough". If the task does wake
172 * up, it will immediately call try_to_freeze.
174 if (!task_is_stopped_or_traced(p
) &&
175 !freezer_should_skip(p
))
177 } while_each_thread(g
, p
);
178 read_unlock(&tasklist_lock
);
179 yield(); /* Yield is okay here */
180 if (time_after(jiffies
, end_time
))
184 do_gettimeofday(&end
);
185 elapsed_csecs64
= timeval_to_ns(&end
) - timeval_to_ns(&start
);
186 do_div(elapsed_csecs64
, NSEC_PER_SEC
/ 100);
187 elapsed_csecs
= elapsed_csecs64
;
190 /* This does not unfreeze processes that are already frozen
191 * (we have slightly ugly calling convention in that respect,
192 * and caller must call thaw_processes() if something fails),
193 * but it cleans up leftover PF_FREEZE requests.
196 printk(KERN_ERR
"Freezing of tasks failed after %d.%02d seconds "
197 "(%d tasks refusing to freeze):\n",
198 elapsed_csecs
/ 100, elapsed_csecs
% 100, todo
);
200 read_lock(&tasklist_lock
);
201 do_each_thread(g
, p
) {
203 if (freezing(p
) && !freezer_should_skip(p
))
204 printk(KERN_ERR
" %s\n", p
->comm
);
207 } while_each_thread(g
, p
);
208 read_unlock(&tasklist_lock
);
210 printk("(elapsed %d.%02d seconds) ", elapsed_csecs
/ 100,
211 elapsed_csecs
% 100);
214 return todo
? -EBUSY
: 0;
218 * freeze_processes - tell processes to enter the refrigerator
220 int freeze_processes(void)
224 printk("Freezing user space processes ... ");
225 error
= try_to_freeze_tasks(true);
230 printk("Freezing remaining freezable tasks ... ");
231 error
= try_to_freeze_tasks(false);
241 static void thaw_tasks(bool nosig_only
)
243 struct task_struct
*g
, *p
;
245 read_lock(&tasklist_lock
);
246 do_each_thread(g
, p
) {
250 if (nosig_only
&& should_send_signal(p
))
254 } while_each_thread(g
, p
);
255 read_unlock(&tasklist_lock
);
258 void thaw_processes(void)
260 printk("Restarting tasks ... ");
267 EXPORT_SYMBOL(refrigerator
);