blob: 99eeb119b06db1d350d04de044fdb624f4627a19 [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
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080023#define FREEZER_KERNEL_THREADS 0
24#define FREEZER_USER_SPACE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static inline int freezeable(struct task_struct * p)
27{
28 if ((p == current) ||
29 (p->flags & PF_NOFREEZE) ||
30 (p->exit_state == EXIT_ZOMBIE) ||
31 (p->exit_state == EXIT_DEAD) ||
Pavel Machek85b6bce2006-03-31 02:30:06 -080032 (p->state == TASK_STOPPED))
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return 0;
34 return 1;
35}
36
37/* Refrigerator is place where frozen processes are stored :-). */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070038void refrigerator(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 /* Hmm, should we be allowed to suspend when there are realtime
41 processes around? */
42 long save;
43 save = current->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 pr_debug("%s entered refrigerator\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070046 frozen_process(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 spin_lock_irq(&current->sighand->siglock);
48 recalc_sigpending(); /* We sent fake signal, clean it up */
49 spin_unlock_irq(&current->sighand->siglock);
50
Pavel Machek2a23b5d2005-09-03 15:56:53 -070051 while (frozen(current)) {
52 current->state = TASK_UNINTERRUPTIBLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 schedule();
Pavel Machek2a23b5d2005-09-03 15:56:53 -070054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 pr_debug("%s left refrigerator\n", current->comm);
56 current->state = save;
57}
58
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080059static inline void freeze_process(struct task_struct *p)
60{
61 unsigned long flags;
62
63 if (!freezing(p)) {
64 freeze(p);
65 spin_lock_irqsave(&p->sighand->siglock, flags);
66 signal_wake_up(p, 0);
67 spin_unlock_irqrestore(&p->sighand->siglock, flags);
68 }
69}
70
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070071static void cancel_freezing(struct task_struct *p)
72{
73 unsigned long flags;
74
75 if (freezing(p)) {
76 pr_debug(" clean up: %s\n", p->comm);
77 do_not_freeze(p);
78 spin_lock_irqsave(&p->sighand->siglock, flags);
79 recalc_sigpending_tsk(p);
80 spin_unlock_irqrestore(&p->sighand->siglock, flags);
81 }
82}
83
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080084static inline int is_user_space(struct task_struct *p)
85{
86 return p->mm && !(p->flags & PF_BORROWED_MM);
87}
88
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080089static unsigned int try_to_freeze_tasks(int freeze_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080092 unsigned long end_time;
93 unsigned int todo;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070094
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080095 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080097 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 read_lock(&tasklist_lock);
99 do_each_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!freezeable(p))
101 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800102
Pavel Machek1322ad42005-07-07 17:56:45 -0700103 if (frozen(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800105
Rafael J. Wysocki3eb1b3a2006-12-06 20:34:34 -0800106 if (p->state == TASK_TRACED &&
107 (frozen(p->parent) ||
108 p->parent->state == TASK_STOPPED)) {
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700109 cancel_freezing(p);
110 continue;
111 }
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800112 if (is_user_space(p)) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800113 if (!freeze_user_space)
114 continue;
115
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800116 /* Freeze the task unless there is a vfork
117 * completion pending
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800118 */
119 if (!p->vfork_done)
120 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800121 } else {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800122 if (freeze_user_space)
123 continue;
124
125 freeze_process(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800126 }
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800127 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } while_each_thread(g, p);
129 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800130 yield(); /* Yield is okay here */
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800131 if (todo && time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800132 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800133 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700134
Pavel Machek6161b2c2005-09-03 15:57:05 -0700135 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800136 /* This does not unfreeze processes that are already frozen
137 * (we have slightly ugly calling convention in that respect,
138 * and caller must call thaw_processes() if something fails),
139 * but it cleans up leftover PF_FREEZE requests.
140 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800141 printk("\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800142 printk(KERN_ERR "Stopping %s timed out after %d seconds "
143 "(%d tasks refusing to freeze):\n",
144 freeze_user_space ? "user space processes" :
145 "kernel threads",
146 TIMEOUT / HZ, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700147 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800148 do_each_thread(g, p) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800149 if (is_user_space(p) == !freeze_user_space)
150 continue;
151
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800152 if (freezeable(p) && !frozen(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800153 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800154
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700155 cancel_freezing(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800156 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700157 read_unlock(&tasklist_lock);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700158 }
159
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800160 return todo;
161}
162
163/**
164 * freeze_processes - tell processes to enter the refrigerator
165 *
166 * Returns 0 on success, or the number of processes that didn't freeze,
167 * although they were told to.
168 */
169int freeze_processes(void)
170{
171 unsigned int nr_unfrozen;
172
173 printk("Stopping tasks ... ");
174 nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
175 if (nr_unfrozen)
176 return nr_unfrozen;
177
178 sys_sync();
179 nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
180 if (nr_unfrozen)
181 return nr_unfrozen;
182
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800183 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 BUG_ON(in_atomic());
185 return 0;
186}
187
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800188static void thaw_tasks(int thaw_user_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct task_struct *g, *p;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800193 do_each_thread(g, p) {
194 if (!freezeable(p))
195 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800196
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800197 if (is_user_space(p) == !thaw_user_space)
198 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800200 if (!thaw_process(p))
201 printk(KERN_WARNING " Strange, %s not stopped\n",
202 p->comm );
203 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800205}
206
207void thaw_processes(void)
208{
209 printk("Restarting tasks ... ");
210 thaw_tasks(FREEZER_KERNEL_THREADS);
211 thaw_tasks(FREEZER_USER_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800213 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216EXPORT_SYMBOL(refrigerator);