process.c 4.7 KB

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