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) ||
30 (p->exit_state == EXIT_ZOMBIE) ||
31 (p->exit_state == EXIT_DEAD) ||
32 (p->state == TASK_STOPPED))
37 /* Refrigerator is place where frozen processes are stored :-). */
38 void refrigerator(void)
40 /* Hmm, should we be allowed to suspend when there are realtime
43 save = current->state;
44 pr_debug("%s entered refrigerator\n", current->comm);
46 frozen_process(current);
47 spin_lock_irq(¤t->sighand->siglock);
48 recalc_sigpending(); /* We sent fake signal, clean it up */
49 spin_unlock_irq(¤t->sighand->siglock);
51 while (frozen(current)) {
52 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)
65 spin_lock_irqsave(&p->sighand->siglock, flags);
67 spin_unlock_irqrestore(&p->sighand->siglock, flags);
71 static void cancel_freezing(struct task_struct *p)
76 pr_debug(" clean up: %s\n", p->comm);
78 spin_lock_irqsave(&p->sighand->siglock, flags);
79 recalc_sigpending_tsk(p);
80 spin_unlock_irqrestore(&p->sighand->siglock, flags);
84 static inline int is_user_space(struct task_struct *p)
86 return p->mm && !(p->flags & PF_BORROWED_MM);
89 /* 0 = success, else # of processes that we failed to stop */
90 int freeze_processes(void)
92 int todo, nr_user, user_frozen;
93 unsigned long start_time;
94 struct task_struct *g, *p;
96 printk("Stopping tasks... ");
101 read_lock(&tasklist_lock);
102 do_each_thread(g, p) {
107 if (p->state == TASK_TRACED &&
108 (frozen(p->parent) ||
109 p->parent->state == TASK_STOPPED)) {
113 if (is_user_space(p)) {
114 /* Freeze the task unless there is a vfork
121 /* Freeze only if the user space is frozen */
126 } while_each_thread(g, p);
127 read_unlock(&tasklist_lock);
129 if (!user_frozen && !nr_user) {
131 start_time = jiffies;
133 user_frozen = !nr_user;
134 yield(); /* Yield is okay here */
135 if (todo && time_after(jiffies, start_time + TIMEOUT))
139 /* This does not unfreeze processes that are already frozen
140 * (we have slightly ugly calling convention in that respect,
141 * and caller must call thaw_processes() if something fails),
142 * but it cleans up leftover PF_FREEZE requests.
146 printk(KERN_ERR "Stopping tasks timed out "
147 "after %d seconds (%d tasks remaining):\n",
149 read_lock(&tasklist_lock);
150 do_each_thread(g, p) {
151 if (freezeable(p) && !frozen(p))
152 printk(KERN_ERR " %s\n", p->comm);
154 } while_each_thread(g, p);
155 read_unlock(&tasklist_lock);
164 static void thaw_tasks(int thaw_user_space)
166 struct task_struct *g, *p;
168 read_lock(&tasklist_lock);
169 do_each_thread(g, p) {
173 if (is_user_space(p) == !thaw_user_space)
176 if (!thaw_process(p))
177 printk(KERN_WARNING " Strange, %s not stopped\n",
179 } while_each_thread(g, p);
180 read_unlock(&tasklist_lock);
183 void thaw_processes(void)
185 printk("Restarting tasks ... ");
186 thaw_tasks(FREEZER_KERNEL_THREADS);
187 thaw_tasks(FREEZER_USER_SPACE);
192 EXPORT_SYMBOL(refrigerator);