freezer.h 5.2 KB

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