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) ||
35 /* Refrigerator is place where frozen processes are stored :-). */
36 void refrigerator(void)
38 /* Hmm, should we be allowed to suspend when there are realtime
41 save = current->state;
42 pr_debug("%s entered refrigerator\n", current->comm);
44 frozen_process(current);
45 spin_lock_irq(¤t->sighand->siglock);
46 recalc_sigpending(); /* We sent fake signal, clean it up */
47 spin_unlock_irq(¤t->sighand->siglock);
50 set_current_state(TASK_UNINTERRUPTIBLE);
55 pr_debug("%s left refrigerator\n", current->comm);
56 current->state = save;
59 static inline void freeze_process(struct task_struct *p)
66 if (p->state == TASK_STOPPED)
67 force_sig_specific(SIGSTOP, p);
70 spin_lock_irqsave(&p->sighand->siglock, flags);
71 signal_wake_up(p, p->state == TASK_STOPPED);
72 spin_unlock_irqrestore(&p->sighand->siglock, flags);
77 static void cancel_freezing(struct task_struct *p)
82 pr_debug(" clean up: %s\n", p->comm);
84 spin_lock_irqsave(&p->sighand->siglock, flags);
85 recalc_sigpending_tsk(p);
86 spin_unlock_irqrestore(&p->sighand->siglock, flags);
90 static inline int is_user_space(struct task_struct *p)
92 return p->mm && !(p->flags & PF_BORROWED_MM);
95 static unsigned int try_to_freeze_tasks(int freeze_user_space)
97 struct task_struct *g, *p;
98 unsigned long end_time;
101 end_time = jiffies + TIMEOUT;
104 read_lock(&tasklist_lock);
105 do_each_thread(g, p) {
112 if (p->state == TASK_TRACED && frozen(p->parent)) {
116 if (is_user_space(p)) {
117 if (!freeze_user_space)
120 /* Freeze the task unless there is a vfork
126 if (freeze_user_space)
132 } while_each_thread(g, p);
133 read_unlock(&tasklist_lock);
134 yield(); /* Yield is okay here */
135 if (todo && time_after(jiffies, end_time))
140 /* This does not unfreeze processes that are already frozen
141 * (we have slightly ugly calling convention in that respect,
142 * and caller must call thaw_processes() if something fails),
143 * but it cleans up leftover PF_FREEZE requests.
146 printk(KERN_ERR "Stopping %s timed out after %d seconds "
147 "(%d tasks refusing to freeze):\n",
148 freeze_user_space ? "user space processes" :
151 read_lock(&tasklist_lock);
152 do_each_thread(g, p) {
153 if (is_user_space(p) == !freeze_user_space)
156 if (freezeable(p) && !frozen(p))
157 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)
204 if (!thaw_process(p))
205 printk(KERN_WARNING " Strange, %s not stopped\n",
207 } while_each_thread(g, p);
208 read_unlock(&tasklist_lock);
211 void thaw_processes(void)
213 printk("Restarting tasks ... ");
214 thaw_tasks(FREEZER_KERNEL_THREADS);
215 thaw_tasks(FREEZER_USER_SPACE);
220 EXPORT_SYMBOL(refrigerator);