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) ||
34 /* Refrigerator is place where frozen processes are stored :-). */
35 void refrigerator(void)
37 /* Hmm, should we be allowed to suspend when there are realtime
42 if (freezing(current)) {
43 frozen_process(current);
49 save = current->state;
50 pr_debug("%s entered refrigerator\n", current->comm);
52 spin_lock_irq(¤t->sighand->siglock);
53 recalc_sigpending(); /* We sent fake signal, clean it up */
54 spin_unlock_irq(¤t->sighand->siglock);
57 set_current_state(TASK_UNINTERRUPTIBLE);
62 pr_debug("%s left refrigerator\n", current->comm);
63 current->state = save;
66 static inline void freeze_process(struct task_struct *p)
73 if (p->state == TASK_STOPPED)
74 force_sig_specific(SIGSTOP, p);
77 spin_lock_irqsave(&p->sighand->siglock, flags);
78 signal_wake_up(p, p->state == TASK_STOPPED);
79 spin_unlock_irqrestore(&p->sighand->siglock, flags);
84 static void cancel_freezing(struct task_struct *p)
89 pr_debug(" clean up: %s\n", p->comm);
91 spin_lock_irqsave(&p->sighand->siglock, flags);
92 recalc_sigpending_tsk(p);
93 spin_unlock_irqrestore(&p->sighand->siglock, flags);
97 static inline int is_user_space(struct task_struct *p)
99 return p->mm && !(p->flags & PF_BORROWED_MM);
102 static unsigned int try_to_freeze_tasks(int freeze_user_space)
104 struct task_struct *g, *p;
105 unsigned long end_time;
108 end_time = jiffies + TIMEOUT;
111 read_lock(&tasklist_lock);
112 do_each_thread(g, p) {
119 if (p->state == TASK_TRACED && frozen(p->parent)) {
123 if (freeze_user_space && !is_user_space(p))
127 if (!freezer_should_skip(p))
129 } while_each_thread(g, p);
130 read_unlock(&tasklist_lock);
131 yield(); /* Yield is okay here */
132 if (todo && time_after(jiffies, end_time))
137 /* This does not unfreeze processes that are already frozen
138 * (we have slightly ugly calling convention in that respect,
139 * and caller must call thaw_processes() if something fails),
140 * but it cleans up leftover PF_FREEZE requests.
143 printk(KERN_ERR "Stopping %s timed out after %d seconds "
144 "(%d tasks refusing to freeze):\n",
145 freeze_user_space ? "user space processes" :
148 read_lock(&tasklist_lock);
149 do_each_thread(g, p) {
150 if (freeze_user_space && !is_user_space(p))
154 if (freezeable(p) && !frozen(p) &&
155 !freezer_should_skip(p))
156 printk(KERN_ERR " %s\n", p->comm);
160 } while_each_thread(g, p);
161 read_unlock(&tasklist_lock);
168 * freeze_processes - tell processes to enter the refrigerator
170 * Returns 0 on success, or the number of processes that didn't freeze,
171 * although they were told to.
173 int freeze_processes(void)
175 unsigned int nr_unfrozen;
177 printk("Stopping tasks ... ");
178 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
183 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
192 static void thaw_tasks(int thaw_user_space)
194 struct task_struct *g, *p;
196 read_lock(&tasklist_lock);
197 do_each_thread(g, p) {
201 if (is_user_space(p) == !thaw_user_space)
205 } while_each_thread(g, p);
206 read_unlock(&tasklist_lock);
209 void thaw_processes(void)
211 printk("Restarting tasks ... ");
212 thaw_tasks(FREEZER_KERNEL_THREADS);
213 thaw_tasks(FREEZER_USER_SPACE);
218 EXPORT_SYMBOL(refrigerator);