freezer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Freezer declarations */
  2. #ifndef FREEZER_H_INCLUDED
  3. #define FREEZER_H_INCLUDED
  4. #include <linux/sched.h>
  5. #include <linux/wait.h>
  6. #ifdef CONFIG_PM_SLEEP
  7. /*
  8. * Check if a process has been frozen
  9. */
  10. static inline int frozen(struct task_struct *p)
  11. {
  12. return p->flags & PF_FROZEN;
  13. }
  14. /*
  15. * Check if there is a request to freeze a process
  16. */
  17. static inline int freezing(struct task_struct *p)
  18. {
  19. return test_tsk_thread_flag(p, TIF_FREEZE);
  20. }
  21. /*
  22. * Request that a process be frozen
  23. */
  24. static inline void set_freeze_flag(struct task_struct *p)
  25. {
  26. set_tsk_thread_flag(p, TIF_FREEZE);
  27. }
  28. /*
  29. * Sometimes we may need to cancel the previous 'freeze' request
  30. */
  31. static inline void clear_freeze_flag(struct task_struct *p)
  32. {
  33. clear_tsk_thread_flag(p, TIF_FREEZE);
  34. }
  35. /*
  36. * Wake up a frozen process
  37. *
  38. * task_lock() is taken to prevent the race with refrigerator() which may
  39. * occur if the freezing of tasks fails. Namely, without the lock, if the
  40. * freezing of tasks failed, thaw_tasks() might have run before a task in
  41. * refrigerator() could call frozen_process(), in which case the task would be
  42. * frozen and no one would thaw it.
  43. */
  44. static inline int thaw_process(struct task_struct *p)
  45. {
  46. task_lock(p);
  47. if (frozen(p)) {
  48. p->flags &= ~PF_FROZEN;
  49. task_unlock(p);
  50. wake_up_process(p);
  51. return 1;
  52. }
  53. clear_freeze_flag(p);
  54. task_unlock(p);
  55. return 0;
  56. }
  57. extern void refrigerator(void);
  58. extern int freeze_processes(void);
  59. extern void thaw_processes(void);
  60. static inline int try_to_freeze(void)
  61. {
  62. if (freezing(current)) {
  63. refrigerator();
  64. return 1;
  65. } else
  66. return 0;
  67. }
  68. /*
  69. * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
  70. * calls wait_for_completion(&vfork) and reset right after it returns from this
  71. * function. Next, the parent should call try_to_freeze() to freeze itself
  72. * appropriately in case the child has exited before the freezing of tasks is
  73. * complete. However, we don't want kernel threads to be frozen in unexpected
  74. * places, so we allow them to block freeze_processes() instead or to set
  75. * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
  76. * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
  77. * really block freeze_processes(), since ____call_usermodehelper() (the child)
  78. * does a little before exec/exit and it can't be frozen before waking up the
  79. * parent.
  80. */
  81. /*
  82. * If the current task is a user space one, tell the freezer not to count it as
  83. * freezable.
  84. */
  85. static inline void freezer_do_not_count(void)
  86. {
  87. if (current->mm)
  88. current->flags |= PF_FREEZER_SKIP;
  89. }
  90. /*
  91. * If the current task is a user space one, tell the freezer to count it as
  92. * freezable again and try to freeze it.
  93. */
  94. static inline void freezer_count(void)
  95. {
  96. if (current->mm) {
  97. current->flags &= ~PF_FREEZER_SKIP;
  98. try_to_freeze();
  99. }
  100. }
  101. /*
  102. * Check if the task should be counted as freezeable by the freezer
  103. */
  104. static inline int freezer_should_skip(struct task_struct *p)
  105. {
  106. return !!(p->flags & PF_FREEZER_SKIP);
  107. }
  108. /*
  109. * Tell the freezer that the current task should be frozen by it
  110. */
  111. static inline void set_freezable(void)
  112. {
  113. current->flags &= ~PF_NOFREEZE;
  114. }
  115. /*
  116. * Freezer-friendly wrappers around wait_event_interruptible() and
  117. * wait_event_interruptible_timeout(), originally defined in <linux/wait.h>
  118. */
  119. #define wait_event_freezable(wq, condition) \
  120. ({ \
  121. int __retval; \
  122. do { \
  123. __retval = wait_event_interruptible(wq, \
  124. (condition) || freezing(current)); \
  125. if (__retval && !freezing(current)) \
  126. break; \
  127. else if (!(condition)) \
  128. __retval = -ERESTARTSYS; \
  129. } while (try_to_freeze()); \
  130. __retval; \
  131. })
  132. #define wait_event_freezable_timeout(wq, condition, timeout) \
  133. ({ \
  134. long __retval = timeout; \
  135. do { \
  136. __retval = wait_event_interruptible_timeout(wq, \
  137. (condition) || freezing(current), \
  138. __retval); \
  139. } while (try_to_freeze()); \
  140. __retval; \
  141. })
  142. #else /* !CONFIG_PM_SLEEP */
  143. static inline int frozen(struct task_struct *p) { return 0; }
  144. static inline int freezing(struct task_struct *p) { return 0; }
  145. static inline void set_freeze_flag(struct task_struct *p) {}
  146. static inline void clear_freeze_flag(struct task_struct *p) {}
  147. static inline int thaw_process(struct task_struct *p) { return 1; }
  148. static inline void refrigerator(void) {}
  149. static inline int freeze_processes(void) { BUG(); return 0; }
  150. static inline void thaw_processes(void) {}
  151. static inline int try_to_freeze(void) { return 0; }
  152. static inline void freezer_do_not_count(void) {}
  153. static inline void freezer_count(void) {}
  154. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  155. static inline void set_freezable(void) {}
  156. #define wait_event_freezable(wq, condition) \
  157. wait_event_interruptible(wq, condition)
  158. #define wait_event_freezable_timeout(wq, condition, timeout) \
  159. wait_event_interruptible_timeout(wq, condition, timeout)
  160. #endif /* !CONFIG_PM_SLEEP */
  161. #endif /* FREEZER_H_INCLUDED */