ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #ifdef DEBUG_SLOW
17 #define MDELAY(a) mdelay(a)
18 #else
19 #define MDELAY(a)
20 #endif
21
22 /* 
23  * Timeout for stopping processes
24  */
25 #define TIMEOUT (6 * HZ)
26
27
28 static inline int freezeable(struct task_struct * p)
29 {
30         if ((p == current) || 
31             (p->flags & PF_NOFREEZE) ||
32             (p->state == TASK_ZOMBIE) ||
33             (p->state == TASK_DEAD) ||
34             (p->state == TASK_STOPPED))
35                 return 0;
36         return 1;
37 }
38
39 /* Refrigerator is place where frozen processes are stored :-). */
40 void refrigerator(unsigned long flag)
41 {
42         /* Hmm, should we be allowed to suspend when there are realtime
43            processes around? */
44         long save;
45         save = current->state;
46         current->state = TASK_UNINTERRUPTIBLE;
47         pr_debug("%s entered refrigerator\n", current->comm);
48         printk("=");
49         current->flags &= ~PF_FREEZE;
50
51         spin_lock_irq(&current->sighand->siglock);
52         recalc_sigpending(); /* We sent fake signal, clean it up */
53         spin_unlock_irq(&current->sighand->siglock);
54
55         current->flags |= PF_FROZEN;
56         while (current->flags & PF_FROZEN)
57                 schedule();
58         pr_debug("%s left refrigerator\n", current->comm);
59         current->state = save;
60 }
61
62 /* 0 = success, else # of processes that we failed to stop */
63 int freeze_processes(void)
64 {
65        int todo;
66        unsigned long start_time;
67         struct task_struct *g, *p;
68         
69         printk( "Stopping tasks: " );
70         start_time = jiffies;
71         do {
72                 todo = 0;
73                 read_lock(&tasklist_lock);
74                 do_each_thread(g, p) {
75                         unsigned long flags;
76                         if (!freezeable(p))
77                                 continue;
78                         if ((p->flags & PF_FROZEN) ||
79                             (p->state == TASK_STOPPED))
80                                 continue;
81
82                         /* FIXME: smp problem here: we may not access other process' flags
83                            without locking */
84                         p->flags |= PF_FREEZE;
85                         spin_lock_irqsave(&p->sighand->siglock, flags);
86                         signal_wake_up(p, 0);
87                         spin_unlock_irqrestore(&p->sighand->siglock, flags);
88                         todo++;
89                 } while_each_thread(g, p);
90                 read_unlock(&tasklist_lock);
91                 yield();                        /* Yield is okay here */
92                 if (time_after(jiffies, start_time + TIMEOUT)) {
93                         printk( "\n" );
94                         printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
95                         return todo;
96                 }
97         } while(todo);
98         
99         printk( "|\n" );
100         BUG_ON(in_atomic());
101         return 0;
102 }
103
104 void thaw_processes(void)
105 {
106         struct task_struct *g, *p;
107
108         printk( "Restarting tasks..." );
109         read_lock(&tasklist_lock);
110         do_each_thread(g, p) {
111                 if (!freezeable(p))
112                         continue;
113                 if (p->flags & PF_FROZEN) {
114                         p->flags &= ~PF_FROZEN;
115                         wake_up_process(p);
116                 } else
117                         printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
118                 wake_up_process(p);
119         } while_each_thread(g, p);
120
121         read_unlock(&tasklist_lock);
122         schedule();
123         printk( " done\n" );
124         MDELAY(500);
125 }
126
127 EXPORT_SYMBOL(refrigerator);