process.c 3.9 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>
L
Linus Torvalds 已提交
16 17 18 19

/* 
 * Timeout for stopping processes
 */
20
#define TIMEOUT	(20 * HZ)
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28


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) ||
29
	    (p->state == TASK_STOPPED))
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 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);
	printk("=");

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

L
Linus Torvalds 已提交
89 90
	printk( "Stopping tasks: " );
	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 139 140
		printk( "\n" );
		printk(KERN_ERR " stopping tasks timed out "
			"after %d seconds (%d tasks remaining):\n",
			TIMEOUT / HZ, todo);
P
Pavel Machek 已提交
141
		read_lock(&tasklist_lock);
142 143 144
		do_each_thread(g, p) {
			if (freezeable(p) && !frozen(p))
				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;
	}

L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164
	printk( "|\n" );
	BUG_ON(in_atomic());
	return 0;
}

void thaw_processes(void)
{
	struct task_struct *g, *p;

	printk( "Restarting tasks..." );
	read_lock(&tasklist_lock);
	do_each_thread(g, p) {
		if (!freezeable(p))
			continue;
165
		if (!thaw_process(p))
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174
			printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
	} while_each_thread(g, p);

	read_unlock(&tasklist_lock);
	schedule();
	printk( " done\n" );
}

EXPORT_SYMBOL(refrigerator);