freezer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * kernel/freezer.c - Function to freeze a process
  3. *
  4. * Originally from kernel/power/process.c
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/suspend.h>
  8. #include <linux/export.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/freezer.h>
  11. #include <linux/kthread.h>
  12. /* total number of freezing conditions in effect */
  13. atomic_t system_freezing_cnt = ATOMIC_INIT(0);
  14. EXPORT_SYMBOL(system_freezing_cnt);
  15. /* indicate whether PM freezing is in effect, protected by pm_mutex */
  16. bool pm_freezing;
  17. bool pm_nosig_freezing;
  18. /* protects freezing and frozen transitions */
  19. static DEFINE_SPINLOCK(freezer_lock);
  20. /**
  21. * freezing_slow_path - slow path for testing whether a task needs to be frozen
  22. * @p: task to be tested
  23. *
  24. * This function is called by freezing() if system_freezing_cnt isn't zero
  25. * and tests whether @p needs to enter and stay in frozen state. Can be
  26. * called under any context. The freezers are responsible for ensuring the
  27. * target tasks see the updated state.
  28. */
  29. bool freezing_slow_path(struct task_struct *p)
  30. {
  31. if (p->flags & PF_NOFREEZE)
  32. return false;
  33. if (test_thread_flag(TIF_MEMDIE))
  34. return false;
  35. if (pm_nosig_freezing || cgroup_freezing(p))
  36. return true;
  37. if (pm_freezing && !(p->flags & PF_KTHREAD))
  38. return true;
  39. return false;
  40. }
  41. EXPORT_SYMBOL(freezing_slow_path);
  42. /* Refrigerator is place where frozen processes are stored :-). */
  43. bool __refrigerator(bool check_kthr_stop)
  44. {
  45. /* Hmm, should we be allowed to suspend when there are realtime
  46. processes around? */
  47. bool was_frozen = false;
  48. long save = current->state;
  49. pr_debug("%s entered refrigerator\n", current->comm);
  50. for (;;) {
  51. set_current_state(TASK_UNINTERRUPTIBLE);
  52. spin_lock_irq(&freezer_lock);
  53. current->flags |= PF_FROZEN;
  54. if (!freezing(current) ||
  55. (check_kthr_stop && kthread_should_stop()))
  56. current->flags &= ~PF_FROZEN;
  57. spin_unlock_irq(&freezer_lock);
  58. if (!(current->flags & PF_FROZEN))
  59. break;
  60. was_frozen = true;
  61. schedule();
  62. }
  63. pr_debug("%s left refrigerator\n", current->comm);
  64. /*
  65. * Restore saved task state before returning. The mb'd version
  66. * needs to be used; otherwise, it might silently break
  67. * synchronization which depends on ordered task state change.
  68. */
  69. set_current_state(save);
  70. return was_frozen;
  71. }
  72. EXPORT_SYMBOL(__refrigerator);
  73. static void fake_signal_wake_up(struct task_struct *p)
  74. {
  75. unsigned long flags;
  76. if (lock_task_sighand(p, &flags)) {
  77. signal_wake_up(p, 0);
  78. unlock_task_sighand(p, &flags);
  79. }
  80. }
  81. /**
  82. * freeze_task - send a freeze request to given task
  83. * @p: task to send the request to
  84. *
  85. * If @p is freezing, the freeze request is sent either by sending a fake
  86. * signal (if it's not a kernel thread) or waking it up (if it's a kernel
  87. * thread).
  88. *
  89. * RETURNS:
  90. * %false, if @p is not freezing or already frozen; %true, otherwise
  91. */
  92. bool freeze_task(struct task_struct *p)
  93. {
  94. unsigned long flags;
  95. /*
  96. * This check can race with freezer_do_not_count, but worst case that
  97. * will result in an extra wakeup being sent to the task. It does not
  98. * race with freezer_count(), the barriers in freezer_count() and
  99. * freezer_should_skip() ensure that either freezer_count() sees
  100. * freezing == true in try_to_freeze() and freezes, or
  101. * freezer_should_skip() sees !PF_FREEZE_SKIP and freezes the task
  102. * normally.
  103. */
  104. if (freezer_should_skip(p))
  105. return false;
  106. spin_lock_irqsave(&freezer_lock, flags);
  107. if (!freezing(p) || frozen(p)) {
  108. spin_unlock_irqrestore(&freezer_lock, flags);
  109. return false;
  110. }
  111. if (!(p->flags & PF_KTHREAD)) {
  112. fake_signal_wake_up(p);
  113. /*
  114. * fake_signal_wake_up() goes through p's scheduler
  115. * lock and guarantees that TASK_STOPPED/TRACED ->
  116. * TASK_RUNNING transition can't race with task state
  117. * testing in try_to_freeze_tasks().
  118. */
  119. } else {
  120. wake_up_state(p, TASK_INTERRUPTIBLE);
  121. }
  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. /*
  129. * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
  130. * be visible to @p as waking up implies wmb. Waking up inside
  131. * freezer_lock also prevents wakeups from leaking outside
  132. * refrigerator.
  133. */
  134. spin_lock_irqsave(&freezer_lock, flags);
  135. if (frozen(p))
  136. wake_up_process(p);
  137. spin_unlock_irqrestore(&freezer_lock, flags);
  138. }
  139. /**
  140. * set_freezable - make %current freezable
  141. *
  142. * Mark %current freezable and enter refrigerator if necessary.
  143. */
  144. bool set_freezable(void)
  145. {
  146. might_sleep();
  147. /*
  148. * Modify flags while holding freezer_lock. This ensures the
  149. * freezer notices that we aren't frozen yet or the freezing
  150. * condition is visible to try_to_freeze() below.
  151. */
  152. spin_lock_irq(&freezer_lock);
  153. current->flags &= ~PF_NOFREEZE;
  154. spin_unlock_irq(&freezer_lock);
  155. return try_to_freeze();
  156. }
  157. EXPORT_SYMBOL(set_freezable);