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
);
51 set_current_state(TASK_UNINTERRUPTIBLE
);
56 pr_debug("%s left refrigerator\n", current
->comm
);
57 current
->state
= save
;
60 static inline void freeze_process(struct task_struct
*p
)
67 if (p
->state
== TASK_STOPPED
)
68 force_sig_specific(SIGSTOP
, p
);
71 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
72 signal_wake_up(p
, p
->state
== TASK_STOPPED
);
73 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
78 static void cancel_freezing(struct task_struct
*p
)
83 pr_debug(" clean up: %s\n", p
->comm
);
85 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
86 recalc_sigpending_tsk(p
);
87 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
91 static inline int is_user_space(struct task_struct
*p
)
93 return p
->mm
&& !(p
->flags
& PF_BORROWED_MM
);
96 static unsigned int try_to_freeze_tasks(int freeze_user_space
)
98 struct task_struct
*g
, *p
;
99 unsigned long end_time
;
102 end_time
= jiffies
+ TIMEOUT
;
105 read_lock(&tasklist_lock
);
106 do_each_thread(g
, p
) {
113 if (p
->state
== TASK_TRACED
&& frozen(p
->parent
)) {
117 if (is_user_space(p
)) {
118 if (!freeze_user_space
)
121 /* Freeze the task unless there is a vfork
127 if (freeze_user_space
)
133 } while_each_thread(g
, p
);
134 read_unlock(&tasklist_lock
);
135 yield(); /* Yield is okay here */
136 if (todo
&& time_after(jiffies
, end_time
))
141 /* This does not unfreeze processes that are already frozen
142 * (we have slightly ugly calling convention in that respect,
143 * and caller must call thaw_processes() if something fails),
144 * but it cleans up leftover PF_FREEZE requests.
147 printk(KERN_ERR
"Stopping %s timed out after %d seconds "
148 "(%d tasks refusing to freeze):\n",
149 freeze_user_space
? "user space processes" :
152 read_lock(&tasklist_lock
);
153 do_each_thread(g
, p
) {
154 if (is_user_space(p
) == !freeze_user_space
)
157 if (freezeable(p
) && !frozen(p
))
158 printk(KERN_ERR
" %s\n", p
->comm
);
161 } while_each_thread(g
, p
);
162 read_unlock(&tasklist_lock
);
169 * freeze_processes - tell processes to enter the refrigerator
171 * Returns 0 on success, or the number of processes that didn't freeze,
172 * although they were told to.
174 int freeze_processes(void)
176 unsigned int nr_unfrozen
;
178 printk("Stopping tasks ... ");
179 nr_unfrozen
= try_to_freeze_tasks(FREEZER_USER_SPACE
);
184 nr_unfrozen
= try_to_freeze_tasks(FREEZER_KERNEL_THREADS
);
193 static void thaw_tasks(int thaw_user_space
)
195 struct task_struct
*g
, *p
;
197 read_lock(&tasklist_lock
);
198 do_each_thread(g
, p
) {
202 if (is_user_space(p
) == !thaw_user_space
)
205 if (!thaw_process(p
))
206 printk(KERN_WARNING
" Strange, %s not stopped\n",
208 } while_each_thread(g
, p
);
209 read_unlock(&tasklist_lock
);
212 void thaw_processes(void)
214 printk("Restarting tasks ... ");
215 thaw_tasks(FREEZER_KERNEL_THREADS
);
216 thaw_tasks(FREEZER_USER_SPACE
);
221 EXPORT_SYMBOL(refrigerator
);