process.c 4.7 KB

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