67b2cddfc16098f6daf784e526337b7d83c15e68
[linux-2.6.git] / kernel / power / process.c
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>
15
16 /* 
17  * Timeout for stopping processes
18  */
19 #define TIMEOUT (6 * HZ)
20
21
22 static inline int freezeable(struct task_struct * p)
23 {
24         if ((p == current) || 
25             (p->flags & PF_NOFREEZE) ||
26             (p->exit_state == EXIT_ZOMBIE) ||
27             (p->exit_state == EXIT_DEAD) ||
28             (p->state == TASK_STOPPED))
29                 return 0;
30         return 1;
31 }
32
33 /* Refrigerator is place where frozen processes are stored :-). */
34 void refrigerator(void)
35 {
36         /* Hmm, should we be allowed to suspend when there are realtime
37            processes around? */
38         long save;
39         save = current->state;
40         pr_debug("%s entered refrigerator\n", current->comm);
41         printk("=");
42
43         frozen_process(current);
44         spin_lock_irq(&current->sighand->siglock);
45         recalc_sigpending(); /* We sent fake signal, clean it up */
46         spin_unlock_irq(&current->sighand->siglock);
47
48         while (frozen(current)) {
49                 current->state = TASK_UNINTERRUPTIBLE;
50                 schedule();
51         }
52         pr_debug("%s left refrigerator\n", current->comm);
53         current->state = save;
54 }
55
56 /* 0 = success, else # of processes that we failed to stop */
57 int freeze_processes(void)
58 {
59         int todo;
60         unsigned long start_time;
61         struct task_struct *g, *p;
62         unsigned long flags;
63
64         printk( "Stopping tasks: " );
65         start_time = jiffies;
66         do {
67                 todo = 0;
68                 read_lock(&tasklist_lock);
69                 do_each_thread(g, p) {
70                         if (!freezeable(p))
71                                 continue;
72                         if (frozen(p))
73                                 continue;
74
75                         freeze(p);
76                         spin_lock_irqsave(&p->sighand->siglock, flags);
77                         signal_wake_up(p, 0);
78                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
79                         todo++;
80                 } while_each_thread(g, p);
81                 read_unlock(&tasklist_lock);
82                 yield();                        /* Yield is okay here */
83                 if (todo && time_after(jiffies, start_time + TIMEOUT)) {
84                         printk( "\n" );
85                         printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
86                         break;
87                 }
88         } while(todo);
89
90         /* This does not unfreeze processes that are already frozen
91          * (we have slightly ugly calling convention in that respect,
92          * and caller must call thaw_processes() if something fails),
93          * but it cleans up leftover PF_FREEZE requests.
94          */
95         if (todo) {
96                 read_lock(&tasklist_lock);
97                 do_each_thread(g, p)
98                         if (freezing(p)) {
99                                 pr_debug("  clean up: %s\n", p->comm);
100                                 p->flags &= ~PF_FREEZE;
101                                 spin_lock_irqsave(&p->sighand->siglock, flags);
102                                 recalc_sigpending_tsk(p);
103                                 spin_unlock_irqrestore(&p->sighand->siglock, flags);
104                         }
105                 while_each_thread(g, p);
106                 read_unlock(&tasklist_lock);
107                 return todo;
108         }
109
110         printk( "|\n" );
111         BUG_ON(in_atomic());
112         return 0;
113 }
114
115 void thaw_processes(void)
116 {
117         struct task_struct *g, *p;
118
119         printk( "Restarting tasks..." );
120         read_lock(&tasklist_lock);
121         do_each_thread(g, p) {
122                 if (!freezeable(p))
123                         continue;
124                 if (!thaw_process(p))
125                         printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
126         } while_each_thread(g, p);
127
128         read_unlock(&tasklist_lock);
129         schedule();
130         printk( " done\n" );
131 }
132
133 EXPORT_SYMBOL(refrigerator);