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


#undef DEBUG

#include <linux/interrupt.h>
#include <linux/suspend.h>
#include <linux/module.h>
14
#include <linux/syscalls.h>
15
#include <linux/freezer.h>
L
Linus Torvalds 已提交
16 17 18 19

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

22 23
#define FREEZER_KERNEL_THREADS 0
#define FREEZER_USER_SPACE 1
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 35 36 37 38 39 40 41 42 43 44 45
/*
 * freezing is complete, mark current process as frozen
 */
static inline void frozen_process(void)
{
	if (!unlikely(current->flags & PF_NOFREEZE)) {
		current->flags |= PF_FROZEN;
		wmb();
	}
	clear_tsk_thread_flag(current, TIF_FREEZE);
}

L
Linus Torvalds 已提交
46
/* Refrigerator is place where frozen processes are stored :-). */
47
void refrigerator(void)
L
Linus Torvalds 已提交
48 49 50 51
{
	/* Hmm, should we be allowed to suspend when there are realtime
	   processes around? */
	long save;
52 53 54

	task_lock(current);
	if (freezing(current)) {
55
		frozen_process();
56 57 58 59 60
		task_unlock(current);
	} else {
		task_unlock(current);
		return;
	}
L
Linus Torvalds 已提交
61 62 63 64 65 66 67
	save = current->state;
	pr_debug("%s entered refrigerator\n", current->comm);

	spin_lock_irq(&current->sighand->siglock);
	recalc_sigpending(); /* We sent fake signal, clean it up */
	spin_unlock_irq(&current->sighand->siglock);

68 69 70 71
	for (;;) {
		set_current_state(TASK_UNINTERRUPTIBLE);
		if (!frozen(current))
			break;
L
Linus Torvalds 已提交
72
		schedule();
73
	}
L
Linus Torvalds 已提交
74 75 76 77
	pr_debug("%s left refrigerator\n", current->comm);
	current->state = save;
}

78 79 80 81 82
static inline void freeze_process(struct task_struct *p)
{
	unsigned long flags;

	if (!freezing(p)) {
83 84 85 86
		rmb();
		if (!frozen(p)) {
			if (p->state == TASK_STOPPED)
				force_sig_specific(SIGSTOP, p);
87

88 89 90 91 92
			freeze(p);
			spin_lock_irqsave(&p->sighand->siglock, flags);
			signal_wake_up(p, p->state == TASK_STOPPED);
			spin_unlock_irqrestore(&p->sighand->siglock, flags);
		}
93 94 95
	}
}

96 97 98 99 100 101 102 103
static void cancel_freezing(struct task_struct *p)
{
	unsigned long flags;

	if (freezing(p)) {
		pr_debug("  clean up: %s\n", p->comm);
		do_not_freeze(p);
		spin_lock_irqsave(&p->sighand->siglock, flags);
R
Roland McGrath 已提交
104
		recalc_sigpending_and_wake(p);
105 106 107 108
		spin_unlock_irqrestore(&p->sighand->siglock, flags);
	}
}

109 110 111 112 113
static inline int is_user_space(struct task_struct *p)
{
	return p->mm && !(p->flags & PF_BORROWED_MM);
}

114
static unsigned int try_to_freeze_tasks(int freeze_user_space)
L
Linus Torvalds 已提交
115 116
{
	struct task_struct *g, *p;
117 118
	unsigned long end_time;
	unsigned int todo;
119

120
	end_time = jiffies + TIMEOUT;
L
Linus Torvalds 已提交
121
	do {
122
		todo = 0;
L
Linus Torvalds 已提交
123 124 125 126
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
			if (!freezeable(p))
				continue;
127

P
Pavel Machek 已提交
128
			if (frozen(p))
L
Linus Torvalds 已提交
129
				continue;
130

131
			if (p->state == TASK_TRACED && frozen(p->parent)) {
132 133 134
				cancel_freezing(p);
				continue;
			}
135
			if (freeze_user_space && !is_user_space(p))
R
Rafael J. Wysocki 已提交
136 137 138 139 140
				continue;

			freeze_process(p);
			if (!freezer_should_skip(p))
				todo++;
L
Linus Torvalds 已提交
141 142 143
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
		yield();			/* Yield is okay here */
144
		if (todo && time_after(jiffies, end_time))
P
Pavel Machek 已提交
145
			break;
146
	} while (todo);
147

P
Pavel Machek 已提交
148
	if (todo) {
149 150 151 152 153
		/* 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.
		 */
154
		printk("\n");
155 156 157 158 159
		printk(KERN_ERR "Stopping %s timed out after %d seconds "
				"(%d tasks refusing to freeze):\n",
				freeze_user_space ? "user space processes" :
					"kernel threads",
				TIMEOUT / HZ, todo);
P
Pavel Machek 已提交
160
		read_lock(&tasklist_lock);
161
		do_each_thread(g, p) {
162
			if (freeze_user_space && !is_user_space(p))
163 164
				continue;

165
			task_lock(p);
R
Rafael J. Wysocki 已提交
166 167
			if (freezeable(p) && !frozen(p) &&
			    !freezer_should_skip(p))
168
				printk(KERN_ERR " %s\n", p->comm);
169

170
			cancel_freezing(p);
171
			task_unlock(p);
172
		} while_each_thread(g, p);
P
Pavel Machek 已提交
173 174 175
		read_unlock(&tasklist_lock);
	}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	return todo;
}

/**
 *	freeze_processes - tell processes to enter the refrigerator
 *
 *	Returns 0 on success, or the number of processes that didn't freeze,
 *	although they were told to.
 */
int freeze_processes(void)
{
	unsigned int nr_unfrozen;

	printk("Stopping tasks ... ");
	nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
	if (nr_unfrozen)
		return nr_unfrozen;

	sys_sync();
	nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
	if (nr_unfrozen)
		return nr_unfrozen;

199
	printk("done.\n");
L
Linus Torvalds 已提交
200 201 202 203
	BUG_ON(in_atomic());
	return 0;
}

204
static void thaw_tasks(int thaw_user_space)
L
Linus Torvalds 已提交
205 206 207 208
{
	struct task_struct *g, *p;

	read_lock(&tasklist_lock);
209 210 211
	do_each_thread(g, p) {
		if (!freezeable(p))
			continue;
212

213 214
		if (is_user_space(p) == !thaw_user_space)
			continue;
L
Linus Torvalds 已提交
215

R
Rafael J. Wysocki 已提交
216
		thaw_process(p);
217
	} while_each_thread(g, p);
L
Linus Torvalds 已提交
218
	read_unlock(&tasklist_lock);
219 220 221 222 223 224 225
}

void thaw_processes(void)
{
	printk("Restarting tasks ... ");
	thaw_tasks(FREEZER_KERNEL_THREADS);
	thaw_tasks(FREEZER_USER_SPACE);
L
Linus Torvalds 已提交
226
	schedule();
227
	printk("done.\n");
L
Linus Torvalds 已提交
228 229 230
}

EXPORT_SYMBOL(refrigerator);