Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 1 | /* Freezer declarations */ |
| 2 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 3 | #ifndef FREEZER_H_INCLUDED |
| 4 | #define FREEZER_H_INCLUDED |
| 5 | |
Mandeep Singh Baines | 6aa9707 | 2013-02-27 17:03:18 -0800 | [diff] [blame] | 6 | #include <linux/debug_locks.h> |
Randy Dunlap | 5c543ef | 2006-12-10 02:18:58 -0800 | [diff] [blame] | 7 | #include <linux/sched.h> |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 8 | #include <linux/wait.h> |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 9 | #include <linux/atomic.h> |
Randy Dunlap | 5c543ef | 2006-12-10 02:18:58 -0800 | [diff] [blame] | 10 | |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 11 | #ifdef CONFIG_FREEZER |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 12 | extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */ |
| 13 | extern bool pm_freezing; /* PM freezing in effect */ |
| 14 | extern bool pm_nosig_freezing; /* PM nosig freezing in effect */ |
| 15 | |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 16 | /* |
Li Fei | 957d128 | 2013-02-01 08:56:03 +0000 | [diff] [blame] | 17 | * Timeout for stopping processes |
| 18 | */ |
| 19 | extern unsigned int freeze_timeout_msecs; |
| 20 | |
| 21 | /* |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 22 | * Check if a process has been frozen |
| 23 | */ |
Tejun Heo | 948246f | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 24 | static inline bool frozen(struct task_struct *p) |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 25 | { |
| 26 | return p->flags & PF_FROZEN; |
| 27 | } |
| 28 | |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 29 | extern bool freezing_slow_path(struct task_struct *p); |
| 30 | |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 31 | /* |
| 32 | * Check if there is a request to freeze a process |
| 33 | */ |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 34 | static inline bool freezing(struct task_struct *p) |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 35 | { |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 36 | if (likely(!atomic_read(&system_freezing_cnt))) |
| 37 | return false; |
| 38 | return freezing_slow_path(p); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 41 | /* Takes and releases task alloc lock using task_lock() */ |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 42 | extern void __thaw_task(struct task_struct *t); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 43 | |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 44 | extern bool __refrigerator(bool check_kthr_stop); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 45 | extern int freeze_processes(void); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 46 | extern int freeze_kernel_threads(void); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 47 | extern void thaw_processes(void); |
Rafael J. Wysocki | 181e9bd | 2012-01-29 20:35:52 +0100 | [diff] [blame] | 48 | extern void thaw_kernel_threads(void); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 49 | |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 50 | static inline bool try_to_freeze(void) |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 51 | { |
Mandeep Singh Baines | 6aa9707 | 2013-02-27 17:03:18 -0800 | [diff] [blame] | 52 | if (!(current->flags & PF_NOFREEZE)) |
| 53 | debug_check_no_locks_held(); |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 54 | might_sleep(); |
| 55 | if (likely(!freezing(current))) |
| 56 | return false; |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 57 | return __refrigerator(false); |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 58 | } |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 59 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 60 | extern bool freeze_task(struct task_struct *p); |
Tejun Heo | 34b087e | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 61 | extern bool set_freezable(void); |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 62 | |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 63 | #ifdef CONFIG_CGROUP_FREEZER |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 64 | extern bool cgroup_freezing(struct task_struct *task); |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 65 | #else /* !CONFIG_CGROUP_FREEZER */ |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 66 | static inline bool cgroup_freezing(struct task_struct *task) |
Matt Helsley | 5a7aadf | 2010-03-26 23:51:44 +0100 | [diff] [blame] | 67 | { |
Tejun Heo | 22b4e11 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 68 | return false; |
Matt Helsley | 5a7aadf | 2010-03-26 23:51:44 +0100 | [diff] [blame] | 69 | } |
Matt Helsley | dc52ddc | 2008-10-18 20:27:21 -0700 | [diff] [blame] | 70 | #endif /* !CONFIG_CGROUP_FREEZER */ |
| 71 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 72 | /* |
| 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. Bhat | 467de1f | 2011-12-06 23:17:51 +0100 | [diff] [blame] | 79 | * 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. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 83 | */ |
| 84 | |
Srivatsa S. Bhat | 467de1f | 2011-12-06 23:17:51 +0100 | [diff] [blame] | 85 | |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 86 | /** |
| 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. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 97 | static inline void freezer_do_not_count(void) |
| 98 | { |
Srivatsa S. Bhat | 467de1f | 2011-12-06 23:17:51 +0100 | [diff] [blame] | 99 | current->flags |= PF_FREEZER_SKIP; |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 102 | /** |
| 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. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 108 | */ |
| 109 | static inline void freezer_count(void) |
| 110 | { |
Srivatsa S. Bhat | 467de1f | 2011-12-06 23:17:51 +0100 | [diff] [blame] | 111 | current->flags &= ~PF_FREEZER_SKIP; |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 112 | /* |
| 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. Bhat | 467de1f | 2011-12-06 23:17:51 +0100 | [diff] [blame] | 118 | try_to_freeze(); |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 121 | /** |
| 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. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 130 | */ |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 131 | static inline bool freezer_should_skip(struct task_struct *p) |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 132 | { |
Tejun Heo | dd67d32 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 133 | /* |
| 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. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 142 | } |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 143 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 144 | /* |
Oleg Nesterov | 5d8f72b | 2012-10-26 19:46:06 +0200 | [diff] [blame] | 145 | * 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 Layton | d310310 | 2011-12-01 22:44:39 +0100 | [diff] [blame] | 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 | |
| 158 | /* Like schedule_timeout_killable(), but should not block the freezer. */ |
| 159 | #define freezable_schedule_timeout_killable(timeout) \ |
| 160 | ({ \ |
Jeff Layton | b3b73ec | 2011-12-26 00:29:55 +0100 | [diff] [blame] | 161 | long __retval; \ |
Jeff Layton | d310310 | 2011-12-01 22:44:39 +0100 | [diff] [blame] | 162 | freezer_do_not_count(); \ |
Jeff Layton | b3b73ec | 2011-12-26 00:29:55 +0100 | [diff] [blame] | 163 | __retval = schedule_timeout_killable(timeout); \ |
Jeff Layton | d310310 | 2011-12-01 22:44:39 +0100 | [diff] [blame] | 164 | freezer_count(); \ |
Jeff Layton | b3b73ec | 2011-12-26 00:29:55 +0100 | [diff] [blame] | 165 | __retval; \ |
Jeff Layton | d310310 | 2011-12-01 22:44:39 +0100 | [diff] [blame] | 166 | }) |
| 167 | |
| 168 | /* |
Jeff Layton | f06ac72 | 2011-10-19 15:30:40 -0400 | [diff] [blame] | 169 | * 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. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 172 | */ |
| 173 | |
Jeff Layton | f06ac72 | 2011-10-19 15:30:40 -0400 | [diff] [blame] | 174 | #define wait_event_freezekillable(wq, condition) \ |
| 175 | ({ \ |
| 176 | int __retval; \ |
Oleg Nesterov | 6f35c4a | 2011-11-03 16:07:49 -0700 | [diff] [blame] | 177 | freezer_do_not_count(); \ |
| 178 | __retval = wait_event_killable(wq, (condition)); \ |
| 179 | freezer_count(); \ |
Jeff Layton | f06ac72 | 2011-10-19 15:30:40 -0400 | [diff] [blame] | 180 | __retval; \ |
| 181 | }) |
| 182 | |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 183 | #define wait_event_freezable(wq, condition) \ |
| 184 | ({ \ |
| 185 | int __retval; \ |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 186 | for (;;) { \ |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 187 | __retval = wait_event_interruptible(wq, \ |
| 188 | (condition) || freezing(current)); \ |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 189 | if (__retval || (condition)) \ |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 190 | break; \ |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 191 | try_to_freeze(); \ |
| 192 | } \ |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 193 | __retval; \ |
| 194 | }) |
| 195 | |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 196 | #define wait_event_freezable_timeout(wq, condition, timeout) \ |
| 197 | ({ \ |
| 198 | long __retval = timeout; \ |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 199 | for (;;) { \ |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 200 | __retval = wait_event_interruptible_timeout(wq, \ |
| 201 | (condition) || freezing(current), \ |
| 202 | __retval); \ |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 203 | if (__retval <= 0 || (condition)) \ |
| 204 | break; \ |
| 205 | try_to_freeze(); \ |
| 206 | } \ |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 207 | __retval; \ |
| 208 | }) |
Oleg Nesterov | 24b7ead | 2011-11-23 09:28:17 -0800 | [diff] [blame] | 209 | |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 210 | #else /* !CONFIG_FREEZER */ |
Tejun Heo | 948246f | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 211 | static inline bool frozen(struct task_struct *p) { return false; } |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 212 | static inline bool freezing(struct task_struct *p) { return false; } |
Stephen Rothwell | 62c9ea6 | 2011-11-25 00:44:55 +0100 | [diff] [blame] | 213 | static inline void __thaw_task(struct task_struct *t) {} |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 214 | |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 215 | static inline bool __refrigerator(bool check_kthr_stop) { return false; } |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 216 | static inline int freeze_processes(void) { return -ENOSYS; } |
| 217 | static inline int freeze_kernel_threads(void) { return -ENOSYS; } |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 218 | static inline void thaw_processes(void) {} |
Rafael J. Wysocki | 181e9bd | 2012-01-29 20:35:52 +0100 | [diff] [blame] | 219 | static inline void thaw_kernel_threads(void) {} |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 220 | |
Li Haifeng | e5f5762 | 2012-11-23 21:55:19 +0100 | [diff] [blame] | 221 | static inline bool try_to_freeze_nowarn(void) { return false; } |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 222 | static inline bool try_to_freeze(void) { return false; } |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 223 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 224 | static inline void freezer_do_not_count(void) {} |
| 225 | static inline void freezer_count(void) {} |
| 226 | static inline int freezer_should_skip(struct task_struct *p) { return 0; } |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 227 | static inline void set_freezable(void) {} |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 228 | |
Jeff Layton | d310310 | 2011-12-01 22:44:39 +0100 | [diff] [blame] | 229 | #define freezable_schedule() schedule() |
| 230 | |
| 231 | #define freezable_schedule_timeout_killable(timeout) \ |
| 232 | schedule_timeout_killable(timeout) |
| 233 | |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 234 | #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 French | e0c8ea1 | 2011-10-25 10:02:53 -0500 | [diff] [blame] | 240 | #define wait_event_freezekillable(wq, condition) \ |
| 241 | wait_event_killable(wq, condition) |
| 242 | |
Matt Helsley | 8174f15 | 2008-10-18 20:27:19 -0700 | [diff] [blame] | 243 | #endif /* !CONFIG_FREEZER */ |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 244 | |
| 245 | #endif /* FREEZER_H_INCLUDED */ |