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/oom.h> 14 #include <linux/kthread.h> 15 16 /* total number of freezing conditions in effect */ 17 DEFINE_STATIC_KEY_FALSE(freezer_active); 18 EXPORT_SYMBOL(freezer_active); 19 20 /* 21 * indicate whether PM freezing is in effect, protected by 22 * system_transition_mutex 23 */ 24 bool pm_freezing; 25 bool pm_nosig_freezing; 26 27 /* protects freezing and frozen transitions */ 28 static DEFINE_SPINLOCK(freezer_lock); 29 30 /** 31 * freezing_slow_path - slow path for testing whether a task needs to be frozen 32 * @p: task to be tested 33 * 34 * This function is called by freezing() if freezer_active isn't zero 35 * and tests whether @p needs to enter and stay in frozen state. Can be 36 * called under any context. The freezers are responsible for ensuring the 37 * target tasks see the updated state. 38 */ 39 bool freezing_slow_path(struct task_struct *p) 40 { 41 if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK)) 42 return false; 43 44 if (tsk_is_oom_victim(p)) 45 return false; 46 47 if (pm_nosig_freezing || cgroup_freezing(p)) 48 return true; 49 50 if (pm_freezing && !(p->flags & PF_KTHREAD)) 51 return true; 52 53 return false; 54 } 55 EXPORT_SYMBOL(freezing_slow_path); 56 57 bool frozen(struct task_struct *p) 58 { 59 return READ_ONCE(p->__state) & TASK_FROZEN; 60 } 61 62 /* Refrigerator is place where frozen processes are stored :-). */ 63 bool __refrigerator(bool check_kthr_stop) 64 { 65 unsigned int state = get_current_state(); 66 bool was_frozen = false; 67 68 pr_debug("%s entered refrigerator\n", current->comm); 69 70 WARN_ON_ONCE(state && !(state & TASK_NORMAL)); 71 72 for (;;) { 73 bool freeze; 74 75 raw_spin_lock_irq(¤t->pi_lock); 76 WRITE_ONCE(current->__state, TASK_FROZEN); 77 /* unstale saved_state so that __thaw_task() will wake us up */ 78 current->saved_state = TASK_RUNNING; 79 raw_spin_unlock_irq(¤t->pi_lock); 80 81 spin_lock_irq(&freezer_lock); 82 freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop()); 83 spin_unlock_irq(&freezer_lock); 84 85 if (!freeze) 86 break; 87 88 was_frozen = true; 89 schedule(); 90 } 91 __set_current_state(TASK_RUNNING); 92 93 pr_debug("%s left refrigerator\n", current->comm); 94 95 return was_frozen; 96 } 97 EXPORT_SYMBOL(__refrigerator); 98 99 static void fake_signal_wake_up(struct task_struct *p) 100 { 101 unsigned long flags; 102 103 if (lock_task_sighand(p, &flags)) { 104 signal_wake_up(p, 0); 105 unlock_task_sighand(p, &flags); 106 } 107 } 108 109 static int __set_task_frozen(struct task_struct *p, void *arg) 110 { 111 unsigned int state = READ_ONCE(p->__state); 112 113 /* 114 * Allow freezing the sched_delayed tasks; they will not execute until 115 * ttwu() fixes them up, so it is safe to swap their state now, instead 116 * of waiting for them to get fully dequeued. 117 */ 118 if (task_is_runnable(p)) 119 return 0; 120 121 if (p != current && task_curr(p)) 122 return 0; 123 124 if (!(state & (TASK_FREEZABLE | __TASK_STOPPED | __TASK_TRACED))) 125 return 0; 126 127 /* 128 * Only TASK_NORMAL can be augmented with TASK_FREEZABLE, since they 129 * can suffer spurious wakeups. 130 */ 131 if (state & TASK_FREEZABLE) 132 WARN_ON_ONCE(!(state & TASK_NORMAL)); 133 134 #ifdef CONFIG_LOCKDEP 135 /* 136 * It's dangerous to freeze with locks held; there be dragons there. 137 */ 138 if (!(state & __TASK_FREEZABLE_UNSAFE)) 139 WARN_ON_ONCE(debug_locks && p->lockdep_depth); 140 #endif 141 142 p->saved_state = p->__state; 143 WRITE_ONCE(p->__state, TASK_FROZEN); 144 return TASK_FROZEN; 145 } 146 147 static bool __freeze_task(struct task_struct *p) 148 { 149 /* TASK_FREEZABLE|TASK_STOPPED|TASK_TRACED -> TASK_FROZEN */ 150 return task_call_func(p, __set_task_frozen, NULL); 151 } 152 153 /** 154 * freeze_task - send a freeze request to given task 155 * @p: task to send the request to 156 * 157 * If @p is freezing, the freeze request is sent either by sending a fake 158 * signal (if it's not a kernel thread) or waking it up (if it's a kernel 159 * thread). 160 * 161 * RETURNS: 162 * %false, if @p is not freezing or already frozen; %true, otherwise 163 */ 164 bool freeze_task(struct task_struct *p) 165 { 166 unsigned long flags; 167 168 spin_lock_irqsave(&freezer_lock, flags); 169 if (!freezing(p) || frozen(p) || __freeze_task(p)) { 170 spin_unlock_irqrestore(&freezer_lock, flags); 171 return false; 172 } 173 174 if (!(p->flags & PF_KTHREAD)) 175 fake_signal_wake_up(p); 176 else 177 wake_up_state(p, TASK_NORMAL); 178 179 spin_unlock_irqrestore(&freezer_lock, flags); 180 return true; 181 } 182 183 /* 184 * Restore the saved_state before the task entered freezer. For typical task 185 * in the __refrigerator(), saved_state == TASK_RUNNING so nothing happens 186 * here. For tasks which were TASK_NORMAL | TASK_FREEZABLE, their initial state 187 * is restored unless they got an expected wakeup (see ttwu_state_match()). 188 * Returns 1 if the task state was restored. 189 */ 190 static int __restore_freezer_state(struct task_struct *p, void *arg) 191 { 192 unsigned int state = p->saved_state; 193 194 if (state != TASK_RUNNING) { 195 WRITE_ONCE(p->__state, state); 196 p->saved_state = TASK_RUNNING; 197 return 1; 198 } 199 200 return 0; 201 } 202 203 void __thaw_task(struct task_struct *p) 204 { 205 guard(spinlock_irqsave)(&freezer_lock); 206 if (frozen(p) && !task_call_func(p, __restore_freezer_state, NULL)) 207 wake_up_state(p, TASK_FROZEN); 208 } 209 210 /* 211 * thaw_process - Thaw a frozen process 212 * @p: the process to be thawed 213 * 214 * Iterate over all threads of @p and call __thaw_task() on each. 215 */ 216 void thaw_process(struct task_struct *p) 217 { 218 struct task_struct *t; 219 220 rcu_read_lock(); 221 for_each_thread(p, t) { 222 __thaw_task(t); 223 } 224 rcu_read_unlock(); 225 } 226 227 /** 228 * set_freezable - make %current freezable 229 * 230 * Mark %current freezable and enter refrigerator if necessary. 231 */ 232 bool set_freezable(void) 233 { 234 might_sleep(); 235 236 /* 237 * Modify flags while holding freezer_lock. This ensures the 238 * freezer notices that we aren't frozen yet or the freezing 239 * condition is visible to try_to_freeze() below. 240 */ 241 spin_lock_irq(&freezer_lock); 242 current->flags &= ~PF_NOFREEZE; 243 spin_unlock_irq(&freezer_lock); 244 245 return try_to_freeze(); 246 } 247 EXPORT_SYMBOL(set_freezable); 248