freezer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 (pm_nosig_freezing || cgroup_freezing(p))
  34. return true;
  35. if (pm_freezing && !(p->flags & PF_FREEZER_NOSIG))
  36. return true;
  37. return false;
  38. }
  39. EXPORT_SYMBOL(freezing_slow_path);
  40. /* Refrigerator is place where frozen processes are stored :-). */
  41. bool __refrigerator(bool check_kthr_stop)
  42. {
  43. /* Hmm, should we be allowed to suspend when there are realtime
  44. processes around? */
  45. bool was_frozen = false;
  46. long save = current->state;
  47. pr_debug("%s entered refrigerator\n", current->comm);
  48. for (;;) {
  49. set_current_state(TASK_UNINTERRUPTIBLE);
  50. spin_lock_irq(&freezer_lock);
  51. current->flags |= PF_FROZEN;
  52. if (!freezing(current) ||
  53. (check_kthr_stop && kthread_should_stop()))
  54. current->flags &= ~PF_FROZEN;
  55. spin_unlock_irq(&freezer_lock);
  56. if (!(current->flags & PF_FROZEN))
  57. break;
  58. was_frozen = true;
  59. schedule();
  60. }
  61. spin_lock_irq(&current->sighand->siglock);
  62. recalc_sigpending(); /* We sent fake signal, clean it up */
  63. spin_unlock_irq(&current->sighand->siglock);
  64. pr_debug("%s left refrigerator\n", current->comm);
  65. /*
  66. * Restore saved task state before returning. The mb'd version
  67. * needs to be used; otherwise, it might silently break
  68. * synchronization which depends on ordered task state change.
  69. */
  70. set_current_state(save);
  71. return was_frozen;
  72. }
  73. EXPORT_SYMBOL(__refrigerator);
  74. static void fake_signal_wake_up(struct task_struct *p)
  75. {
  76. unsigned long flags;
  77. if (lock_task_sighand(p, &flags)) {
  78. signal_wake_up(p, 0);
  79. unlock_task_sighand(p, &flags);
  80. }
  81. }
  82. /**
  83. * freeze_task - send a freeze request to given task
  84. * @p: task to send the request to
  85. * @sig_only: if set, the request will only be sent if the task has the
  86. * PF_FREEZER_NOSIG flag unset
  87. * Return value: 'false', if @sig_only is set and the task has
  88. * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
  89. *
  90. * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
  91. * either sending a fake signal to it or waking it up, depending on whether
  92. * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
  93. * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
  94. * TIF_FREEZE flag will not be set.
  95. */
  96. bool freeze_task(struct task_struct *p, bool sig_only)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&freezer_lock, flags);
  100. if (!freezing(p) || frozen(p)) {
  101. spin_unlock_irqrestore(&freezer_lock, flags);
  102. return false;
  103. }
  104. if (!(p->flags & PF_FREEZER_NOSIG)) {
  105. fake_signal_wake_up(p);
  106. /*
  107. * fake_signal_wake_up() goes through p's scheduler
  108. * lock and guarantees that TASK_STOPPED/TRACED ->
  109. * TASK_RUNNING transition can't race with task state
  110. * testing in try_to_freeze_tasks().
  111. */
  112. } else {
  113. wake_up_state(p, TASK_INTERRUPTIBLE);
  114. }
  115. spin_unlock_irqrestore(&freezer_lock, flags);
  116. return true;
  117. }
  118. void __thaw_task(struct task_struct *p)
  119. {
  120. unsigned long flags;
  121. /*
  122. * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
  123. * be visible to @p as waking up implies wmb. Waking up inside
  124. * freezer_lock also prevents wakeups from leaking outside
  125. * refrigerator.
  126. *
  127. * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to
  128. * avoid leaving dangling TIF_SIGPENDING behind.
  129. */
  130. spin_lock_irqsave(&freezer_lock, flags);
  131. if (frozen(p)) {
  132. wake_up_process(p);
  133. } else {
  134. spin_lock(&p->sighand->siglock);
  135. recalc_sigpending_and_wake(p);
  136. spin_unlock(&p->sighand->siglock);
  137. }
  138. spin_unlock_irqrestore(&freezer_lock, flags);
  139. }
  140. /**
  141. * __set_freezable - make %current freezable
  142. * @with_signal: do we want %TIF_SIGPENDING for notification too?
  143. *
  144. * Mark %current freezable and enter refrigerator if necessary.
  145. */
  146. bool __set_freezable(bool with_signal)
  147. {
  148. might_sleep();
  149. /*
  150. * Modify flags while holding freezer_lock. This ensures the
  151. * freezer notices that we aren't frozen yet or the freezing
  152. * condition is visible to try_to_freeze() below.
  153. */
  154. spin_lock_irq(&freezer_lock);
  155. current->flags &= ~PF_NOFREEZE;
  156. if (with_signal)
  157. current->flags &= ~PF_FREEZER_NOSIG;
  158. spin_unlock_irq(&freezer_lock);
  159. return try_to_freeze();
  160. }
  161. EXPORT_SYMBOL(__set_freezable);