freezer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include <linux/kthread.h>
  12. /* total number of freezing conditions in effect */
  13. atomic_t system_freezing_cnt = ATOMIC_INIT(0);
  14. EXPORT_SYMBOL(system_freezing_cnt);
  15. /* indicate whether PM freezing is in effect, protected by pm_mutex */
  16. bool pm_freezing;
  17. bool pm_nosig_freezing;
  18. /*
  19. * Temporary export for the deadlock workaround in ata_scsi_hotplug().
  20. * Remove once the hack becomes unnecessary.
  21. */
  22. EXPORT_SYMBOL_GPL(pm_freezing);
  23. /* protects freezing and frozen transitions */
  24. static DEFINE_SPINLOCK(freezer_lock);
  25. /**
  26. * freezing_slow_path - slow path for testing whether a task needs to be frozen
  27. * @p: task to be tested
  28. *
  29. * This function is called by freezing() if system_freezing_cnt isn't zero
  30. * and tests whether @p needs to enter and stay in frozen state. Can be
  31. * called under any context. The freezers are responsible for ensuring the
  32. * target tasks see the updated state.
  33. */
  34. bool freezing_slow_path(struct task_struct *p)
  35. {
  36. if (p->flags & (PF_NOFREEZE | PF_SUSPEND_TASK))
  37. return false;
  38. if (pm_nosig_freezing || cgroup_freezing(p))
  39. return true;
  40. if (pm_freezing && !(p->flags & PF_KTHREAD))
  41. return true;
  42. return false;
  43. }
  44. EXPORT_SYMBOL(freezing_slow_path);
  45. /* Refrigerator is place where frozen processes are stored :-). */
  46. bool __refrigerator(bool check_kthr_stop)
  47. {
  48. /* Hmm, should we be allowed to suspend when there are realtime
  49. processes around? */
  50. bool was_frozen = false;
  51. long save = current->state;
  52. pr_debug("%s entered refrigerator\n", current->comm);
  53. for (;;) {
  54. set_current_state(TASK_UNINTERRUPTIBLE);
  55. spin_lock_irq(&freezer_lock);
  56. current->flags |= PF_FROZEN;
  57. if (!freezing(current) ||
  58. (check_kthr_stop && kthread_should_stop()))
  59. current->flags &= ~PF_FROZEN;
  60. spin_unlock_irq(&freezer_lock);
  61. if (!(current->flags & PF_FROZEN))
  62. break;
  63. was_frozen = true;
  64. schedule();
  65. }
  66. pr_debug("%s left refrigerator\n", current->comm);
  67. /*
  68. * Restore saved task state before returning. The mb'd version
  69. * needs to be used; otherwise, it might silently break
  70. * synchronization which depends on ordered task state change.
  71. */
  72. set_current_state(save);
  73. return was_frozen;
  74. }
  75. EXPORT_SYMBOL(__refrigerator);
  76. static void fake_signal_wake_up(struct task_struct *p)
  77. {
  78. unsigned long flags;
  79. if (lock_task_sighand(p, &flags)) {
  80. signal_wake_up(p, 0);
  81. unlock_task_sighand(p, &flags);
  82. }
  83. }
  84. /**
  85. * freeze_task - send a freeze request to given task
  86. * @p: task to send the request to
  87. *
  88. * If @p is freezing, the freeze request is sent either by sending a fake
  89. * signal (if it's not a kernel thread) or waking it up (if it's a kernel
  90. * thread).
  91. *
  92. * RETURNS:
  93. * %false, if @p is not freezing or already frozen; %true, otherwise
  94. */
  95. bool freeze_task(struct task_struct *p)
  96. {
  97. unsigned long flags;
  98. /*
  99. * This check can race with freezer_do_not_count, but worst case that
  100. * will result in an extra wakeup being sent to the task. It does not
  101. * race with freezer_count(), the barriers in freezer_count() and
  102. * freezer_should_skip() ensure that either freezer_count() sees
  103. * freezing == true in try_to_freeze() and freezes, or
  104. * freezer_should_skip() sees !PF_FREEZE_SKIP and freezes the task
  105. * normally.
  106. */
  107. if (freezer_should_skip(p))
  108. return false;
  109. spin_lock_irqsave(&freezer_lock, flags);
  110. if (!freezing(p) || frozen(p)) {
  111. spin_unlock_irqrestore(&freezer_lock, flags);
  112. return false;
  113. }
  114. if (!(p->flags & PF_KTHREAD))
  115. fake_signal_wake_up(p);
  116. else
  117. wake_up_state(p, TASK_INTERRUPTIBLE);
  118. spin_unlock_irqrestore(&freezer_lock, flags);
  119. return true;
  120. }
  121. void __thaw_task(struct task_struct *p)
  122. {
  123. unsigned long flags;
  124. /*
  125. * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
  126. * be visible to @p as waking up implies wmb. Waking up inside
  127. * freezer_lock also prevents wakeups from leaking outside
  128. * refrigerator.
  129. */
  130. spin_lock_irqsave(&freezer_lock, flags);
  131. if (frozen(p))
  132. wake_up_process(p);
  133. spin_unlock_irqrestore(&freezer_lock, flags);
  134. }
  135. /**
  136. * set_freezable - make %current freezable
  137. *
  138. * Mark %current freezable and enter refrigerator if necessary.
  139. */
  140. bool set_freezable(void)
  141. {
  142. might_sleep();
  143. /*
  144. * Modify flags while holding freezer_lock. This ensures the
  145. * freezer notices that we aren't frozen yet or the freezing
  146. * condition is visible to try_to_freeze() below.
  147. */
  148. spin_lock_irq(&freezer_lock);
  149. current->flags &= ~PF_NOFREEZE;
  150. spin_unlock_irq(&freezer_lock);
  151. return try_to_freeze();
  152. }
  153. EXPORT_SYMBOL(set_freezable);