freezer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Freezer declarations */
  2. #ifndef FREEZER_H_INCLUDED
  3. #define FREEZER_H_INCLUDED
  4. #include <linux/sched.h>
  5. #ifdef CONFIG_PM
  6. /*
  7. * Check if a process has been frozen
  8. */
  9. static inline int frozen(struct task_struct *p)
  10. {
  11. return p->flags & PF_FROZEN;
  12. }
  13. /*
  14. * Check if there is a request to freeze a process
  15. */
  16. static inline int freezing(struct task_struct *p)
  17. {
  18. return test_tsk_thread_flag(p, TIF_FREEZE);
  19. }
  20. /*
  21. * Request that a process be frozen
  22. */
  23. static inline void set_freeze_flag(struct task_struct *p)
  24. {
  25. set_tsk_thread_flag(p, TIF_FREEZE);
  26. }
  27. /*
  28. * Sometimes we may need to cancel the previous 'freeze' request
  29. */
  30. static inline void clear_freeze_flag(struct task_struct *p)
  31. {
  32. clear_tsk_thread_flag(p, TIF_FREEZE);
  33. }
  34. /*
  35. * Wake up a frozen process
  36. *
  37. * task_lock() is taken to prevent the race with refrigerator() which may
  38. * occur if the freezing of tasks fails. Namely, without the lock, if the
  39. * freezing of tasks failed, thaw_tasks() might have run before a task in
  40. * refrigerator() could call frozen_process(), in which case the task would be
  41. * frozen and no one would thaw it.
  42. */
  43. static inline int thaw_process(struct task_struct *p)
  44. {
  45. task_lock(p);
  46. if (frozen(p)) {
  47. p->flags &= ~PF_FROZEN;
  48. task_unlock(p);
  49. wake_up_process(p);
  50. return 1;
  51. }
  52. clear_freeze_flag(p);
  53. task_unlock(p);
  54. return 0;
  55. }
  56. extern void refrigerator(void);
  57. extern int freeze_processes(void);
  58. extern void thaw_processes(void);
  59. static inline int try_to_freeze(void)
  60. {
  61. if (freezing(current)) {
  62. refrigerator();
  63. return 1;
  64. } else
  65. return 0;
  66. }
  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 and PF_FREEZER_SKIP is only set for userland vfork
  75. * parents. Fortunately, in the ____call_usermodehelper() case the parent won't
  76. * really block freeze_processes(), since ____call_usermodehelper() (the child)
  77. * does a little before exec/exit and it can't be frozen before waking up the
  78. * parent.
  79. */
  80. /*
  81. * If the current task is a user space one, tell the freezer not to count it as
  82. * freezable.
  83. */
  84. static inline void freezer_do_not_count(void)
  85. {
  86. if (current->mm)
  87. current->flags |= PF_FREEZER_SKIP;
  88. }
  89. /*
  90. * If the current task is a user space one, tell the freezer to count it as
  91. * freezable again and try to freeze it.
  92. */
  93. static inline void freezer_count(void)
  94. {
  95. if (current->mm) {
  96. current->flags &= ~PF_FREEZER_SKIP;
  97. try_to_freeze();
  98. }
  99. }
  100. /*
  101. * Check if the task should be counted as freezeable by the freezer
  102. */
  103. static inline int freezer_should_skip(struct task_struct *p)
  104. {
  105. return !!(p->flags & PF_FREEZER_SKIP);
  106. }
  107. /*
  108. * Tell the freezer that the current task should be frozen by it
  109. */
  110. static inline void set_freezable(void)
  111. {
  112. current->flags &= ~PF_NOFREEZE;
  113. }
  114. #else
  115. static inline int frozen(struct task_struct *p) { return 0; }
  116. static inline int freezing(struct task_struct *p) { return 0; }
  117. static inline void set_freeze_flag(struct task_struct *p) {}
  118. static inline void clear_freeze_flag(struct task_struct *p) {}
  119. static inline int thaw_process(struct task_struct *p) { return 1; }
  120. static inline void refrigerator(void) {}
  121. static inline int freeze_processes(void) { BUG(); return 0; }
  122. static inline void thaw_processes(void) {}
  123. static inline int try_to_freeze(void) { return 0; }
  124. static inline void freezer_do_not_count(void) {}
  125. static inline void freezer_count(void) {}
  126. static inline int freezer_should_skip(struct task_struct *p) { return 0; }
  127. static inline void set_freezable(void) {}
  128. #endif
  129. #endif /* FREEZER_H_INCLUDED */