]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/power/process.c
[PATCH] swsusp: fix error handling and cleanups
[mirror_ubuntu-artful-kernel.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>
15
16/*
17 * Timeout for stopping processes
18 */
19#define TIMEOUT (6 * HZ)
20
21
22static 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 (p->state == TASK_TRACED))
30 return 0;
31 return 1;
32}
33
34/* Refrigerator is place where frozen processes are stored :-). */
3e1d1d28 35void refrigerator(void)
1da177e4
LT
36{
37 /* Hmm, should we be allowed to suspend when there are realtime
38 processes around? */
39 long save;
40 save = current->state;
1da177e4
LT
41 pr_debug("%s entered refrigerator\n", current->comm);
42 printk("=");
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
57/* 0 = success, else # of processes that we failed to stop */
58int freeze_processes(void)
59{
3e1d1d28
CL
60 int todo;
61 unsigned long start_time;
1da177e4 62 struct task_struct *g, *p;
1322ad41 63 unsigned long flags;
3e1d1d28 64
1da177e4
LT
65 printk( "Stopping tasks: " );
66 start_time = jiffies;
67 do {
68 todo = 0;
69 read_lock(&tasklist_lock);
70 do_each_thread(g, p) {
1da177e4
LT
71 if (!freezeable(p))
72 continue;
1322ad41 73 if (frozen(p))
1da177e4
LT
74 continue;
75
3e1d1d28 76 freeze(p);
1da177e4
LT
77 spin_lock_irqsave(&p->sighand->siglock, flags);
78 signal_wake_up(p, 0);
79 spin_unlock_irqrestore(&p->sighand->siglock, flags);
80 todo++;
81 } while_each_thread(g, p);
82 read_unlock(&tasklist_lock);
83 yield(); /* Yield is okay here */
84 if (time_after(jiffies, start_time + TIMEOUT)) {
85 printk( "\n" );
86 printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
87 return todo;
88 }
89 } while(todo);
3e1d1d28 90
1da177e4
LT
91 printk( "|\n" );
92 BUG_ON(in_atomic());
93 return 0;
94}
95
96void thaw_processes(void)
97{
98 struct task_struct *g, *p;
99
100 printk( "Restarting tasks..." );
101 read_lock(&tasklist_lock);
102 do_each_thread(g, p) {
103 if (!freezeable(p))
104 continue;
3e1d1d28 105 if (!thaw_process(p))
1da177e4
LT
106 printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
107 } while_each_thread(g, p);
108
109 read_unlock(&tasklist_lock);
110 schedule();
111 printk( " done\n" );
112}
113
114EXPORT_SYMBOL(refrigerator);