freezer.c 3.7 KB

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