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