process.c 4.2 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 103
			if (p->state == TASK_TRACED && frozen(p->parent)) {
				cancel_freezing(p);
				continue;
			}
104 105 106 107 108 109 110 111 112 113 114 115 116 117
			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 已提交
118 119
		} while_each_thread(g, p);
		read_unlock(&tasklist_lock);
120 121 122 123 124 125
		todo += nr_user;
		if (!user_frozen && !nr_user) {
			sys_sync();
			start_time = jiffies;
		}
		user_frozen = !nr_user;
L
Linus Torvalds 已提交
126
		yield();			/* Yield is okay here */
127
		if (todo && time_after(jiffies, start_time + TIMEOUT))
P
Pavel Machek 已提交
128
			break;
L
Linus Torvalds 已提交
129
	} while(todo);
130

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

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

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

161
	printk("Restarting tasks... ");
L
Linus Torvalds 已提交
162
	read_lock(&tasklist_lock);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	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 已提交
179 180 181

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

EXPORT_SYMBOL(refrigerator);