xref: /linux-6.15/include/linux/freezer.h (revision db4e8395)
1 /* Freezer declarations */
2 
3 #ifndef FREEZER_H_INCLUDED
4 #define FREEZER_H_INCLUDED
5 
6 #include <linux/sched.h>
7 #include <linux/wait.h>
8 
9 #ifdef CONFIG_FREEZER
10 /*
11  * Check if a process has been frozen
12  */
13 static 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  */
21 static inline int freezing(struct task_struct *p)
22 {
23 	return test_tsk_thread_flag(p, TIF_FREEZE);
24 }
25 
26 /*
27  * Request that a process be frozen
28  */
29 static inline void set_freeze_flag(struct task_struct *p)
30 {
31 	set_tsk_thread_flag(p, TIF_FREEZE);
32 }
33 
34 /*
35  * Sometimes we may need to cancel the previous 'freeze' request
36  */
37 static inline void clear_freeze_flag(struct task_struct *p)
38 {
39 	clear_tsk_thread_flag(p, TIF_FREEZE);
40 }
41 
42 static inline bool should_send_signal(struct task_struct *p)
43 {
44 	return !(p->flags & PF_FREEZER_NOSIG);
45 }
46 
47 /* Takes and releases task alloc lock using task_lock() */
48 extern int thaw_process(struct task_struct *p);
49 
50 extern void refrigerator(void);
51 extern int freeze_processes(void);
52 extern int freeze_kernel_threads(void);
53 extern void thaw_processes(void);
54 
55 static inline int try_to_freeze(void)
56 {
57 	if (freezing(current)) {
58 		refrigerator();
59 		return 1;
60 	} else
61 		return 0;
62 }
63 
64 extern bool freeze_task(struct task_struct *p, bool sig_only);
65 extern void cancel_freezing(struct task_struct *p);
66 
67 #ifdef CONFIG_CGROUP_FREEZER
68 extern int cgroup_freezing_or_frozen(struct task_struct *task);
69 #else /* !CONFIG_CGROUP_FREEZER */
70 static inline int cgroup_freezing_or_frozen(struct task_struct *task)
71 {
72 	return 0;
73 }
74 #endif /* !CONFIG_CGROUP_FREEZER */
75 
76 /*
77  * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
78  * calls wait_for_completion(&vfork) and reset right after it returns from this
79  * function.  Next, the parent should call try_to_freeze() to freeze itself
80  * appropriately in case the child has exited before the freezing of tasks is
81  * complete.  However, we don't want kernel threads to be frozen in unexpected
82  * places, so we allow them to block freeze_processes() instead or to set
83  * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
84  * parents.  Fortunately, in the ____call_usermodehelper() case the parent won't
85  * really block freeze_processes(), since ____call_usermodehelper() (the child)
86  * does a little before exec/exit and it can't be frozen before waking up the
87  * parent.
88  */
89 
90 /*
91  * If the current task is a user space one, tell the freezer not to count it as
92  * freezable.
93  */
94 static inline void freezer_do_not_count(void)
95 {
96 	if (current->mm)
97 		current->flags |= PF_FREEZER_SKIP;
98 }
99 
100 /*
101  * If the current task is a user space one, tell the freezer to count it as
102  * freezable again and try to freeze it.
103  */
104 static inline void freezer_count(void)
105 {
106 	if (current->mm) {
107 		current->flags &= ~PF_FREEZER_SKIP;
108 		try_to_freeze();
109 	}
110 }
111 
112 /*
113  * Check if the task should be counted as freezable by the freezer
114  */
115 static inline int freezer_should_skip(struct task_struct *p)
116 {
117 	return !!(p->flags & PF_FREEZER_SKIP);
118 }
119 
120 /*
121  * Tell the freezer that the current task should be frozen by it
122  */
123 static inline void set_freezable(void)
124 {
125 	current->flags &= ~PF_NOFREEZE;
126 }
127 
128 /*
129  * Tell the freezer that the current task should be frozen by it and that it
130  * should send a fake signal to the task to freeze it.
131  */
132 static inline void set_freezable_with_signal(void)
133 {
134 	current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
135 }
136 
137 /*
138  * Freezer-friendly wrappers around wait_event_interruptible(),
139  * wait_event_killable() and wait_event_interruptible_timeout(), originally
140  * defined in <linux/wait.h>
141  */
142 
143 #define wait_event_freezekillable(wq, condition)			\
144 ({									\
145 	int __retval;							\
146 	do {								\
147 		__retval = wait_event_killable(wq,			\
148 				(condition) || freezing(current));	\
149 		if (__retval && !freezing(current))			\
150 			break;						\
151 		else if (!(condition))					\
152 			__retval = -ERESTARTSYS;			\
153 	} while (try_to_freeze());					\
154 	__retval;							\
155 })
156 
157 #define wait_event_freezable(wq, condition)				\
158 ({									\
159 	int __retval;							\
160 	do {								\
161 		__retval = wait_event_interruptible(wq, 		\
162 				(condition) || freezing(current));	\
163 		if (__retval && !freezing(current))			\
164 			break;						\
165 		else if (!(condition))					\
166 			__retval = -ERESTARTSYS;			\
167 	} while (try_to_freeze());					\
168 	__retval;							\
169 })
170 
171 
172 #define wait_event_freezable_timeout(wq, condition, timeout)		\
173 ({									\
174 	long __retval = timeout;					\
175 	do {								\
176 		__retval = wait_event_interruptible_timeout(wq,		\
177 				(condition) || freezing(current),	\
178 				__retval); 				\
179 	} while (try_to_freeze());					\
180 	__retval;							\
181 })
182 #else /* !CONFIG_FREEZER */
183 static inline int frozen(struct task_struct *p) { return 0; }
184 static inline int freezing(struct task_struct *p) { return 0; }
185 static inline void set_freeze_flag(struct task_struct *p) {}
186 static inline void clear_freeze_flag(struct task_struct *p) {}
187 static inline int thaw_process(struct task_struct *p) { return 1; }
188 
189 static inline void refrigerator(void) {}
190 static inline int freeze_processes(void) { return -ENOSYS; }
191 static inline int freeze_kernel_threads(void) { return -ENOSYS; }
192 static inline void thaw_processes(void) {}
193 
194 static inline int try_to_freeze(void) { return 0; }
195 
196 static inline void freezer_do_not_count(void) {}
197 static inline void freezer_count(void) {}
198 static inline int freezer_should_skip(struct task_struct *p) { return 0; }
199 static inline void set_freezable(void) {}
200 static inline void set_freezable_with_signal(void) {}
201 
202 #define wait_event_freezable(wq, condition)				\
203 		wait_event_interruptible(wq, condition)
204 
205 #define wait_event_freezable_timeout(wq, condition, timeout)		\
206 		wait_event_interruptible_timeout(wq, condition, timeout)
207 
208 #define wait_event_freezekillable(wq, condition)		\
209 		wait_event_killable(wq, condition)
210 
211 #endif /* !CONFIG_FREEZER */
212 
213 #endif	/* FREEZER_H_INCLUDED */
214