blob: 5b31e21c485f4ac7ecf5cd6b43cbdece997ca5f1 [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
Randy Dunlap5c543ef2006-12-10 02:18:58 -08006#include <linux/sched.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07007#include <linux/wait.h>
Tejun Heoa3201222011-11-21 12:32:25 -08008#include <linux/atomic.h>
Randy Dunlap5c543ef2006-12-10 02:18:58 -08009
Matt Helsley8174f152008-10-18 20:27:19 -070010#ifdef CONFIG_FREEZER
Tejun Heoa3201222011-11-21 12:32:25 -080011extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */
12extern bool pm_freezing; /* PM freezing in effect */
13extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
14
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080015/*
Li Fei957d1282013-02-01 08:56:03 +000016 * Timeout for stopping processes
17 */
18extern unsigned int freeze_timeout_msecs;
19
20/*
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080021 * Check if a process has been frozen
22 */
Tejun Heo948246f2011-11-21 12:32:25 -080023static inline bool frozen(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024{
25 return p->flags & PF_FROZEN;
26}
27
Tejun Heoa3201222011-11-21 12:32:25 -080028extern bool freezing_slow_path(struct task_struct *p);
29
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080030/*
31 * Check if there is a request to freeze a process
32 */
Tejun Heoa3201222011-11-21 12:32:25 -080033static inline bool freezing(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080034{
Tejun Heoa3201222011-11-21 12:32:25 -080035 if (likely(!atomic_read(&system_freezing_cnt)))
36 return false;
37 return freezing_slow_path(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038}
39
Matt Helsleydc52ddc2008-10-18 20:27:21 -070040/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080041extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080042
Tejun Heo8a32c442011-11-21 12:32:23 -080043extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080044extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020045extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080046extern void thaw_processes(void);
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +010047extern void thaw_kernel_threads(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080048
Colin Cross416ad3c2013-05-06 23:50:06 +000049/*
50 * DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION
51 * If try_to_freeze causes a lockdep warning it means the caller may deadlock
52 */
53static inline bool try_to_freeze_unsafe(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080054{
Tejun Heoa0acae02011-11-21 12:32:22 -080055 might_sleep();
56 if (likely(!freezing(current)))
57 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080058 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080059}
Nigel Cunninghamff395932006-12-06 20:34:28 -080060
Colin Cross416ad3c2013-05-06 23:50:06 +000061static inline bool try_to_freeze(void)
62{
63 return try_to_freeze_unsafe();
64}
65
Tejun Heo839e3402011-11-21 12:32:26 -080066extern bool freeze_task(struct task_struct *p);
Tejun Heo34b087e2011-11-23 09:28:17 -080067extern bool set_freezable(void);
Matt Helsley8174f152008-10-18 20:27:19 -070068
Matt Helsleydc52ddc2008-10-18 20:27:21 -070069#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080070extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070071#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080072static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010073{
Tejun Heo22b4e112011-11-21 12:32:25 -080074 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010075}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070076#endif /* !CONFIG_CGROUP_FREEZER */
77
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070078/*
79 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
80 * calls wait_for_completion(&vfork) and reset right after it returns from this
81 * function. Next, the parent should call try_to_freeze() to freeze itself
82 * appropriately in case the child has exited before the freezing of tasks is
83 * complete. However, we don't want kernel threads to be frozen in unexpected
84 * places, so we allow them to block freeze_processes() instead or to set
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010085 * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
86 * parent won't really block freeze_processes(), since ____call_usermodehelper()
87 * (the child) does a little before exec/exit and it can't be frozen before
88 * waking up the parent.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070089 */
90
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010091
Tejun Heodd67d322012-10-16 15:03:14 -070092/**
93 * freezer_do_not_count - tell freezer to ignore %current
94 *
95 * Tell freezers to ignore the current task when determining whether the
96 * target frozen state is reached. IOW, the current task will be
97 * considered frozen enough by freezers.
98 *
99 * The caller shouldn't do anything which isn't allowed for a frozen task
100 * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
101 * wrap a scheduling operation and nothing much else.
102 */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700103static inline void freezer_do_not_count(void)
104{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100105 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700106}
107
Tejun Heodd67d322012-10-16 15:03:14 -0700108/**
109 * freezer_count - tell freezer to stop ignoring %current
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.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700114 */
115static inline void freezer_count(void)
116{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100117 current->flags &= ~PF_FREEZER_SKIP;
Tejun Heodd67d322012-10-16 15:03:14 -0700118 /*
119 * If freezing is in progress, the following paired with smp_mb()
120 * in freezer_should_skip() ensures that either we see %true
121 * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
122 */
123 smp_mb();
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100124 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700125}
126
Colin Cross416ad3c2013-05-06 23:50:06 +0000127/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
128static inline void freezer_count_unsafe(void)
129{
130 current->flags &= ~PF_FREEZER_SKIP;
131 smp_mb();
132 try_to_freeze_unsafe();
133}
134
Tejun Heodd67d322012-10-16 15:03:14 -0700135/**
136 * freezer_should_skip - whether to skip a task when determining frozen
137 * state is reached
138 * @p: task in quesion
139 *
140 * This function is used by freezers after establishing %true freezing() to
141 * test whether a task should be skipped when determining the target frozen
142 * state is reached. IOW, if this function returns %true, @p is considered
143 * frozen enough.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700144 */
Tejun Heodd67d322012-10-16 15:03:14 -0700145static inline bool freezer_should_skip(struct task_struct *p)
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700146{
Tejun Heodd67d322012-10-16 15:03:14 -0700147 /*
148 * The following smp_mb() paired with the one in freezer_count()
149 * ensures that either freezer_count() sees %true freezing() or we
150 * see cleared %PF_FREEZER_SKIP and return %false. This makes it
151 * impossible for a task to slip frozen state testing after
152 * clearing %PF_FREEZER_SKIP.
153 */
154 smp_mb();
155 return p->flags & PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700156}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800157
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700158/*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200159 * These macros are intended to be used whenever you want allow a sleeping
160 * task to be frozen. Note that neither return any clear indication of
161 * whether a freeze event happened while in this function.
Jeff Laytond3103102011-12-01 22:44:39 +0100162 */
163
164/* Like schedule(), but should not block the freezer. */
165#define freezable_schedule() \
166({ \
167 freezer_do_not_count(); \
168 schedule(); \
169 freezer_count(); \
170})
171
Colin Cross416ad3c2013-05-06 23:50:06 +0000172/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
173#define freezable_schedule_unsafe() \
174({ \
175 freezer_do_not_count(); \
176 schedule(); \
177 freezer_count_unsafe(); \
178})
179
Jeff Laytond3103102011-12-01 22:44:39 +0100180/* Like schedule_timeout_killable(), but should not block the freezer. */
181#define freezable_schedule_timeout_killable(timeout) \
182({ \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100183 long __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100184 freezer_do_not_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100185 __retval = schedule_timeout_killable(timeout); \
Jeff Laytond3103102011-12-01 22:44:39 +0100186 freezer_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100187 __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100188})
189
Colin Cross416ad3c2013-05-06 23:50:06 +0000190/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
191#define freezable_schedule_timeout_killable_unsafe(timeout) \
192({ \
193 long __retval; \
194 freezer_do_not_count(); \
195 __retval = schedule_timeout_killable(timeout); \
196 freezer_count_unsafe(); \
197 __retval; \
198})
199
Jeff Laytond3103102011-12-01 22:44:39 +0100200/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400201 * Freezer-friendly wrappers around wait_event_interruptible(),
202 * wait_event_killable() and wait_event_interruptible_timeout(), originally
203 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700204 */
205
Jeff Laytonf06ac722011-10-19 15:30:40 -0400206#define wait_event_freezekillable(wq, condition) \
207({ \
208 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700209 freezer_do_not_count(); \
210 __retval = wait_event_killable(wq, (condition)); \
211 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400212 __retval; \
213})
214
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700215#define wait_event_freezable(wq, condition) \
216({ \
217 int __retval; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800218 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700219 __retval = wait_event_interruptible(wq, \
220 (condition) || freezing(current)); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800221 if (__retval || (condition)) \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700222 break; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800223 try_to_freeze(); \
224 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700225 __retval; \
226})
227
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700228#define wait_event_freezable_timeout(wq, condition, timeout) \
229({ \
230 long __retval = timeout; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800231 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700232 __retval = wait_event_interruptible_timeout(wq, \
233 (condition) || freezing(current), \
234 __retval); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800235 if (__retval <= 0 || (condition)) \
236 break; \
237 try_to_freeze(); \
238 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700239 __retval; \
240})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800241
Matt Helsley8174f152008-10-18 20:27:19 -0700242#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800243static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800244static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100245static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800246
Tejun Heo8a32c442011-11-21 12:32:23 -0800247static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200248static inline int freeze_processes(void) { return -ENOSYS; }
249static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800250static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100251static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800252
Li Haifenge5f57622012-11-23 21:55:19 +0100253static inline bool try_to_freeze_nowarn(void) { return false; }
Tejun Heoa0acae02011-11-21 12:32:22 -0800254static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800255
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700256static inline void freezer_do_not_count(void) {}
257static inline void freezer_count(void) {}
258static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700259static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700260
Jeff Laytond3103102011-12-01 22:44:39 +0100261#define freezable_schedule() schedule()
262
Colin Cross416ad3c2013-05-06 23:50:06 +0000263#define freezable_schedule_unsafe() schedule()
264
Jeff Laytond3103102011-12-01 22:44:39 +0100265#define freezable_schedule_timeout_killable(timeout) \
266 schedule_timeout_killable(timeout)
267
Colin Cross416ad3c2013-05-06 23:50:06 +0000268#define freezable_schedule_timeout_killable_unsafe(timeout) \
269 schedule_timeout_killable(timeout)
270
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700271#define wait_event_freezable(wq, condition) \
272 wait_event_interruptible(wq, condition)
273
274#define wait_event_freezable_timeout(wq, condition, timeout) \
275 wait_event_interruptible_timeout(wq, condition, timeout)
276
Steve Frenche0c8ea12011-10-25 10:02:53 -0500277#define wait_event_freezekillable(wq, condition) \
278 wait_event_killable(wq, condition)
279
Matt Helsley8174f152008-10-18 20:27:19 -0700280#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700281
282#endif /* FREEZER_H_INCLUDED */