blob: b56d26c53b7eeac910a9520b4f6a09ee33606922 [file] [log] [blame]
Nigel Cunningham7dfb7102006-12-06 20:34:23 -08001/* Freezer declarations */
2
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003#ifndef FREEZER_H_INCLUDED
4#define FREEZER_H_INCLUDED
5
Mandeep Singh Bainese7f8bab2013-05-06 23:50:09 +00006#include <linux/debug_locks.h>
Randy Dunlap5c543ef2006-12-10 02:18:58 -08007#include <linux/sched.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07008#include <linux/wait.h>
Tejun Heoa3201222011-11-21 12:32:25 -08009#include <linux/atomic.h>
Randy Dunlap5c543ef2006-12-10 02:18:58 -080010
Matt Helsley8174f152008-10-18 20:27:19 -070011#ifdef CONFIG_FREEZER
Tejun Heoa3201222011-11-21 12:32:25 -080012extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */
13extern bool pm_freezing; /* PM freezing in effect */
14extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
15
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080016/*
17 * Check if a process has been frozen
18 */
Tejun Heo948246f2011-11-21 12:32:25 -080019static inline bool frozen(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080020{
21 return p->flags & PF_FROZEN;
22}
23
Tejun Heoa3201222011-11-21 12:32:25 -080024extern bool freezing_slow_path(struct task_struct *p);
25
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026/*
27 * Check if there is a request to freeze a process
28 */
Tejun Heoa3201222011-11-21 12:32:25 -080029static inline bool freezing(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080030{
Tejun Heoa3201222011-11-21 12:32:25 -080031 if (likely(!atomic_read(&system_freezing_cnt)))
32 return false;
33 return freezing_slow_path(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080034}
35
Matt Helsleydc52ddc2008-10-18 20:27:21 -070036/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080037extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038
Tejun Heo8a32c442011-11-21 12:32:23 -080039extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020041extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080042extern void thaw_processes(void);
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +010043extern void thaw_kernel_threads(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080044
Colin Cross8d43d952012-08-15 13:10:04 -070045/*
46 * HACK: prevent sleeping while atomic warnings due to ARM signal handling
47 * disabling irqs
48 */
49static inline bool try_to_freeze_nowarn(void)
50{
51 if (likely(!freezing(current)))
52 return false;
53 return __refrigerator(false);
54}
55
Colin Cross3be91672013-05-06 23:50:06 +000056/*
57 * DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION
58 * If try_to_freeze causes a lockdep warning it means the caller may deadlock
59 */
60static inline bool try_to_freeze_unsafe(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080061{
Steve Muckledc0eed42012-05-23 08:49:36 -070062/* This causes problems for ARM targets and is a known
63 * problem upstream.
64 * might_sleep();
65 */
Tejun Heoa0acae02011-11-21 12:32:22 -080066 if (likely(!freezing(current)))
67 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080068 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080069}
Nigel Cunninghamff395932006-12-06 20:34:28 -080070
Colin Cross3be91672013-05-06 23:50:06 +000071static inline bool try_to_freeze(void)
72{
Mandeep Singh Bainese7f8bab2013-05-06 23:50:09 +000073 if (!(current->flags & PF_NOFREEZE))
74 debug_check_no_locks_held();
Colin Cross3be91672013-05-06 23:50:06 +000075 return try_to_freeze_unsafe();
76}
77
Tejun Heo839e3402011-11-21 12:32:26 -080078extern bool freeze_task(struct task_struct *p);
Tejun Heo34b087e2011-11-23 09:28:17 -080079extern bool set_freezable(void);
Matt Helsley8174f152008-10-18 20:27:19 -070080
Matt Helsleydc52ddc2008-10-18 20:27:21 -070081#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080082extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070083#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080084static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010085{
Tejun Heo22b4e112011-11-21 12:32:25 -080086 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010087}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070088#endif /* !CONFIG_CGROUP_FREEZER */
89
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070090/*
91 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
92 * calls wait_for_completion(&vfork) and reset right after it returns from this
93 * function. Next, the parent should call try_to_freeze() to freeze itself
94 * appropriately in case the child has exited before the freezing of tasks is
95 * complete. However, we don't want kernel threads to be frozen in unexpected
96 * places, so we allow them to block freeze_processes() instead or to set
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010097 * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
98 * parent won't really block freeze_processes(), since ____call_usermodehelper()
99 * (the child) does a little before exec/exit and it can't be frozen before
100 * waking up the parent.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700101 */
102
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100103
104/* Tell the freezer not to count the current task as freezable. */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700105static inline void freezer_do_not_count(void)
106{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100107 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700108}
109
110/*
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100111 * Tell the freezer to count the current task as freezable again and try to
112 * freeze it.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700113 */
114static inline void freezer_count(void)
115{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100116 current->flags &= ~PF_FREEZER_SKIP;
117 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700118}
119
Colin Cross3be91672013-05-06 23:50:06 +0000120/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
121static inline void freezer_count_unsafe(void)
122{
123 current->flags &= ~PF_FREEZER_SKIP;
124 smp_mb();
125 try_to_freeze_unsafe();
126}
127
128/**
129 * freezer_should_skip - whether to skip a task when determining frozen
130 * state is reached
131 * @p: task in quesion
132 *
133 * This function is used by freezers after establishing %true freezing() to
134 * test whether a task should be skipped when determining the target frozen
135 * state is reached. IOW, if this function returns %true, @p is considered
136 * frozen enough.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700137 */
138static inline int freezer_should_skip(struct task_struct *p)
139{
140 return !!(p->flags & PF_FREEZER_SKIP);
141}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800142
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700143/*
Colin Cross13098dc2013-05-06 23:50:13 +0000144 * These functions are intended to be used whenever you want allow a task that's
Jeff Laytond3103102011-12-01 22:44:39 +0100145 * sleeping in TASK_UNINTERRUPTIBLE or TASK_KILLABLE state to be frozen. Note
146 * that neither return any clear indication of whether a freeze event happened
147 * while in this function.
148 */
149
150/* Like schedule(), but should not block the freezer. */
Colin Cross13098dc2013-05-06 23:50:13 +0000151static inline void freezable_schedule(void)
152{
153 freezer_do_not_count();
154 schedule();
155 freezer_count();
156}
Jeff Laytond3103102011-12-01 22:44:39 +0100157
Colin Cross3be91672013-05-06 23:50:06 +0000158/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross13098dc2013-05-06 23:50:13 +0000159static inline void freezable_schedule_unsafe(void)
160{
161 freezer_do_not_count();
162 schedule();
163 freezer_count_unsafe();
164}
Colin Cross3be91672013-05-06 23:50:06 +0000165
Colin Cross44cd98f2013-05-06 23:50:14 +0000166/*
167 * Like freezable_schedule_timeout(), but should not block the freezer. Do not
168 * call this with locks held.
169 */
170static inline long freezable_schedule_timeout(long timeout)
171{
172 long __retval;
173 freezer_do_not_count();
174 __retval = schedule_timeout(timeout);
175 freezer_count();
176 return __retval;
177}
178
179/*
180 * Like schedule_timeout_interruptible(), but should not block the freezer. Do not
181 * call this with locks held.
182 */
183static inline long freezable_schedule_timeout_interruptible(long timeout)
184{
185 long __retval;
186 freezer_do_not_count();
187 __retval = schedule_timeout_interruptible(timeout);
188 freezer_count();
189 return __retval;
190}
191
Jeff Laytond3103102011-12-01 22:44:39 +0100192/* Like schedule_timeout_killable(), but should not block the freezer. */
Colin Cross13098dc2013-05-06 23:50:13 +0000193static inline long freezable_schedule_timeout_killable(long timeout)
194{
195 long __retval;
196 freezer_do_not_count();
197 __retval = schedule_timeout_killable(timeout);
198 freezer_count();
199 return __retval;
200}
Jeff Laytond3103102011-12-01 22:44:39 +0100201
Colin Cross3be91672013-05-06 23:50:06 +0000202/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross13098dc2013-05-06 23:50:13 +0000203static inline long freezable_schedule_timeout_killable_unsafe(long timeout)
204{
205 long __retval;
206 freezer_do_not_count();
207 __retval = schedule_timeout_killable(timeout);
208 freezer_count_unsafe();
209 return __retval;
210}
Colin Cross3be91672013-05-06 23:50:06 +0000211
Jeff Laytond3103102011-12-01 22:44:39 +0100212/*
Colin Cross44cd98f2013-05-06 23:50:14 +0000213 * Like schedule_hrtimeout_range(), but should not block the freezer. Do not
214 * call this with locks held.
215 */
216static inline int freezable_schedule_hrtimeout_range(ktime_t *expires,
217 unsigned long delta, const enum hrtimer_mode mode)
218{
219 int __retval;
220 freezer_do_not_count();
221 __retval = schedule_hrtimeout_range(expires, delta, mode);
222 freezer_count();
223 return __retval;
224}
225
226/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400227 * Freezer-friendly wrappers around wait_event_interruptible(),
228 * wait_event_killable() and wait_event_interruptible_timeout(), originally
229 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700230 */
231
Jeff Laytonf06ac722011-10-19 15:30:40 -0400232#define wait_event_freezekillable(wq, condition) \
233({ \
234 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700235 freezer_do_not_count(); \
236 __retval = wait_event_killable(wq, (condition)); \
237 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400238 __retval; \
239})
240
Colin Crosscf46c3d2013-05-07 17:52:05 +0000241/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
242#define wait_event_freezekillable_unsafe(wq, condition) \
243({ \
244 int __retval; \
245 freezer_do_not_count(); \
246 __retval = wait_event_killable(wq, (condition)); \
247 freezer_count_unsafe(); \
248 __retval; \
249})
250
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700251#define wait_event_freezable(wq, condition) \
252({ \
253 int __retval; \
Colin Cross3b784312013-05-06 23:50:12 +0000254 freezer_do_not_count(); \
255 __retval = wait_event_interruptible(wq, (condition)); \
256 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700257 __retval; \
258})
259
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700260#define wait_event_freezable_timeout(wq, condition, timeout) \
261({ \
262 long __retval = timeout; \
Colin Cross3b784312013-05-06 23:50:12 +0000263 freezer_do_not_count(); \
264 __retval = wait_event_interruptible_timeout(wq, (condition), \
265 __retval); \
266 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700267 __retval; \
268})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800269
Colin Cross44cd98f2013-05-06 23:50:14 +0000270#define wait_event_freezable_exclusive(wq, condition) \
271({ \
272 int __retval; \
273 freezer_do_not_count(); \
274 __retval = wait_event_interruptible_exclusive(wq, condition); \
275 freezer_count(); \
276 __retval; \
277})
278
279
Matt Helsley8174f152008-10-18 20:27:19 -0700280#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800281static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800282static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100283static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800284
Tejun Heo8a32c442011-11-21 12:32:23 -0800285static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200286static inline int freeze_processes(void) { return -ENOSYS; }
287static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800288static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100289static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800290
Tejun Heoa0acae02011-11-21 12:32:22 -0800291static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800292
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700293static inline void freezer_do_not_count(void) {}
294static inline void freezer_count(void) {}
295static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700296static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700297
Jeff Laytond3103102011-12-01 22:44:39 +0100298#define freezable_schedule() schedule()
299
Colin Cross3be91672013-05-06 23:50:06 +0000300#define freezable_schedule_unsafe() schedule()
301
Colin Cross44cd98f2013-05-06 23:50:14 +0000302#define freezable_schedule_timeout(timeout) schedule_timeout(timeout)
303
304#define freezable_schedule_timeout_interruptible(timeout) \
305 schedule_timeout_interruptible(timeout)
306
Jeff Laytond3103102011-12-01 22:44:39 +0100307#define freezable_schedule_timeout_killable(timeout) \
308 schedule_timeout_killable(timeout)
309
Colin Cross3be91672013-05-06 23:50:06 +0000310#define freezable_schedule_timeout_killable_unsafe(timeout) \
311 schedule_timeout_killable(timeout)
312
Colin Cross44cd98f2013-05-06 23:50:14 +0000313#define freezable_schedule_hrtimeout_range(expires, delta, mode) \
314 schedule_hrtimeout_range(expires, delta, mode)
315
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700316#define wait_event_freezable(wq, condition) \
317 wait_event_interruptible(wq, condition)
318
319#define wait_event_freezable_timeout(wq, condition, timeout) \
320 wait_event_interruptible_timeout(wq, condition, timeout)
321
Colin Cross44cd98f2013-05-06 23:50:14 +0000322#define wait_event_freezable_exclusive(wq, condition) \
323 wait_event_interruptible_exclusive(wq, condition)
324
Steve Frenche0c8ea12011-10-25 10:02:53 -0500325#define wait_event_freezekillable(wq, condition) \
326 wait_event_killable(wq, condition)
327
Colin Crosscf46c3d2013-05-07 17:52:05 +0000328#define wait_event_freezekillable_unsafe(wq, condition) \
329 wait_event_killable(wq, condition)
330
Matt Helsley8174f152008-10-18 20:27:19 -0700331#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700332
333#endif /* FREEZER_H_INCLUDED */