process.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 fake_signal_wake_up(struct task_struct *p)
  67. {
  68. unsigned long flags;
  69. spin_lock_irqsave(&p->sighand->siglock, flags);
  70. signal_wake_up(p, 0);
  71. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  72. }
  73. static int has_mm(struct task_struct *p)
  74. {
  75. return (p->mm && !(p->flags & PF_BORROWED_MM));
  76. }
  77. /**
  78. * freeze_task - send a freeze request to given task
  79. * @p: task to send the request to
  80. * @with_mm_only: if set, the request will only be sent if the task has its
  81. * own mm
  82. * Return value: 0, if @with_mm_only is set and the task has no mm of its
  83. * own or the task is frozen, 1, otherwise
  84. *
  85. * The freeze request is sent by seting the tasks's TIF_FREEZE flag and
  86. * either sending a fake signal to it or waking it up, depending on whether
  87. * or not it has its own mm (ie. it is a user land task). If @with_mm_only
  88. * is set and the task has no mm of its own (ie. it is a kernel thread),
  89. * its TIF_FREEZE flag should not be set.
  90. *
  91. * The task_lock() is necessary to prevent races with exit_mm() or
  92. * use_mm()/unuse_mm() from occuring.
  93. */
  94. static int freeze_task(struct task_struct *p, int with_mm_only)
  95. {
  96. int ret = 1;
  97. task_lock(p);
  98. if (freezing(p)) {
  99. if (has_mm(p)) {
  100. if (!signal_pending(p))
  101. fake_signal_wake_up(p);
  102. } else {
  103. if (with_mm_only)
  104. ret = 0;
  105. else
  106. wake_up_state(p, TASK_INTERRUPTIBLE);
  107. }
  108. } else {
  109. rmb();
  110. if (frozen(p)) {
  111. ret = 0;
  112. } else {
  113. if (has_mm(p)) {
  114. set_freeze_flag(p);
  115. fake_signal_wake_up(p);
  116. } else {
  117. if (with_mm_only) {
  118. ret = 0;
  119. } else {
  120. set_freeze_flag(p);
  121. wake_up_state(p, TASK_INTERRUPTIBLE);
  122. }
  123. }
  124. }
  125. }
  126. task_unlock(p);
  127. return ret;
  128. }
  129. static void cancel_freezing(struct task_struct *p)
  130. {
  131. unsigned long flags;
  132. if (freezing(p)) {
  133. pr_debug(" clean up: %s\n", p->comm);
  134. clear_freeze_flag(p);
  135. spin_lock_irqsave(&p->sighand->siglock, flags);
  136. recalc_sigpending_and_wake(p);
  137. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  138. }
  139. }
  140. static int try_to_freeze_tasks(int freeze_user_space)
  141. {
  142. struct task_struct *g, *p;
  143. unsigned long end_time;
  144. unsigned int todo;
  145. struct timeval start, end;
  146. s64 elapsed_csecs64;
  147. unsigned int elapsed_csecs;
  148. do_gettimeofday(&start);
  149. end_time = jiffies + TIMEOUT;
  150. do {
  151. todo = 0;
  152. read_lock(&tasklist_lock);
  153. do_each_thread(g, p) {
  154. if (frozen(p) || !freezeable(p))
  155. continue;
  156. if (!freeze_task(p, freeze_user_space))
  157. continue;
  158. /*
  159. * Now that we've done set_freeze_flag, don't
  160. * perturb a task in TASK_STOPPED or TASK_TRACED.
  161. * It is "frozen enough". If the task does wake
  162. * up, it will immediately call try_to_freeze.
  163. */
  164. if (!task_is_stopped_or_traced(p) &&
  165. !freezer_should_skip(p))
  166. todo++;
  167. } while_each_thread(g, p);
  168. read_unlock(&tasklist_lock);
  169. yield(); /* Yield is okay here */
  170. if (time_after(jiffies, end_time))
  171. break;
  172. } while (todo);
  173. do_gettimeofday(&end);
  174. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  175. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  176. elapsed_csecs = elapsed_csecs64;
  177. if (todo) {
  178. /* This does not unfreeze processes that are already frozen
  179. * (we have slightly ugly calling convention in that respect,
  180. * and caller must call thaw_processes() if something fails),
  181. * but it cleans up leftover PF_FREEZE requests.
  182. */
  183. printk("\n");
  184. printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
  185. "(%d tasks refusing to freeze):\n",
  186. elapsed_csecs / 100, elapsed_csecs % 100, todo);
  187. show_state();
  188. read_lock(&tasklist_lock);
  189. do_each_thread(g, p) {
  190. task_lock(p);
  191. if (freezing(p) && !freezer_should_skip(p))
  192. printk(KERN_ERR " %s\n", p->comm);
  193. cancel_freezing(p);
  194. task_unlock(p);
  195. } while_each_thread(g, p);
  196. read_unlock(&tasklist_lock);
  197. } else {
  198. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  199. elapsed_csecs % 100);
  200. }
  201. return todo ? -EBUSY : 0;
  202. }
  203. /**
  204. * freeze_processes - tell processes to enter the refrigerator
  205. */
  206. int freeze_processes(void)
  207. {
  208. int error;
  209. printk("Freezing user space processes ... ");
  210. error = try_to_freeze_tasks(FREEZER_USER_SPACE);
  211. if (error)
  212. goto Exit;
  213. printk("done.\n");
  214. printk("Freezing remaining freezable tasks ... ");
  215. error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
  216. if (error)
  217. goto Exit;
  218. printk("done.");
  219. Exit:
  220. BUG_ON(in_atomic());
  221. printk("\n");
  222. return error;
  223. }
  224. static void thaw_tasks(int thaw_user_space)
  225. {
  226. struct task_struct *g, *p;
  227. read_lock(&tasklist_lock);
  228. do_each_thread(g, p) {
  229. if (!freezeable(p))
  230. continue;
  231. if (!p->mm == thaw_user_space)
  232. continue;
  233. thaw_process(p);
  234. } while_each_thread(g, p);
  235. read_unlock(&tasklist_lock);
  236. }
  237. void thaw_processes(void)
  238. {
  239. printk("Restarting tasks ... ");
  240. thaw_tasks(FREEZER_KERNEL_THREADS);
  241. thaw_tasks(FREEZER_USER_SPACE);
  242. schedule();
  243. printk("done.\n");
  244. }
  245. EXPORT_SYMBOL(refrigerator);