freezer.c 4.4 KB

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