freezer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/module.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. __set_current_state(save);
  53. }
  54. EXPORT_SYMBOL(refrigerator);
  55. static void fake_signal_wake_up(struct task_struct *p)
  56. {
  57. unsigned long flags;
  58. spin_lock_irqsave(&p->sighand->siglock, flags);
  59. signal_wake_up(p, 0);
  60. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  61. }
  62. /**
  63. * freeze_task - send a freeze request to given task
  64. * @p: task to send the request to
  65. * @sig_only: if set, the request will only be sent if the task has the
  66. * PF_FREEZER_NOSIG flag unset
  67. * Return value: 'false', if @sig_only is set and the task has
  68. * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
  69. *
  70. * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
  71. * either sending a fake signal to it or waking it up, depending on whether
  72. * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
  73. * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
  74. * TIF_FREEZE flag will not be set.
  75. */
  76. bool freeze_task(struct task_struct *p, bool sig_only)
  77. {
  78. /*
  79. * We first check if the task is freezing and next if it has already
  80. * been frozen to avoid the race with frozen_process() which first marks
  81. * the task as frozen and next clears its TIF_FREEZE.
  82. */
  83. if (!freezing(p)) {
  84. smp_rmb();
  85. if (frozen(p))
  86. return false;
  87. if (!sig_only || should_send_signal(p))
  88. set_freeze_flag(p);
  89. else
  90. return false;
  91. }
  92. if (should_send_signal(p)) {
  93. fake_signal_wake_up(p);
  94. /*
  95. * fake_signal_wake_up() goes through p's scheduler
  96. * lock and guarantees that TASK_STOPPED/TRACED ->
  97. * TASK_RUNNING transition can't race with task state
  98. * testing in try_to_freeze_tasks().
  99. */
  100. } else if (sig_only) {
  101. return false;
  102. } else {
  103. wake_up_state(p, TASK_INTERRUPTIBLE);
  104. }
  105. return true;
  106. }
  107. void cancel_freezing(struct task_struct *p)
  108. {
  109. unsigned long flags;
  110. if (freezing(p)) {
  111. pr_debug(" clean up: %s\n", p->comm);
  112. clear_freeze_flag(p);
  113. spin_lock_irqsave(&p->sighand->siglock, flags);
  114. recalc_sigpending_and_wake(p);
  115. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  116. }
  117. }
  118. static int __thaw_process(struct task_struct *p)
  119. {
  120. if (frozen(p)) {
  121. p->flags &= ~PF_FROZEN;
  122. return 1;
  123. }
  124. clear_freeze_flag(p);
  125. return 0;
  126. }
  127. /*
  128. * Wake up a frozen process
  129. *
  130. * task_lock() is needed to prevent the race with refrigerator() which may
  131. * occur if the freezing of tasks fails. Namely, without the lock, if the
  132. * freezing of tasks failed, thaw_tasks() might have run before a task in
  133. * refrigerator() could call frozen_process(), in which case the task would be
  134. * frozen and no one would thaw it.
  135. */
  136. int thaw_process(struct task_struct *p)
  137. {
  138. task_lock(p);
  139. if (__thaw_process(p) == 1) {
  140. task_unlock(p);
  141. wake_up_process(p);
  142. return 1;
  143. }
  144. task_unlock(p);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(thaw_process);