commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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/interrupt.h>
12 #include <linux/suspend.h>
13 #include <linux/module.h>
14
15 #ifdef DEBUG_SLOW
16 #define MDELAY(a) mdelay(a)
17 #else
18 #define MDELAY(a)
19 #endif
20
21 /* 
22  * Timeout for stopping processes
23  */
24 #define TIMEOUT (6 * HZ)
25
26
27 static inline int freezeable(struct task_struct * p)
28 {
29         if ((p == current) || 
30             (p->flags & PF_IOTHREAD) || 
31             (p->state == TASK_ZOMBIE) ||
32             (p->state == TASK_DEAD))
33                 return 0;
34         return 1;
35 }
36
37 /* Refrigerator is place where frozen processes are stored :-). */
38 void refrigerator(unsigned long flag)
39 {
40         /* You need correct to work with real-time processes.
41            OTOH, this way one process may see (via /proc/) some other
42            process in stopped state (and thereby discovered we were
43            suspended. We probably do not care. 
44          */
45         long save;
46         save = current->state;
47         current->state = TASK_STOPPED;
48         pr_debug("%s entered refrigerator\n", current->comm);
49         printk("=");
50         current->flags &= ~PF_FREEZE;
51         if (flag)
52                 flush_signals(current); /* We have signaled a kernel thread, which isn't normal behaviour
53                                            and that may lead to 100%CPU sucking because those threads
54                                            just don't manage signals. */
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);