• 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 		WRITE_ONCE(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 	/*
117 	 * Allow freezing the sched_delayed tasks; they will not execute until
118 	 * ttwu() fixes them up, so it is safe to swap their state now, instead
119 	 * of waiting for them to get fully dequeued.
120 	 */
121 	if (task_is_runnable(p))
122 		return 0;
123 
124 	if (p != current && task_curr(p))
125 		return 0;
126 
127 	if (!(state & (TASK_FREEZABLE | __TASK_STOPPED | __TASK_TRACED)))
128 		return 0;
129 
130 	/*
131 	 * Only TASK_NORMAL can be augmented with TASK_FREEZABLE, since they
132 	 * can suffer spurious wakeups.
133 	 */
134 	if (state & TASK_FREEZABLE)
135 		WARN_ON_ONCE(!(state & TASK_NORMAL));
136 
137 #ifdef CONFIG_LOCKDEP
138 	/*
139 	 * It's dangerous to freeze with locks held; there be dragons there.
140 	 */
141 	if (!(state & __TASK_FREEZABLE_UNSAFE))
142 		WARN_ON_ONCE(debug_locks && p->lockdep_depth);
143 #endif
144 
145 	p->saved_state = p->__state;
146 	WRITE_ONCE(p->__state, TASK_FROZEN);
147 	return TASK_FROZEN;
148 }
149 
__freeze_task(struct task_struct * p)150 static bool __freeze_task(struct task_struct *p)
151 {
152 	/* TASK_FREEZABLE|TASK_STOPPED|TASK_TRACED -> TASK_FROZEN */
153 	return task_call_func(p, __set_task_frozen, NULL);
154 }
155 
156 /**
157  * freeze_task - send a freeze request to given task
158  * @p: task to send the request to
159  *
160  * If @p is freezing, the freeze request is sent either by sending a fake
161  * signal (if it's not a kernel thread) or waking it up (if it's a kernel
162  * thread).
163  *
164  * RETURNS:
165  * %false, if @p is not freezing or already frozen; %true, otherwise
166  */
freeze_task(struct task_struct * p)167 bool freeze_task(struct task_struct *p)
168 {
169 	unsigned long flags;
170 
171 	spin_lock_irqsave(&freezer_lock, flags);
172 	if (!freezing(p) || frozen(p) || __freeze_task(p)) {
173 		spin_unlock_irqrestore(&freezer_lock, flags);
174 		return false;
175 	}
176 
177 	if (!(p->flags & PF_KTHREAD))
178 		fake_signal_wake_up(p);
179 	else
180 		wake_up_state(p, TASK_NORMAL);
181 
182 	spin_unlock_irqrestore(&freezer_lock, flags);
183 	return true;
184 }
185 
186 /*
187  * Restore the saved_state before the task entered freezer. For typical task
188  * in the __refrigerator(), saved_state == TASK_RUNNING so nothing happens
189  * here. For tasks which were TASK_NORMAL | TASK_FREEZABLE, their initial state
190  * is restored unless they got an expected wakeup (see ttwu_state_match()).
191  * Returns 1 if the task state was restored.
192  */
__restore_freezer_state(struct task_struct * p,void * arg)193 static int __restore_freezer_state(struct task_struct *p, void *arg)
194 {
195 	unsigned int state = p->saved_state;
196 
197 	if (state != TASK_RUNNING) {
198 		WRITE_ONCE(p->__state, state);
199 		p->saved_state = TASK_RUNNING;
200 		return 1;
201 	}
202 
203 	return 0;
204 }
205 
__thaw_task(struct task_struct * p)206 void __thaw_task(struct task_struct *p)
207 {
208 	guard(spinlock_irqsave)(&freezer_lock);
209 	if (frozen(p) && !task_call_func(p, __restore_freezer_state, NULL))
210 		wake_up_state(p, TASK_FROZEN);
211 }
212 
213 /**
214  * set_freezable - make %current freezable
215  *
216  * Mark %current freezable and enter refrigerator if necessary.
217  */
set_freezable(void)218 bool set_freezable(void)
219 {
220 	might_sleep();
221 
222 	/*
223 	 * Modify flags while holding freezer_lock.  This ensures the
224 	 * freezer notices that we aren't frozen yet or the freezing
225 	 * condition is visible to try_to_freeze() below.
226 	 */
227 	spin_lock_irq(&freezer_lock);
228 	current->flags &= ~PF_NOFREEZE;
229 	spin_unlock_irq(&freezer_lock);
230 
231 	return try_to_freeze();
232 }
233 EXPORT_SYMBOL(set_freezable);
234