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


#undef DEBUG

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

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

23 24
#define FREEZER_KERNEL_THREADS 0
#define FREEZER_USER_SPACE 1
L
Linus Torvalds 已提交
25 26 27 28 29 30 31

static inline int freezeable(struct task_struct * p)
{
	if ((p == current) || 
	    (p->flags & PF_NOFREEZE) ||
	    (p->exit_state == EXIT_ZOMBIE) ||
	    (p->exit_state == EXIT_DEAD) ||
32
	    (p->state == TASK_STOPPED))
L
Linus Torvalds 已提交
33 34 35 36 37
		return 0;
	return 1;
}

/* Refrigerator is place where frozen processes are stored :-). */
38
void refrigerator(void)
L
Linus Torvalds 已提交
39 40 41 42 43 44 45
{
	/* Hmm, should we be allowed to suspend when there are realtime
	   processes around? */
	long save;
	save = current->state;
	pr_debug("%s entered refrigerator\n", current->comm);

46
	frozen_process(current);
L
Linus Torvalds 已提交
47 48 49 50
	spin_lock_irq(&current->sighand->siglock);
	recalc_sigpending(); /* We sent fake signal, clean it up */
	spin_unlock_irq(&current->sighand->siglock);

51 52
	while (frozen(current)) {
		current->state = TASK_UNINTERRUPTIBLE;
L
Linus Torvalds 已提交
53
		schedule();
54
	}
L
Linus Torvalds 已提交
55 56 57 58
	pr_debug("%s left refrigerator\n", current->comm);
	current->state = save;
}

59 60 61 62 63 64 65 66 67 68 69 70
static inline void freeze_process(struct task_struct *p)
{
	unsigned long flags;

	if (!freezing(p)) {
		freeze(p);
		spin_lock_irqsave(&p->sighand->siglock, flags);
		signal_wake_up(p, 0);
		spin_unlock_irqrestore(&p->sighand->siglock, flags);
	}
}

71 72 73 74 75 76 77 78 79 80 81 82 83
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);
	}
}

84 85 86 87 88
static inline int is_user_space(struct task_struct *p)
{
	return p->mm && !(p->flags & PF_BORROWED_MM);
}

L
Linus Torvalds 已提交
89 90 91
/* 0 = success, else # of processes that we failed to stop */
int freeze_processes(void)
{
92
	int todo, nr_user, user_frozen;
93
	unsigned long start_time;
L
Linus Torvalds 已提交
94
	struct task_struct *g, *p;
95

96
	printk("Stopping tasks... ");
L
Linus Torvalds 已提交
97
	start_time = jiffies;
98
	user_frozen = 0;
L
Linus Torvalds 已提交
99
	do {
100
		nr_user = todo = 0;
L
Linus Torvalds 已提交
101 102 103 104
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
			if (!freezeable(p))
				continue;
P
Pavel Machek 已提交
105
			if (frozen(p))
L
Linus Torvalds 已提交
106
				continue;
107 108 109
			if (p->state == TASK_TRACED &&
			    (frozen(p->parent) ||
			     p->parent->state == TASK_STOPPED)) {
110 111 112
				cancel_freezing(p);
				continue;
			}
113 114 115
			if (is_user_space(p)) {
				/* Freeze the task unless there is a vfork
				 * completion pending
116 117 118 119 120 121 122 123 124 125
				 */
				if (!p->vfork_done)
					freeze_process(p);
				nr_user++;
			} else {
				/* Freeze only if the user space is frozen */
				if (user_frozen)
					freeze_process(p);
				todo++;
			}
L
Linus Torvalds 已提交
126 127
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
128 129 130 131 132 133
		todo += nr_user;
		if (!user_frozen && !nr_user) {
			sys_sync();
			start_time = jiffies;
		}
		user_frozen = !nr_user;
L
Linus Torvalds 已提交
134
		yield();			/* Yield is okay here */
135
		if (todo && time_after(jiffies, start_time + TIMEOUT))
P
Pavel Machek 已提交
136
			break;
L
Linus Torvalds 已提交
137
	} while(todo);
138

P
Pavel Machek 已提交
139 140 141 142 143 144
	/* 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.
	 */
	if (todo) {
145 146
		printk("\n");
		printk(KERN_ERR "Stopping tasks timed out "
147 148
			"after %d seconds (%d tasks remaining):\n",
			TIMEOUT / HZ, todo);
P
Pavel Machek 已提交
149
		read_lock(&tasklist_lock);
150 151
		do_each_thread(g, p) {
			if (freezeable(p) && !frozen(p))
152
				printk(KERN_ERR " %s\n", p->comm);
153
			cancel_freezing(p);
154
		} while_each_thread(g, p);
P
Pavel Machek 已提交
155 156 157 158
		read_unlock(&tasklist_lock);
		return todo;
	}

159
	printk("done.\n");
L
Linus Torvalds 已提交
160 161 162 163
	BUG_ON(in_atomic());
	return 0;
}

164
static void thaw_tasks(int thaw_user_space)
L
Linus Torvalds 已提交
165 166 167 168
{
	struct task_struct *g, *p;

	read_lock(&tasklist_lock);
169 170 171
	do_each_thread(g, p) {
		if (!freezeable(p))
			continue;
172

173 174
		if (is_user_space(p) == !thaw_user_space)
			continue;
L
Linus Torvalds 已提交
175

176 177 178 179
		if (!thaw_process(p))
			printk(KERN_WARNING " Strange, %s not stopped\n",
				p->comm );
	} while_each_thread(g, p);
L
Linus Torvalds 已提交
180
	read_unlock(&tasklist_lock);
181 182 183 184 185 186 187
}

void thaw_processes(void)
{
	printk("Restarting tasks ... ");
	thaw_tasks(FREEZER_KERNEL_THREADS);
	thaw_tasks(FREEZER_USER_SPACE);
L
Linus Torvalds 已提交
188
	schedule();
189
	printk("done.\n");
L
Linus Torvalds 已提交
190 191 192
}

EXPORT_SYMBOL(refrigerator);