blob: b2a5f671d6cd3f2d164e44ad20ad7780a7e00377 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/power/process.c - Functions for starting/stopping processes on
3 * suspend transitions.
4 *
5 * Originally from swsusp.
6 */
7
8
9#undef DEBUG
10
11#include <linux/smp_lock.h>
12#include <linux/interrupt.h>
13#include <linux/suspend.h>
14#include <linux/module.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080015#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * Timeout for stopping processes
19 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080020#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22
23static inline int freezeable(struct task_struct * p)
24{
25 if ((p == current) ||
26 (p->flags & PF_NOFREEZE) ||
27 (p->exit_state == EXIT_ZOMBIE) ||
28 (p->exit_state == EXIT_DEAD) ||
Pavel Machek85b6bce2006-03-31 02:30:06 -080029 (p->state == TASK_STOPPED))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31 return 1;
32}
33
34/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070035void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 /* Hmm, should we be allowed to suspend when there are realtime
38 processes around? */
39 long save;
40 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pr_debug("%s entered refrigerator\n", current->comm);
42 printk("=");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070044 frozen_process(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 spin_lock_irq(&current->sighand->siglock);
46 recalc_sigpending(); /* We sent fake signal, clean it up */
47 spin_unlock_irq(&current->sighand->siglock);
48
Pavel Machek2a23b5d2005-09-03 15:56:53 -070049 while (frozen(current)) {
50 current->state = TASK_UNINTERRUPTIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 pr_debug("%s left refrigerator\n", current->comm);
54 current->state = save;
55}
56
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080057static inline void freeze_process(struct task_struct *p)
58{
59 unsigned long flags;
60
61 if (!freezing(p)) {
62 freeze(p);
63 spin_lock_irqsave(&p->sighand->siglock, flags);
64 signal_wake_up(p, 0);
65 spin_unlock_irqrestore(&p->sighand->siglock, flags);
66 }
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* 0 = success, else # of processes that we failed to stop */
70int freeze_processes(void)
71{
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080072 int todo, nr_user, user_frozen;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070073 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct task_struct *g, *p;
Pavel Machek1322ad42005-07-07 17:56:45 -070075 unsigned long flags;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 printk( "Stopping tasks: " );
78 start_time = jiffies;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080079 user_frozen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 do {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080081 nr_user = todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 read_lock(&tasklist_lock);
83 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!freezeable(p))
85 continue;
Pavel Machek1322ad42005-07-07 17:56:45 -070086 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 continue;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080088 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
89 /* The task is a user-space one.
90 * Freeze it unless there's a vfork completion
91 * pending
92 */
93 if (!p->vfork_done)
94 freeze_process(p);
95 nr_user++;
96 } else {
97 /* Freeze only if the user space is frozen */
98 if (user_frozen)
99 freeze_process(p);
100 todo++;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } while_each_thread(g, p);
103 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800104 todo += nr_user;
105 if (!user_frozen && !nr_user) {
106 sys_sync();
107 start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800109 user_frozen = !nr_user;
110 yield(); /* Yield is okay here */
111 if (todo && time_after(jiffies, start_time + TIMEOUT))
112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 } while(todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700114
Pavel Machek6161b2c2005-09-03 15:57:05 -0700115 /* This does not unfreeze processes that are already frozen
116 * (we have slightly ugly calling convention in that respect,
117 * and caller must call thaw_processes() if something fails),
118 * but it cleans up leftover PF_FREEZE requests.
119 */
120 if (todo) {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800121 printk( "\n" );
122 printk(KERN_ERR " stopping tasks timed out "
123 "after %d seconds (%d tasks remaining):\n",
124 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700125 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800126 do_each_thread(g, p) {
127 if (freezeable(p) && !frozen(p))
128 printk(KERN_ERR " %s\n", p->comm);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700129 if (freezing(p)) {
130 pr_debug(" clean up: %s\n", p->comm);
131 p->flags &= ~PF_FREEZE;
132 spin_lock_irqsave(&p->sighand->siglock, flags);
133 recalc_sigpending_tsk(p);
134 spin_unlock_irqrestore(&p->sighand->siglock, flags);
135 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800136 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700137 read_unlock(&tasklist_lock);
138 return todo;
139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk( "|\n" );
142 BUG_ON(in_atomic());
143 return 0;
144}
145
146void thaw_processes(void)
147{
148 struct task_struct *g, *p;
149
150 printk( "Restarting tasks..." );
151 read_lock(&tasklist_lock);
152 do_each_thread(g, p) {
153 if (!freezeable(p))
154 continue;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700155 if (!thaw_process(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
157 } while_each_thread(g, p);
158
159 read_unlock(&tasklist_lock);
160 schedule();
161 printk( " done\n" );
162}
163
164EXPORT_SYMBOL(refrigerator);