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 static unsigned int try_to_freeze_tasks(int freeze_user_space)
91 struct task_struct *g, *p;
92 unsigned long end_time;
95 end_time = jiffies + TIMEOUT;
98 read_lock(&tasklist_lock);
99 do_each_thread(g, p) {
106 if (p->state == TASK_TRACED &&
107 (frozen(p->parent) ||
108 p->parent->state == TASK_STOPPED)) {
112 if (is_user_space(p)) {
113 if (!freeze_user_space)
116 /* Freeze the task unless there is a vfork
122 if (freeze_user_space)
128 } while_each_thread(g, p);
129 read_unlock(&tasklist_lock);
130 yield(); /* Yield is okay here */
131 if (todo && time_after(jiffies, end_time))
136 /* This does not unfreeze processes that are already frozen
137 * (we have slightly ugly calling convention in that respect,
138 * and caller must call thaw_processes() if something fails),
139 * but it cleans up leftover PF_FREEZE requests.
142 printk(KERN_ERR "Stopping %s timed out after %d seconds "
143 "(%d tasks refusing to freeze):\n",
144 freeze_user_space ? "user space processes" :
147 read_lock(&tasklist_lock);
148 do_each_thread(g, p) {
149 if (is_user_space(p) == !freeze_user_space)
152 if (freezeable(p) && !frozen(p))
153 printk(KERN_ERR " %s\n", p->comm);
156 } while_each_thread(g, p);
157 read_unlock(&tasklist_lock);
164 * freeze_processes - tell processes to enter the refrigerator
166 * Returns 0 on success, or the number of processes that didn't freeze,
167 * although they were told to.
169 int freeze_processes(void)
171 unsigned int nr_unfrozen;
173 printk("Stopping tasks ... ");
174 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
179 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
188 static void thaw_tasks(int thaw_user_space)
190 struct task_struct *g, *p;
192 read_lock(&tasklist_lock);
193 do_each_thread(g, p) {
197 if (is_user_space(p) == !thaw_user_space)
200 if (!thaw_process(p))
201 printk(KERN_WARNING " Strange, %s not stopped\n",
203 } while_each_thread(g, p);
204 read_unlock(&tasklist_lock);
207 void thaw_processes(void)
209 printk("Restarting tasks ... ");
210 thaw_tasks(FREEZER_KERNEL_THREADS);
211 thaw_tasks(FREEZER_USER_SPACE);
216 EXPORT_SYMBOL(refrigerator);