blob: 29be608e8349d54e4cf9bdc2ff8fff17ee96e935 [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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080016#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/*
19 * Timeout for stopping processes
20 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080021#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23
24static inline int freezeable(struct task_struct * p)
25{
26 if ((p == current) ||
27 (p->flags & PF_NOFREEZE) ||
28 (p->exit_state == EXIT_ZOMBIE) ||
29 (p->exit_state == EXIT_DEAD) ||
Pavel Machek85b6bce2006-03-31 02:30:06 -080030 (p->state == TASK_STOPPED))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 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
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070070static void cancel_freezing(struct task_struct *p)
71{
72 unsigned long flags;
73
74 if (freezing(p)) {
75 pr_debug(" clean up: %s\n", p->comm);
76 do_not_freeze(p);
77 spin_lock_irqsave(&p->sighand->siglock, flags);
78 recalc_sigpending_tsk(p);
79 spin_unlock_irqrestore(&p->sighand->siglock, flags);
80 }
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* 0 = success, else # of processes that we failed to stop */
84int freeze_processes(void)
85{
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080086 int todo, nr_user, user_frozen;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070087 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct task_struct *g, *p;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 printk( "Stopping tasks: " );
91 start_time = jiffies;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080092 user_frozen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 do {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080094 nr_user = todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 read_lock(&tasklist_lock);
96 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (!freezeable(p))
98 continue;
Pavel Machek1322ad42005-07-07 17:56:45 -070099 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 continue;
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700101 if (p->state == TASK_TRACED && frozen(p->parent)) {
102 cancel_freezing(p);
103 continue;
104 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800105 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
106 /* The task is a user-space one.
107 * Freeze it unless there's a vfork completion
108 * pending
109 */
110 if (!p->vfork_done)
111 freeze_process(p);
112 nr_user++;
113 } else {
114 /* Freeze only if the user space is frozen */
115 if (user_frozen)
116 freeze_process(p);
117 todo++;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 } while_each_thread(g, p);
120 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800121 todo += nr_user;
122 if (!user_frozen && !nr_user) {
123 sys_sync();
124 start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800126 user_frozen = !nr_user;
127 yield(); /* Yield is okay here */
128 if (todo && time_after(jiffies, start_time + TIMEOUT))
129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } while(todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700131
Pavel Machek6161b2c2005-09-03 15:57:05 -0700132 /* This does not unfreeze processes that are already frozen
133 * (we have slightly ugly calling convention in that respect,
134 * and caller must call thaw_processes() if something fails),
135 * but it cleans up leftover PF_FREEZE requests.
136 */
137 if (todo) {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800138 printk( "\n" );
139 printk(KERN_ERR " stopping tasks timed out "
140 "after %d seconds (%d tasks remaining):\n",
141 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700142 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800143 do_each_thread(g, p) {
144 if (freezeable(p) && !frozen(p))
145 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700146 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800147 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700148 read_unlock(&tasklist_lock);
149 return todo;
150 }
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 printk( "|\n" );
153 BUG_ON(in_atomic());
154 return 0;
155}
156
157void thaw_processes(void)
158{
159 struct task_struct *g, *p;
160
161 printk( "Restarting tasks..." );
162 read_lock(&tasklist_lock);
163 do_each_thread(g, p) {
164 if (!freezeable(p))
165 continue;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700166 if (!thaw_process(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
168 } while_each_thread(g, p);
169
170 read_unlock(&tasklist_lock);
171 schedule();
172 printk( " done\n" );
173}
174
175EXPORT_SYMBOL(refrigerator);