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 #define FREEZER_KERNEL_THREADS 0
23 #define FREEZER_USER_SPACE 1
25 static inline int freezeable(struct task_struct
* p
)
28 (p
->flags
& PF_NOFREEZE
) ||
35 * freezing is complete, mark current process as frozen
37 static inline void frozen_process(void)
39 if (!unlikely(current
->flags
& PF_NOFREEZE
)) {
40 current
->flags
|= PF_FROZEN
;
43 clear_tsk_thread_flag(current
, TIF_FREEZE
);
46 /* Refrigerator is place where frozen processes are stored :-). */
47 void refrigerator(void)
49 /* Hmm, should we be allowed to suspend when there are realtime
54 if (freezing(current
)) {
61 save
= current
->state
;
62 pr_debug("%s entered refrigerator\n", current
->comm
);
64 spin_lock_irq(¤t
->sighand
->siglock
);
65 recalc_sigpending(); /* We sent fake signal, clean it up */
66 spin_unlock_irq(¤t
->sighand
->siglock
);
69 set_current_state(TASK_UNINTERRUPTIBLE
);
74 pr_debug("%s left refrigerator\n", current
->comm
);
75 current
->state
= save
;
78 static inline void freeze_process(struct task_struct
*p
)
85 if (p
->state
== TASK_STOPPED
)
86 force_sig_specific(SIGSTOP
, p
);
89 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
90 signal_wake_up(p
, p
->state
== TASK_STOPPED
);
91 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
96 static void cancel_freezing(struct task_struct
*p
)
101 pr_debug(" clean up: %s\n", p
->comm
);
103 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
104 recalc_sigpending_and_wake(p
);
105 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
109 static inline int is_user_space(struct task_struct
*p
)
111 return p
->mm
&& !(p
->flags
& PF_BORROWED_MM
);
114 static unsigned int try_to_freeze_tasks(int freeze_user_space
)
116 struct task_struct
*g
, *p
;
117 unsigned long end_time
;
120 end_time
= jiffies
+ TIMEOUT
;
123 read_lock(&tasklist_lock
);
124 do_each_thread(g
, p
) {
131 if (p
->state
== TASK_TRACED
&& frozen(p
->parent
)) {
135 if (freeze_user_space
&& !is_user_space(p
))
139 if (!freezer_should_skip(p
))
141 } while_each_thread(g
, p
);
142 read_unlock(&tasklist_lock
);
143 yield(); /* Yield is okay here */
144 if (todo
&& time_after(jiffies
, end_time
))
149 /* This does not unfreeze processes that are already frozen
150 * (we have slightly ugly calling convention in that respect,
151 * and caller must call thaw_processes() if something fails),
152 * but it cleans up leftover PF_FREEZE requests.
155 printk(KERN_ERR
"Stopping %s timed out after %d seconds "
156 "(%d tasks refusing to freeze):\n",
157 freeze_user_space
? "user space processes" :
160 read_lock(&tasklist_lock
);
161 do_each_thread(g
, p
) {
162 if (freeze_user_space
&& !is_user_space(p
))
166 if (freezeable(p
) && !frozen(p
) &&
167 !freezer_should_skip(p
))
168 printk(KERN_ERR
" %s\n", p
->comm
);
172 } while_each_thread(g
, p
);
173 read_unlock(&tasklist_lock
);
180 * freeze_processes - tell processes to enter the refrigerator
182 * Returns 0 on success, or the number of processes that didn't freeze,
183 * although they were told to.
185 int freeze_processes(void)
187 unsigned int nr_unfrozen
;
189 printk("Stopping tasks ... ");
190 nr_unfrozen
= try_to_freeze_tasks(FREEZER_USER_SPACE
);
195 nr_unfrozen
= try_to_freeze_tasks(FREEZER_KERNEL_THREADS
);
204 static void thaw_tasks(int thaw_user_space
)
206 struct task_struct
*g
, *p
;
208 read_lock(&tasklist_lock
);
209 do_each_thread(g
, p
) {
213 if (is_user_space(p
) == !thaw_user_space
)
217 } while_each_thread(g
, p
);
218 read_unlock(&tasklist_lock
);
221 void thaw_processes(void)
223 printk("Restarting tasks ... ");
224 thaw_tasks(FREEZER_KERNEL_THREADS
);
225 thaw_tasks(FREEZER_USER_SPACE
);
230 EXPORT_SYMBOL(refrigerator
);