blob: 8ac7c35fad7708ea8136b3e19062ddf05f9f16c0 [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) ||
29 (p->state == TASK_STOPPED) ||
30 (p->state == TASK_TRACED))
31 return 0;
32 return 1;
33}
34
35/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070036void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 /* Hmm, should we be allowed to suspend when there are realtime
39 processes around? */
40 long save;
41 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 pr_debug("%s entered refrigerator\n", current->comm);
43 printk("=");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070045 frozen_process(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 spin_lock_irq(&current->sighand->siglock);
47 recalc_sigpending(); /* We sent fake signal, clean it up */
48 spin_unlock_irq(&current->sighand->siglock);
49
Pavel Machek2a23b5d2005-09-03 15:56:53 -070050 while (frozen(current)) {
51 current->state = TASK_UNINTERRUPTIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 pr_debug("%s left refrigerator\n", current->comm);
55 current->state = save;
56}
57
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080058static inline void freeze_process(struct task_struct *p)
59{
60 unsigned long flags;
61
62 if (!freezing(p)) {
63 freeze(p);
64 spin_lock_irqsave(&p->sighand->siglock, flags);
65 signal_wake_up(p, 0);
66 spin_unlock_irqrestore(&p->sighand->siglock, flags);
67 }
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* 0 = success, else # of processes that we failed to stop */
71int freeze_processes(void)
72{
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080073 int todo, nr_user, user_frozen;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070074 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct task_struct *g, *p;
Pavel Machek1322ad412005-07-07 17:56:45 -070076 unsigned long flags;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 printk( "Stopping tasks: " );
79 start_time = jiffies;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080080 user_frozen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 do {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080082 nr_user = todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 read_lock(&tasklist_lock);
84 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!freezeable(p))
86 continue;
Pavel Machek1322ad412005-07-07 17:56:45 -070087 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 continue;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080089 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
90 /* The task is a user-space one.
91 * Freeze it unless there's a vfork completion
92 * pending
93 */
94 if (!p->vfork_done)
95 freeze_process(p);
96 nr_user++;
97 } else {
98 /* Freeze only if the user space is frozen */
99 if (user_frozen)
100 freeze_process(p);
101 todo++;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } while_each_thread(g, p);
104 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800105 todo += nr_user;
106 if (!user_frozen && !nr_user) {
107 sys_sync();
108 start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800110 user_frozen = !nr_user;
111 yield(); /* Yield is okay here */
112 if (todo && time_after(jiffies, start_time + TIMEOUT))
113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 } while(todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700115
Pavel Machek6161b2c2005-09-03 15:57:05 -0700116 /* This does not unfreeze processes that are already frozen
117 * (we have slightly ugly calling convention in that respect,
118 * and caller must call thaw_processes() if something fails),
119 * but it cleans up leftover PF_FREEZE requests.
120 */
121 if (todo) {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800122 printk( "\n" );
123 printk(KERN_ERR " stopping tasks timed out "
124 "after %d seconds (%d tasks remaining):\n",
125 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700126 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800127 do_each_thread(g, p) {
128 if (freezeable(p) && !frozen(p))
129 printk(KERN_ERR " %s\n", p->comm);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700130 if (freezing(p)) {
131 pr_debug(" clean up: %s\n", p->comm);
132 p->flags &= ~PF_FREEZE;
133 spin_lock_irqsave(&p->sighand->siglock, flags);
134 recalc_sigpending_tsk(p);
135 spin_unlock_irqrestore(&p->sighand->siglock, flags);
136 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800137 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700138 read_unlock(&tasklist_lock);
139 return todo;
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk( "|\n" );
143 BUG_ON(in_atomic());
144 return 0;
145}
146
147void thaw_processes(void)
148{
149 struct task_struct *g, *p;
150
151 printk( "Restarting tasks..." );
152 read_lock(&tasklist_lock);
153 do_each_thread(g, p) {
154 if (!freezeable(p))
155 continue;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700156 if (!thaw_process(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
158 } while_each_thread(g, p);
159
160 read_unlock(&tasklist_lock);
161 schedule();
162 printk( " done\n" );
163}
164
165EXPORT_SYMBOL(refrigerator);