freezer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. extern void thaw_kernel_threads(void);
  35. static inline bool try_to_freeze(void)
  36. {
  37. might_sleep();
  38. if (likely(!freezing(current)))
  39. return false;
  40. return __refrigerator(false);
  41. }
  42. extern bool freeze_task(struct task_struct *p);
  43. extern bool set_freezable(void);
  44. #ifdef CONFIG_CGROUP_FREEZER
  45. extern bool cgroup_freezing(struct task_struct *task);
  46. #else /* !CONFIG_CGROUP_FREEZER */
  47. static inline bool cgroup_freezing(struct task_struct *task)
  48. {
  49. return false;
  50. }
  51. #endif /* !CONFIG_CGROUP_FREEZER */
  52. /*
  53. * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
  54. * calls wait_for_completion(&vfork) and reset right after it returns from this
  55. * function. Next, the parent should call try_to_freeze() to freeze itself
  56. * appropriately in case the child has exited before the freezing of tasks is
  57. * complete. However, we don't want kernel threads to be frozen in unexpected
  58. * places, so we allow them to block freeze_processes() instead or to set
  59. * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
  60. * parent won't really block freeze_processes(), since ____call_usermodehelper()
  61. * (the child) does a little before exec/exit and it can't be frozen before
  62. * waking up the parent.
  63. */
  64. /**
  65. * freezer_do_not_count - tell freezer to ignore %current
  66. *
  67. * Tell freezers to ignore the current task when determining whether the
  68. * target frozen state is reached. IOW, the current task will be
  69. * considered frozen enough by freezers.
  70. *
  71. * The caller shouldn't do anything which isn't allowed for a frozen task
  72. * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
  73. * wrap a scheduling operation and nothing much else.
  74. */
  75. static inline void freezer_do_not_count(void)
  76. {
  77. current->flags |= PF_FREEZER_SKIP;
  78. }
  79. /**
  80. * freezer_count - tell freezer to stop ignoring %current
  81. *
  82. * Undo freezer_do_not_count(). It tells freezers that %current should be
  83. * considered again and tries to freeze if freezing condition is already in
  84. * effect.
  85. */
  86. static inline void freezer_count(void)
  87. {
  88. current->flags &= ~PF_FREEZER_SKIP;
  89. /*
  90. * If freezing is in progress, the following paired with smp_mb()
  91. * in freezer_should_skip() ensures that either we see %true
  92. * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
  93. */
  94. smp_mb();
  95. try_to_freeze();
  96. }
  97. /**
  98. * freezer_should_skip - whether to skip a task when determining frozen
  99. * state is reached
  100. * @p: task in quesion
  101. *
  102. * This function is used by freezers after establishing %true freezing() to
  103. * test whether a task should be skipped when determining the target frozen
  104. * state is reached. IOW, if this function returns %true, @p is considered
  105. * frozen enough.
  106. */
  107. static inline bool freezer_should_skip(struct task_struct *p)
  108. {
  109. /*
  110. * The following smp_mb() paired with the one in freezer_count()
  111. * ensures that either freezer_count() sees %true freezing() or we
  112. * see cleared %PF_FREEZER_SKIP and return %false. This makes it
  113. * impossible for a task to slip frozen state testing after
  114. * clearing %PF_FREEZER_SKIP.
  115. */
  116. smp_mb();
  117. return p->flags & PF_FREEZER_SKIP;
  118. }
  119. /*
  120. * These macros are intended to be used whenever you want allow a task that's
  121. * sleeping in TASK_UNINTERRUPTIBLE or TASK_KILLABLE state to be frozen. Note
  122. * that neither return any clear indication of whether a freeze event happened
  123. * while in this function.
  124. */
  125. /* Like schedule(), but should not block the freezer. */
  126. #define freezable_schedule() \
  127. ({ \
  128. freezer_do_not_count(); \
  129. schedule(); \
  130. freezer_count(); \
  131. })
  132. /* Like schedule_timeout_killable(), but should not block the freezer. */
  133. #define freezable_schedule_timeout_killable(timeout) \
  134. ({ \
  135. long __retval; \
  136. freezer_do_not_count(); \
  137. __retval = schedule_timeout_killable(timeout); \
  138. freezer_count(); \
  139. __retval; \
  140. })
  141. /*
  142. * Freezer-friendly wrappers around wait_event_interruptible(),
  143. * wait_event_killable() and wait_event_interruptible_timeout(), originally
  144. * defined in <linux/wait.h>
  145. */
  146. #define wait_event_freezekillable(wq, condition) \
  147. ({ \
  148. int __retval; \
  149. freezer_do_not_count(); \
  150. __retval = wait_event_killable(wq, (condition)); \
  151. freezer_count(); \
  152. __retval; \
  153. })
  154. #define wait_event_freezable(wq, condition) \
  155. ({ \
  156. int __retval; \
  157. for (;;) { \
  158. __retval = wait_event_interruptible(wq, \
  159. (condition) || freezing(current)); \
  160. if (__retval || (condition)) \
  161. break; \
  162. try_to_freeze(); \
  163. } \
  164. __retval; \
  165. })
  166. #define wait_event_freezable_timeout(wq, condition, timeout) \
  167. ({ \
  168. long __retval = timeout; \
  169. for (;;) { \
  170. __retval = wait_event_interruptible_timeout(wq, \
  171. (condition) || freezing(current), \
  172. __retval); \
  173. if (__retval <= 0 || (condition)) \
  174. break; \
  175. try_to_freeze(); \
  176. } \
  177. __retval; \
  178. })
  179. #else /* !CONFIG_FREEZER */
  180. static inline bool frozen(struct task_struct *p) { return false; }
  181. static inline bool freezing(struct task_struct *p) { return false; }
  182. static inline void __thaw_task(struct task_struct *t) {}
  183. static inline bool __refrigerator(bool check_kthr_stop) { return false; }
  184. static inline int freeze_processes(void) { return -ENOSYS; }
  185. static inline int freeze_kernel_threads(void) { return -ENOSYS; }
  186. static inline void thaw_processes(void) {}
  187. static inline void thaw_kernel_threads(void) {}
  188. static inline bool try_to_freeze(void) { return false; }
  189. static inline void freezer_do_not_count(void) {}
  190. static inline void freezer_count(void) {}
  191. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  192. static inline void set_freezable(void) {}
  193. #define freezable_schedule() schedule()
  194. #define freezable_schedule_timeout_killable(timeout) \
  195. schedule_timeout_killable(timeout)
  196. #define wait_event_freezable(wq, condition) \
  197. wait_event_interruptible(wq, condition)
  198. #define wait_event_freezable_timeout(wq, condition, timeout) \
  199. wait_event_interruptible_timeout(wq, condition, timeout)
  200. #define wait_event_freezekillable(wq, condition) \
  201. wait_event_killable(wq, condition)
  202. #endif /* !CONFIG_FREEZER */
  203. #endif /* FREEZER_H_INCLUDED */