process.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * drivers/power/process.c - Functions for starting/stopping processes on
  3. * suspend transitions.
  4. *
  5. * Originally from swsusp.
  6. */
  7. #undef DEBUG
  8. #include <linux/interrupt.h>
  9. #include <linux/suspend.h>
  10. #include <linux/module.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/freezer.h>
  13. /*
  14. * Timeout for stopping processes
  15. */
  16. #define TIMEOUT (20 * HZ)
  17. #define FREEZER_KERNEL_THREADS 0
  18. #define FREEZER_USER_SPACE 1
  19. static inline int freezeable(struct task_struct * p)
  20. {
  21. if ((p == current) ||
  22. (p->flags & PF_NOFREEZE) ||
  23. (p->exit_state != 0))
  24. return 0;
  25. return 1;
  26. }
  27. /*
  28. * freezing is complete, mark current process as frozen
  29. */
  30. static inline void frozen_process(void)
  31. {
  32. if (!unlikely(current->flags & PF_NOFREEZE)) {
  33. current->flags |= PF_FROZEN;
  34. wmb();
  35. }
  36. clear_tsk_thread_flag(current, TIF_FREEZE);
  37. }
  38. /* Refrigerator is place where frozen processes are stored :-). */
  39. void refrigerator(void)
  40. {
  41. /* Hmm, should we be allowed to suspend when there are realtime
  42. processes around? */
  43. long save;
  44. task_lock(current);
  45. if (freezing(current)) {
  46. frozen_process();
  47. task_unlock(current);
  48. } else {
  49. task_unlock(current);
  50. return;
  51. }
  52. save = current->state;
  53. pr_debug("%s entered refrigerator\n", current->comm);
  54. spin_lock_irq(&current->sighand->siglock);
  55. recalc_sigpending(); /* We sent fake signal, clean it up */
  56. spin_unlock_irq(&current->sighand->siglock);
  57. for (;;) {
  58. set_current_state(TASK_UNINTERRUPTIBLE);
  59. if (!frozen(current))
  60. break;
  61. schedule();
  62. }
  63. pr_debug("%s left refrigerator\n", current->comm);
  64. current->state = save;
  65. }
  66. static inline void freeze_process(struct task_struct *p)
  67. {
  68. unsigned long flags;
  69. if (!freezing(p)) {
  70. rmb();
  71. if (!frozen(p)) {
  72. if (p->state == TASK_STOPPED)
  73. force_sig_specific(SIGSTOP, p);
  74. freeze(p);
  75. spin_lock_irqsave(&p->sighand->siglock, flags);
  76. signal_wake_up(p, p->state == TASK_STOPPED);
  77. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  78. }
  79. }
  80. }
  81. static void cancel_freezing(struct task_struct *p)
  82. {
  83. unsigned long flags;
  84. if (freezing(p)) {
  85. pr_debug(" clean up: %s\n", p->comm);
  86. do_not_freeze(p);
  87. spin_lock_irqsave(&p->sighand->siglock, flags);
  88. recalc_sigpending_and_wake(p);
  89. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  90. }
  91. }
  92. static inline int is_user_space(struct task_struct *p)
  93. {
  94. return p->mm && !(p->flags & PF_BORROWED_MM);
  95. }
  96. static unsigned int try_to_freeze_tasks(int freeze_user_space)
  97. {
  98. struct task_struct *g, *p;
  99. unsigned long end_time;
  100. unsigned int todo;
  101. end_time = jiffies + TIMEOUT;
  102. do {
  103. todo = 0;
  104. read_lock(&tasklist_lock);
  105. do_each_thread(g, p) {
  106. if (!freezeable(p))
  107. continue;
  108. if (frozen(p))
  109. continue;
  110. if (p->state == TASK_TRACED && frozen(p->parent)) {
  111. cancel_freezing(p);
  112. continue;
  113. }
  114. if (freeze_user_space && !is_user_space(p))
  115. continue;
  116. freeze_process(p);
  117. if (!freezer_should_skip(p))
  118. todo++;
  119. } while_each_thread(g, p);
  120. read_unlock(&tasklist_lock);
  121. yield(); /* Yield is okay here */
  122. if (todo && time_after(jiffies, end_time))
  123. break;
  124. } while (todo);
  125. if (todo) {
  126. /* This does not unfreeze processes that are already frozen
  127. * (we have slightly ugly calling convention in that respect,
  128. * and caller must call thaw_processes() if something fails),
  129. * but it cleans up leftover PF_FREEZE requests.
  130. */
  131. printk("\n");
  132. printk(KERN_ERR "Stopping %s timed out after %d seconds "
  133. "(%d tasks refusing to freeze):\n",
  134. freeze_user_space ? "user space processes" :
  135. "kernel threads",
  136. TIMEOUT / HZ, todo);
  137. read_lock(&tasklist_lock);
  138. do_each_thread(g, p) {
  139. if (freeze_user_space && !is_user_space(p))
  140. continue;
  141. task_lock(p);
  142. if (freezeable(p) && !frozen(p) &&
  143. !freezer_should_skip(p))
  144. printk(KERN_ERR " %s\n", p->comm);
  145. cancel_freezing(p);
  146. task_unlock(p);
  147. } while_each_thread(g, p);
  148. read_unlock(&tasklist_lock);
  149. }
  150. return todo;
  151. }
  152. /**
  153. * freeze_processes - tell processes to enter the refrigerator
  154. *
  155. * Returns 0 on success, or the number of processes that didn't freeze,
  156. * although they were told to.
  157. */
  158. int freeze_processes(void)
  159. {
  160. unsigned int nr_unfrozen;
  161. printk("Stopping tasks ... ");
  162. nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
  163. if (nr_unfrozen)
  164. return nr_unfrozen;
  165. sys_sync();
  166. nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
  167. if (nr_unfrozen)
  168. return nr_unfrozen;
  169. printk("done.\n");
  170. BUG_ON(in_atomic());
  171. return 0;
  172. }
  173. static void thaw_tasks(int thaw_user_space)
  174. {
  175. struct task_struct *g, *p;
  176. read_lock(&tasklist_lock);
  177. do_each_thread(g, p) {
  178. if (!freezeable(p))
  179. continue;
  180. if (is_user_space(p) == !thaw_user_space)
  181. continue;
  182. thaw_process(p);
  183. } while_each_thread(g, p);
  184. read_unlock(&tasklist_lock);
  185. }
  186. void thaw_processes(void)
  187. {
  188. printk("Restarting tasks ... ");
  189. thaw_tasks(FREEZER_KERNEL_THREADS);
  190. thaw_tasks(FREEZER_USER_SPACE);
  191. schedule();
  192. printk("done.\n");
  193. }
  194. EXPORT_SYMBOL(refrigerator);