freezer.c 4.9 KB

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