Commit | Line | Data |
---|---|---|
7dfb7103 NC |
1 | /* Freezer declarations */ |
2 | ||
83144186 RW |
3 | #ifndef FREEZER_H_INCLUDED |
4 | #define FREEZER_H_INCLUDED | |
5 | ||
5c543eff | 6 | #include <linux/sched.h> |
e42837bc | 7 | #include <linux/wait.h> |
a3201227 | 8 | #include <linux/atomic.h> |
5c543eff | 9 | |
8174f150 | 10 | #ifdef CONFIG_FREEZER |
a3201227 TH |
11 | extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */ |
12 | extern bool pm_freezing; /* PM freezing in effect */ | |
13 | extern bool pm_nosig_freezing; /* PM nosig freezing in effect */ | |
14 | ||
7dfb7103 NC |
15 | /* |
16 | * Check if a process has been frozen | |
17 | */ | |
948246f7 | 18 | static inline bool frozen(struct task_struct *p) |
7dfb7103 NC |
19 | { |
20 | return p->flags & PF_FROZEN; | |
21 | } | |
22 | ||
a3201227 | 23 | extern bool freezing_slow_path(struct task_struct *p); |
7dfb7103 NC |
24 | |
25 | /* | |
a3201227 | 26 | * Check if there is a request to freeze a process |
7dfb7103 | 27 | */ |
a3201227 | 28 | static inline bool freezing(struct task_struct *p) |
7dfb7103 | 29 | { |
a3201227 TH |
30 | if (likely(!atomic_read(&system_freezing_cnt))) |
31 | return false; | |
32 | return freezing_slow_path(p); | |
7dfb7103 NC |
33 | } |
34 | ||
dc52ddc0 | 35 | /* Takes and releases task alloc lock using task_lock() */ |
a5be2d0d | 36 | extern void __thaw_task(struct task_struct *t); |
7dfb7103 | 37 | |
8a32c441 | 38 | extern bool __refrigerator(bool check_kthr_stop); |
7dfb7103 | 39 | extern int freeze_processes(void); |
2aede851 | 40 | extern int freeze_kernel_threads(void); |
a9b6f562 | 41 | extern void thaw_processes(void); |
181e9bde | 42 | extern void thaw_kernel_threads(void); |
7dfb7103 | 43 | |
a0acae0e | 44 | static inline bool try_to_freeze(void) |
7dfb7103 | 45 | { |
a0acae0e TH |
46 | might_sleep(); |
47 | if (likely(!freezing(current))) | |
48 | return false; | |
8a32c441 | 49 | return __refrigerator(false); |
7dfb7103 | 50 | } |
ff39593a | 51 | |
839e3407 | 52 | extern bool freeze_task(struct task_struct *p); |
34b087e4 | 53 | extern bool set_freezable(void); |
8174f150 | 54 | |
dc52ddc0 | 55 | #ifdef CONFIG_CGROUP_FREEZER |
22b4e111 | 56 | extern bool cgroup_freezing(struct task_struct *task); |
dc52ddc0 | 57 | #else /* !CONFIG_CGROUP_FREEZER */ |
22b4e111 | 58 | static inline bool cgroup_freezing(struct task_struct *task) |
5a7aadfe | 59 | { |
22b4e111 | 60 | return false; |
5a7aadfe | 61 | } |
dc52ddc0 MH |
62 | #endif /* !CONFIG_CGROUP_FREEZER */ |
63 | ||
ba96a0c8 RW |
64 | /* |
65 | * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it | |
66 | * calls wait_for_completion(&vfork) and reset right after it returns from this | |
67 | * function. Next, the parent should call try_to_freeze() to freeze itself | |
68 | * appropriately in case the child has exited before the freezing of tasks is | |
69 | * complete. However, we don't want kernel threads to be frozen in unexpected | |
70 | * places, so we allow them to block freeze_processes() instead or to set | |
467de1fc SB |
71 | * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the |
72 | * parent won't really block freeze_processes(), since ____call_usermodehelper() | |
73 | * (the child) does a little before exec/exit and it can't be frozen before | |
74 | * waking up the parent. | |
ba96a0c8 RW |
75 | */ |
76 | ||
467de1fc SB |
77 | |
78 | /* Tell the freezer not to count the current task as freezable. */ | |
ba96a0c8 RW |
79 | static inline void freezer_do_not_count(void) |
80 | { | |
467de1fc | 81 | current->flags |= PF_FREEZER_SKIP; |
ba96a0c8 RW |
82 | } |
83 | ||
84 | /* | |
467de1fc SB |
85 | * Tell the freezer to count the current task as freezable again and try to |
86 | * freeze it. | |
ba96a0c8 RW |
87 | */ |
88 | static inline void freezer_count(void) | |
89 | { | |
467de1fc SB |
90 | current->flags &= ~PF_FREEZER_SKIP; |
91 | try_to_freeze(); | |
ba96a0c8 RW |
92 | } |
93 | ||
94 | /* | |
58a69cb4 | 95 | * Check if the task should be counted as freezable by the freezer |
ba96a0c8 RW |
96 | */ |
97 | static inline int freezer_should_skip(struct task_struct *p) | |
98 | { | |
99 | return !!(p->flags & PF_FREEZER_SKIP); | |
100 | } | |
ff39593a | 101 | |
d310310c JL |
102 | /* |
103 | * These macros are intended to be used whenever you want allow a task that's | |
104 | * sleeping in TASK_UNINTERRUPTIBLE or TASK_KILLABLE state to be frozen. Note | |
105 | * that neither return any clear indication of whether a freeze event happened | |
106 | * while in this function. | |
107 | */ | |
108 | ||
109 | /* Like schedule(), but should not block the freezer. */ | |
110 | #define freezable_schedule() \ | |
111 | ({ \ | |
112 | freezer_do_not_count(); \ | |
113 | schedule(); \ | |
114 | freezer_count(); \ | |
115 | }) | |
116 | ||
117 | /* Like schedule_timeout_killable(), but should not block the freezer. */ | |
118 | #define freezable_schedule_timeout_killable(timeout) \ | |
119 | ({ \ | |
b3b73ec0 | 120 | long __retval; \ |
d310310c | 121 | freezer_do_not_count(); \ |
b3b73ec0 | 122 | __retval = schedule_timeout_killable(timeout); \ |
d310310c | 123 | freezer_count(); \ |
b3b73ec0 | 124 | __retval; \ |
d310310c JL |
125 | }) |
126 | ||
e42837bc | 127 | /* |
f06ac72e JL |
128 | * Freezer-friendly wrappers around wait_event_interruptible(), |
129 | * wait_event_killable() and wait_event_interruptible_timeout(), originally | |
130 | * defined in <linux/wait.h> | |
e42837bc RW |
131 | */ |
132 | ||
f06ac72e JL |
133 | #define wait_event_freezekillable(wq, condition) \ |
134 | ({ \ | |
135 | int __retval; \ | |
6f35c4ab ON |
136 | freezer_do_not_count(); \ |
137 | __retval = wait_event_killable(wq, (condition)); \ | |
138 | freezer_count(); \ | |
f06ac72e JL |
139 | __retval; \ |
140 | }) | |
141 | ||
e42837bc RW |
142 | #define wait_event_freezable(wq, condition) \ |
143 | ({ \ | |
144 | int __retval; \ | |
24b7ead3 | 145 | for (;;) { \ |
e42837bc RW |
146 | __retval = wait_event_interruptible(wq, \ |
147 | (condition) || freezing(current)); \ | |
24b7ead3 | 148 | if (__retval || (condition)) \ |
e42837bc | 149 | break; \ |
24b7ead3 ON |
150 | try_to_freeze(); \ |
151 | } \ | |
e42837bc RW |
152 | __retval; \ |
153 | }) | |
154 | ||
e42837bc RW |
155 | #define wait_event_freezable_timeout(wq, condition, timeout) \ |
156 | ({ \ | |
157 | long __retval = timeout; \ | |
24b7ead3 | 158 | for (;;) { \ |
e42837bc RW |
159 | __retval = wait_event_interruptible_timeout(wq, \ |
160 | (condition) || freezing(current), \ | |
161 | __retval); \ | |
24b7ead3 ON |
162 | if (__retval <= 0 || (condition)) \ |
163 | break; \ | |
164 | try_to_freeze(); \ | |
165 | } \ | |
e42837bc RW |
166 | __retval; \ |
167 | }) | |
24b7ead3 | 168 | |
8174f150 | 169 | #else /* !CONFIG_FREEZER */ |
948246f7 | 170 | static inline bool frozen(struct task_struct *p) { return false; } |
a3201227 | 171 | static inline bool freezing(struct task_struct *p) { return false; } |
62c9ea6b | 172 | static inline void __thaw_task(struct task_struct *t) {} |
7dfb7103 | 173 | |
8a32c441 | 174 | static inline bool __refrigerator(bool check_kthr_stop) { return false; } |
2aede851 RW |
175 | static inline int freeze_processes(void) { return -ENOSYS; } |
176 | static inline int freeze_kernel_threads(void) { return -ENOSYS; } | |
7dfb7103 | 177 | static inline void thaw_processes(void) {} |
181e9bde | 178 | static inline void thaw_kernel_threads(void) {} |
7dfb7103 | 179 | |
a0acae0e | 180 | static inline bool try_to_freeze(void) { return false; } |
7dfb7103 | 181 | |
ba96a0c8 RW |
182 | static inline void freezer_do_not_count(void) {} |
183 | static inline void freezer_count(void) {} | |
184 | static inline int freezer_should_skip(struct task_struct *p) { return 0; } | |
83144186 | 185 | static inline void set_freezable(void) {} |
e42837bc | 186 | |
d310310c JL |
187 | #define freezable_schedule() schedule() |
188 | ||
189 | #define freezable_schedule_timeout_killable(timeout) \ | |
190 | schedule_timeout_killable(timeout) | |
191 | ||
e42837bc RW |
192 | #define wait_event_freezable(wq, condition) \ |
193 | wait_event_interruptible(wq, condition) | |
194 | ||
195 | #define wait_event_freezable_timeout(wq, condition, timeout) \ | |
196 | wait_event_interruptible_timeout(wq, condition, timeout) | |
197 | ||
e0c8ea1a SF |
198 | #define wait_event_freezekillable(wq, condition) \ |
199 | wait_event_killable(wq, condition) | |
200 | ||
8174f150 | 201 | #endif /* !CONFIG_FREEZER */ |
83144186 RW |
202 | |
203 | #endif /* FREEZER_H_INCLUDED */ |