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);
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);
49 while (frozen(current)) {
50 current->state = TASK_UNINTERRUPTIBLE;
53 pr_debug("%s left refrigerator\n", current->comm);
54 current->state = save;
57 static inline void freeze_process(struct task_struct *p)
63 spin_lock_irqsave(&p->sighand->siglock, flags);
65 spin_unlock_irqrestore(&p->sighand->siglock, flags);
69 static void cancel_freezing(struct task_struct *p)
74 pr_debug(" clean up: %s\n", p->comm);
76 spin_lock_irqsave(&p->sighand->siglock, flags);
77 recalc_sigpending_tsk(p);
78 spin_unlock_irqrestore(&p->sighand->siglock, flags);
82 /* 0 = success, else # of processes that we failed to stop */
83 int freeze_processes(void)
85 int todo, nr_user, user_frozen;
86 unsigned long start_time;
87 struct task_struct *g, *p;
89 printk("Stopping tasks... ");
94 read_lock(&tasklist_lock);
95 do_each_thread(g, p) {
100 if (p->state == TASK_TRACED &&
101 (frozen(p->parent) ||
102 p->parent->state == TASK_STOPPED)) {
106 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
107 /* The task is a user-space one.
108 * Freeze it unless there's a vfork completion
115 /* Freeze only if the user space is frozen */
120 } while_each_thread(g, p);
121 read_unlock(&tasklist_lock);
123 if (!user_frozen && !nr_user) {
125 start_time = jiffies;
127 user_frozen = !nr_user;
128 yield(); /* Yield is okay here */
129 if (todo && time_after(jiffies, start_time + TIMEOUT))
133 /* This does not unfreeze processes that are already frozen
134 * (we have slightly ugly calling convention in that respect,
135 * and caller must call thaw_processes() if something fails),
136 * but it cleans up leftover PF_FREEZE requests.
140 printk(KERN_ERR "Stopping tasks timed out "
141 "after %d seconds (%d tasks remaining):\n",
143 read_lock(&tasklist_lock);
144 do_each_thread(g, p) {
145 if (freezeable(p) && !frozen(p))
146 printk(KERN_ERR " %s\n", p->comm);
148 } while_each_thread(g, p);
149 read_unlock(&tasklist_lock);
158 void thaw_some_processes(int all)
160 struct task_struct *g, *p;
161 int pass = 0; /* Pass 0 = Kernel space, 1 = Userspace */
163 printk("Restarting tasks... ");
164 read_lock(&tasklist_lock);
166 do_each_thread(g, p) {
168 * is_user = 0 if kernel thread or borrowed mm,
171 int is_user = !!(p->mm && !(p->flags & PF_BORROWED_MM));
172 if (!freezeable(p) || (is_user != pass))
174 if (!thaw_process(p))
176 "Strange, %s not stopped\n", p->comm);
177 } while_each_thread(g, p);
180 } while (pass < 2 && all);
182 read_unlock(&tasklist_lock);
187 EXPORT_SYMBOL(refrigerator);