freezer.c 5.6 KB

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