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) ||
35 * freezing is complete, mark current process as frozen
37 static inline void frozen_process(void)
39 if (!unlikely(current->flags & PF_NOFREEZE)) {
40 current->flags |= PF_FROZEN;
43 clear_freeze_flag(current);
46 /* Refrigerator is place where frozen processes are stored :-). */
47 void refrigerator(void)
49 /* Hmm, should we be allowed to suspend when there are realtime
54 if (freezing(current)) {
61 save = current->state;
62 pr_debug("%s entered refrigerator\n", current->comm);
64 spin_lock_irq(¤t->sighand->siglock);
65 recalc_sigpending(); /* We sent fake signal, clean it up */
66 spin_unlock_irq(¤t->sighand->siglock);
69 set_current_state(TASK_UNINTERRUPTIBLE);
74 pr_debug("%s left refrigerator\n", current->comm);
75 __set_current_state(save);
78 static void freeze_task(struct task_struct *p)
86 if (p->state == TASK_STOPPED)
87 force_sig_specific(SIGSTOP, p);
88 spin_lock_irqsave(&p->sighand->siglock, flags);
89 signal_wake_up(p, p->state == TASK_STOPPED);
90 spin_unlock_irqrestore(&p->sighand->siglock, flags);
95 static void cancel_freezing(struct task_struct *p)
100 pr_debug(" clean up: %s\n", p->comm);
101 clear_freeze_flag(p);
102 spin_lock_irqsave(&p->sighand->siglock, flags);
103 recalc_sigpending_and_wake(p);
104 spin_unlock_irqrestore(&p->sighand->siglock, flags);
108 static int try_to_freeze_tasks(int freeze_user_space)
110 struct task_struct *g, *p;
111 unsigned long end_time;
114 end_time = jiffies + TIMEOUT;
117 read_lock(&tasklist_lock);
118 do_each_thread(g, p) {
119 if (frozen(p) || !freezeable(p))
122 if (freeze_user_space) {
123 if (p->state == TASK_TRACED &&
129 * Kernel threads should not have TIF_FREEZE set
130 * at this point, so we must ensure that either
131 * p->mm is not NULL *and* PF_BORROWED_MM is
132 * unset, or TIF_FRREZE is left unset.
133 * The task_lock() is necessary to prevent races
134 * with exit_mm() or use_mm()/unuse_mm() from
138 if (!p->mm || (p->flags & PF_BORROWED_MM)) {
147 if (!freezer_should_skip(p))
149 } while_each_thread(g, p);
150 read_unlock(&tasklist_lock);
151 yield(); /* Yield is okay here */
152 if (time_after(jiffies, end_time))
157 /* This does not unfreeze processes that are already frozen
158 * (we have slightly ugly calling convention in that respect,
159 * and caller must call thaw_processes() if something fails),
160 * but it cleans up leftover PF_FREEZE requests.
163 printk(KERN_ERR "Freezing of %s timed out after %d seconds "
164 "(%d tasks refusing to freeze):\n",
165 freeze_user_space ? "user space " : "tasks ",
168 read_lock(&tasklist_lock);
169 do_each_thread(g, p) {
171 if (freezing(p) && !freezer_should_skip(p))
172 printk(KERN_ERR " %s\n", p->comm);
175 } while_each_thread(g, p);
176 read_unlock(&tasklist_lock);
179 return todo ? -EBUSY : 0;
183 * freeze_processes - tell processes to enter the refrigerator
185 int freeze_processes(void)
189 printk("Stopping tasks ... ");
190 error = try_to_freeze_tasks(FREEZER_USER_SPACE);
195 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
204 static void thaw_tasks(int thaw_user_space)
206 struct task_struct *g, *p;
208 read_lock(&tasklist_lock);
209 do_each_thread(g, p) {
213 if (!p->mm == thaw_user_space)
217 } while_each_thread(g, p);
218 read_unlock(&tasklist_lock);
221 void thaw_processes(void)
223 printk("Restarting tasks ... ");
224 thaw_tasks(FREEZER_KERNEL_THREADS);
225 thaw_tasks(FREEZER_USER_SPACE);
230 EXPORT_SYMBOL(refrigerator);