process.c 4.7 KB

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