freezer.c 4.3 KB

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