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>
19
#include <linux/kmod.h>
L
Linus Torvalds 已提交
20 21 22 23

/* 
 * Timeout for stopping processes
 */
24
unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
L
Linus Torvalds 已提交
25

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

	do_gettimeofday(&start);
38

39
	end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
40

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

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

51
			if (!freezer_should_skip(p))
R
Rafael J. Wysocki 已提交
52
				todo++;
L
Linus Torvalds 已提交
53 54
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
55

56
		if (!user_only) {
57 58 59 60
			wq_busy = freeze_workqueues_busy();
			todo += wq_busy;
		}

61
		if (!todo || time_after(jiffies, end_time))
P
Pavel Machek 已提交
62
			break;
63

64
		if (pm_wakeup_pending()) {
65 66 67 68
			wakeup = true;
			break;
		}

69 70
		/*
		 * We need to retry, but first give the freezing tasks some
71
		 * time to enter the refrigerator.
72 73 74
		 */
		msleep(10);
	}
75

R
Rafael J. Wysocki 已提交
76 77 78 79 80
	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 已提交
81
	if (todo) {
82
		printk("\n");
83
		printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
84
		       "(%d tasks refusing to freeze, wq_busy=%d):\n",
85
		       wakeup ? "aborted" : "failed",
86 87 88
		       elapsed_csecs / 100, elapsed_csecs % 100,
		       todo - wq_busy, wq_busy);

89 90 91 92 93 94 95 96 97
		if (!wakeup) {
			read_lock(&tasklist_lock);
			do_each_thread(g, p) {
				if (p != current && !freezer_should_skip(p)
				    && freezing(p) && !frozen(p))
					sched_show_task(p);
			} while_each_thread(g, p);
			read_unlock(&tasklist_lock);
		}
R
Rafael J. Wysocki 已提交
98 99 100
	} else {
		printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
			elapsed_csecs % 100);
P
Pavel Machek 已提交
101 102
	}

103
	return todo ? -EBUSY : 0;
104 105 106
}

/**
107
 * freeze_processes - Signal user space processes to enter the refrigerator.
108 109
 *
 * On success, returns 0.  On failure, -errno and system is fully thawed.
110 111 112
 */
int freeze_processes(void)
{
113
	int error;
114

115
	error = __usermodehelper_disable(UMH_FREEZING);
116 117 118
	if (error)
		return error;

119 120 121
	if (!pm_freezing)
		atomic_inc(&system_freezing_cnt);

R
Rafael J. Wysocki 已提交
122
	printk("Freezing user space processes ... ");
123
	pm_freezing = true;
124
	error = try_to_freeze_tasks(true);
125 126
	if (!error) {
		printk("done.");
127
		__usermodehelper_set_disable_depth(UMH_DISABLED);
128 129 130 131 132
		oom_killer_disable();
	}
	printk("\n");
	BUG_ON(in_atomic());

133 134
	if (error)
		thaw_processes();
135 136 137 138 139
	return error;
}

/**
 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
140
 *
141 142 143 144
 * On success, returns 0.  On failure, -errno and only the kernel threads are
 * thawed, so as to give a chance to the caller to do additional cleanups
 * (if any) before thawing the userspace tasks. So, it is the responsibility
 * of the caller to thaw the userspace tasks, when the time is right.
145 146 147 148
 */
int freeze_kernel_threads(void)
{
	int error;
149

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

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

159
	if (error)
160
		thaw_kernel_threads();
R
Rafael J. Wysocki 已提交
161
	return error;
L
Linus Torvalds 已提交
162 163
}

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

168 169 170 171 172
	if (pm_freezing)
		atomic_dec(&system_freezing_cnt);
	pm_freezing = false;
	pm_nosig_freezing = false;

173 174 175 176 177 178
	oom_killer_enable();

	printk("Restarting tasks ... ");

	thaw_workqueues();

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

185 186
	usermodehelper_enable();

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");
}