freezer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. 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. if (!signal_pending(p))
  94. fake_signal_wake_up(p);
  95. } else if (sig_only) {
  96. return false;
  97. } else {
  98. wake_up_state(p, TASK_INTERRUPTIBLE);
  99. }
  100. return true;
  101. }
  102. void cancel_freezing(struct task_struct *p)
  103. {
  104. unsigned long flags;
  105. if (freezing(p)) {
  106. pr_debug(" clean up: %s\n", p->comm);
  107. clear_freeze_flag(p);
  108. spin_lock_irqsave(&p->sighand->siglock, flags);
  109. recalc_sigpending_and_wake(p);
  110. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  111. }
  112. }
  113. static int __thaw_process(struct task_struct *p)
  114. {
  115. if (frozen(p)) {
  116. p->flags &= ~PF_FROZEN;
  117. return 1;
  118. }
  119. clear_freeze_flag(p);
  120. return 0;
  121. }
  122. /*
  123. * Wake up a frozen process
  124. *
  125. * task_lock() is needed to prevent the race with refrigerator() which may
  126. * occur if the freezing of tasks fails. Namely, without the lock, if the
  127. * freezing of tasks failed, thaw_tasks() might have run before a task in
  128. * refrigerator() could call frozen_process(), in which case the task would be
  129. * frozen and no one would thaw it.
  130. */
  131. int thaw_process(struct task_struct *p)
  132. {
  133. task_lock(p);
  134. if (__thaw_process(p) == 1) {
  135. task_unlock(p);
  136. wake_up_process(p);
  137. return 1;
  138. }
  139. task_unlock(p);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(thaw_process);