[PATCH] swsusp: clean up whitespace in freezer output
[deliverable/linux.git] / kernel / power / process.c
CommitLineData
1da177e4
LT
1/*
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>
02aaeb9b 15#include <linux/syscalls.h>
7dfb7103 16#include <linux/freezer.h>
1da177e4
LT
17
18/*
19 * Timeout for stopping processes
20 */
02aaeb9b 21#define TIMEOUT (20 * HZ)
1da177e4
LT
22
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) ||
85b6bce3 30 (p->state == TASK_STOPPED))
1da177e4
LT
31 return 0;
32 return 1;
33}
34
35/* Refrigerator is place where frozen processes are stored :-). */
3e1d1d28 36void refrigerator(void)
1da177e4
LT
37{
38 /* Hmm, should we be allowed to suspend when there are realtime
39 processes around? */
40 long save;
41 save = current->state;
1da177e4 42 pr_debug("%s entered refrigerator\n", current->comm);
1da177e4 43
3e1d1d28 44 frozen_process(current);
1da177e4
LT
45 spin_lock_irq(&current->sighand->siglock);
46 recalc_sigpending(); /* We sent fake signal, clean it up */
47 spin_unlock_irq(&current->sighand->siglock);
48
2a23b5d1
PM
49 while (frozen(current)) {
50 current->state = TASK_UNINTERRUPTIBLE;
1da177e4 51 schedule();
2a23b5d1 52 }
1da177e4
LT
53 pr_debug("%s left refrigerator\n", current->comm);
54 current->state = save;
55}
56
02aaeb9b
RW
57static 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
a7ef7878
RW
69static 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
1da177e4
LT
82/* 0 = success, else # of processes that we failed to stop */
83int freeze_processes(void)
84{
02aaeb9b 85 int todo, nr_user, user_frozen;
3e1d1d28 86 unsigned long start_time;
1da177e4 87 struct task_struct *g, *p;
3e1d1d28 88
14b5b7cf 89 printk("Stopping tasks... ");
1da177e4 90 start_time = jiffies;
02aaeb9b 91 user_frozen = 0;
1da177e4 92 do {
02aaeb9b 93 nr_user = todo = 0;
1da177e4
LT
94 read_lock(&tasklist_lock);
95 do_each_thread(g, p) {
1da177e4
LT
96 if (!freezeable(p))
97 continue;
1322ad41 98 if (frozen(p))
1da177e4 99 continue;
a7ef7878
RW
100 if (p->state == TASK_TRACED && frozen(p->parent)) {
101 cancel_freezing(p);
102 continue;
103 }
02aaeb9b
RW
104 if (p->mm && !(p->flags & PF_BORROWED_MM)) {
105 /* The task is a user-space one.
106 * Freeze it unless there's a vfork completion
107 * pending
108 */
109 if (!p->vfork_done)
110 freeze_process(p);
111 nr_user++;
112 } else {
113 /* Freeze only if the user space is frozen */
114 if (user_frozen)
115 freeze_process(p);
116 todo++;
117 }
1da177e4
LT
118 } while_each_thread(g, p);
119 read_unlock(&tasklist_lock);
02aaeb9b
RW
120 todo += nr_user;
121 if (!user_frozen && !nr_user) {
122 sys_sync();
123 start_time = jiffies;
124 }
125 user_frozen = !nr_user;
1da177e4 126 yield(); /* Yield is okay here */
02aaeb9b 127 if (todo && time_after(jiffies, start_time + TIMEOUT))
6161b2ce 128 break;
1da177e4 129 } while(todo);
3e1d1d28 130
6161b2ce
PM
131 /* This does not unfreeze processes that are already frozen
132 * (we have slightly ugly calling convention in that respect,
133 * and caller must call thaw_processes() if something fails),
134 * but it cleans up leftover PF_FREEZE requests.
135 */
136 if (todo) {
14b5b7cf
NC
137 printk("\n");
138 printk(KERN_ERR "Stopping tasks timed out "
02aaeb9b
RW
139 "after %d seconds (%d tasks remaining):\n",
140 TIMEOUT / HZ, todo);
6161b2ce 141 read_lock(&tasklist_lock);
02aaeb9b
RW
142 do_each_thread(g, p) {
143 if (freezeable(p) && !frozen(p))
14b5b7cf 144 printk(KERN_ERR " %s\n", p->comm);
a7ef7878 145 cancel_freezing(p);
02aaeb9b 146 } while_each_thread(g, p);
6161b2ce
PM
147 read_unlock(&tasklist_lock);
148 return todo;
149 }
150
14b5b7cf 151 printk("done.\n");
1da177e4
LT
152 BUG_ON(in_atomic());
153 return 0;
154}
155
156void thaw_processes(void)
157{
158 struct task_struct *g, *p;
159
14b5b7cf 160 printk("Restarting tasks... ");
1da177e4
LT
161 read_lock(&tasklist_lock);
162 do_each_thread(g, p) {
163 if (!freezeable(p))
164 continue;
3e1d1d28 165 if (!thaw_process(p))
14b5b7cf 166 printk(KERN_INFO "Strange, %s not stopped\n", p->comm);
1da177e4
LT
167 } while_each_thread(g, p);
168
169 read_unlock(&tasklist_lock);
170 schedule();
14b5b7cf 171 printk("done.\n");
1da177e4
LT
172}
173
174EXPORT_SYMBOL(refrigerator);
This page took 0.27982 seconds and 5 git commands to generate.