freezer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #undef CREATE_TRACE_POINT
  14. #include <trace/hooks/cgroup.h>
  15. /* total number of freezing conditions in effect */
  16. DEFINE_STATIC_KEY_FALSE(freezer_active);
  17. EXPORT_SYMBOL(freezer_active);
  18. /*
  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 freezer_active 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. bool frozen(struct task_struct *p)
  49. {
  50. return READ_ONCE(p->__state) & TASK_FROZEN;
  51. }
  52. /* Refrigerator is place where frozen processes are stored :-). */
  53. bool __refrigerator(bool check_kthr_stop)
  54. {
  55. unsigned int state = get_current_state();
  56. bool was_frozen = false;
  57. pr_debug("%s entered refrigerator\n", current->comm);
  58. WARN_ON_ONCE(state && !(state & TASK_NORMAL));
  59. for (;;) {
  60. bool freeze;
  61. raw_spin_lock_irq(&current->pi_lock);
  62. set_current_state(TASK_FROZEN);
  63. /* unstale saved_state so that __thaw_task() will wake us up */
  64. current->saved_state = TASK_RUNNING;
  65. raw_spin_unlock_irq(&current->pi_lock);
  66. spin_lock_irq(&freezer_lock);
  67. freeze = freezing(current) && !(check_kthr_stop && kthread_should_stop());
  68. trace_android_rvh_refrigerator(pm_nosig_freezing);
  69. spin_unlock_irq(&freezer_lock);
  70. if (!freeze)
  71. break;
  72. was_frozen = true;
  73. schedule();
  74. }
  75. __set_current_state(TASK_RUNNING);
  76. pr_debug("%s left refrigerator\n", current->comm);
  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. static int __set_task_frozen(struct task_struct *p, void *arg)
  89. {
  90. unsigned int state = READ_ONCE(p->__state);
  91. if (p->on_rq)
  92. return 0;
  93. if (p != current && task_curr(p))
  94. return 0;
  95. if (!(state & (TASK_FREEZABLE | __TASK_STOPPED | __TASK_TRACED)))
  96. return 0;
  97. /*
  98. * Only TASK_NORMAL can be augmented with TASK_FREEZABLE, since they
  99. * can suffer spurious wakeups.
  100. */
  101. if (state & TASK_FREEZABLE)
  102. WARN_ON_ONCE(!(state & TASK_NORMAL));
  103. #ifdef CONFIG_LOCKDEP
  104. /*
  105. * It's dangerous to freeze with locks held; there be dragons there.
  106. */
  107. if (!(state & __TASK_FREEZABLE_UNSAFE))
  108. WARN_ON_ONCE(debug_locks && p->lockdep_depth);
  109. #endif
  110. p->saved_state = p->__state;
  111. WRITE_ONCE(p->__state, TASK_FROZEN);
  112. return TASK_FROZEN;
  113. }
  114. static bool __freeze_task(struct task_struct *p)
  115. {
  116. /* TASK_FREEZABLE|TASK_STOPPED|TASK_TRACED -> TASK_FROZEN */
  117. return task_call_func(p, __set_task_frozen, NULL);
  118. }
  119. /**
  120. * freeze_task - send a freeze request to given task
  121. * @p: task to send the request to
  122. *
  123. * If @p is freezing, the freeze request is sent either by sending a fake
  124. * signal (if it's not a kernel thread) or waking it up (if it's a kernel
  125. * thread).
  126. *
  127. * RETURNS:
  128. * %false, if @p is not freezing or already frozen; %true, otherwise
  129. */
  130. bool freeze_task(struct task_struct *p)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&freezer_lock, flags);
  134. if (!freezing(p) || frozen(p) || __freeze_task(p)) {
  135. spin_unlock_irqrestore(&freezer_lock, flags);
  136. return false;
  137. }
  138. if (!(p->flags & PF_KTHREAD))
  139. fake_signal_wake_up(p);
  140. else
  141. wake_up_state(p, TASK_NORMAL);
  142. spin_unlock_irqrestore(&freezer_lock, flags);
  143. return true;
  144. }
  145. /*
  146. * Restore the saved_state before the task entered freezer. For typical task
  147. * in the __refrigerator(), saved_state == TASK_RUNNING so nothing happens
  148. * here. For tasks which were TASK_NORMAL | TASK_FREEZABLE, their initial state
  149. * is restored unless they got an expected wakeup (see ttwu_state_match()).
  150. * Returns 1 if the task state was restored.
  151. */
  152. static int __restore_freezer_state(struct task_struct *p, void *arg)
  153. {
  154. unsigned int state = p->saved_state;
  155. if (state != TASK_RUNNING) {
  156. WRITE_ONCE(p->__state, state);
  157. p->saved_state = TASK_RUNNING;
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. void __thaw_task(struct task_struct *p)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&freezer_lock, flags);
  166. if (WARN_ON_ONCE(freezing(p)))
  167. goto unlock;
  168. if (!frozen(p) || task_call_func(p, __restore_freezer_state, NULL))
  169. goto unlock;
  170. wake_up_state(p, TASK_FROZEN);
  171. unlock:
  172. spin_unlock_irqrestore(&freezer_lock, flags);
  173. }
  174. /**
  175. * set_freezable - make %current freezable
  176. *
  177. * Mark %current freezable and enter refrigerator if necessary.
  178. */
  179. bool set_freezable(void)
  180. {
  181. might_sleep();
  182. /*
  183. * Modify flags while holding freezer_lock. This ensures the
  184. * freezer notices that we aren't frozen yet or the freezing
  185. * condition is visible to try_to_freeze() below.
  186. */
  187. spin_lock_irq(&freezer_lock);
  188. current->flags &= ~PF_NOFREEZE;
  189. spin_unlock_irq(&freezer_lock);
  190. return try_to_freeze();
  191. }
  192. EXPORT_SYMBOL(set_freezable);