blob: 21f5aa0b217f3c0ce3c04a7d459f7a2e44e93076 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Nigel Cunningham7dfb7102006-12-06 20:34:23 -08002/* Freezer declarations */
3
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004#ifndef FREEZER_H_INCLUDED
5#define FREEZER_H_INCLUDED
6
Mandeep Singh Baines0f9548c2013-05-06 23:50:09 +00007#include <linux/debug_locks.h>
Randy Dunlap5c543ef2006-12-10 02:18:58 -08008#include <linux/sched.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07009#include <linux/wait.h>
Tejun Heoa3201222011-11-21 12:32:25 -080010#include <linux/atomic.h>
Randy Dunlap5c543ef2006-12-10 02:18:58 -080011
Matt Helsley8174f152008-10-18 20:27:19 -070012#ifdef CONFIG_FREEZER
Tejun Heoa3201222011-11-21 12:32:25 -080013extern 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
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080017/*
Li Fei957d1282013-02-01 08:56:03 +000018 * Timeout for stopping processes
19 */
20extern unsigned int freeze_timeout_msecs;
21
22/*
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023 * Check if a process has been frozen
24 */
Tejun Heo948246f2011-11-21 12:32:25 -080025static inline bool frozen(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026{
27 return p->flags & PF_FROZEN;
28}
29
Tejun Heoa3201222011-11-21 12:32:25 -080030extern bool freezing_slow_path(struct task_struct *p);
31
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080032/*
33 * Check if there is a request to freeze a process
34 */
Tejun Heoa3201222011-11-21 12:32:25 -080035static inline bool freezing(struct task_struct *p)
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036{
Tejun Heoa3201222011-11-21 12:32:25 -080037 if (likely(!atomic_read(&system_freezing_cnt)))
38 return false;
39 return freezing_slow_path(p);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040}
41
Matt Helsleydc52ddc2008-10-18 20:27:21 -070042/* Takes and releases task alloc lock using task_lock() */
Tejun Heoa5be2d02011-11-21 12:32:23 -080043extern void __thaw_task(struct task_struct *t);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080044
Tejun Heo8a32c442011-11-21 12:32:23 -080045extern bool __refrigerator(bool check_kthr_stop);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046extern int freeze_processes(void);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +020047extern int freeze_kernel_threads(void);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -080048extern void thaw_processes(void);
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +010049extern void thaw_kernel_threads(void);
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080050
Colin Cross416ad3c2013-05-06 23:50:06 +000051/*
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)
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
Colin Cross416ad3c2013-05-06 23:50:06 +000063static inline bool try_to_freeze(void)
64{
Mandeep Singh Baines0f9548c2013-05-06 23:50:09 +000065 if (!(current->flags & PF_NOFREEZE))
66 debug_check_no_locks_held();
Colin Cross416ad3c2013-05-06 23:50:06 +000067 return try_to_freeze_unsafe();
68}
69
Tejun Heo839e3402011-11-21 12:32:26 -080070extern bool freeze_task(struct task_struct *p);
Tejun Heo34b087e2011-11-23 09:28:17 -080071extern bool set_freezable(void);
Matt Helsley8174f152008-10-18 20:27:19 -070072
Matt Helsleydc52ddc2008-10-18 20:27:21 -070073#ifdef CONFIG_CGROUP_FREEZER
Tejun Heo22b4e112011-11-21 12:32:25 -080074extern bool cgroup_freezing(struct task_struct *task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070075#else /* !CONFIG_CGROUP_FREEZER */
Tejun Heo22b4e112011-11-21 12:32:25 -080076static inline bool cgroup_freezing(struct task_struct *task)
Matt Helsley5a7aadf2010-03-26 23:51:44 +010077{
Tejun Heo22b4e112011-11-21 12:32:25 -080078 return false;
Matt Helsley5a7aadf2010-03-26 23:51:44 +010079}
Matt Helsleydc52ddc2008-10-18 20:27:21 -070080#endif /* !CONFIG_CGROUP_FREEZER */
81
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070082/*
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
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010089 * 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.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070093 */
94
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +010095
Tejun Heodd67d322012-10-16 15:03:14 -070096/**
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 */
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700107static inline void freezer_do_not_count(void)
108{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100109 current->flags |= PF_FREEZER_SKIP;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700110}
111
Tejun Heodd67d322012-10-16 15:03:14 -0700112/**
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.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700118 */
119static inline void freezer_count(void)
120{
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100121 current->flags &= ~PF_FREEZER_SKIP;
Tejun Heodd67d322012-10-16 15:03:14 -0700122 /*
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();
Srivatsa S. Bhat467de1f2011-12-06 23:17:51 +0100128 try_to_freeze();
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700129}
130
Colin Cross416ad3c2013-05-06 23:50:06 +0000131/* 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
Tejun Heodd67d322012-10-16 15:03:14 -0700139/**
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.
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700148 */
Tejun Heodd67d322012-10-16 15:03:14 -0700149static inline bool freezer_should_skip(struct task_struct *p)
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700150{
Tejun Heodd67d322012-10-16 15:03:14 -0700151 /*
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;
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700160}
Nigel Cunninghamff395932006-12-06 20:34:28 -0800161
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700162/*
Colin Cross8ee492d2013-05-06 23:50:13 +0000163 * These functions are intended to be used whenever you want allow a sleeping
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200164 * task to be frozen. Note that neither return any clear indication of
165 * whether a freeze event happened while in this function.
Jeff Laytond3103102011-12-01 22:44:39 +0100166 */
167
168/* Like schedule(), but should not block the freezer. */
Colin Cross8ee492d2013-05-06 23:50:13 +0000169static inline void freezable_schedule(void)
170{
171 freezer_do_not_count();
172 schedule();
173 freezer_count();
174}
Jeff Laytond3103102011-12-01 22:44:39 +0100175
Colin Cross416ad3c2013-05-06 23:50:06 +0000176/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross8ee492d2013-05-06 23:50:13 +0000177static inline void freezable_schedule_unsafe(void)
178{
179 freezer_do_not_count();
180 schedule();
181 freezer_count_unsafe();
182}
Colin Cross416ad3c2013-05-06 23:50:06 +0000183
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000184/*
Himanshu Jha2dd97892017-11-05 03:27:32 +0530185 * Like schedule_timeout(), but should not block the freezer. Do not
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000186 * 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
Jeff Laytond3103102011-12-01 22:44:39 +0100210/* Like schedule_timeout_killable(), but should not block the freezer. */
Colin Cross8ee492d2013-05-06 23:50:13 +0000211static 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}
Jeff Laytond3103102011-12-01 22:44:39 +0100219
Colin Cross416ad3c2013-05-06 23:50:06 +0000220/* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
Colin Cross8ee492d2013-05-06 23:50:13 +0000221static 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}
Colin Cross416ad3c2013-05-06 23:50:06 +0000229
Jeff Laytond3103102011-12-01 22:44:39 +0100230/*
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000231 * 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,
John Stultzda8b44d2016-03-17 14:20:51 -0700235 u64 delta, const enum hrtimer_mode mode)
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000236{
237 int __retval;
238 freezer_do_not_count();
239 __retval = schedule_hrtimeout_range(expires, delta, mode);
240 freezer_count();
241 return __retval;
242}
243
244/*
Jeff Laytonf06ac722011-10-19 15:30:40 -0400245 * Freezer-friendly wrappers around wait_event_interruptible(),
246 * wait_event_killable() and wait_event_interruptible_timeout(), originally
247 * defined in <linux/wait.h>
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700248 */
249
Colin Cross5853cc22013-05-07 17:52:05 +0000250/* 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
Matt Helsley8174f152008-10-18 20:27:19 -0700260#else /* !CONFIG_FREEZER */
Tejun Heo948246f2011-11-21 12:32:25 -0800261static inline bool frozen(struct task_struct *p) { return false; }
Tejun Heoa3201222011-11-21 12:32:25 -0800262static inline bool freezing(struct task_struct *p) { return false; }
Stephen Rothwell62c9ea62011-11-25 00:44:55 +0100263static inline void __thaw_task(struct task_struct *t) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800264
Tejun Heo8a32c442011-11-21 12:32:23 -0800265static inline bool __refrigerator(bool check_kthr_stop) { return false; }
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200266static inline int freeze_processes(void) { return -ENOSYS; }
267static inline int freeze_kernel_threads(void) { return -ENOSYS; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800268static inline void thaw_processes(void) {}
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100269static inline void thaw_kernel_threads(void) {}
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800270
Li Haifenge5f57622012-11-23 21:55:19 +0100271static inline bool try_to_freeze_nowarn(void) { return false; }
Tejun Heoa0acae02011-11-21 12:32:22 -0800272static inline bool try_to_freeze(void) { return false; }
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800273
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700274static 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; }
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700277static inline void set_freezable(void) {}
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700278
Jeff Laytond3103102011-12-01 22:44:39 +0100279#define freezable_schedule() schedule()
280
Colin Cross416ad3c2013-05-06 23:50:06 +0000281#define freezable_schedule_unsafe() schedule()
282
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000283#define freezable_schedule_timeout(timeout) schedule_timeout(timeout)
284
285#define freezable_schedule_timeout_interruptible(timeout) \
286 schedule_timeout_interruptible(timeout)
287
Jeff Laytond3103102011-12-01 22:44:39 +0100288#define freezable_schedule_timeout_killable(timeout) \
289 schedule_timeout_killable(timeout)
290
Colin Cross416ad3c2013-05-06 23:50:06 +0000291#define freezable_schedule_timeout_killable_unsafe(timeout) \
292 schedule_timeout_killable(timeout)
293
Colin Crossdd5ec0f2013-05-06 23:50:14 +0000294#define freezable_schedule_hrtimeout_range(expires, delta, mode) \
295 schedule_hrtimeout_range(expires, delta, mode)
296
Colin Cross5853cc22013-05-07 17:52:05 +0000297#define wait_event_freezekillable_unsafe(wq, condition) \
298 wait_event_killable(wq, condition)
299
Matt Helsley8174f152008-10-18 20:27:19 -0700300#endif /* !CONFIG_FREEZER */
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700301
302#endif /* FREEZER_H_INCLUDED */