043a5cf8b5baf3e917dd9ba265c365a334365216
[linux-3.10.git] / include / linux / freezer.h
1 /* Freezer declarations */
2
3 #ifndef FREEZER_H_INCLUDED
4 #define FREEZER_H_INCLUDED
5
6 #include <linux/debug_locks.h>
7 #include <linux/sched.h>
8 #include <linux/wait.h>
9 #include <linux/atomic.h>
10
11 #ifdef CONFIG_FREEZER
12 extern atomic_t system_freezing_cnt;    /* nr of freezing conds in effect */
13 extern bool pm_freezing;                /* PM freezing in effect */
14 extern bool pm_nosig_freezing;          /* PM nosig freezing in effect */
15
16 /*
17  * Timeout for stopping processes
18  */
19 extern unsigned int freeze_timeout_msecs;
20
21 /*
22  * Check if a process has been frozen
23  */
24 static inline bool frozen(struct task_struct *p)
25 {
26         return p->flags & PF_FROZEN;
27 }
28
29 extern bool freezing_slow_path(struct task_struct *p);
30
31 /*
32  * Check if there is a request to freeze a process
33  */
34 static inline bool freezing(struct task_struct *p)
35 {
36         if (likely(!atomic_read(&system_freezing_cnt)))
37                 return false;
38         return freezing_slow_path(p);
39 }
40
41 /* Takes and releases task alloc lock using task_lock() */
42 extern void __thaw_task(struct task_struct *t);
43
44 extern bool __refrigerator(bool check_kthr_stop);
45 extern int freeze_processes(void);
46 extern int freeze_kernel_threads(void);
47 extern void thaw_processes(void);
48 extern void thaw_kernel_threads(void);
49
50 static inline bool try_to_freeze(void)
51 {
52         if (!(current->flags & PF_NOFREEZE))
53                 debug_check_no_locks_held();
54         might_sleep();
55         if (likely(!freezing(current)))
56                 return false;
57         return __refrigerator(false);
58 }
59
60 extern bool freeze_task(struct task_struct *p);
61 extern bool set_freezable(void);
62
63 #ifdef CONFIG_CGROUP_FREEZER
64 extern bool cgroup_freezing(struct task_struct *task);
65 #else /* !CONFIG_CGROUP_FREEZER */
66 static inline bool cgroup_freezing(struct task_struct *task)
67 {
68         return false;
69 }
70 #endif /* !CONFIG_CGROUP_FREEZER */
71
72 /*
73  * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
74  * calls wait_for_completion(&vfork) and reset right after it returns from this
75  * function.  Next, the parent should call try_to_freeze() to freeze itself
76  * appropriately in case the child has exited before the freezing of tasks is
77  * complete.  However, we don't want kernel threads to be frozen in unexpected
78  * places, so we allow them to block freeze_processes() instead or to set
79  * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
80  * parent won't really block freeze_processes(), since ____call_usermodehelper()
81  * (the child) does a little before exec/exit and it can't be frozen before
82  * waking up the parent.
83  */
84
85
86 /**
87  * freezer_do_not_count - tell freezer to ignore %current
88  *
89  * Tell freezers to ignore the current task when determining whether the
90  * target frozen state is reached.  IOW, the current task will be
91  * considered frozen enough by freezers.
92  *
93  * The caller shouldn't do anything which isn't allowed for a frozen task
94  * until freezer_cont() is called.  Usually, freezer[_do_not]_count() pair
95  * wrap a scheduling operation and nothing much else.
96  */
97 static inline void freezer_do_not_count(void)
98 {
99         current->flags |= PF_FREEZER_SKIP;
100 }
101
102 /**
103  * freezer_count - tell freezer to stop ignoring %current
104  *
105  * Undo freezer_do_not_count().  It tells freezers that %current should be
106  * considered again and tries to freeze if freezing condition is already in
107  * effect.
108  */
109 static inline void freezer_count(void)
110 {
111         current->flags &= ~PF_FREEZER_SKIP;
112         /*
113          * If freezing is in progress, the following paired with smp_mb()
114          * in freezer_should_skip() ensures that either we see %true
115          * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
116          */
117         smp_mb();
118         try_to_freeze();
119 }
120
121 /**
122  * freezer_should_skip - whether to skip a task when determining frozen
123  *                       state is reached
124  * @p: task in quesion
125  *
126  * This function is used by freezers after establishing %true freezing() to
127  * test whether a task should be skipped when determining the target frozen
128  * state is reached.  IOW, if this function returns %true, @p is considered
129  * frozen enough.
130  */
131 static inline bool freezer_should_skip(struct task_struct *p)
132 {
133         /*
134          * The following smp_mb() paired with the one in freezer_count()
135          * ensures that either freezer_count() sees %true freezing() or we
136          * see cleared %PF_FREEZER_SKIP and return %false.  This makes it
137          * impossible for a task to slip frozen state testing after
138          * clearing %PF_FREEZER_SKIP.
139          */
140         smp_mb();
141         return p->flags & PF_FREEZER_SKIP;
142 }
143
144 /*
145  * These macros are intended to be used whenever you want allow a sleeping
146  * task to be frozen. Note that neither return any clear indication of
147  * whether a freeze event happened while in this function.
148  */
149
150 /* Like schedule(), but should not block the freezer. */
151 #define freezable_schedule()                                            \
152 ({                                                                      \
153         freezer_do_not_count();                                         \
154         schedule();                                                     \
155         freezer_count();                                                \
156 })
157
158 /* Like schedule_timeout_killable(), but should not block the freezer. */
159 #define freezable_schedule_timeout_killable(timeout)                    \
160 ({                                                                      \
161         long __retval;                                                  \
162         freezer_do_not_count();                                         \
163         __retval = schedule_timeout_killable(timeout);                  \
164         freezer_count();                                                \
165         __retval;                                                       \
166 })
167
168 /*
169  * Freezer-friendly wrappers around wait_event_interruptible(),
170  * wait_event_killable() and wait_event_interruptible_timeout(), originally
171  * defined in <linux/wait.h>
172  */
173
174 #define wait_event_freezekillable(wq, condition)                        \
175 ({                                                                      \
176         int __retval;                                                   \
177         freezer_do_not_count();                                         \
178         __retval = wait_event_killable(wq, (condition));                \
179         freezer_count();                                                \
180         __retval;                                                       \
181 })
182
183 #define wait_event_freezable(wq, condition)                             \
184 ({                                                                      \
185         int __retval;                                                   \
186         for (;;) {                                                      \
187                 __retval = wait_event_interruptible(wq,                 \
188                                 (condition) || freezing(current));      \
189                 if (__retval || (condition))                            \
190                         break;                                          \
191                 try_to_freeze();                                        \
192         }                                                               \
193         __retval;                                                       \
194 })
195
196 #define wait_event_freezable_timeout(wq, condition, timeout)            \
197 ({                                                                      \
198         long __retval = timeout;                                        \
199         for (;;) {                                                      \
200                 __retval = wait_event_interruptible_timeout(wq,         \
201                                 (condition) || freezing(current),       \
202                                 __retval);                              \
203                 if (__retval <= 0 || (condition))                       \
204                         break;                                          \
205                 try_to_freeze();                                        \
206         }                                                               \
207         __retval;                                                       \
208 })
209
210 #else /* !CONFIG_FREEZER */
211 static inline bool frozen(struct task_struct *p) { return false; }
212 static inline bool freezing(struct task_struct *p) { return false; }
213 static inline void __thaw_task(struct task_struct *t) {}
214
215 static inline bool __refrigerator(bool check_kthr_stop) { return false; }
216 static inline int freeze_processes(void) { return -ENOSYS; }
217 static inline int freeze_kernel_threads(void) { return -ENOSYS; }
218 static inline void thaw_processes(void) {}
219 static inline void thaw_kernel_threads(void) {}
220
221 static inline bool try_to_freeze_nowarn(void) { return false; }
222 static inline bool try_to_freeze(void) { return false; }
223
224 static inline void freezer_do_not_count(void) {}
225 static inline void freezer_count(void) {}
226 static inline int freezer_should_skip(struct task_struct *p) { return 0; }
227 static inline void set_freezable(void) {}
228
229 #define freezable_schedule()  schedule()
230
231 #define freezable_schedule_timeout_killable(timeout)                    \
232         schedule_timeout_killable(timeout)
233
234 #define wait_event_freezable(wq, condition)                             \
235                 wait_event_interruptible(wq, condition)
236
237 #define wait_event_freezable_timeout(wq, condition, timeout)            \
238                 wait_event_interruptible_timeout(wq, condition, timeout)
239
240 #define wait_event_freezekillable(wq, condition)                \
241                 wait_event_killable(wq, condition)
242
243 #endif /* !CONFIG_FREEZER */
244
245 #endif  /* FREEZER_H_INCLUDED */