freezer.h 5.4 KB

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