process.c 4.8 KB

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