freezer.c 4.5 KB

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