process.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/oom.h>
  10. #include <linux/suspend.h>
  11. #include <linux/module.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/freezer.h>
  14. #include <linux/delay.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/kmod.h>
  17. #include <linux/wakelock.h>
  18. #include <linux/wakeup_reason.h>
  19. #include "power.h"
  20. /*
  21. * Timeout for stopping processes
  22. */
  23. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  24. static int try_to_freeze_tasks(bool user_only)
  25. {
  26. struct task_struct *g, *p;
  27. struct task_struct *q = NULL;
  28. unsigned long end_time;
  29. unsigned int todo;
  30. bool wq_busy = false;
  31. struct timeval start, end;
  32. u64 elapsed_msecs64;
  33. unsigned int elapsed_msecs;
  34. bool wakeup = false;
  35. int sleep_usecs = USEC_PER_MSEC;
  36. char suspend_abort[MAX_SUSPEND_ABORT_LEN];
  37. do_gettimeofday(&start);
  38. end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
  39. if (!user_only)
  40. freeze_workqueues_begin();
  41. while (true) {
  42. todo = 0;
  43. read_lock(&tasklist_lock);
  44. do_each_thread(g, p) {
  45. if (p == current || !freeze_task(p))
  46. continue;
  47. /*
  48. * Now that we've done set_freeze_flag, don't
  49. * perturb a task in TASK_STOPPED or TASK_TRACED.
  50. * It is "frozen enough". If the task does wake
  51. * up, it will immediately call try_to_freeze.
  52. *
  53. * Because freeze_task() goes through p's scheduler lock, it's
  54. * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING
  55. * transition can't race with task state testing here.
  56. */
  57. if (!task_is_stopped_or_traced(p) &&
  58. !freezer_should_skip(p)) {
  59. todo++;
  60. q = p;
  61. }
  62. } while_each_thread(g, p);
  63. read_unlock(&tasklist_lock);
  64. if (!user_only) {
  65. wq_busy = freeze_workqueues_busy();
  66. todo += wq_busy;
  67. }
  68. if (!todo || time_after(jiffies, end_time))
  69. break;
  70. if (pm_wakeup_pending()) {
  71. pm_get_active_wakeup_sources(suspend_abort,
  72. MAX_SUSPEND_ABORT_LEN);
  73. log_suspend_abort_reason(suspend_abort);
  74. wakeup = true;
  75. break;
  76. }
  77. /*
  78. * We need to retry, but first give the freezing tasks some
  79. * time to enter the refrigerator. Start with an initial
  80. * 1 ms sleep followed by exponential backoff until 8 ms.
  81. */
  82. usleep_range(sleep_usecs / 2, sleep_usecs);
  83. if (sleep_usecs < 8 * USEC_PER_MSEC)
  84. sleep_usecs *= 2;
  85. }
  86. do_gettimeofday(&end);
  87. elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  88. do_div(elapsed_msecs64, NSEC_PER_MSEC);
  89. elapsed_msecs = elapsed_msecs64;
  90. if (todo) {
  91. /* This does not unfreeze processes that are already frozen
  92. * (we have slightly ugly calling convention in that respect,
  93. * and caller must call thaw_processes() if something fails),
  94. * but it cleans up leftover PF_FREEZE requests.
  95. */
  96. if(wakeup) {
  97. printk("\n");
  98. printk(KERN_ERR "Freezing of %s aborted (%d) (%s)\n",
  99. user_only ? "user space " : "tasks ",
  100. q ? q->pid : 0, q ? q->comm : "NONE");
  101. }
  102. else {
  103. printk("\n");
  104. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  105. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  106. wakeup ? "aborted" : "failed",
  107. elapsed_msecs / 1000, elapsed_msecs % 1000,
  108. todo - wq_busy, wq_busy);
  109. }
  110. if (!wakeup) {
  111. read_lock(&tasklist_lock);
  112. do_each_thread(g, p) {
  113. if (p != current && !freezer_should_skip(p)
  114. && freezing(p) && !frozen(p) &&
  115. elapsed_msecs > 1000)
  116. sched_show_task(p);
  117. } while_each_thread(g, p);
  118. read_unlock(&tasklist_lock);
  119. }
  120. } else {
  121. printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  122. elapsed_msecs % 1000);
  123. }
  124. return todo ? -EBUSY : 0;
  125. }
  126. /*
  127. * Returns true if all freezable tasks (except for current) are frozen already
  128. */
  129. static bool check_frozen_processes(void)
  130. {
  131. struct task_struct *g, *p;
  132. bool ret = true;
  133. read_lock(&tasklist_lock);
  134. for_each_process_thread(g, p) {
  135. if (p != current && !freezer_should_skip(p) &&
  136. !frozen(p)) {
  137. ret = false;
  138. goto done;
  139. }
  140. }
  141. done:
  142. read_unlock(&tasklist_lock);
  143. return ret;
  144. }
  145. /**
  146. * freeze_processes - Signal user space processes to enter the refrigerator.
  147. *
  148. * On success, returns 0. On failure, -errno and system is fully thawed.
  149. */
  150. int freeze_processes(void)
  151. {
  152. int error;
  153. int oom_kills_saved;
  154. error = __usermodehelper_disable(UMH_FREEZING);
  155. if (error)
  156. return error;
  157. if (!pm_freezing)
  158. atomic_inc(&system_freezing_cnt);
  159. pm_wakeup_clear();
  160. printk("Freezing user space processes ... ");
  161. pm_freezing = true;
  162. oom_kills_saved = oom_kills_count();
  163. error = try_to_freeze_tasks(true);
  164. if (!error) {
  165. __usermodehelper_set_disable_depth(UMH_DISABLED);
  166. oom_killer_disable();
  167. /*
  168. * There might have been an OOM kill while we were
  169. * freezing tasks and the killed task might be still
  170. * on the way out so we have to double check for race.
  171. */
  172. if (oom_kills_count() != oom_kills_saved &&
  173. !check_frozen_processes()) {
  174. __usermodehelper_set_disable_depth(UMH_ENABLED);
  175. printk("OOM in progress.");
  176. error = -EBUSY;
  177. goto done;
  178. }
  179. printk("done.");
  180. }
  181. done:
  182. printk("\n");
  183. BUG_ON(in_atomic());
  184. if (error)
  185. thaw_processes();
  186. return error;
  187. }
  188. /**
  189. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  190. *
  191. * On success, returns 0. On failure, -errno and only the kernel threads are
  192. * thawed, so as to give a chance to the caller to do additional cleanups
  193. * (if any) before thawing the userspace tasks. So, it is the responsibility
  194. * of the caller to thaw the userspace tasks, when the time is right.
  195. */
  196. int freeze_kernel_threads(void)
  197. {
  198. int error;
  199. printk("Freezing remaining freezable tasks ... ");
  200. pm_nosig_freezing = true;
  201. error = try_to_freeze_tasks(false);
  202. if (!error)
  203. printk("done.");
  204. printk("\n");
  205. BUG_ON(in_atomic());
  206. if (error)
  207. thaw_kernel_threads();
  208. return error;
  209. }
  210. void thaw_processes(void)
  211. {
  212. struct task_struct *g, *p;
  213. if (pm_freezing)
  214. atomic_dec(&system_freezing_cnt);
  215. pm_freezing = false;
  216. pm_nosig_freezing = false;
  217. oom_killer_enable();
  218. printk("Restarting tasks ... ");
  219. __usermodehelper_set_disable_depth(UMH_FREEZING);
  220. thaw_workqueues();
  221. read_lock(&tasklist_lock);
  222. do_each_thread(g, p) {
  223. __thaw_task(p);
  224. } while_each_thread(g, p);
  225. read_unlock(&tasklist_lock);
  226. usermodehelper_enable();
  227. schedule();
  228. printk("done.\n");
  229. }
  230. void thaw_kernel_threads(void)
  231. {
  232. struct task_struct *g, *p;
  233. pm_nosig_freezing = false;
  234. printk("Restarting kernel threads ... ");
  235. thaw_workqueues();
  236. read_lock(&tasklist_lock);
  237. do_each_thread(g, p) {
  238. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  239. __thaw_task(p);
  240. } while_each_thread(g, p);
  241. read_unlock(&tasklist_lock);
  242. schedule();
  243. printk("done.\n");
  244. }