1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kernel/freezer.c - Function to freeze a process
4  *
5  * Originally from kernel/power/process.c
6  */
7 
8 #include <linux/interrupt.h>
9 #include <linux/suspend.h>
10 #include <linux/export.h>
11 #include <linux/syscalls.h>
12 #include <linux/freezer.h>
13 #include <linux/kthread.h>
14 
15 /* total number of freezing conditions in effect */
16 DEFINE_STATIC_KEY_FALSE(freezer_active);
17 EXPORT_SYMBOL(freezer_active);
18 
19 /*
20  * indicate whether PM freezing is in effect, protected by
21  * system_transition_mutex
22  */
23 bool pm_freezing;
24 bool pm_nosig_freezing;
25 
26 /* protects freezing and frozen transitions */
27 static DEFINE_SPINLOCK(freezer_lock);
28 
29 /**
30  * freezing_slow_path - slow path for testing whether a task needs to be frozen
31  * @p: task to be tested
32  *
33  * This function is called by freezing() if freezer_active isn't zero
34  * and tests whether @p needs to enter and stay in frozen state.  Can be
35  * called under any context.  The freezers are responsible for ensuring the
36  * target tasks see the updated state.
37  */
freezing_slow_path(struct task_struct * p)38 bool freezing_slow_path(struct task_struct *p)
39 {
40 	if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
41 		return false;
42 
43 	if (test_tsk_thread_flag(p, TIF_MEMDIE))
44 		return false;
45 
46 	if (pm_nosig_freezing || cgroup_freezing(p))
47 		return true;
48 
49 	if (pm_freezing && !(p->flags & PF_KTHREAD))
50 		return true;
51 
52 	return false;
53 }
54 EXPORT_SYMBOL(freezing_slow_path);
55 
frozen(struct task_struct * p)56 bool frozen(struct task_struct *p)
57 {
58 	return READ_ONCE(p->__state) & TASK_FROZEN;
59 }
60 
61 /* Refrigerator is place where frozen processes are stored :-). */
__refrigerator(bool check_kthr_stop)62 bool __refrigerator(bool check_kthr_stop)
63 {
64 	unsigned int state = get_current_state();
65 	bool was_frozen = false;
66 
67 	pr_debug("%s entered refrigerator\n", current->comm);
68 
69 	WARN_ON_ONCE(state && !(state & TASK_NORMAL));
70 
71 	for (;;) {
72 		bool freeze;
73 
74 		raw_spin_lock_irq(&current->pi_lock);
75 		WRITE_ONCE(current->__state, TASK_FROZEN);
76 		/* unstale saved_state so that __thaw_task() will wake us up */
77 		current->saved_state = TASK_RUNNING;
78 		raw_spin_unlock_irq(&current->pi_lock);
79 
80 		spin_lock_irq(&freezer_lock);
81 		freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop());
82 		spin_unlock_irq(&freezer_lock);
83 
84 		if (!freeze)
85 			break;
86 
87 		was_frozen = true;
88 		schedule();
89 	}
90 	__set_current_state(TASK_RUNNING);
91 
92 	pr_debug("%s left refrigerator\n", current->comm);
93 
94 	return was_frozen;
95 }
96 EXPORT_SYMBOL(__refrigerator);
97 
fake_signal_wake_up(struct task_struct * p)98 static void fake_signal_wake_up(struct task_struct *p)
99 {
100 	unsigned long flags;
101 
102 	if (lock_task_sighand(p, &flags)) {
103 		signal_wake_up(p, 0);
104 		unlock_task_sighand(p, &flags);
105 	}
106 }
107 
__set_task_frozen(struct task_struct * p,void * arg)108 static int __set_task_frozen(struct task_struct *p, void *arg)
109 {
110 	unsigned int state = READ_ONCE(p->__state);
111 
112 	/*
113 	 * Allow freezing the sched_delayed tasks; they will not execute until
114 	 * ttwu() fixes them up, so it is safe to swap their state now, instead
115 	 * of waiting for them to get fully dequeued.
116 	 */
117 	if (task_is_runnable(p))
118 		return 0;
119 
120 	if (p != current && task_curr(p))
121 		return 0;
122 
123 	if (!(state & (TASK_FREEZABLE | __TASK_STOPPED | __TASK_TRACED)))
124 		return 0;
125 
126 	/*
127 	 * Only TASK_NORMAL can be augmented with TASK_FREEZABLE, since they
128 	 * can suffer spurious wakeups.
129 	 */
130 	if (state & TASK_FREEZABLE)
131 		WARN_ON_ONCE(!(state & TASK_NORMAL));
132 
133 #ifdef CONFIG_LOCKDEP
134 	/*
135 	 * It's dangerous to freeze with locks held; there be dragons there.
136 	 */
137 	if (!(state & __TASK_FREEZABLE_UNSAFE))
138 		WARN_ON_ONCE(debug_locks && p->lockdep_depth);
139 #endif
140 
141 	p->saved_state = p->__state;
142 	WRITE_ONCE(p->__state, TASK_FROZEN);
143 	return TASK_FROZEN;
144 }
145 
__freeze_task(struct task_struct * p)146 static bool __freeze_task(struct task_struct *p)
147 {
148 	/* TASK_FREEZABLE|TASK_STOPPED|TASK_TRACED -> TASK_FROZEN */
149 	return task_call_func(p, __set_task_frozen, NULL);
150 }
151 
152 /**
153  * freeze_task - send a freeze request to given task
154  * @p: task to send the request to
155  *
156  * If @p is freezing, the freeze request is sent either by sending a fake
157  * signal (if it's not a kernel thread) or waking it up (if it's a kernel
158  * thread).
159  *
160  * RETURNS:
161  * %false, if @p is not freezing or already frozen; %true, otherwise
162  */
freeze_task(struct task_struct * p)163 bool freeze_task(struct task_struct *p)
164 {
165 	unsigned long flags;
166 
167 	spin_lock_irqsave(&freezer_lock, flags);
168 	if (!freezing(p) || frozen(p) || __freeze_task(p)) {
169 		spin_unlock_irqrestore(&freezer_lock, flags);
170 		return false;
171 	}
172 
173 	if (!(p->flags & PF_KTHREAD))
174 		fake_signal_wake_up(p);
175 	else
176 		wake_up_state(p, TASK_NORMAL);
177 
178 	spin_unlock_irqrestore(&freezer_lock, flags);
179 	return true;
180 }
181 
182 /*
183  * Restore the saved_state before the task entered freezer. For typical task
184  * in the __refrigerator(), saved_state == TASK_RUNNING so nothing happens
185  * here. For tasks which were TASK_NORMAL | TASK_FREEZABLE, their initial state
186  * is restored unless they got an expected wakeup (see ttwu_state_match()).
187  * Returns 1 if the task state was restored.
188  */
__restore_freezer_state(struct task_struct * p,void * arg)189 static int __restore_freezer_state(struct task_struct *p, void *arg)
190 {
191 	unsigned int state = p->saved_state;
192 
193 	if (state != TASK_RUNNING) {
194 		WRITE_ONCE(p->__state, state);
195 		p->saved_state = TASK_RUNNING;
196 		return 1;
197 	}
198 
199 	return 0;
200 }
201 
__thaw_task(struct task_struct * p)202 void __thaw_task(struct task_struct *p)
203 {
204 	unsigned long flags;
205 
206 	spin_lock_irqsave(&freezer_lock, flags);
207 	if (WARN_ON_ONCE(freezing(p)))
208 		goto unlock;
209 
210 	if (!frozen(p) || task_call_func(p, __restore_freezer_state, NULL))
211 		goto unlock;
212 
213 	wake_up_state(p, TASK_FROZEN);
214 unlock:
215 	spin_unlock_irqrestore(&freezer_lock, flags);
216 }
217 
218 /**
219  * set_freezable - make %current freezable
220  *
221  * Mark %current freezable and enter refrigerator if necessary.
222  */
set_freezable(void)223 bool set_freezable(void)
224 {
225 	might_sleep();
226 
227 	/*
228 	 * Modify flags while holding freezer_lock.  This ensures the
229 	 * freezer notices that we aren't frozen yet or the freezing
230 	 * condition is visible to try_to_freeze() below.
231 	 */
232 	spin_lock_irq(&freezer_lock);
233 	current->flags &= ~PF_NOFREEZE;
234 	spin_unlock_irq(&freezer_lock);
235 
236 	return try_to_freeze();
237 }
238 EXPORT_SYMBOL(set_freezable);
239