process.c 4.9 KB

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