2 * drivers/power/process.c - Functions for starting/stopping processes on
5 * Originally from swsusp.
11 #include <linux/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
18 * Timeout for stopping processes
20 #define TIMEOUT (20 * HZ)
23 static inline int freezeable(struct task_struct
* p
)
26 (p
->flags
& PF_NOFREEZE
) ||
27 (p
->exit_state
== EXIT_ZOMBIE
) ||
28 (p
->exit_state
== EXIT_DEAD
) ||
29 (p
->state
== TASK_STOPPED
))
34 /* Refrigerator is place where frozen processes are stored :-). */
35 void refrigerator(void)
37 /* Hmm, should we be allowed to suspend when there are realtime
40 save
= current
->state
;
41 pr_debug("%s entered refrigerator\n", current
->comm
);
44 frozen_process(current
);
45 spin_lock_irq(¤t
->sighand
->siglock
);
46 recalc_sigpending(); /* We sent fake signal, clean it up */
47 spin_unlock_irq(¤t
->sighand
->siglock
);
49 while (frozen(current
)) {
50 current
->state
= TASK_UNINTERRUPTIBLE
;
53 pr_debug("%s left refrigerator\n", current
->comm
);
54 current
->state
= save
;
57 static inline void freeze_process(struct task_struct
*p
)
63 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
65 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
69 /* 0 = success, else # of processes that we failed to stop */
70 int freeze_processes(void)
72 int todo
, nr_user
, user_frozen
;
73 unsigned long start_time
;
74 struct task_struct
*g
, *p
;
77 printk( "Stopping tasks: " );
82 read_lock(&tasklist_lock
);
83 do_each_thread(g
, p
) {
88 if (p
->mm
&& !(p
->flags
& PF_BORROWED_MM
)) {
89 /* The task is a user-space one.
90 * Freeze it unless there's a vfork completion
97 /* Freeze only if the user space is frozen */
102 } while_each_thread(g
, p
);
103 read_unlock(&tasklist_lock
);
105 if (!user_frozen
&& !nr_user
) {
107 start_time
= jiffies
;
109 user_frozen
= !nr_user
;
110 yield(); /* Yield is okay here */
111 if (todo
&& time_after(jiffies
, start_time
+ TIMEOUT
))
115 /* This does not unfreeze processes that are already frozen
116 * (we have slightly ugly calling convention in that respect,
117 * and caller must call thaw_processes() if something fails),
118 * but it cleans up leftover PF_FREEZE requests.
122 printk(KERN_ERR
" stopping tasks timed out "
123 "after %d seconds (%d tasks remaining):\n",
125 read_lock(&tasklist_lock
);
126 do_each_thread(g
, p
) {
127 if (freezeable(p
) && !frozen(p
))
128 printk(KERN_ERR
" %s\n", p
->comm
);
130 pr_debug(" clean up: %s\n", p
->comm
);
131 p
->flags
&= ~PF_FREEZE
;
132 spin_lock_irqsave(&p
->sighand
->siglock
, flags
);
133 recalc_sigpending_tsk(p
);
134 spin_unlock_irqrestore(&p
->sighand
->siglock
, flags
);
136 } while_each_thread(g
, p
);
137 read_unlock(&tasklist_lock
);
146 void thaw_processes(void)
148 struct task_struct
*g
, *p
;
150 printk( "Restarting tasks..." );
151 read_lock(&tasklist_lock
);
152 do_each_thread(g
, p
) {
155 if (!thaw_process(p
))
156 printk(KERN_INFO
" Strange, %s not stopped\n", p
->comm
);
157 } while_each_thread(g
, p
);
159 read_unlock(&tasklist_lock
);
164 EXPORT_SYMBOL(refrigerator
);