process.c 4.7 KB

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