blob: c450fa8b8b5ef4472cac53a70d99975ccca82c01 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Matt Helsley8174f152008-10-18 20:27:19 -07002/*
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>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Matt Helsley8174f152008-10-18 20:27:19 -070011#include <linux/syscalls.h>
12#include <linux/freezer.h>
Tejun Heo8a32c442011-11-21 12:32:23 -080013#include <linux/kthread.h>
Matt Helsley8174f152008-10-18 20:27:19 -070014
Tejun Heoa3201222011-11-21 12:32:25 -080015/* total number of freezing conditions in effect */
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020016DEFINE_STATIC_KEY_FALSE(freezer_active);
17EXPORT_SYMBOL(freezer_active);
Tejun Heoa3201222011-11-21 12:32:25 -080018
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020019/*
20 * indicate whether PM freezing is in effect, protected by
Pingfan Liu55f25032018-07-31 16:51:32 +080021 * system_transition_mutex
22 */
Tejun Heoa3201222011-11-21 12:32:25 -080023bool pm_freezing;
24bool pm_nosig_freezing;
25
Tejun Heo0c9af092011-11-21 12:32:24 -080026/* protects freezing and frozen transitions */
27static DEFINE_SPINLOCK(freezer_lock);
Matt Helsley8174f152008-10-18 20:27:19 -070028
Tejun Heoa3201222011-11-21 12:32:25 -080029/**
30 * freezing_slow_path - slow path for testing whether a task needs to be frozen
31 * @p: task to be tested
32 *
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020033 * This function is called by freezing() if freezer_active isn't zero
Tejun Heoa3201222011-11-21 12:32:25 -080034 * 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{
Colin Cross2b44c4d2013-07-24 17:41:33 -070040 if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
Tejun Heoa3201222011-11-21 12:32:25 -080041 return false;
42
Michal Hockoa34c80a2016-07-28 15:45:16 -070043 if (test_tsk_thread_flag(p, TIF_MEMDIE))
Cong Wang51fae6da2014-10-21 09:27:12 +020044 return false;
45
Tejun Heoa3201222011-11-21 12:32:25 -080046 if (pm_nosig_freezing || cgroup_freezing(p))
47 return true;
48
Tejun Heo34b087e2011-11-23 09:28:17 -080049 if (pm_freezing && !(p->flags & PF_KTHREAD))
Tejun Heoa3201222011-11-21 12:32:25 -080050 return true;
51
52 return false;
53}
54EXPORT_SYMBOL(freezing_slow_path);
55
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020056bool frozen(struct task_struct *p)
57{
58 return READ_ONCE(p->__state) & TASK_FROZEN;
59}
60
Matt Helsley8174f152008-10-18 20:27:19 -070061/* Refrigerator is place where frozen processes are stored :-). */
Tejun Heo8a32c442011-11-21 12:32:23 -080062bool __refrigerator(bool check_kthr_stop)
Matt Helsley8174f152008-10-18 20:27:19 -070063{
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020064 unsigned int state = get_current_state();
Tejun Heoa0acae02011-11-21 12:32:22 -080065 bool was_frozen = false;
Matt Helsley8174f152008-10-18 20:27:19 -070066
Matt Helsley8174f152008-10-18 20:27:19 -070067 pr_debug("%s entered refrigerator\n", current->comm);
68
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020069 WARN_ON_ONCE(state && !(state & TASK_NORMAL));
70
Matt Helsley8174f152008-10-18 20:27:19 -070071 for (;;) {
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020072 bool freeze;
73
Elliot Berman8f0eed42023-09-08 15:49:16 -070074 raw_spin_lock_irq(&current->pi_lock);
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020075 set_current_state(TASK_FROZEN);
Elliot Berman8f0eed42023-09-08 15:49:16 -070076 /* 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);
Tejun Heo5ece3ea2011-11-21 12:32:26 -080079
80 spin_lock_irq(&freezer_lock);
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020081 freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop());
Tejun Heo5ece3ea2011-11-21 12:32:26 -080082 spin_unlock_irq(&freezer_lock);
83
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020084 if (!freeze)
Matt Helsley8174f152008-10-18 20:27:19 -070085 break;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020086
Tejun Heoa0acae02011-11-21 12:32:22 -080087 was_frozen = true;
Matt Helsley8174f152008-10-18 20:27:19 -070088 schedule();
89 }
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020090 __set_current_state(TASK_RUNNING);
Thomas Gleixner6301cb92009-07-17 14:15:47 +020091
Matt Helsley8174f152008-10-18 20:27:19 -070092 pr_debug("%s left refrigerator\n", current->comm);
Tejun Heo50fb4f7f2011-11-21 12:32:22 -080093
Tejun Heoa0acae02011-11-21 12:32:22 -080094 return was_frozen;
Matt Helsley8174f152008-10-18 20:27:19 -070095}
Tejun Heoa0acae02011-11-21 12:32:22 -080096EXPORT_SYMBOL(__refrigerator);
Matt Helsley8174f152008-10-18 20:27:19 -070097
98static void fake_signal_wake_up(struct task_struct *p)
99{
100 unsigned long flags;
101
Tejun Heo37ad8ac2011-11-21 12:32:26 -0800102 if (lock_task_sighand(p, &flags)) {
103 signal_wake_up(p, 0);
104 unlock_task_sighand(p, &flags);
105 }
Matt Helsley8174f152008-10-18 20:27:19 -0700106}
107
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200108static 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
Elliot Berman8f0eed42023-09-08 15:49:16 -0700136 p->saved_state = p->__state;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200137 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
Matt Helsley8174f152008-10-18 20:27:19 -0700147/**
Tejun Heo839e3402011-11-21 12:32:26 -0800148 * freeze_task - send a freeze request to given task
149 * @p: task to send the request to
Matt Helsley8174f152008-10-18 20:27:19 -0700150 *
Marcos Paulo de Souza37f08be2012-02-21 23:57:47 +0100151 * 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).
Tejun Heo839e3402011-11-21 12:32:26 -0800154 *
155 * RETURNS:
156 * %false, if @p is not freezing or already frozen; %true, otherwise
Matt Helsley8174f152008-10-18 20:27:19 -0700157 */
Tejun Heo839e3402011-11-21 12:32:26 -0800158bool freeze_task(struct task_struct *p)
Matt Helsley8174f152008-10-18 20:27:19 -0700159{
Tejun Heo0c9af092011-11-21 12:32:24 -0800160 unsigned long flags;
Matt Helsley8174f152008-10-18 20:27:19 -0700161
Tejun Heo0c9af092011-11-21 12:32:24 -0800162 spin_lock_irqsave(&freezer_lock, flags);
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200163 if (!freezing(p) || frozen(p) || __freeze_task(p)) {
Tejun Heoa3201222011-11-21 12:32:25 -0800164 spin_unlock_irqrestore(&freezer_lock, flags);
165 return false;
166 }
Matt Helsley8174f152008-10-18 20:27:19 -0700167
Jens Axboed3dc04c2021-03-25 18:22:11 -0600168 if (!(p->flags & PF_KTHREAD))
Tejun Heo8cfe4002010-11-26 23:07:27 +0100169 fake_signal_wake_up(p);
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200170 else
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200171 wake_up_state(p, TASK_NORMAL);
Tejun Heoa3201222011-11-21 12:32:25 -0800172
Tejun Heo0c9af092011-11-21 12:32:24 -0800173 spin_unlock_irqrestore(&freezer_lock, flags);
Tejun Heoa3201222011-11-21 12:32:25 -0800174 return true;
Matt Helsley8174f152008-10-18 20:27:19 -0700175}
176
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200177/*
Elliot Berman8f0eed42023-09-08 15:49:16 -0700178 * 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.
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200183 */
Elliot Berman8f0eed42023-09-08 15:49:16 -0700184static int __restore_freezer_state(struct task_struct *p, void *arg)
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200185{
Elliot Berman8f0eed42023-09-08 15:49:16 -0700186 unsigned int state = p->saved_state;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200187
Elliot Berman8f0eed42023-09-08 15:49:16 -0700188 if (state != TASK_RUNNING) {
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200189 WRITE_ONCE(p->__state, state);
Elliot Berman8f0eed42023-09-08 15:49:16 -0700190 return 1;
191 }
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200192
Elliot Berman8f0eed42023-09-08 15:49:16 -0700193 return 0;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200194}
195
Tejun Heoa5be2d02011-11-21 12:32:23 -0800196void __thaw_task(struct task_struct *p)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700197{
Elliot Berman8f0eed42023-09-08 15:49:16 -0700198 unsigned long flags;
Tejun Heoa5be2d02011-11-21 12:32:23 -0800199
Tejun Heo0c9af092011-11-21 12:32:24 -0800200 spin_lock_irqsave(&freezer_lock, flags);
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200201 if (WARN_ON_ONCE(freezing(p)))
202 goto unlock;
203
Elliot Berman8f0eed42023-09-08 15:49:16 -0700204 if (task_call_func(p, __restore_freezer_state, NULL))
205 goto unlock;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200206
207 wake_up_state(p, TASK_FROZEN);
208unlock:
Tejun Heo0c9af092011-11-21 12:32:24 -0800209 spin_unlock_irqrestore(&freezer_lock, flags);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700210}
Tejun Heo96ee6d82011-11-21 12:32:25 -0800211
212/**
Tejun Heo34b087e2011-11-23 09:28:17 -0800213 * set_freezable - make %current freezable
Tejun Heo96ee6d82011-11-21 12:32:25 -0800214 *
215 * Mark %current freezable and enter refrigerator if necessary.
216 */
Tejun Heo34b087e2011-11-23 09:28:17 -0800217bool set_freezable(void)
Tejun Heo96ee6d82011-11-21 12:32:25 -0800218{
219 might_sleep();
220
221 /*
222 * Modify flags while holding freezer_lock. This ensures the
223 * freezer notices that we aren't frozen yet or the freezing
224 * condition is visible to try_to_freeze() below.
225 */
226 spin_lock_irq(&freezer_lock);
227 current->flags &= ~PF_NOFREEZE;
Tejun Heo96ee6d82011-11-21 12:32:25 -0800228 spin_unlock_irq(&freezer_lock);
229
230 return try_to_freeze();
231}
Tejun Heo34b087e2011-11-23 09:28:17 -0800232EXPORT_SYMBOL(set_freezable);