process.c 6.3 KB

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