freezer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /*
  13. * freezing is complete, mark current process as frozen
  14. */
  15. static inline void frozen_process(void)
  16. {
  17. if (!unlikely(current->flags & PF_NOFREEZE)) {
  18. current->flags |= PF_FROZEN;
  19. smp_wmb();
  20. }
  21. clear_freeze_flag(current);
  22. }
  23. /* Refrigerator is place where frozen processes are stored :-). */
  24. bool __refrigerator(bool check_kthr_stop)
  25. {
  26. /* Hmm, should we be allowed to suspend when there are realtime
  27. processes around? */
  28. bool was_frozen = false;
  29. long save;
  30. task_lock(current);
  31. if (freezing(current)) {
  32. frozen_process();
  33. task_unlock(current);
  34. } else {
  35. task_unlock(current);
  36. return was_frozen;
  37. }
  38. save = current->state;
  39. pr_debug("%s entered refrigerator\n", current->comm);
  40. spin_lock_irq(&current->sighand->siglock);
  41. recalc_sigpending(); /* We sent fake signal, clean it up */
  42. spin_unlock_irq(&current->sighand->siglock);
  43. /* prevent accounting of that task to load */
  44. current->flags |= PF_FREEZING;
  45. for (;;) {
  46. set_current_state(TASK_UNINTERRUPTIBLE);
  47. if (!frozen(current) ||
  48. (check_kthr_stop && kthread_should_stop()))
  49. break;
  50. was_frozen = true;
  51. schedule();
  52. }
  53. /* Remove the accounting blocker */
  54. current->flags &= ~PF_FREEZING;
  55. pr_debug("%s left refrigerator\n", current->comm);
  56. /*
  57. * Restore saved task state before returning. The mb'd version
  58. * needs to be used; otherwise, it might silently break
  59. * synchronization which depends on ordered task state change.
  60. */
  61. set_current_state(save);
  62. return was_frozen;
  63. }
  64. EXPORT_SYMBOL(__refrigerator);
  65. static void fake_signal_wake_up(struct task_struct *p)
  66. {
  67. unsigned long flags;
  68. spin_lock_irqsave(&p->sighand->siglock, flags);
  69. signal_wake_up(p, 0);
  70. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  71. }
  72. /**
  73. * freeze_task - send a freeze request to given task
  74. * @p: task to send the request to
  75. * @sig_only: if set, the request will only be sent if the task has the
  76. * PF_FREEZER_NOSIG flag unset
  77. * Return value: 'false', if @sig_only is set and the task has
  78. * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
  79. *
  80. * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
  81. * either sending a fake signal to it or waking it up, depending on whether
  82. * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
  83. * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
  84. * TIF_FREEZE flag will not be set.
  85. */
  86. bool freeze_task(struct task_struct *p, bool sig_only)
  87. {
  88. /*
  89. * We first check if the task is freezing and next if it has already
  90. * been frozen to avoid the race with frozen_process() which first marks
  91. * the task as frozen and next clears its TIF_FREEZE.
  92. */
  93. if (!freezing(p)) {
  94. smp_rmb();
  95. if (frozen(p))
  96. return false;
  97. if (!sig_only || should_send_signal(p))
  98. set_freeze_flag(p);
  99. else
  100. return false;
  101. }
  102. if (should_send_signal(p)) {
  103. fake_signal_wake_up(p);
  104. /*
  105. * fake_signal_wake_up() goes through p's scheduler
  106. * lock and guarantees that TASK_STOPPED/TRACED ->
  107. * TASK_RUNNING transition can't race with task state
  108. * testing in try_to_freeze_tasks().
  109. */
  110. } else if (sig_only) {
  111. return false;
  112. } else {
  113. wake_up_state(p, TASK_INTERRUPTIBLE);
  114. }
  115. return true;
  116. }
  117. void cancel_freezing(struct task_struct *p)
  118. {
  119. unsigned long flags;
  120. if (freezing(p)) {
  121. pr_debug(" clean up: %s\n", p->comm);
  122. clear_freeze_flag(p);
  123. spin_lock_irqsave(&p->sighand->siglock, flags);
  124. recalc_sigpending_and_wake(p);
  125. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  126. }
  127. }
  128. static int __thaw_process(struct task_struct *p)
  129. {
  130. if (frozen(p)) {
  131. p->flags &= ~PF_FROZEN;
  132. return 1;
  133. }
  134. clear_freeze_flag(p);
  135. return 0;
  136. }
  137. /*
  138. * Wake up a frozen process
  139. *
  140. * task_lock() is needed to prevent the race with refrigerator() which may
  141. * occur if the freezing of tasks fails. Namely, without the lock, if the
  142. * freezing of tasks failed, thaw_tasks() might have run before a task in
  143. * refrigerator() could call frozen_process(), in which case the task would be
  144. * frozen and no one would thaw it.
  145. */
  146. int thaw_process(struct task_struct *p)
  147. {
  148. task_lock(p);
  149. if (__thaw_process(p) == 1) {
  150. task_unlock(p);
  151. wake_up_process(p);
  152. return 1;
  153. }
  154. task_unlock(p);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(thaw_process);