blob: 043a5cf8b5baf3e917dd9ba265c365a334365216 [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 Baines6aa97072013-02-27 17:03:18 -08006#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
Tejun Heoa0acae02011-11-21 12:32:22 -080050static inline bool try_to_freeze(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080051{
Mandeep Singh Baines6aa97072013-02-27 17:03:18 -080052 if (!(current->flags & PF_NOFREEZE))
53 debug_check_no_locks_held();
Tejun Heoa0acae02011-11-21 12:32:22 -080054 might_sleep();
55 if (likely(!freezing(current)))
56 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080057 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080058}
Nigel Cunninghamff395932006-12-06 20:34:28 -080059
Tejun Heo839e3402011-11-21 12:32:26 -080060extern bool freeze_task(struct task_struct *p);
Tejun Heo34b087e2011-11-23 09:28:17 -080061extern bool set_freezable(void);
Matt Helsley8174f152008-10-18 20:27:19 -070062
Matt Helsleydc52ddc2008-10-18 20:27:21 -070063#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080064extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070065#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080066static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010067{
Tejun Heo22b4e112011-11-21 12:32:25 -080068 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010069}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070070#endif /* !CONFIG_CGROUP_FREEZER */
71
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070072/*
73 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
74 * calls wait_for_completion(&vfork) and reset right after it returns from this
75 * function. Next, the parent should call try_to_freeze() to freeze itself
76 * appropriately in case the child has exited before the freezing of tasks is
77 * complete. However, we don't want kernel threads to be frozen in unexpected
78 * places, so we allow them to block freeze_processes() instead or to set
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010079 * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
80 * parent won't really block freeze_processes(), since ____call_usermodehelper()
81 * (the child) does a little before exec/exit and it can't be frozen before
82 * waking up the parent.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070083 */
84
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010085
Tejun Heodd67d322012-10-16 15:03:14 -070086/**
87 * freezer_do_not_count - tell freezer to ignore %current
88 *
89 * Tell freezers to ignore the current task when determining whether the
90 * target frozen state is reached. IOW, the current task will be
91 * considered frozen enough by freezers.
92 *
93 * The caller shouldn't do anything which isn't allowed for a frozen task
94 * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
95 * wrap a scheduling operation and nothing much else.
96 */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070097static inline void freezer_do_not_count(void)
98{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010099 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700100}
101
Tejun Heodd67d322012-10-16 15:03:14 -0700102/**
103 * freezer_count - tell freezer to stop ignoring %current
104 *
105 * Undo freezer_do_not_count(). It tells freezers that %current should be
106 * considered again and tries to freeze if freezing condition is already in
107 * effect.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700108 */
109static inline void freezer_count(void)
110{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100111 current->flags &= ~PF_FREEZER_SKIP;
Tejun Heodd67d322012-10-16 15:03:14 -0700112 /*
113 * If freezing is in progress, the following paired with smp_mb()
114 * in freezer_should_skip() ensures that either we see %true
115 * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
116 */
117 smp_mb();
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100118 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700119}
120
Tejun Heodd67d322012-10-16 15:03:14 -0700121/**
122 * freezer_should_skip - whether to skip a task when determining frozen
123 * state is reached
124 * @p: task in quesion
125 *
126 * This function is used by freezers after establishing %true freezing() to
127 * test whether a task should be skipped when determining the target frozen
128 * state is reached. IOW, if this function returns %true, @p is considered
129 * frozen enough.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700130 */
Tejun Heodd67d322012-10-16 15:03:14 -0700131static inline bool freezer_should_skip(struct task_struct *p)
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700132{
Tejun Heodd67d322012-10-16 15:03:14 -0700133 /*
134 * The following smp_mb() paired with the one in freezer_count()
135 * ensures that either freezer_count() sees %true freezing() or we
136 * see cleared %PF_FREEZER_SKIP and return %false. This makes it
137 * impossible for a task to slip frozen state testing after
138 * clearing %PF_FREEZER_SKIP.
139 */
140 smp_mb();
141 return p->flags & PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700142}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800143
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700144/*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200145 * These macros are intended to be used whenever you want allow a sleeping
146 * task to be frozen. Note that neither return any clear indication of
147 * whether a freeze event happened while in this function.
Jeff Laytond3103102011-12-01 22:44:39 +0100148 */
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
158/* Like schedule_timeout_killable(), but should not block the freezer. */
159#define freezable_schedule_timeout_killable(timeout) \
160({ \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100161 long __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100162 freezer_do_not_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100163 __retval = schedule_timeout_killable(timeout); \
Jeff Laytond3103102011-12-01 22:44:39 +0100164 freezer_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100165 __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100166})
167
168/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400169 * Freezer-friendly wrappers around wait_event_interruptible(),
170 * wait_event_killable() and wait_event_interruptible_timeout(), originally
171 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700172 */
173
Jeff Laytonf06ac722011-10-19 15:30:40 -0400174#define wait_event_freezekillable(wq, condition) \
175({ \
176 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700177 freezer_do_not_count(); \
178 __retval = wait_event_killable(wq, (condition)); \
179 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400180 __retval; \
181})
182
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700183#define wait_event_freezable(wq, condition) \
184({ \
185 int __retval; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800186 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700187 __retval = wait_event_interruptible(wq, \
188 (condition) || freezing(current)); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800189 if (__retval || (condition)) \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700190 break; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800191 try_to_freeze(); \
192 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700193 __retval; \
194})
195
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700196#define wait_event_freezable_timeout(wq, condition, timeout) \
197({ \
198 long __retval = timeout; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800199 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700200 __retval = wait_event_interruptible_timeout(wq, \
201 (condition) || freezing(current), \
202 __retval); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800203 if (__retval <= 0 || (condition)) \
204 break; \
205 try_to_freeze(); \
206 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700207 __retval; \
208})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800209
Matt Helsley8174f152008-10-18 20:27:19 -0700210#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800211static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800212static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100213static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800214
Tejun Heo8a32c442011-11-21 12:32:23 -0800215static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200216static inline int freeze_processes(void) { return -ENOSYS; }
217static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800218static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100219static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800220
Li Haifenge5f57622012-11-23 21:55:19 +0100221static inline bool try_to_freeze_nowarn(void) { return false; }
Tejun Heoa0acae02011-11-21 12:32:22 -0800222static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800223
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700224static inline void freezer_do_not_count(void) {}
225static inline void freezer_count(void) {}
226static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700227static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700228
Jeff Laytond3103102011-12-01 22:44:39 +0100229#define freezable_schedule() schedule()
230
231#define freezable_schedule_timeout_killable(timeout) \
232 schedule_timeout_killable(timeout)
233
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700234#define wait_event_freezable(wq, condition) \
235 wait_event_interruptible(wq, condition)
236
237#define wait_event_freezable_timeout(wq, condition, timeout) \
238 wait_event_interruptible_timeout(wq, condition, timeout)
239
Steve Frenche0c8ea12011-10-25 10:02:53 -0500240#define wait_event_freezekillable(wq, condition) \
241 wait_event_killable(wq, condition)
242
Matt Helsley8174f152008-10-18 20:27:19 -0700243#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700244
245#endif /* FREEZER_H_INCLUDED */