blob: fa354335ed514dad86f7ee3b0dbe6e41ec7d72a0 [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/*
16 * Check if a process has been frozen
17 */
Tejun Heo948246f2011-11-21 12:32:25 -080018static inline bool frozen(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080019{
20 return p->flags & PF_FROZEN;
21}
22
Tejun Heoa3201222011-11-21 12:32:25 -080023extern bool freezing_slow_path(struct task_struct *p);
24
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025/*
26 * Check if there is a request to freeze a process
27 */
Tejun Heoa3201222011-11-21 12:32:25 -080028static inline bool freezing(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080029{
Tejun Heoa3201222011-11-21 12:32:25 -080030 if (likely(!atomic_read(&system_freezing_cnt)))
31 return false;
32 return freezing_slow_path(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080033}
34
Matt Helsleydc52ddc2008-10-18 20:27:21 -070035/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080036extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037
Tejun Heo8a32c442011-11-21 12:32:23 -080038extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080039extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020040extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080041extern void thaw_processes(void);
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +010042extern void thaw_kernel_threads(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080043
Colin Cross8d43d952012-08-15 13:10:04 -070044/*
45 * HACK: prevent sleeping while atomic warnings due to ARM signal handling
46 * disabling irqs
47 */
48static inline bool try_to_freeze_nowarn(void)
49{
50 if (likely(!freezing(current)))
51 return false;
52 return __refrigerator(false);
53}
54
Tejun Heoa0acae02011-11-21 12:32:22 -080055static inline bool try_to_freeze(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080056{
Steve Muckledc0eed42012-05-23 08:49:36 -070057/* This causes problems for ARM targets and is a known
58 * problem upstream.
59 * might_sleep();
60 */
Tejun Heoa0acae02011-11-21 12:32:22 -080061 if (likely(!freezing(current)))
62 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080063 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080064}
Nigel Cunninghamff395932006-12-06 20:34:28 -080065
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
92/* Tell the freezer not to count the current task as freezable. */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070093static inline void freezer_do_not_count(void)
94{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010095 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070096}
97
98/*
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010099 * Tell the freezer to count the current task as freezable again and try to
100 * freeze it.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700101 */
102static inline void freezer_count(void)
103{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100104 current->flags &= ~PF_FREEZER_SKIP;
105 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700106}
107
108/*
Tejun Heo58a69cb2011-02-16 09:25:31 +0100109 * Check if the task should be counted as freezable by the freezer
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700110 */
111static inline int freezer_should_skip(struct task_struct *p)
112{
113 return !!(p->flags & PF_FREEZER_SKIP);
114}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800115
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700116/*
Jeff Laytond3103102011-12-01 22:44:39 +0100117 * These macros are intended to be used whenever you want allow a task that's
118 * sleeping in TASK_UNINTERRUPTIBLE or TASK_KILLABLE state to be frozen. Note
119 * that neither return any clear indication of whether a freeze event happened
120 * while in this function.
121 */
122
123/* Like schedule(), but should not block the freezer. */
124#define freezable_schedule() \
125({ \
126 freezer_do_not_count(); \
127 schedule(); \
128 freezer_count(); \
129})
130
131/* Like schedule_timeout_killable(), but should not block the freezer. */
132#define freezable_schedule_timeout_killable(timeout) \
133({ \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100134 long __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100135 freezer_do_not_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100136 __retval = schedule_timeout_killable(timeout); \
Jeff Laytond3103102011-12-01 22:44:39 +0100137 freezer_count(); \
Jeff Laytonb3b73ec2011-12-26 00:29:55 +0100138 __retval; \
Jeff Laytond3103102011-12-01 22:44:39 +0100139})
140
141/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400142 * Freezer-friendly wrappers around wait_event_interruptible(),
143 * wait_event_killable() and wait_event_interruptible_timeout(), originally
144 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700145 */
146
Jeff Laytonf06ac722011-10-19 15:30:40 -0400147#define wait_event_freezekillable(wq, condition) \
148({ \
149 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700150 freezer_do_not_count(); \
151 __retval = wait_event_killable(wq, (condition)); \
152 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400153 __retval; \
154})
155
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700156#define wait_event_freezable(wq, condition) \
157({ \
158 int __retval; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800159 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700160 __retval = wait_event_interruptible(wq, \
161 (condition) || freezing(current)); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800162 if (__retval || (condition)) \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700163 break; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800164 try_to_freeze(); \
165 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700166 __retval; \
167})
168
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700169#define wait_event_freezable_timeout(wq, condition, timeout) \
170({ \
171 long __retval = timeout; \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800172 for (;;) { \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700173 __retval = wait_event_interruptible_timeout(wq, \
174 (condition) || freezing(current), \
175 __retval); \
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800176 if (__retval <= 0 || (condition)) \
177 break; \
178 try_to_freeze(); \
179 } \
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700180 __retval; \
181})
Oleg Nesterov24b7ead2011-11-23 09:28:17 -0800182
Matt Helsley8174f152008-10-18 20:27:19 -0700183#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800184static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800185static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100186static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800187
Tejun Heo8a32c442011-11-21 12:32:23 -0800188static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200189static inline int freeze_processes(void) { return -ENOSYS; }
190static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800191static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100192static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800193
Tejun Heoa0acae02011-11-21 12:32:22 -0800194static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800195
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700196static inline void freezer_do_not_count(void) {}
197static inline void freezer_count(void) {}
198static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700199static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700200
Jeff Laytond3103102011-12-01 22:44:39 +0100201#define freezable_schedule() schedule()
202
203#define freezable_schedule_timeout_killable(timeout) \
204 schedule_timeout_killable(timeout)
205
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700206#define wait_event_freezable(wq, condition) \
207 wait_event_interruptible(wq, condition)
208
209#define wait_event_freezable_timeout(wq, condition, timeout) \
210 wait_event_interruptible_timeout(wq, condition, timeout)
211
Steve Frenche0c8ea12011-10-25 10:02:53 -0500212#define wait_event_freezekillable(wq, condition) \
213 wait_event_killable(wq, condition)
214
Matt Helsley8174f152008-10-18 20:27:19 -0700215#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700216
217#endif /* FREEZER_H_INCLUDED */