Merge tag 'phy-fixes-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/phy/linux-phy
[linux.git] / kernel / freezer.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
8174f150
MH
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>
9984de1a 10#include <linux/export.h>
8174f150
MH
11#include <linux/syscalls.h>
12#include <linux/freezer.h>
8a32c441 13#include <linux/kthread.h>
8174f150 14
a3201227 15/* total number of freezing conditions in effect */
f5d39b02
PZ
16DEFINE_STATIC_KEY_FALSE(freezer_active);
17EXPORT_SYMBOL(freezer_active);
a3201227 18
f5d39b02
PZ
19/*
20 * indicate whether PM freezing is in effect, protected by
55f2503c
PL
21 * system_transition_mutex
22 */
a3201227
TH
23bool pm_freezing;
24bool pm_nosig_freezing;
25
0c9af092
TH
26/* protects freezing and frozen transitions */
27static DEFINE_SPINLOCK(freezer_lock);
8174f150 28
a3201227
TH
29/**
30 * freezing_slow_path - slow path for testing whether a task needs to be frozen
31 * @p: task to be tested
32 *
f5d39b02 33 * This function is called by freezing() if freezer_active isn't zero
a3201227
TH
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 */
38bool freezing_slow_path(struct task_struct *p)
39{
2b44c4db 40 if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
a3201227
TH
41 return false;
42
a34c80a7 43 if (test_tsk_thread_flag(p, TIF_MEMDIE))
51fae6da
CW
44 return false;
45
a3201227
TH
46 if (pm_nosig_freezing || cgroup_freezing(p))
47 return true;
48
34b087e4 49 if (pm_freezing && !(p->flags & PF_KTHREAD))
a3201227
TH
50 return true;
51
52 return false;
53}
54EXPORT_SYMBOL(freezing_slow_path);
55
f5d39b02
PZ
56bool frozen(struct task_struct *p)
57{
58 return READ_ONCE(p->__state) & TASK_FROZEN;
59}
60
8174f150 61/* Refrigerator is place where frozen processes are stored :-). */
8a32c441 62bool __refrigerator(bool check_kthr_stop)
8174f150 63{
f5d39b02 64 unsigned int state = get_current_state();
a0acae0e 65 bool was_frozen = false;
8174f150 66
8174f150
MH
67 pr_debug("%s entered refrigerator\n", current->comm);
68
f5d39b02
PZ
69 WARN_ON_ONCE(state && !(state & TASK_NORMAL));
70
8174f150 71 for (;;) {
f5d39b02
PZ
72 bool freeze;
73
8f0eed4a 74 raw_spin_lock_irq(&current->pi_lock);
a1c44661 75 WRITE_ONCE(current->__state, TASK_FROZEN);
8f0eed4a
EB
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);
5ece3eae
TH
79
80 spin_lock_irq(&freezer_lock);
f5d39b02 81 freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop());
5ece3eae
TH
82 spin_unlock_irq(&freezer_lock);
83
f5d39b02 84 if (!freeze)
8174f150 85 break;
f5d39b02 86
a0acae0e 87 was_frozen = true;
8174f150
MH
88 schedule();
89 }
f5d39b02 90 __set_current_state(TASK_RUNNING);
6301cb95 91
8174f150 92 pr_debug("%s left refrigerator\n", current->comm);
50fb4f7f 93
a0acae0e 94 return was_frozen;
8174f150 95}
a0acae0e 96EXPORT_SYMBOL(__refrigerator);
8174f150
MH
97
98static void fake_signal_wake_up(struct task_struct *p)
99{
100 unsigned long flags;
101
37ad8aca
TH
102 if (lock_task_sighand(p, &flags)) {
103 signal_wake_up(p, 0);
104 unlock_task_sighand(p, &flags);
105 }
8174f150
MH
106}
107
f5d39b02
PZ
108static int __set_task_frozen(struct task_struct *p, void *arg)
109{
110 unsigned int state = READ_ONCE(p->__state);
111
cd9626e9
PZ
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))
f5d39b02
PZ
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
8f0eed4a 141 p->saved_state = p->__state;
f5d39b02
PZ
142 WRITE_ONCE(p->__state, TASK_FROZEN);
143 return TASK_FROZEN;
144}
145
146static 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
8174f150 152/**
839e3407
TH
153 * freeze_task - send a freeze request to given task
154 * @p: task to send the request to
8174f150 155 *
37f08be1
MPS
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).
839e3407
TH
159 *
160 * RETURNS:
161 * %false, if @p is not freezing or already frozen; %true, otherwise
8174f150 162 */
839e3407 163bool freeze_task(struct task_struct *p)
8174f150 164{
0c9af092 165 unsigned long flags;
0c9af092
TH
166
167 spin_lock_irqsave(&freezer_lock, flags);
f5d39b02 168 if (!freezing(p) || frozen(p) || __freeze_task(p)) {
a3201227
TH
169 spin_unlock_irqrestore(&freezer_lock, flags);
170 return false;
171 }
8174f150 172
d3dc04cd 173 if (!(p->flags & PF_KTHREAD))
8cfe400c 174 fake_signal_wake_up(p);
5d8f72b5 175 else
f5d39b02 176 wake_up_state(p, TASK_NORMAL);
a3201227 177
0c9af092 178 spin_unlock_irqrestore(&freezer_lock, flags);
a3201227 179 return true;
8174f150
MH
180}
181
f5d39b02 182/*
8f0eed4a
EB
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.
f5d39b02 188 */
8f0eed4a 189static int __restore_freezer_state(struct task_struct *p, void *arg)
f5d39b02 190{
8f0eed4a 191 unsigned int state = p->saved_state;
f5d39b02 192
8f0eed4a 193 if (state != TASK_RUNNING) {
f5d39b02 194 WRITE_ONCE(p->__state, state);
418146e3 195 p->saved_state = TASK_RUNNING;
8f0eed4a
EB
196 return 1;
197 }
f5d39b02 198
8f0eed4a 199 return 0;
f5d39b02
PZ
200}
201
a5be2d0d 202void __thaw_task(struct task_struct *p)
dc52ddc0 203{
8f0eed4a 204 unsigned long flags;
a5be2d0d 205
0c9af092 206 spin_lock_irqsave(&freezer_lock, flags);
f5d39b02
PZ
207 if (WARN_ON_ONCE(freezing(p)))
208 goto unlock;
209
23ab79e8 210 if (!frozen(p) || task_call_func(p, __restore_freezer_state, NULL))
8f0eed4a 211 goto unlock;
f5d39b02
PZ
212
213 wake_up_state(p, TASK_FROZEN);
214unlock:
0c9af092 215 spin_unlock_irqrestore(&freezer_lock, flags);
dc52ddc0 216}
96ee6d85
TH
217
218/**
34b087e4 219 * set_freezable - make %current freezable
96ee6d85
TH
220 *
221 * Mark %current freezable and enter refrigerator if necessary.
222 */
34b087e4 223bool set_freezable(void)
96ee6d85
TH
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;
96ee6d85
TH
234 spin_unlock_irq(&freezer_lock);
235
236 return try_to_freeze();
237}
34b087e4 238EXPORT_SYMBOL(set_freezable);
This page took 0.856853 seconds and 4 git commands to generate.