scsi: core: Move scsi_host_busy() out of host lock for waking up EH handler
[linux-block.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);
f5d39b02 75 set_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
112 if (p->on_rq)
113 return 0;
114
115 if (p != current && task_curr(p))
116 return 0;
117
118 if (!(state & (TASK_FREEZABLE | __TASK_STOPPED | __TASK_TRACED)))
119 return 0;
120
121 /*
122 * Only TASK_NORMAL can be augmented with TASK_FREEZABLE, since they
123 * can suffer spurious wakeups.
124 */
125 if (state & TASK_FREEZABLE)
126 WARN_ON_ONCE(!(state & TASK_NORMAL));
127
128#ifdef CONFIG_LOCKDEP
129 /*
130 * It's dangerous to freeze with locks held; there be dragons there.
131 */
132 if (!(state & __TASK_FREEZABLE_UNSAFE))
133 WARN_ON_ONCE(debug_locks && p->lockdep_depth);
134#endif
135
8f0eed4a 136 p->saved_state = p->__state;
f5d39b02
PZ
137 WRITE_ONCE(p->__state, TASK_FROZEN);
138 return TASK_FROZEN;
139}
140
141static bool __freeze_task(struct task_struct *p)
142{
143 /* TASK_FREEZABLE|TASK_STOPPED|TASK_TRACED -> TASK_FROZEN */
144 return task_call_func(p, __set_task_frozen, NULL);
145}
146
8174f150 147/**
839e3407
TH
148 * freeze_task - send a freeze request to given task
149 * @p: task to send the request to
8174f150 150 *
37f08be1
MPS
151 * If @p is freezing, the freeze request is sent either by sending a fake
152 * signal (if it's not a kernel thread) or waking it up (if it's a kernel
153 * thread).
839e3407
TH
154 *
155 * RETURNS:
156 * %false, if @p is not freezing or already frozen; %true, otherwise
8174f150 157 */
839e3407 158bool freeze_task(struct task_struct *p)
8174f150 159{
0c9af092 160 unsigned long flags;
0c9af092
TH
161
162 spin_lock_irqsave(&freezer_lock, flags);
f5d39b02 163 if (!freezing(p) || frozen(p) || __freeze_task(p)) {
a3201227
TH
164 spin_unlock_irqrestore(&freezer_lock, flags);
165 return false;
166 }
8174f150 167
d3dc04cd 168 if (!(p->flags & PF_KTHREAD))
8cfe400c 169 fake_signal_wake_up(p);
5d8f72b5 170 else
f5d39b02 171 wake_up_state(p, TASK_NORMAL);
a3201227 172
0c9af092 173 spin_unlock_irqrestore(&freezer_lock, flags);
a3201227 174 return true;
8174f150
MH
175}
176
f5d39b02 177/*
8f0eed4a
EB
178 * Restore the saved_state before the task entered freezer. For typical task
179 * in the __refrigerator(), saved_state == TASK_RUNNING so nothing happens
180 * here. For tasks which were TASK_NORMAL | TASK_FREEZABLE, their initial state
181 * is restored unless they got an expected wakeup (see ttwu_state_match()).
182 * Returns 1 if the task state was restored.
f5d39b02 183 */
8f0eed4a 184static int __restore_freezer_state(struct task_struct *p, void *arg)
f5d39b02 185{
8f0eed4a 186 unsigned int state = p->saved_state;
f5d39b02 187
8f0eed4a 188 if (state != TASK_RUNNING) {
f5d39b02 189 WRITE_ONCE(p->__state, state);
418146e3 190 p->saved_state = TASK_RUNNING;
8f0eed4a
EB
191 return 1;
192 }
f5d39b02 193
8f0eed4a 194 return 0;
f5d39b02
PZ
195}
196
a5be2d0d 197void __thaw_task(struct task_struct *p)
dc52ddc0 198{
8f0eed4a 199 unsigned long flags;
a5be2d0d 200
0c9af092 201 spin_lock_irqsave(&freezer_lock, flags);
f5d39b02
PZ
202 if (WARN_ON_ONCE(freezing(p)))
203 goto unlock;
204
23ab79e8 205 if (!frozen(p) || task_call_func(p, __restore_freezer_state, NULL))
8f0eed4a 206 goto unlock;
f5d39b02
PZ
207
208 wake_up_state(p, TASK_FROZEN);
209unlock:
0c9af092 210 spin_unlock_irqrestore(&freezer_lock, flags);
dc52ddc0 211}
96ee6d85
TH
212
213/**
34b087e4 214 * set_freezable - make %current freezable
96ee6d85
TH
215 *
216 * Mark %current freezable and enter refrigerator if necessary.
217 */
34b087e4 218bool set_freezable(void)
96ee6d85
TH
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;
96ee6d85
TH
229 spin_unlock_irq(&freezer_lock);
230
231 return try_to_freeze();
232}
34b087e4 233EXPORT_SYMBOL(set_freezable);