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 25 26 27 28 29


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) ||
30
	    (p->state == TASK_STOPPED))
L
Linus Torvalds 已提交
31 32 33 34 35
		return 0;
	return 1;
}

/* Refrigerator is place where frozen processes are stored :-). */
36
void refrigerator(void)
L
Linus Torvalds 已提交
37 38 39 40 41 42 43
{
	/* 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);

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

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

57 58 59 60 61 62 63 64 65 66 67 68
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);
	}
}

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

L
Linus Torvalds 已提交
82 83 84
/* 0 = success, else # of processes that we failed to stop */
int freeze_processes(void)
{
85
	int todo, nr_user, user_frozen;
86
	unsigned long start_time;
L
Linus Torvalds 已提交
87
	struct task_struct *g, *p;
88

89
	printk("Stopping tasks... ");
L
Linus Torvalds 已提交
90
	start_time = jiffies;
91
	user_frozen = 0;
L
Linus Torvalds 已提交
92
	do {
93
		nr_user = todo = 0;
L
Linus Torvalds 已提交
94 95 96 97
		read_lock(&tasklist_lock);
		do_each_thread(g, p) {
			if (!freezeable(p))
				continue;
P
Pavel Machek 已提交
98
			if (frozen(p))
L
Linus Torvalds 已提交
99
				continue;
100 101 102
			if (p->state == TASK_TRACED &&
			    (frozen(p->parent) ||
			     p->parent->state == TASK_STOPPED)) {
103 104 105
				cancel_freezing(p);
				continue;
			}
106 107 108 109 110 111 112 113 114 115 116 117 118 119
			if (p->mm && !(p->flags & PF_BORROWED_MM)) {
				/* The task is a user-space one.
				 * Freeze it unless there's a vfork completion
				 * pending
				 */
				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 已提交
120 121
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
122 123 124 125 126 127
		todo += nr_user;
		if (!user_frozen && !nr_user) {
			sys_sync();
			start_time = jiffies;
		}
		user_frozen = !nr_user;
L
Linus Torvalds 已提交
128
		yield();			/* Yield is okay here */
129
		if (todo && time_after(jiffies, start_time + TIMEOUT))
P
Pavel Machek 已提交
130
			break;
L
Linus Torvalds 已提交
131
	} while(todo);
132

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

153
	printk("done.\n");
L
Linus Torvalds 已提交
154 155 156 157
	BUG_ON(in_atomic());
	return 0;
}

158
void thaw_some_processes(int all)
L
Linus Torvalds 已提交
159 160
{
	struct task_struct *g, *p;
161
	int pass = 0; /* Pass 0 = Kernel space, 1 = Userspace */
L
Linus Torvalds 已提交
162

163
	printk("Restarting tasks... ");
L
Linus Torvalds 已提交
164
	read_lock(&tasklist_lock);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	do {
		do_each_thread(g, p) {
			/*
			 * is_user = 0 if kernel thread or borrowed mm,
			 * 1 otherwise.
			 */
			int is_user = !!(p->mm && !(p->flags & PF_BORROWED_MM));
			if (!freezeable(p) || (is_user != pass))
				continue;
			if (!thaw_process(p))
				printk(KERN_INFO
					"Strange, %s not stopped\n", p->comm);
		} while_each_thread(g, p);

		pass++;
	} while (pass < 2 && all);
L
Linus Torvalds 已提交
181 182 183

	read_unlock(&tasklist_lock);
	schedule();
184
	printk("done.\n");
L
Linus Torvalds 已提交
185 186 187
}

EXPORT_SYMBOL(refrigerator);