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)
24 static inline int freezeable(struct task_struct * p)
27 (p->flags & PF_NOFREEZE) ||
28 (p->exit_state == EXIT_ZOMBIE) ||
29 (p->exit_state == EXIT_DEAD) ||
30 (p->state == TASK_STOPPED))
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);
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);
50 while (frozen(current)) {
51 current->state = TASK_UNINTERRUPTIBLE;
54 pr_debug("%s left refrigerator\n", current->comm);
55 current->state = save;
58 static inline void freeze_process(struct task_struct *p)
64 spin_lock_irqsave(&p->sighand->siglock, flags);
66 spin_unlock_irqrestore(&p->sighand->siglock, flags);
70 static void cancel_freezing(struct task_struct *p)
75 pr_debug(" clean up: %s\n", p->comm);
77 spin_lock_irqsave(&p->sighand->siglock, flags);
78 recalc_sigpending_tsk(p);
79 spin_unlock_irqrestore(&p->sighand->siglock, flags);
83 /* 0 = success, else # of processes that we failed to stop */
84 int freeze_processes(void)
86 int todo, nr_user, user_frozen;
87 unsigned long start_time;
88 struct task_struct *g, *p;
90 printk( "Stopping tasks: " );
95 read_lock(&tasklist_lock);
96 do_each_thread(g, p) {
101 if (p->state == TASK_TRACED && frozen(p->parent)) {
105 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
106 /* The task is a user-space one.
107 * Freeze it unless there's a vfork completion
114 /* Freeze only if the user space is frozen */
119 } while_each_thread(g, p);
120 read_unlock(&tasklist_lock);
122 if (!user_frozen && !nr_user) {
124 start_time = jiffies;
126 user_frozen = !nr_user;
127 yield(); /* Yield is okay here */
128 if (todo && time_after(jiffies, start_time + TIMEOUT))
132 /* This does not unfreeze processes that are already frozen
133 * (we have slightly ugly calling convention in that respect,
134 * and caller must call thaw_processes() if something fails),
135 * but it cleans up leftover PF_FREEZE requests.
139 printk(KERN_ERR " stopping tasks timed out "
140 "after %d seconds (%d tasks remaining):\n",
142 read_lock(&tasklist_lock);
143 do_each_thread(g, p) {
144 if (freezeable(p) && !frozen(p))
145 printk(KERN_ERR " %s\n", p->comm);
147 } while_each_thread(g, p);
148 read_unlock(&tasklist_lock);
157 void thaw_processes(void)
159 struct task_struct *g, *p;
161 printk( "Restarting tasks..." );
162 read_lock(&tasklist_lock);
163 do_each_thread(g, p) {
166 if (!thaw_process(p))
167 printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
168 } while_each_thread(g, p);
170 read_unlock(&tasklist_lock);
175 EXPORT_SYMBOL(refrigerator);