bonding: process the err returned by dev_set_allmulti properly in bond_enslave
[pandora-kernel.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 int thaw_process(struct task_struct *p);
49
50 extern void refrigerator(void);
51 extern int freeze_processes(void);
52 extern int freeze_kernel_threads(void);
53 extern void thaw_processes(void);
54 extern void thaw_kernel_threads(void);
55
56 static inline int try_to_freeze(void)
57 {
58         if (freezing(current)) {
59                 refrigerator();
60                 return 1;
61         } else
62                 return 0;
63 }
64
65 extern bool freeze_task(struct task_struct *p, bool sig_only);
66 extern void cancel_freezing(struct task_struct *p);
67
68 #ifdef CONFIG_CGROUP_FREEZER
69 extern int cgroup_freezing_or_frozen(struct task_struct *task);
70 #else /* !CONFIG_CGROUP_FREEZER */
71 static inline int cgroup_freezing_or_frozen(struct task_struct *task)
72 {
73         return 0;
74 }
75 #endif /* !CONFIG_CGROUP_FREEZER */
76
77 /*
78  * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
79  * calls wait_for_completion(&vfork) and reset right after it returns from this
80  * function.  Next, the parent should call try_to_freeze() to freeze itself
81  * appropriately in case the child has exited before the freezing of tasks is
82  * complete.  However, we don't want kernel threads to be frozen in unexpected
83  * places, so we allow them to block freeze_processes() instead or to set
84  * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
85  * parents.  Fortunately, in the ____call_usermodehelper() case the parent won't
86  * really block freeze_processes(), since ____call_usermodehelper() (the child)
87  * does a little before exec/exit and it can't be frozen before waking up the
88  * parent.
89  */
90
91 /**
92  * freezer_do_not_count - tell freezer to ignore %current if a user space task
93  *
94  * Tell freezers to ignore the current task when determining whether the
95  * target frozen state is reached.  IOW, the current task will be
96  * considered frozen enough by freezers.
97  *
98  * The caller shouldn't do anything which isn't allowed for a frozen task
99  * until freezer_cont() is called.  Usually, freezer[_do_not]_count() pair
100  * wrap a scheduling operation and nothing much else.
101  */
102 static inline void freezer_do_not_count(void)
103 {
104         if (current->mm)
105                 current->flags |= PF_FREEZER_SKIP;
106 }
107
108 /**
109  * freezer_count - tell freezer to stop ignoring %current if a user space task
110  *
111  * Undo freezer_do_not_count().  It tells freezers that %current should be
112  * considered again and tries to freeze if freezing condition is already in
113  * effect.
114  */
115 static inline void freezer_count(void)
116 {
117         if (current->mm) {
118                 current->flags &= ~PF_FREEZER_SKIP;
119                 /*
120                  * If freezing is in progress, the following paired with smp_mb()
121                  * in freezer_should_skip() ensures that either we see %true
122                  * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
123                  */
124                 smp_mb();
125                 try_to_freeze();
126         }
127 }
128
129 /**
130  * freezer_should_skip - whether to skip a task when determining frozen
131  *                       state is reached
132  * @p: task in quesion
133  *
134  * This function is used by freezers after establishing %true freezing() to
135  * test whether a task should be skipped when determining the target frozen
136  * state is reached.  IOW, if this function returns %true, @p is considered
137  * frozen enough.
138  */
139 static inline bool freezer_should_skip(struct task_struct *p)
140 {
141         /*
142          * The following smp_mb() paired with the one in freezer_count()
143          * ensures that either freezer_count() sees %true freezing() or we
144          * see cleared %PF_FREEZER_SKIP and return %false.  This makes it
145          * impossible for a task to slip frozen state testing after
146          * clearing %PF_FREEZER_SKIP.
147          */
148         smp_mb();
149         return p->flags & PF_FREEZER_SKIP;
150 }
151
152 /*
153  * Tell the freezer that the current task should be frozen by it
154  */
155 static inline void set_freezable(void)
156 {
157         current->flags &= ~PF_NOFREEZE;
158 }
159
160 /*
161  * Tell the freezer that the current task should be frozen by it and that it
162  * should send a fake signal to the task to freeze it.
163  */
164 static inline void set_freezable_with_signal(void)
165 {
166         current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
167 }
168
169 /*
170  * Freezer-friendly wrappers around wait_event_interruptible(),
171  * wait_event_killable() and wait_event_interruptible_timeout(), originally
172  * defined in <linux/wait.h>
173  */
174
175 #define wait_event_freezekillable(wq, condition)                        \
176 ({                                                                      \
177         int __retval;                                                   \
178         freezer_do_not_count();                                         \
179         __retval = wait_event_killable(wq, (condition));                \
180         freezer_count();                                                \
181         __retval;                                                       \
182 })
183
184 #define wait_event_freezable(wq, condition)                             \
185 ({                                                                      \
186         int __retval;                                                   \
187         do {                                                            \
188                 __retval = wait_event_interruptible(wq,                 \
189                                 (condition) || freezing(current));      \
190                 if (__retval && !freezing(current))                     \
191                         break;                                          \
192                 else if (!(condition))                                  \
193                         __retval = -ERESTARTSYS;                        \
194         } while (try_to_freeze());                                      \
195         __retval;                                                       \
196 })
197
198
199 #define wait_event_freezable_timeout(wq, condition, timeout)            \
200 ({                                                                      \
201         long __retval = timeout;                                        \
202         do {                                                            \
203                 __retval = wait_event_interruptible_timeout(wq,         \
204                                 (condition) || freezing(current),       \
205                                 __retval);                              \
206         } while (try_to_freeze());                                      \
207         __retval;                                                       \
208 })
209 #else /* !CONFIG_FREEZER */
210 static inline int frozen(struct task_struct *p) { return 0; }
211 static inline int freezing(struct task_struct *p) { return 0; }
212 static inline void set_freeze_flag(struct task_struct *p) {}
213 static inline void clear_freeze_flag(struct task_struct *p) {}
214 static inline int thaw_process(struct task_struct *p) { return 1; }
215
216 static inline void refrigerator(void) {}
217 static inline int freeze_processes(void) { return -ENOSYS; }
218 static inline int freeze_kernel_threads(void) { return -ENOSYS; }
219 static inline void thaw_processes(void) {}
220 static inline void thaw_kernel_threads(void) {}
221
222 static inline int try_to_freeze(void) { return 0; }
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 static inline void set_freezable_with_signal(void) {}
229
230 #define wait_event_freezable(wq, condition)                             \
231                 wait_event_interruptible(wq, condition)
232
233 #define wait_event_freezable_timeout(wq, condition, timeout)            \
234                 wait_event_interruptible_timeout(wq, condition, timeout)
235
236 #define wait_event_freezekillable(wq, condition)                \
237                 wait_event_killable(wq, condition)
238
239 #endif /* !CONFIG_FREEZER */
240
241 #endif  /* FREEZER_H_INCLUDED */