freezer.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Freezer declarations */
  2. #ifndef FREEZER_H_INCLUDED
  3. #define FREEZER_H_INCLUDED
  4. #include <linux/debug_locks.h>
  5. #include <linux/sched.h>
  6. #include <linux/wait.h>
  7. #include <linux/atomic.h>
  8. #ifdef CONFIG_FREEZER
  9. extern atomic_t system_freezing_cnt; /* nr of freezing conds in effect */
  10. extern bool pm_freezing; /* PM freezing in effect */
  11. extern bool pm_nosig_freezing; /* PM nosig freezing in effect */
  12. /*
  13. * Timeout for stopping processes
  14. */
  15. extern unsigned int freeze_timeout_msecs;
  16. /*
  17. * Check if a process has been frozen
  18. */
  19. static inline bool frozen(struct task_struct *p)
  20. {
  21. return p->flags & PF_FROZEN;
  22. }
  23. extern bool freezing_slow_path(struct task_struct *p);
  24. /*
  25. * Check if there is a request to freeze a process
  26. */
  27. static inline bool freezing(struct task_struct *p)
  28. {
  29. if (likely(!atomic_read(&system_freezing_cnt)))
  30. return false;
  31. return freezing_slow_path(p);
  32. }
  33. /* Takes and releases task alloc lock using task_lock() */
  34. extern void __thaw_task(struct task_struct *t);
  35. extern bool __refrigerator(bool check_kthr_stop);
  36. extern int freeze_processes(void);
  37. extern int freeze_kernel_threads(void);
  38. extern void thaw_processes(void);
  39. extern void thaw_kernel_threads(void);
  40. /*
  41. * DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION
  42. * If try_to_freeze causes a lockdep warning it means the caller may deadlock
  43. */
  44. static inline bool try_to_freeze_unsafe(void)
  45. {
  46. might_sleep();
  47. if (likely(!freezing(current)))
  48. return false;
  49. return __refrigerator(false);
  50. }
  51. static inline bool try_to_freeze(void)
  52. {
  53. if (!(current->flags & PF_NOFREEZE))
  54. debug_check_no_locks_held();
  55. return try_to_freeze_unsafe();
  56. }
  57. extern bool freeze_task(struct task_struct *p);
  58. extern bool set_freezable(void);
  59. #ifdef CONFIG_CGROUP_FREEZER
  60. extern bool cgroup_freezing(struct task_struct *task);
  61. #else /* !CONFIG_CGROUP_FREEZER */
  62. static inline bool cgroup_freezing(struct task_struct *task)
  63. {
  64. return false;
  65. }
  66. #endif /* !CONFIG_CGROUP_FREEZER */
  67. /*
  68. * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
  69. * calls wait_for_completion(&vfork) and reset right after it returns from this
  70. * function. Next, the parent should call try_to_freeze() to freeze itself
  71. * appropriately in case the child has exited before the freezing of tasks is
  72. * complete. However, we don't want kernel threads to be frozen in unexpected
  73. * places, so we allow them to block freeze_processes() instead or to set
  74. * PF_NOFREEZE if needed. Fortunately, in the ____call_usermodehelper() case the
  75. * parent won't really block freeze_processes(), since ____call_usermodehelper()
  76. * (the child) does a little before exec/exit and it can't be frozen before
  77. * waking up the parent.
  78. */
  79. /**
  80. * freezer_do_not_count - tell freezer to ignore %current
  81. *
  82. * Tell freezers to ignore the current task when determining whether the
  83. * target frozen state is reached. IOW, the current task will be
  84. * considered frozen enough by freezers.
  85. *
  86. * The caller shouldn't do anything which isn't allowed for a frozen task
  87. * until freezer_cont() is called. Usually, freezer[_do_not]_count() pair
  88. * wrap a scheduling operation and nothing much else.
  89. */
  90. static inline void freezer_do_not_count(void)
  91. {
  92. current->flags |= PF_FREEZER_SKIP;
  93. }
  94. /**
  95. * freezer_count - tell freezer to stop ignoring %current
  96. *
  97. * Undo freezer_do_not_count(). It tells freezers that %current should be
  98. * considered again and tries to freeze if freezing condition is already in
  99. * effect.
  100. */
  101. static inline void freezer_count(void)
  102. {
  103. current->flags &= ~PF_FREEZER_SKIP;
  104. /*
  105. * If freezing is in progress, the following paired with smp_mb()
  106. * in freezer_should_skip() ensures that either we see %true
  107. * freezing() or freezer_should_skip() sees !PF_FREEZER_SKIP.
  108. */
  109. smp_mb();
  110. try_to_freeze();
  111. }
  112. /* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
  113. static inline void freezer_count_unsafe(void)
  114. {
  115. current->flags &= ~PF_FREEZER_SKIP;
  116. smp_mb();
  117. try_to_freeze_unsafe();
  118. }
  119. /**
  120. * freezer_should_skip - whether to skip a task when determining frozen
  121. * state is reached
  122. * @p: task in quesion
  123. *
  124. * This function is used by freezers after establishing %true freezing() to
  125. * test whether a task should be skipped when determining the target frozen
  126. * state is reached. IOW, if this function returns %true, @p is considered
  127. * frozen enough.
  128. */
  129. static inline bool freezer_should_skip(struct task_struct *p)
  130. {
  131. /*
  132. * The following smp_mb() paired with the one in freezer_count()
  133. * ensures that either freezer_count() sees %true freezing() or we
  134. * see cleared %PF_FREEZER_SKIP and return %false. This makes it
  135. * impossible for a task to slip frozen state testing after
  136. * clearing %PF_FREEZER_SKIP.
  137. */
  138. smp_mb();
  139. return p->flags & PF_FREEZER_SKIP;
  140. }
  141. /*
  142. * These functions are intended to be used whenever you want allow a sleeping
  143. * task to be frozen. Note that neither return any clear indication of
  144. * whether a freeze event happened while in this function.
  145. */
  146. /* Like schedule(), but should not block the freezer. */
  147. static inline void freezable_schedule(void)
  148. {
  149. freezer_do_not_count();
  150. schedule();
  151. freezer_count();
  152. }
  153. /* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
  154. static inline void freezable_schedule_unsafe(void)
  155. {
  156. freezer_do_not_count();
  157. schedule();
  158. freezer_count_unsafe();
  159. }
  160. /*
  161. * Like freezable_schedule_timeout(), but should not block the freezer. Do not
  162. * call this with locks held.
  163. */
  164. static inline long freezable_schedule_timeout(long timeout)
  165. {
  166. long __retval;
  167. freezer_do_not_count();
  168. __retval = schedule_timeout(timeout);
  169. freezer_count();
  170. return __retval;
  171. }
  172. /*
  173. * Like schedule_timeout_interruptible(), but should not block the freezer. Do not
  174. * call this with locks held.
  175. */
  176. static inline long freezable_schedule_timeout_interruptible(long timeout)
  177. {
  178. long __retval;
  179. freezer_do_not_count();
  180. __retval = schedule_timeout_interruptible(timeout);
  181. freezer_count();
  182. return __retval;
  183. }
  184. /* Like schedule_timeout_killable(), but should not block the freezer. */
  185. static inline long freezable_schedule_timeout_killable(long timeout)
  186. {
  187. long __retval;
  188. freezer_do_not_count();
  189. __retval = schedule_timeout_killable(timeout);
  190. freezer_count();
  191. return __retval;
  192. }
  193. /* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
  194. static inline long freezable_schedule_timeout_killable_unsafe(long timeout)
  195. {
  196. long __retval;
  197. freezer_do_not_count();
  198. __retval = schedule_timeout_killable(timeout);
  199. freezer_count_unsafe();
  200. return __retval;
  201. }
  202. /*
  203. * Like schedule_hrtimeout_range(), but should not block the freezer. Do not
  204. * call this with locks held.
  205. */
  206. static inline int freezable_schedule_hrtimeout_range(ktime_t *expires,
  207. unsigned long delta, const enum hrtimer_mode mode)
  208. {
  209. int __retval;
  210. freezer_do_not_count();
  211. __retval = schedule_hrtimeout_range(expires, delta, mode);
  212. freezer_count();
  213. return __retval;
  214. }
  215. /*
  216. * Freezer-friendly wrappers around wait_event_interruptible(),
  217. * wait_event_killable() and wait_event_interruptible_timeout(), originally
  218. * defined in <linux/wait.h>
  219. */
  220. /* DO NOT ADD ANY NEW CALLERS OF THIS FUNCTION */
  221. #define wait_event_freezekillable_unsafe(wq, condition) \
  222. ({ \
  223. int __retval; \
  224. freezer_do_not_count(); \
  225. __retval = wait_event_killable(wq, (condition)); \
  226. freezer_count_unsafe(); \
  227. __retval; \
  228. })
  229. #else /* !CONFIG_FREEZER */
  230. static inline bool frozen(struct task_struct *p) { return false; }
  231. static inline bool freezing(struct task_struct *p) { return false; }
  232. static inline void __thaw_task(struct task_struct *t) {}
  233. static inline bool __refrigerator(bool check_kthr_stop) { return false; }
  234. static inline int freeze_processes(void) { return -ENOSYS; }
  235. static inline int freeze_kernel_threads(void) { return -ENOSYS; }
  236. static inline void thaw_processes(void) {}
  237. static inline void thaw_kernel_threads(void) {}
  238. static inline bool try_to_freeze_nowarn(void) { return false; }
  239. static inline bool try_to_freeze(void) { return false; }
  240. static inline void freezer_do_not_count(void) {}
  241. static inline void freezer_count(void) {}
  242. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  243. static inline void set_freezable(void) {}
  244. #define freezable_schedule() schedule()
  245. #define freezable_schedule_unsafe() schedule()
  246. #define freezable_schedule_timeout(timeout) schedule_timeout(timeout)
  247. #define freezable_schedule_timeout_interruptible(timeout) \
  248. schedule_timeout_interruptible(timeout)
  249. #define freezable_schedule_timeout_killable(timeout) \
  250. schedule_timeout_killable(timeout)
  251. #define freezable_schedule_timeout_killable_unsafe(timeout) \
  252. schedule_timeout_killable(timeout)
  253. #define freezable_schedule_hrtimeout_range(expires, delta, mode) \
  254. schedule_hrtimeout_range(expires, delta, mode)
  255. #define wait_event_freezekillable_unsafe(wq, condition) \
  256. wait_event_killable(wq, condition)
  257. #endif /* !CONFIG_FREEZER */
  258. #endif /* FREEZER_H_INCLUDED */