blob: b2b4abc5a739face19747ef36c1990ef81a18965 [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>
Randy Dunlap5c543ef2006-12-10 02:18:58 -08008
Matt Helsley8174f152008-10-18 20:27:19 -07009#ifdef CONFIG_FREEZER
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080010/*
11 * Check if a process has been frozen
12 */
13static inline int frozen(struct task_struct *p)
14{
15 return p->flags & PF_FROZEN;
16}
17
18/*
19 * Check if there is a request to freeze a process
20 */
21static inline int freezing(struct task_struct *p)
22{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080023 return test_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024}
25
26/*
27 * Request that a process be frozen
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028 */
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070029static inline void set_freeze_flag(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080030{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080031 set_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080032}
33
34/*
35 * Sometimes we may need to cancel the previous 'freeze' request
36 */
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070037static inline void clear_freeze_flag(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080038{
Rafael J. Wysocki8a102ee2006-12-13 00:34:30 -080039 clear_tsk_thread_flag(p, TIF_FREEZE);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040}
41
Matt Helsley8174f152008-10-18 20:27:19 -070042static inline bool should_send_signal(struct task_struct *p)
43{
44 return !(p->flags & PF_FREEZER_NOSIG);
45}
46
Matt Helsleydc52ddc2008-10-18 20:27:21 -070047/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080048extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080049
Tejun Heo8a32c442011-11-21 12:32:23 -080050extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080051extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020052extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080053extern void thaw_processes(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080054
Tejun Heoa0acae02011-11-21 12:32:22 -080055static inline bool try_to_freeze(void)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080056{
Tejun Heoa0acae02011-11-21 12:32:22 -080057 might_sleep();
58 if (likely(!freezing(current)))
59 return false;
Tejun Heo8a32c442011-11-21 12:32:23 -080060 return __refrigerator(false);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080061}
Nigel Cunninghamff395932006-12-06 20:34:28 -080062
Matt Helsley8174f152008-10-18 20:27:19 -070063extern bool freeze_task(struct task_struct *p, bool sig_only);
Matt Helsley8174f152008-10-18 20:27:19 -070064
Matt Helsleydc52ddc2008-10-18 20:27:21 -070065#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080066extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070067#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080068static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010069{
Tejun Heo22b4e112011-11-21 12:32:25 -080070 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010071}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070072#endif /* !CONFIG_CGROUP_FREEZER */
73
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070074/*
75 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
76 * calls wait_for_completion(&vfork) and reset right after it returns from this
77 * function. Next, the parent should call try_to_freeze() to freeze itself
78 * appropriately in case the child has exited before the freezing of tasks is
79 * complete. However, we don't want kernel threads to be frozen in unexpected
80 * places, so we allow them to block freeze_processes() instead or to set
81 * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
82 * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
83 * really block freeze_processes(), since ____call_usermodehelper() (the child)
84 * does a little before exec/exit and it can't be frozen before waking up the
85 * parent.
86 */
87
88/*
89 * If the current task is a user space one, tell the freezer not to count it as
90 * freezable.
91 */
92static inline void freezer_do_not_count(void)
93{
94 if (current->mm)
95 current->flags |= PF_FREEZER_SKIP;
96}
97
98/*
99 * If the current task is a user space one, tell the freezer to count it as
100 * freezable again and try to freeze it.
101 */
102static inline void freezer_count(void)
103{
104 if (current->mm) {
105 current->flags &= ~PF_FREEZER_SKIP;
106 try_to_freeze();
107 }
108}
109
110/*
Tejun Heo58a69cb2011-02-16 09:25:31 +0100111 * Check if the task should be counted as freezable by the freezer
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700112 */
113static inline int freezer_should_skip(struct task_struct *p)
114{
115 return !!(p->flags & PF_FREEZER_SKIP);
116}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800117
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700118/*
119 * Tell the freezer that the current task should be frozen by it
120 */
121static inline void set_freezable(void)
122{
123 current->flags &= ~PF_NOFREEZE;
124}
125
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700126/*
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200127 * Tell the freezer that the current task should be frozen by it and that it
128 * should send a fake signal to the task to freeze it.
129 */
130static inline void set_freezable_with_signal(void)
131{
132 current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
133}
134
135/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400136 * Freezer-friendly wrappers around wait_event_interruptible(),
137 * wait_event_killable() and wait_event_interruptible_timeout(), originally
138 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700139 */
140
Jeff Laytonf06ac722011-10-19 15:30:40 -0400141#define wait_event_freezekillable(wq, condition) \
142({ \
143 int __retval; \
Oleg Nesterov6f35c4a2011-11-03 16:07:49 -0700144 freezer_do_not_count(); \
145 __retval = wait_event_killable(wq, (condition)); \
146 freezer_count(); \
Jeff Laytonf06ac722011-10-19 15:30:40 -0400147 __retval; \
148})
149
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700150#define wait_event_freezable(wq, condition) \
151({ \
152 int __retval; \
153 do { \
154 __retval = wait_event_interruptible(wq, \
155 (condition) || freezing(current)); \
156 if (__retval && !freezing(current)) \
157 break; \
158 else if (!(condition)) \
159 __retval = -ERESTARTSYS; \
160 } while (try_to_freeze()); \
161 __retval; \
162})
163
164
165#define wait_event_freezable_timeout(wq, condition, timeout) \
166({ \
167 long __retval = timeout; \
168 do { \
169 __retval = wait_event_interruptible_timeout(wq, \
170 (condition) || freezing(current), \
171 __retval); \
172 } while (try_to_freeze()); \
173 __retval; \
174})
Matt Helsley8174f152008-10-18 20:27:19 -0700175#else /* !CONFIG_FREEZER */
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800176static inline int frozen(struct task_struct *p) { return 0; }
177static inline int freezing(struct task_struct *p) { return 0; }
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -0700178static inline void set_freeze_flag(struct task_struct *p) {}
179static inline void clear_freeze_flag(struct task_struct *p) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800180
Tejun Heo8a32c442011-11-21 12:32:23 -0800181static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200182static inline int freeze_processes(void) { return -ENOSYS; }
183static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800184static inline void thaw_processes(void) {}
185
Tejun Heoa0acae02011-11-21 12:32:22 -0800186static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800187
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700188static inline void freezer_do_not_count(void) {}
189static inline void freezer_count(void) {}
190static inline int freezer_should_skip(struct task_struct *p) { return 0; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700191static inline void set_freezable(void) {}
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200192static inline void set_freezable_with_signal(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700193
194#define wait_event_freezable(wq, condition) \
195 wait_event_interruptible(wq, condition)
196
197#define wait_event_freezable_timeout(wq, condition, timeout) \
198 wait_event_interruptible_timeout(wq, condition, timeout)
199
Steve Frenche0c8ea12011-10-25 10:02:53 -0500200#define wait_event_freezekillable(wq, condition) \
201 wait_event_killable(wq, condition)
202
Matt Helsley8174f152008-10-18 20:27:19 -0700203#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700204
205#endif /* FREEZER_H_INCLUDED */