freezer.h 7.2 KB

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