blob: 2f8ecd994d47d98b8e7a415891982478228f21f4 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * kernel/freezer.c - Function to freeze a process
3 *
4 * Originally from kernel/power/process.c
5 */
6
7#include <linux/interrupt.h>
8#include <linux/suspend.h>
9#include <linux/export.h>
10#include <linux/syscalls.h>
11#include <linux/freezer.h>
12#include <linux/kthread.h>
13
14/* total number of freezing conditions in effect */
15atomic_t system_freezing_cnt = ATOMIC_INIT(0);
16EXPORT_SYMBOL(system_freezing_cnt);
17
18/* indicate whether PM freezing is in effect, protected by pm_mutex */
19bool pm_freezing;
20bool pm_nosig_freezing;
21
22/* protects freezing and frozen transitions */
23static DEFINE_SPINLOCK(freezer_lock);
24
25/**
26 * freezing_slow_path - slow path for testing whether a task needs to be frozen
27 * @p: task to be tested
28 *
29 * This function is called by freezing() if system_freezing_cnt isn't zero
30 * and tests whether @p needs to enter and stay in frozen state. Can be
31 * called under any context. The freezers are responsible for ensuring the
32 * target tasks see the updated state.
33 */
34bool freezing_slow_path(struct task_struct *p)
35{
36 if (p->flags & PF_NOFREEZE)
37 return false;
38
39 if (test_thread_flag(TIF_MEMDIE))
40 return false;
41
42 if (pm_nosig_freezing || cgroup_freezing(p))
43 return true;
44
45 if (pm_freezing && !(p->flags & PF_KTHREAD))
46 return true;
47
48 return false;
49}
50EXPORT_SYMBOL(freezing_slow_path);
51
52/* Refrigerator is place where frozen processes are stored :-). */
53bool __refrigerator(bool check_kthr_stop)
54{
55 /* Hmm, should we be allowed to suspend when there are realtime
56 processes around? */
57 bool was_frozen = false;
58 long save = current->state;
59
60 pr_debug("%s entered refrigerator\n", current->comm);
61
62 for (;;) {
63 set_current_state(TASK_UNINTERRUPTIBLE);
64
65 spin_lock_irq(&freezer_lock);
66 current->flags |= PF_FROZEN;
67 if (!freezing(current) ||
68 (check_kthr_stop && kthread_should_stop()))
69 current->flags &= ~PF_FROZEN;
70 spin_unlock_irq(&freezer_lock);
71
72 if (!(current->flags & PF_FROZEN))
73 break;
74 was_frozen = true;
75 schedule();
76 }
77
78 pr_debug("%s left refrigerator\n", current->comm);
79
80 /*
81 * Restore saved task state before returning. The mb'd version
82 * needs to be used; otherwise, it might silently break
83 * synchronization which depends on ordered task state change.
84 */
85 set_current_state(save);
86
87 return was_frozen;
88}
89EXPORT_SYMBOL(__refrigerator);
90
91static void fake_signal_wake_up(struct task_struct *p)
92{
93 unsigned long flags;
94
95 if (lock_task_sighand(p, &flags)) {
96 signal_wake_up(p, 0);
97 unlock_task_sighand(p, &flags);
98 }
99}
100
101/**
102 * freeze_task - send a freeze request to given task
103 * @p: task to send the request to
104 *
105 * If @p is freezing, the freeze request is sent either by sending a fake
106 * signal (if it's not a kernel thread) or waking it up (if it's a kernel
107 * thread).
108 *
109 * RETURNS:
110 * %false, if @p is not freezing or already frozen; %true, otherwise
111 */
112bool freeze_task(struct task_struct *p)
113{
114 unsigned long flags;
115
116 spin_lock_irqsave(&freezer_lock, flags);
117 if (!freezing(p) || frozen(p)) {
118 spin_unlock_irqrestore(&freezer_lock, flags);
119 return false;
120 }
121
122 if (!(p->flags & PF_KTHREAD)) {
123 fake_signal_wake_up(p);
124 /*
125 * fake_signal_wake_up() goes through p's scheduler
126 * lock and guarantees that TASK_STOPPED/TRACED ->
127 * TASK_RUNNING transition can't race with task state
128 * testing in try_to_freeze_tasks().
129 */
130 } else {
131 wake_up_state(p, TASK_INTERRUPTIBLE);
132 }
133
134 spin_unlock_irqrestore(&freezer_lock, flags);
135 return true;
136}
137
138void __thaw_task(struct task_struct *p)
139{
140 unsigned long flags;
141
142 /*
143 * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
144 * be visible to @p as waking up implies wmb. Waking up inside
145 * freezer_lock also prevents wakeups from leaking outside
146 * refrigerator.
147 */
148 spin_lock_irqsave(&freezer_lock, flags);
149 if (frozen(p))
150 wake_up_process(p);
151 spin_unlock_irqrestore(&freezer_lock, flags);
152}
153
154/**
155 * set_freezable - make %current freezable
156 *
157 * Mark %current freezable and enter refrigerator if necessary.
158 */
159bool set_freezable(void)
160{
161 might_sleep();
162
163 /*
164 * Modify flags while holding freezer_lock. This ensures the
165 * freezer notices that we aren't frozen yet or the freezing
166 * condition is visible to try_to_freeze() below.
167 */
168 spin_lock_irq(&freezer_lock);
169 current->flags &= ~PF_NOFREEZE;
170 spin_unlock_irq(&freezer_lock);
171
172 return try_to_freeze();
173}
174EXPORT_SYMBOL(set_freezable);