process.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. static inline int freezeable(struct task_struct * p)
  18. {
  19. if ((p == current) ||
  20. (p->flags & PF_NOFREEZE) ||
  21. (p->exit_state != 0))
  22. return 0;
  23. return 1;
  24. }
  25. /*
  26. * freezing is complete, mark current process as frozen
  27. */
  28. static inline void frozen_process(void)
  29. {
  30. if (!unlikely(current->flags & PF_NOFREEZE)) {
  31. current->flags |= PF_FROZEN;
  32. wmb();
  33. }
  34. clear_freeze_flag(current);
  35. }
  36. /* Refrigerator is place where frozen processes are stored :-). */
  37. void refrigerator(void)
  38. {
  39. /* Hmm, should we be allowed to suspend when there are realtime
  40. processes around? */
  41. long save;
  42. task_lock(current);
  43. if (freezing(current)) {
  44. frozen_process();
  45. task_unlock(current);
  46. } else {
  47. task_unlock(current);
  48. return;
  49. }
  50. save = current->state;
  51. pr_debug("%s entered refrigerator\n", current->comm);
  52. spin_lock_irq(&current->sighand->siglock);
  53. recalc_sigpending(); /* We sent fake signal, clean it up */
  54. spin_unlock_irq(&current->sighand->siglock);
  55. for (;;) {
  56. set_current_state(TASK_UNINTERRUPTIBLE);
  57. if (!frozen(current))
  58. break;
  59. schedule();
  60. }
  61. pr_debug("%s left refrigerator\n", current->comm);
  62. __set_current_state(save);
  63. }
  64. static void fake_signal_wake_up(struct task_struct *p)
  65. {
  66. unsigned long flags;
  67. spin_lock_irqsave(&p->sighand->siglock, flags);
  68. signal_wake_up(p, 0);
  69. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  70. }
  71. static inline bool should_send_signal(struct task_struct *p)
  72. {
  73. return !(p->flags & PF_FREEZER_NOSIG);
  74. }
  75. /**
  76. * freeze_task - send a freeze request to given task
  77. * @p: task to send the request to
  78. * @sig_only: if set, the request will only be sent if the task has the
  79. * PF_FREEZER_NOSIG flag unset
  80. * Return value: 'false', if @sig_only is set and the task has
  81. * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
  82. *
  83. * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
  84. * either sending a fake signal to it or waking it up, depending on whether
  85. * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
  86. * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
  87. * TIF_FREEZE flag will not be set.
  88. */
  89. static bool freeze_task(struct task_struct *p, bool sig_only)
  90. {
  91. /*
  92. * We first check if the task is freezing and next if it has already
  93. * been frozen to avoid the race with frozen_process() which first marks
  94. * the task as frozen and next clears its TIF_FREEZE.
  95. */
  96. if (!freezing(p)) {
  97. rmb();
  98. if (frozen(p))
  99. return false;
  100. if (!sig_only || should_send_signal(p))
  101. set_freeze_flag(p);
  102. else
  103. return false;
  104. }
  105. if (should_send_signal(p)) {
  106. if (!signal_pending(p))
  107. fake_signal_wake_up(p);
  108. } else if (sig_only) {
  109. return false;
  110. } else {
  111. wake_up_state(p, TASK_INTERRUPTIBLE);
  112. }
  113. return true;
  114. }
  115. static void cancel_freezing(struct task_struct *p)
  116. {
  117. unsigned long flags;
  118. if (freezing(p)) {
  119. pr_debug(" clean up: %s\n", p->comm);
  120. clear_freeze_flag(p);
  121. spin_lock_irqsave(&p->sighand->siglock, flags);
  122. recalc_sigpending_and_wake(p);
  123. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  124. }
  125. }
  126. static int try_to_freeze_tasks(bool sig_only)
  127. {
  128. struct task_struct *g, *p;
  129. unsigned long end_time;
  130. unsigned int todo;
  131. struct timeval start, end;
  132. u64 elapsed_csecs64;
  133. unsigned int elapsed_csecs;
  134. do_gettimeofday(&start);
  135. end_time = jiffies + TIMEOUT;
  136. do {
  137. todo = 0;
  138. read_lock(&tasklist_lock);
  139. do_each_thread(g, p) {
  140. if (frozen(p) || !freezeable(p))
  141. continue;
  142. if (!freeze_task(p, sig_only))
  143. continue;
  144. /*
  145. * Now that we've done set_freeze_flag, don't
  146. * perturb a task in TASK_STOPPED or TASK_TRACED.
  147. * It is "frozen enough". If the task does wake
  148. * up, it will immediately call try_to_freeze.
  149. */
  150. if (!task_is_stopped_or_traced(p) &&
  151. !freezer_should_skip(p))
  152. todo++;
  153. } while_each_thread(g, p);
  154. read_unlock(&tasklist_lock);
  155. yield(); /* Yield is okay here */
  156. if (time_after(jiffies, end_time))
  157. break;
  158. } while (todo);
  159. do_gettimeofday(&end);
  160. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  161. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  162. elapsed_csecs = elapsed_csecs64;
  163. if (todo) {
  164. /* This does not unfreeze processes that are already frozen
  165. * (we have slightly ugly calling convention in that respect,
  166. * and caller must call thaw_processes() if something fails),
  167. * but it cleans up leftover PF_FREEZE requests.
  168. */
  169. printk("\n");
  170. printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
  171. "(%d tasks refusing to freeze):\n",
  172. elapsed_csecs / 100, elapsed_csecs % 100, todo);
  173. show_state();
  174. read_lock(&tasklist_lock);
  175. do_each_thread(g, p) {
  176. task_lock(p);
  177. if (freezing(p) && !freezer_should_skip(p))
  178. printk(KERN_ERR " %s\n", p->comm);
  179. cancel_freezing(p);
  180. task_unlock(p);
  181. } while_each_thread(g, p);
  182. read_unlock(&tasklist_lock);
  183. } else {
  184. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  185. elapsed_csecs % 100);
  186. }
  187. return todo ? -EBUSY : 0;
  188. }
  189. /**
  190. * freeze_processes - tell processes to enter the refrigerator
  191. */
  192. int freeze_processes(void)
  193. {
  194. int error;
  195. printk("Freezing user space processes ... ");
  196. error = try_to_freeze_tasks(true);
  197. if (error)
  198. goto Exit;
  199. printk("done.\n");
  200. printk("Freezing remaining freezable tasks ... ");
  201. error = try_to_freeze_tasks(false);
  202. if (error)
  203. goto Exit;
  204. printk("done.");
  205. Exit:
  206. BUG_ON(in_atomic());
  207. printk("\n");
  208. return error;
  209. }
  210. static void thaw_tasks(bool nosig_only)
  211. {
  212. struct task_struct *g, *p;
  213. read_lock(&tasklist_lock);
  214. do_each_thread(g, p) {
  215. if (!freezeable(p))
  216. continue;
  217. if (nosig_only && should_send_signal(p))
  218. continue;
  219. thaw_process(p);
  220. } while_each_thread(g, p);
  221. read_unlock(&tasklist_lock);
  222. }
  223. void thaw_processes(void)
  224. {
  225. printk("Restarting tasks ... ");
  226. thaw_tasks(true);
  227. thaw_tasks(false);
  228. schedule();
  229. printk("done.\n");
  230. }
  231. EXPORT_SYMBOL(refrigerator);