freezer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Freezer declarations */
  2. #ifndef FREEZER_H_INCLUDED
  3. #define FREEZER_H_INCLUDED
  4. #include <linux/sched.h>
  5. #include <linux/wait.h>
  6. #include <linux/atomic.h>
  7. #ifdef CONFIG_FREEZER
  8. extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */
  9. extern bool pm_freezing; /* PM freezing in effect */
  10. extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
  11. /*
  12. * Check if a process has been frozen
  13. */
  14. static inline bool frozen(struct task_struct *p)
  15. {
  16. return p->flags & PF_FROZEN;
  17. }
  18. extern bool freezing_slow_path(struct task_struct *p);
  19. /*
  20. * Check if there is a request to freeze a process
  21. */
  22. static inline bool freezing(struct task_struct *p)
  23. {
  24. if (likely(!atomic_read(&system_freezing_cnt)))
  25. return false;
  26. return freezing_slow_path(p);
  27. }
  28. /* Takes and releases task alloc lock using task_lock() */
  29. extern void __thaw_task(struct task_struct *t);
  30. extern bool __refrigerator(bool check_kthr_stop);
  31. extern int freeze_processes(void);
  32. extern int freeze_kernel_threads(void);
  33. extern void thaw_processes(void);
  34. static inline bool try_to_freeze(void)
  35. {
  36. might_sleep();
  37. if (likely(!freezing(current)))
  38. return false;
  39. return __refrigerator(false);
  40. }
  41. extern bool freeze_task(struct task_struct *p);
  42. extern bool set_freezable(void);
  43. #ifdef CONFIG_CGROUP_FREEZER
  44. extern bool cgroup_freezing(struct task_struct *task);
  45. #else /* !CONFIG_CGROUP_FREEZER */
  46. static inline bool cgroup_freezing(struct task_struct *task)
  47. {
  48. return false;
  49. }
  50. #endif /* !CONFIG_CGROUP_FREEZER */
  51. /*
  52. * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
  53. * calls wait_for_completion(&vfork) and reset right after it returns from this
  54. * function. Next, the parent should call try_to_freeze() to freeze itself
  55. * appropriately in case the child has exited before the freezing of tasks is
  56. * complete. However, we don't want kernel threads to be frozen in unexpected
  57. * places, so we allow them to block freeze_processes() instead or to set
  58. * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
  59. * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
  60. * really block freeze_processes(), since ____call_usermodehelper() (the child)
  61. * does a little before exec/exit and it can't be frozen before waking up the
  62. * parent.
  63. */
  64. /*
  65. * If the current task is a user space one, tell the freezer not to count it as
  66. * freezable.
  67. */
  68. static inline void freezer_do_not_count(void)
  69. {
  70. if (current->mm)
  71. current->flags |= PF_FREEZER_SKIP;
  72. }
  73. /*
  74. * If the current task is a user space one, tell the freezer to count it as
  75. * freezable again and try to freeze it.
  76. */
  77. static inline void freezer_count(void)
  78. {
  79. if (current->mm) {
  80. current->flags &= ~PF_FREEZER_SKIP;
  81. try_to_freeze();
  82. }
  83. }
  84. /*
  85. * Check if the task should be counted as freezable by the freezer
  86. */
  87. static inline int freezer_should_skip(struct task_struct *p)
  88. {
  89. return !!(p->flags & PF_FREEZER_SKIP);
  90. }
  91. /*
  92. * These macros are intended to be used whenever you want allow a task that's
  93. * sleeping in TASK_UNINTERRUPTIBLE or TASK_KILLABLE state to be frozen. Note
  94. * that neither return any clear indication of whether a freeze event happened
  95. * while in this function.
  96. */
  97. /* Like schedule(), but should not block the freezer. */
  98. #define freezable_schedule() \
  99. ({ \
  100. freezer_do_not_count(); \
  101. schedule(); \
  102. freezer_count(); \
  103. })
  104. /* Like schedule_timeout_killable(), but should not block the freezer. */
  105. #define freezable_schedule_timeout_killable(timeout) \
  106. ({ \
  107. freezer_do_not_count(); \
  108. schedule_timeout_killable(timeout); \
  109. freezer_count(); \
  110. })
  111. /*
  112. * Freezer-friendly wrappers around wait_event_interruptible(),
  113. * wait_event_killable() and wait_event_interruptible_timeout(), originally
  114. * defined in <linux/wait.h>
  115. */
  116. #define wait_event_freezekillable(wq, condition) \
  117. ({ \
  118. int __retval; \
  119. freezer_do_not_count(); \
  120. __retval = wait_event_killable(wq, (condition)); \
  121. freezer_count(); \
  122. __retval; \
  123. })
  124. #define wait_event_freezable(wq, condition) \
  125. ({ \
  126. int __retval; \
  127. for (;;) { \
  128. __retval = wait_event_interruptible(wq, \
  129. (condition) || freezing(current)); \
  130. if (__retval || (condition)) \
  131. break; \
  132. try_to_freeze(); \
  133. } \
  134. __retval; \
  135. })
  136. #define wait_event_freezable_timeout(wq, condition, timeout) \
  137. ({ \
  138. long __retval = timeout; \
  139. for (;;) { \
  140. __retval = wait_event_interruptible_timeout(wq, \
  141. (condition) || freezing(current), \
  142. __retval); \
  143. if (__retval <= 0 || (condition)) \
  144. break; \
  145. try_to_freeze(); \
  146. } \
  147. __retval; \
  148. })
  149. #else /* !CONFIG_FREEZER */
  150. static inline bool frozen(struct task_struct *p) { return false; }
  151. static inline bool freezing(struct task_struct *p) { return false; }
  152. static inline void __thaw_task(struct task_struct *t) {}
  153. static inline bool __refrigerator(bool check_kthr_stop) { return false; }
  154. static inline int freeze_processes(void) { return -ENOSYS; }
  155. static inline int freeze_kernel_threads(void) { return -ENOSYS; }
  156. static inline void thaw_processes(void) {}
  157. static inline bool try_to_freeze(void) { return false; }
  158. static inline void freezer_do_not_count(void) {}
  159. static inline void freezer_count(void) {}
  160. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  161. static inline void set_freezable(void) {}
  162. #define freezable_schedule() schedule()
  163. #define freezable_schedule_timeout_killable(timeout) \
  164. schedule_timeout_killable(timeout)
  165. #define wait_event_freezable(wq, condition) \
  166. wait_event_interruptible(wq, condition)
  167. #define wait_event_freezable_timeout(wq, condition, timeout) \
  168. wait_event_interruptible_timeout(wq, condition, timeout)
  169. #define wait_event_freezekillable(wq, condition) \
  170. wait_event_killable(wq, condition)
  171. #endif /* !CONFIG_FREEZER */
  172. #endif /* FREEZER_H_INCLUDED */