process.c 3.7 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 26

static inline int freezeable(struct task_struct * p)
{
27
	if ((p == current) ||
L
Linus Torvalds 已提交
28
	    (p->flags & PF_NOFREEZE) ||
29
	    (p->exit_state != 0))
L
Linus Torvalds 已提交
30 31 32 33
		return 0;
	return 1;
}

34
static int try_to_freeze_tasks(bool sig_only)
L
Linus Torvalds 已提交
35 36
{
	struct task_struct *g, *p;
37 38
	unsigned long end_time;
	unsigned int todo;
39
	bool wq_busy = false;
R
Rafael J. Wysocki 已提交
40
	struct timeval start, end;
41
	u64 elapsed_csecs64;
R
Rafael J. Wysocki 已提交
42 43 44
	unsigned int elapsed_csecs;

	do_gettimeofday(&start);
45

46
	end_time = jiffies + TIMEOUT;
47 48 49 50

	if (!sig_only)
		freeze_workqueues_begin();

51
	while (true) {
52
		todo = 0;
L
Linus Torvalds 已提交
53 54
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
55
			if (frozen(p) || !freezeable(p))
L
Linus Torvalds 已提交
56
				continue;
57

58
			if (!freeze_task(p, sig_only))
59 60
				continue;

R
Roland McGrath 已提交
61 62 63 64 65 66 67 68
			/*
			 * 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.
			 */
			if (!task_is_stopped_or_traced(p) &&
			    !freezer_should_skip(p))
R
Rafael J. Wysocki 已提交
69
				todo++;
L
Linus Torvalds 已提交
70 71
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
72 73 74 75 76 77

		if (!sig_only) {
			wq_busy = freeze_workqueues_busy();
			todo += wq_busy;
		}

78
		if (!todo || time_after(jiffies, end_time))
P
Pavel Machek 已提交
79
			break;
80 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 95 96 97 98
		/* This does not unfreeze processes that are already frozen
		 * (we have slightly ugly calling convention in that respect,
		 * and caller must call thaw_processes() if something fails),
		 * but it cleans up leftover PF_FREEZE requests.
		 */
99
		printk("\n");
R
Rafael J. Wysocki 已提交
100
		printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
101 102 103 104 105 106
		       "(%d tasks refusing to freeze, wq_busy=%d):\n",
		       elapsed_csecs / 100, elapsed_csecs % 100,
		       todo - wq_busy, wq_busy);

		thaw_workqueues();

P
Pavel Machek 已提交
107
		read_lock(&tasklist_lock);
108
		do_each_thread(g, p) {
109
			task_lock(p);
110
			if (freezing(p) && !freezer_should_skip(p))
111
				sched_show_task(p);
112
			cancel_freezing(p);
113
			task_unlock(p);
114
		} while_each_thread(g, p);
P
Pavel Machek 已提交
115
		read_unlock(&tasklist_lock);
R
Rafael J. Wysocki 已提交
116 117 118
	} else {
		printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
			elapsed_csecs % 100);
P
Pavel Machek 已提交
119 120
	}

121
	return todo ? -EBUSY : 0;
122 123 124 125 126 127 128
}

/**
 *	freeze_processes - tell processes to enter the refrigerator
 */
int freeze_processes(void)
{
129
	int error;
130

R
Rafael J. Wysocki 已提交
131
	printk("Freezing user space processes ... ");
132
	error = try_to_freeze_tasks(true);
133
	if (error)
R
Rafael J. Wysocki 已提交
134 135
		goto Exit;
	printk("done.\n");
136

R
Rafael J. Wysocki 已提交
137
	printk("Freezing remaining freezable tasks ... ");
138
	error = try_to_freeze_tasks(false);
139
	if (error)
R
Rafael J. Wysocki 已提交
140 141
		goto Exit;
	printk("done.");
142 143

	oom_killer_disable();
R
Rafael J. Wysocki 已提交
144
 Exit:
L
Linus Torvalds 已提交
145
	BUG_ON(in_atomic());
R
Rafael J. Wysocki 已提交
146
	printk("\n");
147

R
Rafael J. Wysocki 已提交
148
	return error;
L
Linus Torvalds 已提交
149 150
}

151
static void thaw_tasks(bool nosig_only)
L
Linus Torvalds 已提交
152 153 154 155
{
	struct task_struct *g, *p;

	read_lock(&tasklist_lock);
156 157 158
	do_each_thread(g, p) {
		if (!freezeable(p))
			continue;
159

160
		if (nosig_only && should_send_signal(p))
161
			continue;
L
Linus Torvalds 已提交
162

163
		if (cgroup_freezing_or_frozen(p))
164 165
			continue;

R
Rafael J. Wysocki 已提交
166
		thaw_process(p);
167
	} while_each_thread(g, p);
L
Linus Torvalds 已提交
168
	read_unlock(&tasklist_lock);
169 170 171 172
}

void thaw_processes(void)
{
173 174
	oom_killer_enable();

175
	printk("Restarting tasks ... ");
176
	thaw_workqueues();
177 178
	thaw_tasks(true);
	thaw_tasks(false);
L
Linus Torvalds 已提交
179
	schedule();
180
	printk("done.\n");
L
Linus Torvalds 已提交
181 182
}