Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * kernel/freezer.c - Function to freeze a process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Originally from kernel/power/process.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #undef CREATE_TRACE_POINT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <trace/hooks/cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* total number of freezing conditions in effect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) atomic_t system_freezing_cnt = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) EXPORT_SYMBOL(system_freezing_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* indicate whether PM freezing is in effect, protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * system_transition_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) bool pm_freezing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) bool pm_nosig_freezing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* protects freezing and frozen transitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_SPINLOCK(freezer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * freezing_slow_path - slow path for testing whether a task needs to be frozen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @p: task to be tested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * This function is called by freezing() if system_freezing_cnt isn't zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * and tests whether @p needs to enter and stay in frozen state.  Can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * called under any context.  The freezers are responsible for ensuring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * target tasks see the updated state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) bool freezing_slow_path(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (test_tsk_thread_flag(p, TIF_MEMDIE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (pm_nosig_freezing || cgroup_freezing(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (pm_freezing && !(p->flags & PF_KTHREAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) EXPORT_SYMBOL(freezing_slow_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) /* Refrigerator is place where frozen processes are stored :-). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) bool __refrigerator(bool check_kthr_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	/* Hmm, should we be allowed to suspend when there are realtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	   processes around? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	bool was_frozen = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	long save = current->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	pr_debug("%s entered refrigerator\n", current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		spin_lock_irq(&freezer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		current->flags |= PF_FROZEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (!freezing(current) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		    (check_kthr_stop && kthread_should_stop()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			current->flags &= ~PF_FROZEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		trace_android_rvh_refrigerator(pm_nosig_freezing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		spin_unlock_irq(&freezer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (!(current->flags & PF_FROZEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		was_frozen = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	pr_debug("%s left refrigerator\n", current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * Restore saved task state before returning.  The mb'd version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * needs to be used; otherwise, it might silently break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * synchronization which depends on ordered task state change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	set_current_state(save);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return was_frozen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) EXPORT_SYMBOL(__refrigerator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static void fake_signal_wake_up(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (lock_task_sighand(p, &flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		signal_wake_up(p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		unlock_task_sighand(p, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * freeze_task - send a freeze request to given task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * @p: task to send the request to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * If @p is freezing, the freeze request is sent either by sending a fake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * signal (if it's not a kernel thread) or waking it up (if it's a kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * thread).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * %false, if @p is not freezing or already frozen; %true, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) bool freeze_task(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	 * This check can race with freezer_do_not_count, but worst case that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * will result in an extra wakeup being sent to the task.  It does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * race with freezer_count(), the barriers in freezer_count() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 * freezer_should_skip() ensure that either freezer_count() sees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 * freezing == true in try_to_freeze() and freezes, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	 * freezer_should_skip() sees !PF_FREEZE_SKIP and freezes the task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * normally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (freezer_should_skip(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	spin_lock_irqsave(&freezer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (!freezing(p) || frozen(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		spin_unlock_irqrestore(&freezer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!(p->flags & PF_KTHREAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		fake_signal_wake_up(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		wake_up_state(p, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	spin_unlock_irqrestore(&freezer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void __thaw_task(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	const struct cpumask *mask = task_cpu_possible_mask(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	spin_lock_irqsave(&freezer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * Wake up frozen tasks. On asymmetric systems where tasks cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * run on all CPUs, ttwu() may have deferred a wakeup generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	 * before thaw_secondary_cpus() had completed so we generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	 * additional wakeups here for tasks in the PF_FREEZER_SKIP state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (frozen(p) || (frozen_or_skipped(p) && mask != cpu_possible_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		wake_up_process(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	spin_unlock_irqrestore(&freezer_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * set_freezable - make %current freezable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * Mark %current freezable and enter refrigerator if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) bool set_freezable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * Modify flags while holding freezer_lock.  This ensures the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * freezer notices that we aren't frozen yet or the freezing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * condition is visible to try_to_freeze() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	spin_lock_irq(&freezer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	current->flags &= ~PF_NOFREEZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	spin_unlock_irq(&freezer_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return try_to_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) EXPORT_SYMBOL(set_freezable);