freezer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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)
  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. spin_lock_irqsave(&freezer_lock, flags);
  94. if (!freezing(p) || frozen(p)) {
  95. spin_unlock_irqrestore(&freezer_lock, flags);
  96. return false;
  97. }
  98. if (!(p->flags & PF_KTHREAD))
  99. fake_signal_wake_up(p);
  100. else
  101. wake_up_state(p, TASK_INTERRUPTIBLE);
  102. spin_unlock_irqrestore(&freezer_lock, flags);
  103. return true;
  104. }
  105. void __thaw_task(struct task_struct *p)
  106. {
  107. unsigned long flags;
  108. /*
  109. * Clear freezing and kick @p if FROZEN. Clearing is guaranteed to
  110. * be visible to @p as waking up implies wmb. Waking up inside
  111. * freezer_lock also prevents wakeups from leaking outside
  112. * refrigerator.
  113. */
  114. spin_lock_irqsave(&freezer_lock, flags);
  115. if (frozen(p))
  116. wake_up_process(p);
  117. spin_unlock_irqrestore(&freezer_lock, flags);
  118. }
  119. /**
  120. * set_freezable - make %current freezable
  121. *
  122. * Mark %current freezable and enter refrigerator if necessary.
  123. */
  124. bool set_freezable(void)
  125. {
  126. might_sleep();
  127. /*
  128. * Modify flags while holding freezer_lock. This ensures the
  129. * freezer notices that we aren't frozen yet or the freezing
  130. * condition is visible to try_to_freeze() below.
  131. */
  132. spin_lock_irq(&freezer_lock);
  133. current->flags &= ~PF_NOFREEZE;
  134. spin_unlock_irq(&freezer_lock);
  135. return try_to_freeze();
  136. }
  137. EXPORT_SYMBOL(set_freezable);