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 (is_user_space(p)) {
124 if (!freeze_user_space)
127 /* Freeze the task unless there is a vfork
133 if (freeze_user_space)
139 } while_each_thread(g, p);
140 read_unlock(&tasklist_lock);
141 yield(); /* Yield is okay here */
142 if (todo && time_after(jiffies, end_time))
147 /* This does not unfreeze processes that are already frozen
148 * (we have slightly ugly calling convention in that respect,
149 * and caller must call thaw_processes() if something fails),
150 * but it cleans up leftover PF_FREEZE requests.
153 printk(KERN_ERR "Stopping %s timed out after %d seconds "
154 "(%d tasks refusing to freeze):\n",
155 freeze_user_space ? "user space processes" :
158 read_lock(&tasklist_lock);
159 do_each_thread(g, p) {
160 if (is_user_space(p) == !freeze_user_space)
164 if (freezeable(p) && !frozen(p))
165 printk(KERN_ERR " %s\n", p->comm);
169 } while_each_thread(g, p);
170 read_unlock(&tasklist_lock);
177 * freeze_processes - tell processes to enter the refrigerator
179 * Returns 0 on success, or the number of processes that didn't freeze,
180 * although they were told to.
182 int freeze_processes(void)
184 unsigned int nr_unfrozen;
186 printk("Stopping tasks ... ");
187 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
192 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
201 static void thaw_tasks(int thaw_user_space)
203 struct task_struct *g, *p;
205 read_lock(&tasklist_lock);
206 do_each_thread(g, p) {
210 if (is_user_space(p) == !thaw_user_space)
213 if (!thaw_process(p))
214 printk(KERN_WARNING " Strange, %s not stopped\n",
216 } while_each_thread(g, p);
217 read_unlock(&tasklist_lock);
220 void thaw_processes(void)
222 printk("Restarting tasks ... ");
223 thaw_tasks(FREEZER_KERNEL_THREADS);
224 thaw_tasks(FREEZER_USER_SPACE);
229 EXPORT_SYMBOL(refrigerator);