blob: 1badb9a89ade03e45b705513deac04c8f0ab1e57 [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);
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
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070069static void cancel_freezing(struct task_struct *p)
70{
71 unsigned long flags;
72
73 if (freezing(p)) {
74 pr_debug(" clean up: %s\n", p->comm);
75 do_not_freeze(p);
76 spin_lock_irqsave(&p->sighand->siglock, flags);
77 recalc_sigpending_tsk(p);
78 spin_unlock_irqrestore(&p->sighand->siglock, flags);
79 }
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* 0 = success, else # of processes that we failed to stop */
83int freeze_processes(void)
84{
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080085 int todo, nr_user, user_frozen;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070086 unsigned long start_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct task_struct *g, *p;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070088
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080089 printk("Stopping tasks... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 start_time = jiffies;
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080091 user_frozen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 do {
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080093 nr_user = todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 read_lock(&tasklist_lock);
95 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!freezeable(p))
97 continue;
Pavel Machek1322ad42005-07-07 17:56:45 -070098 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 continue;
Rafael J. Wysocki3eb1b3a2006-12-06 20:34:34 -0800100 if (p->state == TASK_TRACED &&
101 (frozen(p->parent) ||
102 p->parent->state == TASK_STOPPED)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700103 cancel_freezing(p);
104 continue;
105 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800106 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
107 /* The task is a user-space one.
108 * Freeze it unless there's a vfork completion
109 * pending
110 */
111 if (!p->vfork_done)
112 freeze_process(p);
113 nr_user++;
114 } else {
115 /* Freeze only if the user space is frozen */
116 if (user_frozen)
117 freeze_process(p);
118 todo++;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 } while_each_thread(g, p);
121 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800122 todo += nr_user;
123 if (!user_frozen && !nr_user) {
124 sys_sync();
125 start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800127 user_frozen = !nr_user;
128 yield(); /* Yield is okay here */
129 if (todo && time_after(jiffies, start_time + TIMEOUT))
130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } while(todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700132
Pavel Machek6161b2c2005-09-03 15:57:05 -0700133 /* This does not unfreeze processes that are already frozen
134 * (we have slightly ugly calling convention in that respect,
135 * and caller must call thaw_processes() if something fails),
136 * but it cleans up leftover PF_FREEZE requests.
137 */
138 if (todo) {
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800139 printk("\n");
140 printk(KERN_ERR "Stopping tasks timed out "
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800141 "after %d seconds (%d tasks remaining):\n",
142 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700143 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800144 do_each_thread(g, p) {
145 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800146 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700147 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800148 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700149 read_unlock(&tasklist_lock);
150 return todo;
151 }
152
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800153 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 BUG_ON(in_atomic());
155 return 0;
156}
157
Nigel Cunninghamff395932006-12-06 20:34:28 -0800158void thaw_some_processes(int all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct task_struct *g, *p;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800161 int pass = 0; /* Pass 0 = Kernel space, 1 = Userspace */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800163 printk("Restarting tasks... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 read_lock(&tasklist_lock);
Nigel Cunninghamff395932006-12-06 20:34:28 -0800165 do {
166 do_each_thread(g, p) {
167 /*
168 * is_user = 0 if kernel thread or borrowed mm,
169 * 1 otherwise.
170 */
171 int is_user = !!(p->mm && !(p->flags & PF_BORROWED_MM));
172 if (!freezeable(p) || (is_user != pass))
173 continue;
174 if (!thaw_process(p))
175 printk(KERN_INFO
176 "Strange, %s not stopped\n", p->comm);
177 } while_each_thread(g, p);
178
179 pass++;
180 } while (pass < 2 && all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 read_unlock(&tasklist_lock);
183 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800184 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187EXPORT_SYMBOL(refrigerator);