blob: ed6dc73311eb2adb5f4fc5d861ae9756b9c3f3e3 [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/*
Jeff Laytond3103102011-12-01 22:44:39 +0100144 * These macros are intended to be used whenever you want allow a task that's
145 * 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. */
151#define freezable_schedule() \
152({ \
153 freezer_do_not_count(); \
154 schedule(); \
155 freezer_count(); \
156})
157
Colin Cross3be91672013-05-06 23:50:06 +0000158/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
159#define freezable_schedule_unsafe() \
160({ \
161 freezer_do_not_count(); \
162 schedule(); \
163 freezer_count_unsafe(); \
164})
165
Jeff Laytond3103102011-12-01 22:44:39 +0100166/* Like schedule_timeout_killable(), but should not block the freezer. */
167#define freezable_schedule_timeout_killable(timeout) \
168({ \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100169 long __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100170 freezer_do_not_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100171 __retval = schedule_timeout_killable(timeout); \
Jeff Laytond3103102011-12-01 22:44:39 +0100172 freezer_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100173 __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100174})
175
Colin Cross3be91672013-05-06 23:50:06 +0000176/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
177#define freezable_schedule_timeout_killable_unsafe(timeout) \
178({ \
179 long __retval; \
180 freezer_do_not_count(); \
181 __retval = schedule_timeout_killable(timeout); \
182 freezer_count_unsafe(); \
183 __retval; \
184})
185
Jeff Laytond3103102011-12-01 22:44:39 +0100186/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400187 * Freezer-friendly wrappers around wait_event_interruptible(),
188 * wait_event_killable() and wait_event_interruptible_timeout(), originally
189 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700190 */
191
Jeff Laytonf06ac722011-10-19 15:30:40 -0400192#define wait_event_freezekillable(wq, condition) \
193({ \
194 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700195 freezer_do_not_count(); \
196 __retval = wait_event_killable(wq, (condition)); \
197 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400198 __retval; \
199})
200
Colin Crosscf46c3d2013-05-07 17:52:05 +0000201/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
202#define wait_event_freezekillable_unsafe(wq, condition) \
203({ \
204 int __retval; \
205 freezer_do_not_count(); \
206 __retval = wait_event_killable(wq, (condition)); \
207 freezer_count_unsafe(); \
208 __retval; \
209})
210
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700211#define wait_event_freezable(wq, condition) \
212({ \
213 int __retval; \
Colin Cross3b784312013-05-06 23:50:12 +0000214 freezer_do_not_count(); \
215 __retval = wait_event_interruptible(wq, (condition)); \
216 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700217 __retval; \
218})
219
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700220#define wait_event_freezable_timeout(wq, condition, timeout) \
221({ \
222 long __retval = timeout; \
Colin Cross3b784312013-05-06 23:50:12 +0000223 freezer_do_not_count(); \
224 __retval = wait_event_interruptible_timeout(wq, (condition), \
225 __retval); \
226 freezer_count(); \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700227 __retval; \
228})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800229
Matt Helsley8174f152008-10-18 20:27:19 -0700230#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800231static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800232static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100233static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800234
Tejun Heo8a32c442011-11-21 12:32:23 -0800235static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200236static inline int freeze_processes(void) { return -ENOSYS; }
237static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800238static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100239static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800240
Tejun Heoa0acae02011-11-21 12:32:22 -0800241static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800242
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700243static inline void freezer_do_not_count(void) {}
244static inline void freezer_count(void) {}
245static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700246static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700247
Jeff Laytond3103102011-12-01 22:44:39 +0100248#define freezable_schedule() schedule()
249
Colin Cross3be91672013-05-06 23:50:06 +0000250#define freezable_schedule_unsafe() schedule()
251
Jeff Laytond3103102011-12-01 22:44:39 +0100252#define freezable_schedule_timeout_killable(timeout) \
253 schedule_timeout_killable(timeout)
254
Colin Cross3be91672013-05-06 23:50:06 +0000255#define freezable_schedule_timeout_killable_unsafe(timeout) \
256 schedule_timeout_killable(timeout)
257
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700258#define wait_event_freezable(wq, condition) \
259 wait_event_interruptible(wq, condition)
260
261#define wait_event_freezable_timeout(wq, condition, timeout) \
262 wait_event_interruptible_timeout(wq, condition, timeout)
263
Steve Frenche0c8ea12011-10-25 10:02:53 -0500264#define wait_event_freezekillable(wq, condition) \
265 wait_event_killable(wq, condition)
266
Colin Crosscf46c3d2013-05-07 17:52:05 +0000267#define wait_event_freezekillable_unsafe(wq, condition) \
268 wait_event_killable(wq, condition)
269
Matt Helsley8174f152008-10-18 20:27:19 -0700270#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700271
272#endif /* FREEZER_H_INCLUDED */