blob: c9435252e8e45e9cc9b7857cc7c4185359a51b7a [file] [log] [blame]
Nigel Cunningham7dfb7102006-12-06 20:34:23 -08001/* Freezer declarations */
2
Randy Dunlap5c543ef2006-12-10 02:18:58 -08003#include <linux/sched.h>
4
Nigel Cunningham7dfb7102006-12-06 20:34:23 -08005#ifdef CONFIG_PM
6/*
7 * Check if a process has been frozen
8 */
9static inline int frozen(struct task_struct *p)
10{
11 return p->flags & PF_FROZEN;
12}
13
14/*
15 * Check if there is a request to freeze a process
16 */
17static inline int freezing(struct task_struct *p)
18{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080019 return test_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080020}
21
22/*
23 * Request that a process be frozen
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024 */
25static inline void freeze(struct task_struct *p)
26{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080027 set_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028}
29
30/*
31 * Sometimes we may need to cancel the previous 'freeze' request
32 */
33static inline void do_not_freeze(struct task_struct *p)
34{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080035 clear_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036}
37
38/*
39 * Wake up a frozen process
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070040 *
41 * task_lock() is taken to prevent the race with refrigerator() which may
42 * occur if the freezing of tasks fails. Namely, without the lock, if the
43 * freezing of tasks failed, thaw_tasks() might have run before a task in
44 * refrigerator() could call frozen_process(), in which case the task would be
45 * frozen and no one would thaw it.
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046 */
47static inline int thaw_process(struct task_struct *p)
48{
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070049 task_lock(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080050 if (frozen(p)) {
51 p->flags &= ~PF_FROZEN;
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070052 task_unlock(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080053 wake_up_process(p);
54 return 1;
55 }
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070056 clear_tsk_thread_flag(p, TIF_FREEZE);
57 task_unlock(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058 return 0;
59}
60
61/*
62 * freezing is complete, mark process as frozen
63 */
64static inline void frozen_process(struct task_struct *p)
65{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080066 p->flags |= PF_FROZEN;
67 wmb();
68 clear_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080069}
70
71extern void refrigerator(void);
72extern int freeze_processes(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080073extern void thaw_processes(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080074
75static inline int try_to_freeze(void)
76{
77 if (freezing(current)) {
78 refrigerator();
79 return 1;
80 } else
81 return 0;
82}
Nigel Cunninghamff395932006-12-06 20:34:28 -080083
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070084/*
85 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
86 * calls wait_for_completion(&vfork) and reset right after it returns from this
87 * function. Next, the parent should call try_to_freeze() to freeze itself
88 * appropriately in case the child has exited before the freezing of tasks is
89 * complete. However, we don't want kernel threads to be frozen in unexpected
90 * places, so we allow them to block freeze_processes() instead or to set
91 * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
92 * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
93 * really block freeze_processes(), since ____call_usermodehelper() (the child)
94 * does a little before exec/exit and it can't be frozen before waking up the
95 * parent.
96 */
97
98/*
99 * If the current task is a user space one, tell the freezer not to count it as
100 * freezable.
101 */
102static inline void freezer_do_not_count(void)
103{
104 if (current->mm)
105 current->flags |= PF_FREEZER_SKIP;
106}
107
108/*
109 * If the current task is a user space one, tell the freezer to count it as
110 * freezable again and try to freeze it.
111 */
112static inline void freezer_count(void)
113{
114 if (current->mm) {
115 current->flags &= ~PF_FREEZER_SKIP;
116 try_to_freeze();
117 }
118}
119
120/*
121 * Check if the task should be counted as freezeable by the freezer
122 */
123static inline int freezer_should_skip(struct task_struct *p)
124{
125 return !!(p->flags & PF_FREEZER_SKIP);
126}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800127
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800128#else
129static inline int frozen(struct task_struct *p) { return 0; }
130static inline int freezing(struct task_struct *p) { return 0; }
131static inline void freeze(struct task_struct *p) { BUG(); }
132static inline int thaw_process(struct task_struct *p) { return 1; }
133static inline void frozen_process(struct task_struct *p) { BUG(); }
134
135static inline void refrigerator(void) {}
136static inline int freeze_processes(void) { BUG(); return 0; }
137static inline void thaw_processes(void) {}
138
139static inline int try_to_freeze(void) { return 0; }
140
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700141static inline void freezer_do_not_count(void) {}
142static inline void freezer_count(void) {}
143static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800144#endif