process.c 4.8 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 34
		return 0;
	return 1;
}

/* Refrigerator is place where frozen processes are stored :-). */
35
void refrigerator(void)
L
Linus Torvalds 已提交
36 37 38 39
{
	/* Hmm, should we be allowed to suspend when there are realtime
	   processes around? */
	long save;
40 41 42 43 44 45 46 47 48

	task_lock(current);
	if (freezing(current)) {
		frozen_process(current);
		task_unlock(current);
	} else {
		task_unlock(current);
		return;
	}
L
Linus Torvalds 已提交
49 50 51 52 53 54 55
	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);

56 57 58 59
	for (;;) {
		set_current_state(TASK_UNINTERRUPTIBLE);
		if (!frozen(current))
			break;
L
Linus Torvalds 已提交
60
		schedule();
61
	}
L
Linus Torvalds 已提交
62 63 64 65
	pr_debug("%s left refrigerator\n", current->comm);
	current->state = save;
}

66 67 68 69 70
static inline void freeze_process(struct task_struct *p)
{
	unsigned long flags;

	if (!freezing(p)) {
71 72 73 74
		rmb();
		if (!frozen(p)) {
			if (p->state == TASK_STOPPED)
				force_sig_specific(SIGSTOP, p);
75

76 77 78 79 80
			freeze(p);
			spin_lock_irqsave(&p->sighand->siglock, flags);
			signal_wake_up(p, p->state == TASK_STOPPED);
			spin_unlock_irqrestore(&p->sighand->siglock, flags);
		}
81 82 83
	}
}

84 85 86 87 88 89 90 91 92 93 94 95 96
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);
		recalc_sigpending_tsk(p);
		spin_unlock_irqrestore(&p->sighand->siglock, flags);
	}
}

97 98 99 100 101
static inline int is_user_space(struct task_struct *p)
{
	return p->mm && !(p->flags & PF_BORROWED_MM);
}

102
static unsigned int try_to_freeze_tasks(int freeze_user_space)
L
Linus Torvalds 已提交
103 104
{
	struct task_struct *g, *p;
105 106
	unsigned long end_time;
	unsigned int todo;
107

108
	end_time = jiffies + TIMEOUT;
L
Linus Torvalds 已提交
109
	do {
110
		todo = 0;
L
Linus Torvalds 已提交
111 112 113 114
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
			if (!freezeable(p))
				continue;
115

P
Pavel Machek 已提交
116
			if (frozen(p))
L
Linus Torvalds 已提交
117
				continue;
118

119
			if (p->state == TASK_TRACED && frozen(p->parent)) {
120 121 122
				cancel_freezing(p);
				continue;
			}
123
			if (is_user_space(p)) {
124 125 126
				if (!freeze_user_space)
					continue;

127 128
				/* Freeze the task unless there is a vfork
				 * completion pending
129 130 131 132
				 */
				if (!p->vfork_done)
					freeze_process(p);
			} else {
133 134 135 136
				if (freeze_user_space)
					continue;

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

P
Pavel Machek 已提交
146
	if (todo) {
147 148 149 150 151
		/* 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.
		 */
152
		printk("\n");
153 154 155 156 157
		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 已提交
158
		read_lock(&tasklist_lock);
159
		do_each_thread(g, p) {
160 161 162
			if (is_user_space(p) == !freeze_user_space)
				continue;

163
			task_lock(p);
164
			if (freezeable(p) && !frozen(p))
165
				printk(KERN_ERR " %s\n", p->comm);
166

167
			cancel_freezing(p);
168
			task_unlock(p);
169
		} while_each_thread(g, p);
P
Pavel Machek 已提交
170 171 172
		read_unlock(&tasklist_lock);
	}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	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;

196
	printk("done.\n");
L
Linus Torvalds 已提交
197 198 199 200
	BUG_ON(in_atomic());
	return 0;
}

201
static void thaw_tasks(int thaw_user_space)
L
Linus Torvalds 已提交
202 203 204 205
{
	struct task_struct *g, *p;

	read_lock(&tasklist_lock);
206 207 208
	do_each_thread(g, p) {
		if (!freezeable(p))
			continue;
209

210 211
		if (is_user_space(p) == !thaw_user_space)
			continue;
L
Linus Torvalds 已提交
212

213 214 215 216
		if (!thaw_process(p))
			printk(KERN_WARNING " Strange, %s not stopped\n",
				p->comm );
	} while_each_thread(g, p);
L
Linus Torvalds 已提交
217
	read_unlock(&tasklist_lock);
218 219 220 221 222 223 224
}

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

EXPORT_SYMBOL(refrigerator);