freezer.c 4.2 KB

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