process.c 4.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * drivers/power/process.c - Functions for starting/stopping processes on 
 *                           suspend transitions.
 *
 * Originally from swsusp.
 */


#undef DEBUG

#include <linux/interrupt.h>
12
#include <linux/oom.h>
L
Linus Torvalds 已提交
13 14
#include <linux/suspend.h>
#include <linux/module.h>
15
#include <linux/syscalls.h>
16
#include <linux/freezer.h>
17
#include <linux/delay.h>
18
#include <linux/workqueue.h>
L
Linus Torvalds 已提交
19 20 21 22

/* 
 * Timeout for stopping processes
 */
23
#define TIMEOUT	(20 * HZ)
L
Linus Torvalds 已提交
24

25
static int try_to_freeze_tasks(bool user_only)
L
Linus Torvalds 已提交
26 27
{
	struct task_struct *g, *p;
28 29
	unsigned long end_time;
	unsigned int todo;
30
	bool wq_busy = false;
R
Rafael J. Wysocki 已提交
31
	struct timeval start, end;
32
	u64 elapsed_csecs64;
R
Rafael J. Wysocki 已提交
33
	unsigned int elapsed_csecs;
34
	bool wakeup = false;
R
Rafael J. Wysocki 已提交
35 36

	do_gettimeofday(&start);
37

38
	end_time = jiffies + TIMEOUT;
39

40
	if (!user_only)
41 42
		freeze_workqueues_begin();

43
	while (true) {
44
		todo = 0;
L
Linus Torvalds 已提交
45 46
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
47
			if (p == current || !freeze_task(p))
48 49
				continue;

R
Roland McGrath 已提交
50 51 52 53 54
			/*
			 * Now that we've done set_freeze_flag, don't
			 * perturb a task in TASK_STOPPED or TASK_TRACED.
			 * It is "frozen enough".  If the task does wake
			 * up, it will immediately call try_to_freeze.
55 56 57 58 59 60
			 *
			 * Because freeze_task() goes through p's
			 * scheduler lock after setting TIF_FREEZE, it's
			 * guaranteed that either we see TASK_RUNNING or
			 * try_to_stop() after schedule() in ptrace/signal
			 * stop sees TIF_FREEZE.
R
Roland McGrath 已提交
61 62 63
			 */
			if (!task_is_stopped_or_traced(p) &&
			    !freezer_should_skip(p))
R
Rafael J. Wysocki 已提交
64
				todo++;
L
Linus Torvalds 已提交
65 66
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
67

68
		if (!user_only) {
69 70 71 72
			wq_busy = freeze_workqueues_busy();
			todo += wq_busy;
		}

73
		if (!todo || time_after(jiffies, end_time))
P
Pavel Machek 已提交
74
			break;
75

76
		if (pm_wakeup_pending()) {
77 78 79 80
			wakeup = true;
			break;
		}

81 82 83 84 85 86
		/*
		 * We need to retry, but first give the freezing tasks some
		 * time to enter the regrigerator.
		 */
		msleep(10);
	}
87

R
Rafael J. Wysocki 已提交
88 89 90 91 92
	do_gettimeofday(&end);
	elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
	do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
	elapsed_csecs = elapsed_csecs64;

P
Pavel Machek 已提交
93
	if (todo) {
94
		printk("\n");
95
		printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
96
		       "(%d tasks refusing to freeze, wq_busy=%d):\n",
97
		       wakeup ? "aborted" : "failed",
98 99 100
		       elapsed_csecs / 100, elapsed_csecs % 100,
		       todo - wq_busy, wq_busy);

P
Pavel Machek 已提交
101
		read_lock(&tasklist_lock);
102
		do_each_thread(g, p) {
103
			if (!wakeup && !freezer_should_skip(p) &&
104
			    p != current && freezing(p) && !frozen(p))
105
				sched_show_task(p);
106
		} while_each_thread(g, p);
P
Pavel Machek 已提交
107
		read_unlock(&tasklist_lock);
R
Rafael J. Wysocki 已提交
108 109 110
	} else {
		printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
			elapsed_csecs % 100);
P
Pavel Machek 已提交
111 112
	}

113
	return todo ? -EBUSY : 0;
114 115 116
}

/**
117
 * freeze_processes - Signal user space processes to enter the refrigerator.
118 119
 *
 * On success, returns 0.  On failure, -errno and system is fully thawed.
120 121 122
 */
int freeze_processes(void)
{
123
	int error;
124

125 126 127
	if (!pm_freezing)
		atomic_inc(&system_freezing_cnt);

R
Rafael J. Wysocki 已提交
128
	printk("Freezing user space processes ... ");
129
	pm_freezing = true;
130
	error = try_to_freeze_tasks(true);
131 132 133 134 135 136 137
	if (!error) {
		printk("done.");
		oom_killer_disable();
	}
	printk("\n");
	BUG_ON(in_atomic());

138 139
	if (error)
		thaw_processes();
140 141 142 143 144
	return error;
}

/**
 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
145 146
 *
 * On success, returns 0.  On failure, -errno and system is fully thawed.
147 148 149 150
 */
int freeze_kernel_threads(void)
{
	int error;
151

R
Rafael J. Wysocki 已提交
152
	printk("Freezing remaining freezable tasks ... ");
153
	pm_nosig_freezing = true;
154
	error = try_to_freeze_tasks(false);
155 156
	if (!error)
		printk("done.");
157

R
Rafael J. Wysocki 已提交
158
	printk("\n");
159
	BUG_ON(in_atomic());
160

161 162
	if (error)
		thaw_processes();
R
Rafael J. Wysocki 已提交
163
	return error;
L
Linus Torvalds 已提交
164 165
}

166
void thaw_processes(void)
L
Linus Torvalds 已提交
167 168 169
{
	struct task_struct *g, *p;

170 171 172 173 174
	if (pm_freezing)
		atomic_dec(&system_freezing_cnt);
	pm_freezing = false;
	pm_nosig_freezing = false;

175 176 177 178 179 180
	oom_killer_enable();

	printk("Restarting tasks ... ");

	thaw_workqueues();

L
Linus Torvalds 已提交
181
	read_lock(&tasklist_lock);
182
	do_each_thread(g, p) {
183
		__thaw_task(p);
184
	} while_each_thread(g, p);
L
Linus Torvalds 已提交
185
	read_unlock(&tasklist_lock);
186

L
Linus Torvalds 已提交
187
	schedule();
188
	printk("done.\n");
L
Linus Torvalds 已提交
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
void thaw_kernel_threads(void)
{
	struct task_struct *g, *p;

	pm_nosig_freezing = false;
	printk("Restarting kernel threads ... ");

	thaw_workqueues();

	read_lock(&tasklist_lock);
	do_each_thread(g, p) {
		if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
			__thaw_task(p);
	} while_each_thread(g, p);
	read_unlock(&tasklist_lock);

	schedule();
	printk("done.\n");
}