2 * drivers/power/process.c - Functions for starting/stopping processes on
5 * Originally from swsusp.
11 #include <linux/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
19 * Timeout for stopping processes
21 #define TIMEOUT (20 * HZ)
23 #define FREEZER_KERNEL_THREADS 0
24 #define FREEZER_USER_SPACE 1
26 static inline int freezeable(struct task_struct
* p
)
29 (p
->flags
& PF_NOFREEZE
) ||
30 (p
->exit_state
== EXIT_ZOMBIE
) ||
31 (p
->exit_state
== EXIT_DEAD
))
36 /* Refrigerator is place where frozen processes are stored :-). */
37 void refrigerator(void)
39 /* Hmm, should we be allowed to suspend when there are realtime
42 save
= current
->state
;
43 pr_debug("%s entered refrigerator\n", current
->comm
);
45 frozen_process(current
);
46 spin_lock_irq(¤t
->sighand
->siglock
);
47 recalc_sigpending(); /* We sent fake signal, clean it up */
48 spin_unlock_irq(¤t
->sighand
->siglock
);
50 while (frozen(current
)) {
51 current
->state
= TASK_UNINTERRUPTIBLE
;
54 pr_debug("%s left refrigerator\n", current
->comm
);
55 current
->state
= save
;
58 static inline void freeze_process(struct task_struct
*p
)
65 if (p
->state
== TASK_STOPPED
)
66 force_sig_specific(SIGSTOP
, 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
);
76 static void cancel_freezing(struct task_struct
*p
)
81 pr_debug(" clean up: %s\n", p
->comm
);
83 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
84 recalc_sigpending_tsk(p
);
85 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
89 static inline int is_user_space(struct task_struct
*p
)
91 return p
->mm
&& !(p
->flags
& PF_BORROWED_MM
);
94 static unsigned int try_to_freeze_tasks(int freeze_user_space
)
96 struct task_struct
*g
, *p
;
97 unsigned long end_time
;
100 end_time
= jiffies
+ TIMEOUT
;
103 read_lock(&tasklist_lock
);
104 do_each_thread(g
, p
) {
111 if (p
->state
== TASK_TRACED
&& frozen(p
->parent
)) {
115 if (is_user_space(p
)) {
116 if (!freeze_user_space
)
119 /* Freeze the task unless there is a vfork
125 if (freeze_user_space
)
131 } while_each_thread(g
, p
);
132 read_unlock(&tasklist_lock
);
133 yield(); /* Yield is okay here */
134 if (todo
&& time_after(jiffies
, end_time
))
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.
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" :
150 read_lock(&tasklist_lock
);
151 do_each_thread(g
, p
) {
152 if (is_user_space(p
) == !freeze_user_space
)
155 if (freezeable(p
) && !frozen(p
))
156 printk(KERN_ERR
" %s\n", p
->comm
);
159 } while_each_thread(g
, p
);
160 read_unlock(&tasklist_lock
);
167 * freeze_processes - tell processes to enter the refrigerator
169 * Returns 0 on success, or the number of processes that didn't freeze,
170 * although they were told to.
172 int freeze_processes(void)
174 unsigned int nr_unfrozen
;
176 printk("Stopping tasks ... ");
177 nr_unfrozen
= try_to_freeze_tasks(FREEZER_USER_SPACE
);
182 nr_unfrozen
= try_to_freeze_tasks(FREEZER_KERNEL_THREADS
);
191 static void thaw_tasks(int thaw_user_space
)
193 struct task_struct
*g
, *p
;
195 read_lock(&tasklist_lock
);
196 do_each_thread(g
, p
) {
200 if (is_user_space(p
) == !thaw_user_space
)
203 if (!thaw_process(p
))
204 printk(KERN_WARNING
" Strange, %s not stopped\n",
206 } while_each_thread(g
, p
);
207 read_unlock(&tasklist_lock
);
210 void thaw_processes(void)
212 printk("Restarting tasks ... ");
213 thaw_tasks(FREEZER_KERNEL_THREADS
);
214 thaw_tasks(FREEZER_USER_SPACE
);
219 EXPORT_SYMBOL(refrigerator
);