]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/freezer.h
ACPI: PM: Introduce "poweroff" callbacks for ACPI PM domain and LPSS
[mirror_ubuntu-bionic-kernel.git] / include / linux / freezer.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
7dfb7103
NC
2/* Freezer declarations */
3
83144186
RW
4#ifndef FREEZER_H_INCLUDED
5#define FREEZER_H_INCLUDED
6
0f9548ca 7#include <linux/debug_locks.h>
5c543eff 8#include <linux/sched.h>
e42837bc 9#include <linux/wait.h>
a3201227 10#include <linux/atomic.h>
5c543eff 11
8174f150 12#ifdef CONFIG_FREEZER
a3201227
TH
13extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */
14extern bool pm_freezing; /* PM freezing in effect */
15extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
16
957d1282
LF
17/*
18 * Timeout for stopping processes
19 */
20extern unsigned int freeze_timeout_msecs;
21
7dfb7103
NC
22/*
23 * Check if a process has been frozen
24 */
948246f7 25static inline bool frozen(struct task_struct *p)
7dfb7103
NC
26{
27 return p->flags & PF_FROZEN;
28}
29
a3201227 30extern bool freezing_slow_path(struct task_struct *p);
7dfb7103
NC
31
32/*
a3201227 33 * Check if there is a request to freeze a process
7dfb7103 34 */
a3201227 35static inline bool freezing(struct task_struct *p)
7dfb7103 36{
a3201227
TH
37 if (likely(!atomic_read(&system_freezing_cnt)))
38 return false;
39 return freezing_slow_path(p);
7dfb7103
NC
40}
41
dc52ddc0 42/* Takes and releases task alloc lock using task_lock() */
a5be2d0d 43extern void __thaw_task(struct task_struct *t);
7dfb7103 44
8a32c441 45extern bool __refrigerator(bool check_kthr_stop);
7dfb7103 46extern int freeze_processes(void);
2aede851 47extern int freeze_kernel_threads(void);
a9b6f562 48extern void thaw_processes(void);
181e9bde 49extern void thaw_kernel_threads(void);
7dfb7103 50
416ad3c9
CC
51/*
52 * DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION
53 * If try_to_freeze causes a lockdep warning it means the caller may deadlock
54 */
55static inline bool try_to_freeze_unsafe(void)
7dfb7103 56{
a0acae0e
TH
57 might_sleep();
58 if (likely(!freezing(current)))
59 return false;
8a32c441 60 return __refrigerator(false);
7dfb7103 61}
ff39593a 62
416ad3c9
CC
63static inline bool try_to_freeze(void)
64{
0f9548ca
MSB
65 if (!(current->flags & PF_NOFREEZE))
66 debug_check_no_locks_held();
416ad3c9
CC
67 return try_to_freeze_unsafe();
68}
69
839e3407 70extern bool freeze_task(struct task_struct *p);
34b087e4 71extern bool set_freezable(void);
8174f150 72
dc52ddc0 73#ifdef CONFIG_CGROUP_FREEZER
22b4e111 74extern bool cgroup_freezing(struct task_struct *task);
dc52ddc0 75#else /* !CONFIG_CGROUP_FREEZER */
22b4e111 76static inline bool cgroup_freezing(struct task_struct *task)
5a7aadfe 77{
22b4e111 78 return false;
5a7aadfe 79}
dc52ddc0
MH
80#endif /* !CONFIG_CGROUP_FREEZER */
81
ba96a0c8
RW
82/*
83 * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
84 * calls wait_for_completion(&vfork) and reset right after it returns from this
85 * function. Next, the parent should call try_to_freeze() to freeze itself
86 * appropriately in case the child has exited before the freezing of tasks is
87 * complete. However, we don't want kernel threads to be frozen in unexpected
88 * places, so we allow them to block freeze_processes() instead or to set
467de1fc
SB
89 * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
90 * parent won't really block freeze_processes(), since ____call_usermodehelper()
91 * (the child) does a little before exec/exit and it can't be frozen before
92 * waking up the parent.
ba96a0c8
RW
93 */
94
467de1fc 95
dd67d32d
TH
96/**
97 * freezer_do_not_count - tell freezer to ignore %current
98 *
99 * Tell freezers to ignore the current task when determining whether the
100 * target frozen state is reached. IOW, the current task will be
101 * considered frozen enough by freezers.
102 *
103 * The caller shouldn't do anything which isn't allowed for a frozen task
104 * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
105 * wrap a scheduling operation and nothing much else.
106 */
ba96a0c8
RW
107static inline void freezer_do_not_count(void)
108{
467de1fc 109 current->flags |= PF_FREEZER_SKIP;
ba96a0c8
RW
110}
111
dd67d32d
TH
112/**
113 * freezer_count - tell freezer to stop ignoring %current
114 *
115 * Undo freezer_do_not_count(). It tells freezers that %current should be
116 * considered again and tries to freeze if freezing condition is already in
117 * effect.
ba96a0c8
RW
118 */
119static inline void freezer_count(void)
120{
467de1fc 121 current->flags &= ~PF_FREEZER_SKIP;
dd67d32d
TH
122 /*
123 * If freezing is in progress, the following paired with smp_mb()
124 * in freezer_should_skip() ensures that either we see %true
125 * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
126 */
127 smp_mb();
467de1fc 128 try_to_freeze();
ba96a0c8
RW
129}
130
416ad3c9
CC
131/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
132static inline void freezer_count_unsafe(void)
133{
134 current->flags &= ~PF_FREEZER_SKIP;
135 smp_mb();
136 try_to_freeze_unsafe();
137}
138
dd67d32d
TH
139/**
140 * freezer_should_skip - whether to skip a task when determining frozen
141 * state is reached
142 * @p: task in quesion
143 *
144 * This function is used by freezers after establishing %true freezing() to
145 * test whether a task should be skipped when determining the target frozen
146 * state is reached. IOW, if this function returns %true, @p is considered
147 * frozen enough.
ba96a0c8 148 */
dd67d32d 149static inline bool freezer_should_skip(struct task_struct *p)
ba96a0c8 150{
dd67d32d
TH
151 /*
152 * The following smp_mb() paired with the one in freezer_count()
153 * ensures that either freezer_count() sees %true freezing() or we
154 * see cleared %PF_FREEZER_SKIP and return %false. This makes it
155 * impossible for a task to slip frozen state testing after
156 * clearing %PF_FREEZER_SKIP.
157 */
158 smp_mb();
159 return p->flags & PF_FREEZER_SKIP;
ba96a0c8 160}
ff39593a 161
d310310c 162/*
8ee492d6 163 * These functions are intended to be used whenever you want allow a sleeping
5d8f72b5
ON
164 * task to be frozen. Note that neither return any clear indication of
165 * whether a freeze event happened while in this function.
d310310c
JL
166 */
167
168/* Like schedule(), but should not block the freezer. */
8ee492d6
CC
169static inline void freezable_schedule(void)
170{
171 freezer_do_not_count();
172 schedule();
173 freezer_count();
174}
d310310c 175
416ad3c9 176/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
8ee492d6
CC
177static inline void freezable_schedule_unsafe(void)
178{
179 freezer_do_not_count();
180 schedule();
181 freezer_count_unsafe();
182}
416ad3c9 183
dd5ec0f4 184/*
2dd9789c 185 * Like schedule_timeout(), but should not block the freezer. Do not
dd5ec0f4
CC
186 * call this with locks held.
187 */
188static inline long freezable_schedule_timeout(long timeout)
189{
190 long __retval;
191 freezer_do_not_count();
192 __retval = schedule_timeout(timeout);
193 freezer_count();
194 return __retval;
195}
196
197/*
198 * Like schedule_timeout_interruptible(), but should not block the freezer. Do not
199 * call this with locks held.
200 */
201static inline long freezable_schedule_timeout_interruptible(long timeout)
202{
203 long __retval;
204 freezer_do_not_count();
205 __retval = schedule_timeout_interruptible(timeout);
206 freezer_count();
207 return __retval;
208}
209
d310310c 210/* Like schedule_timeout_killable(), but should not block the freezer. */
8ee492d6
CC
211static inline long freezable_schedule_timeout_killable(long timeout)
212{
213 long __retval;
214 freezer_do_not_count();
215 __retval = schedule_timeout_killable(timeout);
216 freezer_count();
217 return __retval;
218}
d310310c 219
416ad3c9 220/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
8ee492d6
CC
221static inline long freezable_schedule_timeout_killable_unsafe(long timeout)
222{
223 long __retval;
224 freezer_do_not_count();
225 __retval = schedule_timeout_killable(timeout);
226 freezer_count_unsafe();
227 return __retval;
228}
416ad3c9 229
dd5ec0f4
CC
230/*
231 * Like schedule_hrtimeout_range(), but should not block the freezer. Do not
232 * call this with locks held.
233 */
234static inline int freezable_schedule_hrtimeout_range(ktime_t *expires,
da8b44d5 235 u64 delta, const enum hrtimer_mode mode)
dd5ec0f4
CC
236{
237 int __retval;
238 freezer_do_not_count();
239 __retval = schedule_hrtimeout_range(expires, delta, mode);
240 freezer_count();
241 return __retval;
242}
243
e42837bc 244/*
f06ac72e
JL
245 * Freezer-friendly wrappers around wait_event_interruptible(),
246 * wait_event_killable() and wait_event_interruptible_timeout(), originally
247 * defined in <linux/wait.h>
e42837bc
RW
248 */
249
5853cc2a
CC
250/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
251#define wait_event_freezekillable_unsafe(wq, condition) \
252({ \
253 int __retval; \
254 freezer_do_not_count(); \
255 __retval = wait_event_killable(wq, (condition)); \
256 freezer_count_unsafe(); \
257 __retval; \
258})
259
8174f150 260#else /* !CONFIG_FREEZER */
948246f7 261static inline bool frozen(struct task_struct *p) { return false; }
a3201227 262static inline bool freezing(struct task_struct *p) { return false; }
62c9ea6b 263static inline void __thaw_task(struct task_struct *t) {}
7dfb7103 264
8a32c441 265static inline bool __refrigerator(bool check_kthr_stop) { return false; }
2aede851
RW
266static inline int freeze_processes(void) { return -ENOSYS; }
267static inline int freeze_kernel_threads(void) { return -ENOSYS; }
7dfb7103 268static inline void thaw_processes(void) {}
181e9bde 269static inline void thaw_kernel_threads(void) {}
7dfb7103 270
e5f57621 271static inline bool try_to_freeze_nowarn(void) { return false; }
a0acae0e 272static inline bool try_to_freeze(void) { return false; }
7dfb7103 273
ba96a0c8
RW
274static inline void freezer_do_not_count(void) {}
275static inline void freezer_count(void) {}
276static inline int freezer_should_skip(struct task_struct *p) { return 0; }
83144186 277static inline void set_freezable(void) {}
e42837bc 278
d310310c
JL
279#define freezable_schedule() schedule()
280
416ad3c9
CC
281#define freezable_schedule_unsafe() schedule()
282
dd5ec0f4
CC
283#define freezable_schedule_timeout(timeout) schedule_timeout(timeout)
284
285#define freezable_schedule_timeout_interruptible(timeout) \
286 schedule_timeout_interruptible(timeout)
287
d310310c
JL
288#define freezable_schedule_timeout_killable(timeout) \
289 schedule_timeout_killable(timeout)
290
416ad3c9
CC
291#define freezable_schedule_timeout_killable_unsafe(timeout) \
292 schedule_timeout_killable(timeout)
293
dd5ec0f4
CC
294#define freezable_schedule_hrtimeout_range(expires, delta, mode) \
295 schedule_hrtimeout_range(expires, delta, mode)
296
5853cc2a
CC
297#define wait_event_freezekillable_unsafe(wq, condition) \
298 wait_event_killable(wq, condition)
299
8174f150 300#endif /* !CONFIG_FREEZER */
83144186
RW
301
302#endif /* FREEZER_H_INCLUDED */