freezer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  47. /*
  48. * No point in checking freezing() again - the caller already did.
  49. * Proceed to enter FROZEN.
  50. */
  51. spin_lock_irq(&freezer_lock);
  52. repeat:
  53. current->flags |= PF_FROZEN;
  54. spin_unlock_irq(&freezer_lock);
  55. save = current->state;
  56. pr_debug("%s entered refrigerator\n", current->comm);
  57. spin_lock_irq(&current->sighand->siglock);
  58. recalc_sigpending(); /* We sent fake signal, clean it up */
  59. spin_unlock_irq(&current->sighand->siglock);
  60. for (;;) {
  61. set_current_state(TASK_UNINTERRUPTIBLE);
  62. if (!freezing(current) ||
  63. (check_kthr_stop && kthread_should_stop()))
  64. break;
  65. was_frozen = true;
  66. schedule();
  67. }
  68. /* leave FROZEN */
  69. spin_lock_irq(&freezer_lock);
  70. if (freezing(current))
  71. goto repeat;
  72. current->flags &= ~PF_FROZEN;
  73. spin_unlock_irq(&freezer_lock);
  74. pr_debug("%s left refrigerator\n", current->comm);
  75. /*
  76. * Restore saved task state before returning. The mb'd version
  77. * needs to be used; otherwise, it might silently break
  78. * synchronization which depends on ordered task state change.
  79. */
  80. set_current_state(save);
  81. return was_frozen;
  82. }
  83. EXPORT_SYMBOL(__refrigerator);
  84. static void fake_signal_wake_up(struct task_struct *p)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&p->sighand->siglock, flags);
  88. signal_wake_up(p, 0);
  89. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  90. }
  91. /**
  92. * freeze_task - send a freeze request to given task
  93. * @p: task to send the request to
  94. * @sig_only: if set, the request will only be sent if the task has the
  95. * PF_FREEZER_NOSIG flag unset
  96. * Return value: 'false', if @sig_only is set and the task has
  97. * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
  98. *
  99. * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
  100. * either sending a fake signal to it or waking it up, depending on whether
  101. * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
  102. * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
  103. * TIF_FREEZE flag will not be set.
  104. */
  105. bool freeze_task(struct task_struct *p, bool sig_only)
  106. {
  107. unsigned long flags;
  108. spin_lock_irqsave(&freezer_lock, flags);
  109. if (!freezing(p) || frozen(p)) {
  110. spin_unlock_irqrestore(&freezer_lock, flags);
  111. return false;
  112. }
  113. if (should_send_signal(p)) {
  114. fake_signal_wake_up(p);
  115. /*
  116. * fake_signal_wake_up() goes through p's scheduler
  117. * lock and guarantees that TASK_STOPPED/TRACED ->
  118. * TASK_RUNNING transition can't race with task state
  119. * testing in try_to_freeze_tasks().
  120. */
  121. } else {
  122. wake_up_state(p, TASK_INTERRUPTIBLE);
  123. }
  124. spin_unlock_irqrestore(&freezer_lock, flags);
  125. return true;
  126. }
  127. void __thaw_task(struct task_struct *p)
  128. {
  129. unsigned long flags;
  130. /*
  131. * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
  132. * be visible to @p as waking up implies wmb. Waking up inside
  133. * freezer_lock also prevents wakeups from leaking outside
  134. * refrigerator.
  135. *
  136. * If !FROZEN, @p hasn't reached refrigerator, recalc sigpending to
  137. * avoid leaving dangling TIF_SIGPENDING behind.
  138. */
  139. spin_lock_irqsave(&freezer_lock, flags);
  140. if (frozen(p)) {
  141. wake_up_process(p);
  142. } else {
  143. spin_lock(&p->sighand->siglock);
  144. recalc_sigpending_and_wake(p);
  145. spin_unlock(&p->sighand->siglock);
  146. }
  147. spin_unlock_irqrestore(&freezer_lock, flags);
  148. }