2 * drivers/power/process.c - Functions for starting/stopping processes on
5 * Originally from swsusp.
11 #include <linux/interrupt.h>
12 #include <linux/oom.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
21 * Timeout for stopping processes
23 #define TIMEOUT (20 * HZ)
25 static inline int freezable(struct task_struct
* p
)
28 (p
->flags
& PF_NOFREEZE
))
33 static int try_to_freeze_tasks(bool sig_only
)
35 struct task_struct
*g
, *p
;
36 unsigned long end_time
;
39 struct timeval start
, end
;
41 unsigned int elapsed_csecs
;
44 do_gettimeofday(&start
);
46 end_time
= jiffies
+ TIMEOUT
;
49 freeze_workqueues_begin();
53 read_lock(&tasklist_lock
);
54 do_each_thread(g
, p
) {
55 if (frozen(p
) || !freezable(p
))
58 if (!freeze_task(p
, sig_only
))
62 * Now that we've done set_freeze_flag, don't
63 * perturb a task in TASK_STOPPED or TASK_TRACED.
64 * It is "frozen enough". If the task does wake
65 * up, it will immediately call try_to_freeze.
67 * Because freeze_task() goes through p's
68 * scheduler lock after setting TIF_FREEZE, it's
69 * guaranteed that either we see TASK_RUNNING or
70 * try_to_stop() after schedule() in ptrace/signal
71 * stop sees TIF_FREEZE.
73 if (!task_is_stopped_or_traced(p
) &&
74 !freezer_should_skip(p
))
76 } while_each_thread(g
, p
);
77 read_unlock(&tasklist_lock
);
80 wq_busy
= freeze_workqueues_busy();
84 if (!todo
|| time_after(jiffies
, end_time
))
87 if (pm_wakeup_pending()) {
93 * We need to retry, but first give the freezing tasks some
94 * time to enter the regrigerator.
99 do_gettimeofday(&end
);
100 elapsed_csecs64
= timeval_to_ns(&end
) - timeval_to_ns(&start
);
101 do_div(elapsed_csecs64
, NSEC_PER_SEC
/ 100);
102 elapsed_csecs
= elapsed_csecs64
;
105 /* This does not unfreeze processes that are already frozen
106 * (we have slightly ugly calling convention in that respect,
107 * and caller must call thaw_processes() if something fails),
108 * but it cleans up leftover PF_FREEZE requests.
111 printk(KERN_ERR
"Freezing of tasks %s after %d.%02d seconds "
112 "(%d tasks refusing to freeze, wq_busy=%d):\n",
113 wakeup
? "aborted" : "failed",
114 elapsed_csecs
/ 100, elapsed_csecs
% 100,
115 todo
- wq_busy
, wq_busy
);
119 read_lock(&tasklist_lock
);
120 do_each_thread(g
, p
) {
121 if (!wakeup
&& !freezer_should_skip(p
) &&
122 freezing(p
) && !frozen(p
))
125 } while_each_thread(g
, p
);
126 read_unlock(&tasklist_lock
);
128 printk("(elapsed %d.%02d seconds) ", elapsed_csecs
/ 100,
129 elapsed_csecs
% 100);
132 return todo
? -EBUSY
: 0;
136 * freeze_processes - Signal user space processes to enter the refrigerator.
138 int freeze_processes(void)
142 printk("Freezing user space processes ... ");
143 error
= try_to_freeze_tasks(true);
146 oom_killer_disable();
155 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
157 int freeze_kernel_threads(void)
161 printk("Freezing remaining freezable tasks ... ");
162 error
= try_to_freeze_tasks(false);
172 void thaw_processes(void)
174 struct task_struct
*g
, *p
;
178 printk("Restarting tasks ... ");
182 read_lock(&tasklist_lock
);
183 do_each_thread(g
, p
) {
187 if (cgroup_freezing_or_frozen(p
))
191 } while_each_thread(g
, p
);
192 read_unlock(&tasklist_lock
);