process.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. end_time = jiffies + TIMEOUT;
  152. do {
  153. todo = 0;
  154. read_lock(&tasklist_lock);
  155. do_each_thread(g, p) {
  156. if (frozen(p) || !freezeable(p))
  157. continue;
  158. if (p->state == TASK_TRACED && frozen(p->parent)) {
  159. cancel_freezing(p);
  160. continue;
  161. }
  162. if (!freeze_task(p, freeze_user_space))
  163. continue;
  164. if (!freezer_should_skip(p))
  165. todo++;
  166. } while_each_thread(g, p);
  167. read_unlock(&tasklist_lock);
  168. yield(); /* Yield is okay here */
  169. if (time_after(jiffies, end_time))
  170. break;
  171. } while (todo);
  172. if (todo) {
  173. /* This does not unfreeze processes that are already frozen
  174. * (we have slightly ugly calling convention in that respect,
  175. * and caller must call thaw_processes() if something fails),
  176. * but it cleans up leftover PF_FREEZE requests.
  177. */
  178. printk("\n");
  179. printk(KERN_ERR "Freezing of %s timed out after %d seconds "
  180. "(%d tasks refusing to freeze):\n",
  181. freeze_user_space ? "user space " : "tasks ",
  182. TIMEOUT / HZ, todo);
  183. show_state();
  184. read_lock(&tasklist_lock);
  185. do_each_thread(g, p) {
  186. task_lock(p);
  187. if (freezing(p) && !freezer_should_skip(p))
  188. printk(KERN_ERR " %s\n", p->comm);
  189. cancel_freezing(p);
  190. task_unlock(p);
  191. } while_each_thread(g, p);
  192. read_unlock(&tasklist_lock);
  193. }
  194. return todo ? -EBUSY : 0;
  195. }
  196. /**
  197. * freeze_processes - tell processes to enter the refrigerator
  198. */
  199. int freeze_processes(void)
  200. {
  201. int error;
  202. printk("Stopping tasks ... ");
  203. error = try_to_freeze_tasks(FREEZER_USER_SPACE);
  204. if (error)
  205. return error;
  206. error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
  207. if (error)
  208. return error;
  209. printk("done.\n");
  210. BUG_ON(in_atomic());
  211. return 0;
  212. }
  213. static void thaw_tasks(int thaw_user_space)
  214. {
  215. struct task_struct *g, *p;
  216. read_lock(&tasklist_lock);
  217. do_each_thread(g, p) {
  218. if (!freezeable(p))
  219. continue;
  220. if (!p->mm == thaw_user_space)
  221. continue;
  222. thaw_process(p);
  223. } while_each_thread(g, p);
  224. read_unlock(&tasklist_lock);
  225. }
  226. void thaw_processes(void)
  227. {
  228. printk("Restarting tasks ... ");
  229. thaw_tasks(FREEZER_KERNEL_THREADS);
  230. thaw_tasks(FREEZER_USER_SPACE);
  231. schedule();
  232. printk("done.\n");
  233. }
  234. EXPORT_SYMBOL(refrigerator);