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 static inline int freezeable(struct task_struct * p)
25 (p->flags & PF_NOFREEZE) ||
31 static int try_to_freeze_tasks(bool sig_only)
33 struct task_struct *g, *p;
34 unsigned long end_time;
36 struct timeval start, end;
38 unsigned int elapsed_csecs;
40 do_gettimeofday(&start);
42 end_time = jiffies + TIMEOUT;
45 read_lock(&tasklist_lock);
46 do_each_thread(g, p) {
47 if (frozen(p) || !freezeable(p))
50 if (!freeze_task(p, sig_only))
54 * Now that we've done set_freeze_flag, don't
55 * perturb a task in TASK_STOPPED or TASK_TRACED.
56 * It is "frozen enough". If the task does wake
57 * up, it will immediately call try_to_freeze.
59 if (!task_is_stopped_or_traced(p) &&
60 !freezer_should_skip(p))
62 } while_each_thread(g, p);
63 read_unlock(&tasklist_lock);
64 yield(); /* Yield is okay here */
65 if (time_after(jiffies, end_time))
69 do_gettimeofday(&end);
70 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
71 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
72 elapsed_csecs = elapsed_csecs64;
75 /* This does not unfreeze processes that are already frozen
76 * (we have slightly ugly calling convention in that respect,
77 * and caller must call thaw_processes() if something fails),
78 * but it cleans up leftover PF_FREEZE requests.
81 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
82 "(%d tasks refusing to freeze):\n",
83 elapsed_csecs / 100, elapsed_csecs % 100, todo);
85 read_lock(&tasklist_lock);
86 do_each_thread(g, p) {
88 if (freezing(p) && !freezer_should_skip(p))
89 printk(KERN_ERR " %s\n", p->comm);
92 } while_each_thread(g, p);
93 read_unlock(&tasklist_lock);
95 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
99 return todo ? -EBUSY : 0;
103 * freeze_processes - tell processes to enter the refrigerator
105 int freeze_processes(void)
109 printk("Freezing user space processes ... ");
110 error = try_to_freeze_tasks(true);
115 printk("Freezing remaining freezable tasks ... ");
116 error = try_to_freeze_tasks(false);
126 static void thaw_tasks(bool nosig_only)
128 struct task_struct *g, *p;
130 read_lock(&tasklist_lock);
131 do_each_thread(g, p) {
135 if (nosig_only && should_send_signal(p))
138 if (cgroup_frozen(p))
142 } while_each_thread(g, p);
143 read_unlock(&tasklist_lock);
146 void thaw_processes(void)
148 printk("Restarting tasks ... ");