blob: 7fd81b8c48971676cd52362f09722dfd2dd28153 [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 Baines0f9548c2013-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/*
Li Fei957d1282013-02-01 08:56:03 +000017 * Timeout for stopping processes
18 */
19extern unsigned int freeze_timeout_msecs;
20
21/*
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080022 * Check if a process has been frozen
23 */
Tejun Heo948246f2011-11-21 12:32:25 -080024static inline bool frozen(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025{
26 return p->flags & PF_FROZEN;
27}
28
Tejun Heoa3201222011-11-21 12:32:25 -080029extern bool freezing_slow_path(struct task_struct *p);
30
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080031/*
32 * Check if there is a request to freeze a process
33 */
Tejun Heoa3201222011-11-21 12:32:25 -080034static inline bool freezing(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035{
Tejun Heoa3201222011-11-21 12:32:25 -080036 if (likely(!atomic_read(&system_freezing_cnt)))
37 return false;
38 return freezing_slow_path(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080039}
40
Matt Helsleydc52ddc2008-10-18 20:27:21 -070041/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080042extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080043
Tejun Heo8a32c442011-11-21 12:32:23 -080044extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020046extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080047extern void thaw_processes(void);
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +010048extern void thaw_kernel_threads(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080049
Colin Cross416ad3c2013-05-06 23:50:06 +000050/*
51 * DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION
52 * If try_to_freeze causes a lockdep warning it means the caller may deadlock
53 */
54static inline bool try_to_freeze_unsafe(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080055{
Tejun Heoa0acae02011-11-21 12:32:22 -080056 might_sleep();
57 if (likely(!freezing(current)))
58 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080059 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080060}
Nigel Cunninghamff395932006-12-06 20:34:28 -080061
Colin Cross416ad3c2013-05-06 23:50:06 +000062static inline bool try_to_freeze(void)
63{
Mandeep Singh Baines0f9548c2013-05-06 23:50:09 +000064 if (!(current->flags & PF_NOFREEZE))
65 debug_check_no_locks_held();
Colin Cross416ad3c2013-05-06 23:50:06 +000066 return try_to_freeze_unsafe();
67}
68
Tejun Heo839e3402011-11-21 12:32:26 -080069extern bool freeze_task(struct task_struct *p);
Tejun Heo34b087e2011-11-23 09:28:17 -080070extern bool set_freezable(void);
Matt Helsley8174f152008-10-18 20:27:19 -070071
Matt Helsleydc52ddc2008-10-18 20:27:21 -070072#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080073extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070074#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080075static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010076{
Tejun Heo22b4e112011-11-21 12:32:25 -080077 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010078}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070079#endif /* !CONFIG_CGROUP_FREEZER */
80
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070081/*
82 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
83 * calls wait_for_completion(&vfork) and reset right after it returns from this
84 * function. Next, the parent should call try_to_freeze() to freeze itself
85 * appropriately in case the child has exited before the freezing of tasks is
86 * complete. However, we don't want kernel threads to be frozen in unexpected
87 * places, so we allow them to block freeze_processes() instead or to set
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010088 * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
89 * parent won't really block freeze_processes(), since ____call_usermodehelper()
90 * (the child) does a little before exec/exit and it can't be frozen before
91 * waking up the parent.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070092 */
93
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010094
Tejun Heodd67d322012-10-16 15:03:14 -070095/**
96 * freezer_do_not_count - tell freezer to ignore %current
97 *
98 * Tell freezers to ignore the current task when determining whether the
99 * target frozen state is reached. IOW, the current task will be
100 * considered frozen enough by freezers.
101 *
102 * The caller shouldn't do anything which isn't allowed for a frozen task
103 * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
104 * wrap a scheduling operation and nothing much else.
105 */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700106static inline void freezer_do_not_count(void)
107{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100108 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700109}
110
Tejun Heodd67d322012-10-16 15:03:14 -0700111/**
112 * freezer_count - tell freezer to stop ignoring %current
113 *
114 * Undo freezer_do_not_count(). It tells freezers that %current should be
115 * considered again and tries to freeze if freezing condition is already in
116 * effect.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700117 */
118static inline void freezer_count(void)
119{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100120 current->flags &= ~PF_FREEZER_SKIP;
Tejun Heodd67d322012-10-16 15:03:14 -0700121 /*
122 * If freezing is in progress, the following paired with smp_mb()
123 * in freezer_should_skip() ensures that either we see %true
124 * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
125 */
126 smp_mb();
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100127 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700128}
129
Colin Cross416ad3c2013-05-06 23:50:06 +0000130/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
131static inline void freezer_count_unsafe(void)
132{
133 current->flags &= ~PF_FREEZER_SKIP;
134 smp_mb();
135 try_to_freeze_unsafe();
136}
137
Tejun Heodd67d322012-10-16 15:03:14 -0700138/**
139 * freezer_should_skip - whether to skip a task when determining frozen
140 * state is reached
141 * @p: task in quesion
142 *
143 * This function is used by freezers after establishing %true freezing() to
144 * test whether a task should be skipped when determining the target frozen
145 * state is reached. IOW, if this function returns %true, @p is considered
146 * frozen enough.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700147 */
Tejun Heodd67d322012-10-16 15:03:14 -0700148static inline bool freezer_should_skip(struct task_struct *p)
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700149{
Tejun Heodd67d322012-10-16 15:03:14 -0700150 /*
151 * The following smp_mb() paired with the one in freezer_count()
152 * ensures that either freezer_count() sees %true freezing() or we
153 * see cleared %PF_FREEZER_SKIP and return %false. This makes it
154 * impossible for a task to slip frozen state testing after
155 * clearing %PF_FREEZER_SKIP.
156 */
157 smp_mb();
158 return p->flags & PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700159}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800160
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700161/*
Colin Cross8ee492d2013-05-06 23:50:13 +0000162 * These functions are intended to be used whenever you want allow a sleeping
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200163 * task to be frozen. Note that neither return any clear indication of
164 * whether a freeze event happened while in this function.
Jeff Laytond3103102011-12-01 22:44:39 +0100165 */
166
167/* Like schedule(), but should not block the freezer. */
Colin Cross8ee492d2013-05-06 23:50:13 +0000168static inline void freezable_schedule(void)
169{
170 freezer_do_not_count();
171 schedule();
172 freezer_count();
173}
Jeff Laytond3103102011-12-01 22:44:39 +0100174
Colin Cross416ad3c2013-05-06 23:50:06 +0000175/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross8ee492d2013-05-06 23:50:13 +0000176static inline void freezable_schedule_unsafe(void)
177{
178 freezer_do_not_count();
179 schedule();
180 freezer_count_unsafe();
181}
Colin Cross416ad3c2013-05-06 23:50:06 +0000182
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000183/*
184 * Like freezable_schedule_timeout(), but should not block the freezer. Do not
185 * call this with locks held.
186 */
187static inline long freezable_schedule_timeout(long timeout)
188{
189 long __retval;
190 freezer_do_not_count();
191 __retval = schedule_timeout(timeout);
192 freezer_count();
193 return __retval;
194}
195
196/*
197 * Like schedule_timeout_interruptible(), but should not block the freezer. Do not
198 * call this with locks held.
199 */
200static inline long freezable_schedule_timeout_interruptible(long timeout)
201{
202 long __retval;
203 freezer_do_not_count();
204 __retval = schedule_timeout_interruptible(timeout);
205 freezer_count();
206 return __retval;
207}
208
Jeff Laytond3103102011-12-01 22:44:39 +0100209/* Like schedule_timeout_killable(), but should not block the freezer. */
Colin Cross8ee492d2013-05-06 23:50:13 +0000210static inline long freezable_schedule_timeout_killable(long timeout)
211{
212 long __retval;
213 freezer_do_not_count();
214 __retval = schedule_timeout_killable(timeout);
215 freezer_count();
216 return __retval;
217}
Jeff Laytond3103102011-12-01 22:44:39 +0100218
Colin Cross416ad3c2013-05-06 23:50:06 +0000219/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross8ee492d2013-05-06 23:50:13 +0000220static inline long freezable_schedule_timeout_killable_unsafe(long timeout)
221{
222 long __retval;
223 freezer_do_not_count();
224 __retval = schedule_timeout_killable(timeout);
225 freezer_count_unsafe();
226 return __retval;
227}
Colin Cross416ad3c2013-05-06 23:50:06 +0000228
Jeff Laytond3103102011-12-01 22:44:39 +0100229/*
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000230 * Like schedule_hrtimeout_range(), but should not block the freezer. Do not
231 * call this with locks held.
232 */
233static inline int freezable_schedule_hrtimeout_range(ktime_t *expires,
234 unsigned long delta, const enum hrtimer_mode mode)
235{
236 int __retval;
237 freezer_do_not_count();
238 __retval = schedule_hrtimeout_range(expires, delta, mode);
239 freezer_count();
240 return __retval;
241}
242
243/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400244 * Freezer-friendly wrappers around wait_event_interruptible(),
245 * wait_event_killable() and wait_event_interruptible_timeout(), originally
246 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700247 */
248
Jeff Laytonf06ac722011-10-19 15:30:40 -0400249#define wait_event_freezekillable(wq, condition) \
250({ \
251 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700252 freezer_do_not_count(); \
253 __retval = wait_event_killable(wq, (condition)); \
254 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400255 __retval; \
256})
257
Colin Cross5853cc22013-05-07 17:52:05 +0000258/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
259#define wait_event_freezekillable_unsafe(wq, condition) \
260({ \
261 int __retval; \
262 freezer_do_not_count(); \
263 __retval = wait_event_killable(wq, (condition)); \
264 freezer_count_unsafe(); \
265 __retval; \
266})
267
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700268#define wait_event_freezable(wq, condition) \
269({ \
270 int __retval; \
Colin Crossb0123582013-05-06 23:50:12 +0000271 freezer_do_not_count(); \
272 __retval = wait_event_interruptible(wq, (condition)); \
273 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700274 __retval; \
275})
276
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700277#define wait_event_freezable_timeout(wq, condition, timeout) \
278({ \
279 long __retval = timeout; \
Colin Crossb0123582013-05-06 23:50:12 +0000280 freezer_do_not_count(); \
281 __retval = wait_event_interruptible_timeout(wq, (condition), \
282 __retval); \
283 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700284 __retval; \
285})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800286
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000287#define wait_event_freezable_exclusive(wq, condition) \
288({ \
289 int __retval; \
290 freezer_do_not_count(); \
291 __retval = wait_event_interruptible_exclusive(wq, condition); \
292 freezer_count(); \
293 __retval; \
294})
295
296
Matt Helsley8174f152008-10-18 20:27:19 -0700297#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800298static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800299static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100300static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800301
Tejun Heo8a32c442011-11-21 12:32:23 -0800302static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200303static inline int freeze_processes(void) { return -ENOSYS; }
304static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800305static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100306static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800307
Li Haifenge5f57622012-11-23 21:55:19 +0100308static inline bool try_to_freeze_nowarn(void) { return false; }
Tejun Heoa0acae02011-11-21 12:32:22 -0800309static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800310
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700311static inline void freezer_do_not_count(void) {}
312static inline void freezer_count(void) {}
313static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700314static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700315
Jeff Laytond3103102011-12-01 22:44:39 +0100316#define freezable_schedule() schedule()
317
Colin Cross416ad3c2013-05-06 23:50:06 +0000318#define freezable_schedule_unsafe() schedule()
319
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000320#define freezable_schedule_timeout(timeout) schedule_timeout(timeout)
321
322#define freezable_schedule_timeout_interruptible(timeout) \
323 schedule_timeout_interruptible(timeout)
324
Jeff Laytond3103102011-12-01 22:44:39 +0100325#define freezable_schedule_timeout_killable(timeout) \
326 schedule_timeout_killable(timeout)
327
Colin Cross416ad3c2013-05-06 23:50:06 +0000328#define freezable_schedule_timeout_killable_unsafe(timeout) \
329 schedule_timeout_killable(timeout)
330
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000331#define freezable_schedule_hrtimeout_range(expires, delta, mode) \
332 schedule_hrtimeout_range(expires, delta, mode)
333
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700334#define wait_event_freezable(wq, condition) \
335 wait_event_interruptible(wq, condition)
336
337#define wait_event_freezable_timeout(wq, condition, timeout) \
338 wait_event_interruptible_timeout(wq, condition, timeout)
339
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000340#define wait_event_freezable_exclusive(wq, condition) \
341 wait_event_interruptible_exclusive(wq, condition)
342
Steve Frenche0c8ea12011-10-25 10:02:53 -0500343#define wait_event_freezekillable(wq, condition) \
344 wait_event_killable(wq, condition)
345
Colin Cross5853cc22013-05-07 17:52:05 +0000346#define wait_event_freezekillable_unsafe(wq, condition) \
347 wait_event_killable(wq, condition)
348
Matt Helsley8174f152008-10-18 20:27:19 -0700349#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700350
351#endif /* FREEZER_H_INCLUDED */