b2b4abc5a739face19747ef36c1990ef81a18965
[cascardo/linux.git] / include / linux / freezer.h
1 /* Freezer declarations */
2
3 #ifndef FREEZER_H_INCLUDED
4 #define FREEZER_H_INCLUDED
5
6 #include <linux/sched.h>
7 #include <linux/wait.h>
8
9 #ifdef CONFIG_FREEZER
10 /*
11  * Check if a process has been frozen
12  */
13 static inline int frozen(struct task_struct *p)
14 {
15         return p->flags & PF_FROZEN;
16 }
17
18 /*
19  * Check if there is a request to freeze a process
20  */
21 static inline int freezing(struct task_struct *p)
22 {
23         return test_tsk_thread_flag(p, TIF_FREEZE);
24 }
25
26 /*
27  * Request that a process be frozen
28  */
29 static inline void set_freeze_flag(struct task_struct *p)
30 {
31         set_tsk_thread_flag(p, TIF_FREEZE);
32 }
33
34 /*
35  * Sometimes we may need to cancel the previous 'freeze' request
36  */
37 static inline void clear_freeze_flag(struct task_struct *p)
38 {
39         clear_tsk_thread_flag(p, TIF_FREEZE);
40 }
41
42 static inline bool should_send_signal(struct task_struct *p)
43 {
44         return !(p->flags & PF_FREEZER_NOSIG);
45 }
46
47 /* Takes and releases task alloc lock using task_lock() */
48 extern void __thaw_task(struct task_struct *t);
49
50 extern bool __refrigerator(bool check_kthr_stop);
51 extern int freeze_processes(void);
52 extern int freeze_kernel_threads(void);
53 extern void thaw_processes(void);
54
55 static inline bool try_to_freeze(void)
56 {
57         might_sleep();
58         if (likely(!freezing(current)))
59                 return false;
60         return __refrigerator(false);
61 }
62
63 extern bool freeze_task(struct task_struct *p, bool sig_only);
64
65 #ifdef CONFIG_CGROUP_FREEZER
66 extern bool cgroup_freezing(struct task_struct *task);
67 #else /* !CONFIG_CGROUP_FREEZER */
68 static inline bool cgroup_freezing(struct task_struct *task)
69 {
70         return false;
71 }
72 #endif /* !CONFIG_CGROUP_FREEZER */
73
74 /*
75  * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
76  * calls wait_for_completion(&vfork) and reset right after it returns from this
77  * function.  Next, the parent should call try_to_freeze() to freeze itself
78  * appropriately in case the child has exited before the freezing of tasks is
79  * complete.  However, we don't want kernel threads to be frozen in unexpected
80  * places, so we allow them to block freeze_processes() instead or to set
81  * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
82  * parents.  Fortunately, in the ____call_usermodehelper() case the parent won't
83  * really block freeze_processes(), since ____call_usermodehelper() (the child)
84  * does a little before exec/exit and it can't be frozen before waking up the
85  * parent.
86  */
87
88 /*
89  * If the current task is a user space one, tell the freezer not to count it as
90  * freezable.
91  */
92 static inline void freezer_do_not_count(void)
93 {
94         if (current->mm)
95                 current->flags |= PF_FREEZER_SKIP;
96 }
97
98 /*
99  * If the current task is a user space one, tell the freezer to count it as
100  * freezable again and try to freeze it.
101  */
102 static inline void freezer_count(void)
103 {
104         if (current->mm) {
105                 current->flags &= ~PF_FREEZER_SKIP;
106                 try_to_freeze();
107         }
108 }
109
110 /*
111  * Check if the task should be counted as freezable by the freezer
112  */
113 static inline int freezer_should_skip(struct task_struct *p)
114 {
115         return !!(p->flags & PF_FREEZER_SKIP);
116 }
117
118 /*
119  * Tell the freezer that the current task should be frozen by it
120  */
121 static inline void set_freezable(void)
122 {
123         current->flags &= ~PF_NOFREEZE;
124 }
125
126 /*
127  * Tell the freezer that the current task should be frozen by it and that it
128  * should send a fake signal to the task to freeze it.
129  */
130 static inline void set_freezable_with_signal(void)
131 {
132         current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
133 }
134
135 /*
136  * Freezer-friendly wrappers around wait_event_interruptible(),
137  * wait_event_killable() and wait_event_interruptible_timeout(), originally
138  * defined in <linux/wait.h>
139  */
140
141 #define wait_event_freezekillable(wq, condition)                        \
142 ({                                                                      \
143         int __retval;                                                   \
144         freezer_do_not_count();                                         \
145         __retval = wait_event_killable(wq, (condition));                \
146         freezer_count();                                                \
147         __retval;                                                       \
148 })
149
150 #define wait_event_freezable(wq, condition)                             \
151 ({                                                                      \
152         int __retval;                                                   \
153         do {                                                            \
154                 __retval = wait_event_interruptible(wq,                 \
155                                 (condition) || freezing(current));      \
156                 if (__retval && !freezing(current))                     \
157                         break;                                          \
158                 else if (!(condition))                                  \
159                         __retval = -ERESTARTSYS;                        \
160         } while (try_to_freeze());                                      \
161         __retval;                                                       \
162 })
163
164
165 #define wait_event_freezable_timeout(wq, condition, timeout)            \
166 ({                                                                      \
167         long __retval = timeout;                                        \
168         do {                                                            \
169                 __retval = wait_event_interruptible_timeout(wq,         \
170                                 (condition) || freezing(current),       \
171                                 __retval);                              \
172         } while (try_to_freeze());                                      \
173         __retval;                                                       \
174 })
175 #else /* !CONFIG_FREEZER */
176 static inline int frozen(struct task_struct *p) { return 0; }
177 static inline int freezing(struct task_struct *p) { return 0; }
178 static inline void set_freeze_flag(struct task_struct *p) {}
179 static inline void clear_freeze_flag(struct task_struct *p) {}
180
181 static inline bool __refrigerator(bool check_kthr_stop) { return false; }
182 static inline int freeze_processes(void) { return -ENOSYS; }
183 static inline int freeze_kernel_threads(void) { return -ENOSYS; }
184 static inline void thaw_processes(void) {}
185
186 static inline bool try_to_freeze(void) { return false; }
187
188 static inline void freezer_do_not_count(void) {}
189 static inline void freezer_count(void) {}
190 static inline int freezer_should_skip(struct task_struct *p) { return 0; }
191 static inline void set_freezable(void) {}
192 static inline void set_freezable_with_signal(void) {}
193
194 #define wait_event_freezable(wq, condition)                             \
195                 wait_event_interruptible(wq, condition)
196
197 #define wait_event_freezable_timeout(wq, condition, timeout)            \
198                 wait_event_interruptible_timeout(wq, condition, timeout)
199
200 #define wait_event_freezekillable(wq, condition)                \
201                 wait_event_killable(wq, condition)
202
203 #endif /* !CONFIG_FREEZER */
204
205 #endif  /* FREEZER_H_INCLUDED */