freezer.h 5.5 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. #ifdef CONFIG_FREEZER
  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. static inline bool should_send_signal(struct task_struct *p)
  36. {
  37. return !(p->flags & PF_FREEZER_NOSIG);
  38. }
  39. /* Takes and releases task alloc lock using task_lock() */
  40. extern void __thaw_task(struct task_struct *t);
  41. extern bool __refrigerator(bool check_kthr_stop);
  42. extern int freeze_processes(void);
  43. extern int freeze_kernel_threads(void);
  44. extern void thaw_processes(void);
  45. static inline bool try_to_freeze(void)
  46. {
  47. might_sleep();
  48. if (likely(!freezing(current)))
  49. return false;
  50. return __refrigerator(false);
  51. }
  52. extern bool freeze_task(struct task_struct *p, bool sig_only);
  53. #ifdef CONFIG_CGROUP_FREEZER
  54. extern bool cgroup_freezing(struct task_struct *task);
  55. #else /* !CONFIG_CGROUP_FREEZER */
  56. static inline bool cgroup_freezing(struct task_struct *task)
  57. {
  58. return false;
  59. }
  60. #endif /* !CONFIG_CGROUP_FREEZER */
  61. /*
  62. * The PF_FREEZER_SKIP flag should be set by a vfork parent right before it
  63. * calls wait_for_completion(&vfork) and reset right after it returns from this
  64. * function. Next, the parent should call try_to_freeze() to freeze itself
  65. * appropriately in case the child has exited before the freezing of tasks is
  66. * complete. However, we don't want kernel threads to be frozen in unexpected
  67. * places, so we allow them to block freeze_processes() instead or to set
  68. * PF_NOFREEZE if needed and PF_FREEZER_SKIP is only set for userland vfork
  69. * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
  70. * really block freeze_processes(), since ____call_usermodehelper() (the child)
  71. * does a little before exec/exit and it can't be frozen before waking up the
  72. * parent.
  73. */
  74. /*
  75. * If the current task is a user space one, tell the freezer not to count it as
  76. * freezable.
  77. */
  78. static inline void freezer_do_not_count(void)
  79. {
  80. if (current->mm)
  81. current->flags |= PF_FREEZER_SKIP;
  82. }
  83. /*
  84. * If the current task is a user space one, tell the freezer to count it as
  85. * freezable again and try to freeze it.
  86. */
  87. static inline void freezer_count(void)
  88. {
  89. if (current->mm) {
  90. current->flags &= ~PF_FREEZER_SKIP;
  91. try_to_freeze();
  92. }
  93. }
  94. /*
  95. * Check if the task should be counted as freezable by the freezer
  96. */
  97. static inline int freezer_should_skip(struct task_struct *p)
  98. {
  99. return !!(p->flags & PF_FREEZER_SKIP);
  100. }
  101. /*
  102. * Tell the freezer that the current task should be frozen by it
  103. */
  104. static inline void set_freezable(void)
  105. {
  106. current->flags &= ~PF_NOFREEZE;
  107. }
  108. /*
  109. * Tell the freezer that the current task should be frozen by it and that it
  110. * should send a fake signal to the task to freeze it.
  111. */
  112. static inline void set_freezable_with_signal(void)
  113. {
  114. current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
  115. }
  116. /*
  117. * Freezer-friendly wrappers around wait_event_interruptible(),
  118. * wait_event_killable() and wait_event_interruptible_timeout(), originally
  119. * defined in <linux/wait.h>
  120. */
  121. #define wait_event_freezekillable(wq, condition) \
  122. ({ \
  123. int __retval; \
  124. freezer_do_not_count(); \
  125. __retval = wait_event_killable(wq, (condition)); \
  126. freezer_count(); \
  127. __retval; \
  128. })
  129. #define wait_event_freezable(wq, condition) \
  130. ({ \
  131. int __retval; \
  132. do { \
  133. __retval = wait_event_interruptible(wq, \
  134. (condition) || freezing(current)); \
  135. if (__retval && !freezing(current)) \
  136. break; \
  137. else if (!(condition)) \
  138. __retval = -ERESTARTSYS; \
  139. } while (try_to_freeze()); \
  140. __retval; \
  141. })
  142. #define wait_event_freezable_timeout(wq, condition, timeout) \
  143. ({ \
  144. long __retval = timeout; \
  145. do { \
  146. __retval = wait_event_interruptible_timeout(wq, \
  147. (condition) || freezing(current), \
  148. __retval); \
  149. } while (try_to_freeze()); \
  150. __retval; \
  151. })
  152. #else /* !CONFIG_FREEZER */
  153. static inline int frozen(struct task_struct *p) { return 0; }
  154. static inline int freezing(struct task_struct *p) { return 0; }
  155. static inline void set_freeze_flag(struct task_struct *p) {}
  156. static inline void clear_freeze_flag(struct task_struct *p) {}
  157. static inline bool __refrigerator(bool check_kthr_stop) { return false; }
  158. static inline int freeze_processes(void) { return -ENOSYS; }
  159. static inline int freeze_kernel_threads(void) { return -ENOSYS; }
  160. static inline void thaw_processes(void) {}
  161. static inline bool try_to_freeze(void) { return false; }
  162. static inline void freezer_do_not_count(void) {}
  163. static inline void freezer_count(void) {}
  164. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  165. static inline void set_freezable(void) {}
  166. static inline void set_freezable_with_signal(void) {}
  167. #define wait_event_freezable(wq, condition) \
  168. wait_event_interruptible(wq, condition)
  169. #define wait_event_freezable_timeout(wq, condition, timeout) \
  170. wait_event_interruptible_timeout(wq, condition, timeout)
  171. #define wait_event_freezekillable(wq, condition) \
  172. wait_event_killable(wq, condition)
  173. #endif /* !CONFIG_FREEZER */
  174. #endif /* FREEZER_H_INCLUDED */