process.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. show_state();
  138. read_lock(&tasklist_lock);
  139. do_each_thread(g, p) {
  140. if (freeze_user_space && !is_user_space(p))
  141. continue;
  142. task_lock(p);
  143. if (freezeable(p) && !frozen(p) &&
  144. !freezer_should_skip(p))
  145. printk(KERN_ERR " %s\n", p->comm);
  146. cancel_freezing(p);
  147. task_unlock(p);
  148. } while_each_thread(g, p);
  149. read_unlock(&tasklist_lock);
  150. }
  151. return todo;
  152. }
  153. /**
  154. * freeze_processes - tell processes to enter the refrigerator
  155. *
  156. * Returns 0 on success, or the number of processes that didn't freeze,
  157. * although they were told to.
  158. */
  159. int freeze_processes(void)
  160. {
  161. unsigned int nr_unfrozen;
  162. printk("Stopping tasks ... ");
  163. nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
  164. if (nr_unfrozen)
  165. return nr_unfrozen;
  166. sys_sync();
  167. nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
  168. if (nr_unfrozen)
  169. return nr_unfrozen;
  170. printk("done.\n");
  171. BUG_ON(in_atomic());
  172. return 0;
  173. }
  174. static void thaw_tasks(int thaw_user_space)
  175. {
  176. struct task_struct *g, *p;
  177. read_lock(&tasklist_lock);
  178. do_each_thread(g, p) {
  179. if (!freezeable(p))
  180. continue;
  181. if (is_user_space(p) == !thaw_user_space)
  182. continue;
  183. thaw_process(p);
  184. } while_each_thread(g, p);
  185. read_unlock(&tasklist_lock);
  186. }
  187. void thaw_processes(void)
  188. {
  189. printk("Restarting tasks ... ");
  190. thaw_tasks(FREEZER_KERNEL_THREADS);
  191. thaw_tasks(FREEZER_USER_SPACE);
  192. schedule();
  193. printk("done.\n");
  194. }
  195. EXPORT_SYMBOL(refrigerator);