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