process.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 <trace/events/power.h>
  18. #include <linux/wakeup_reason.h>
  19. /*
  20. * Timeout for stopping processes
  21. */
  22. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  23. static int try_to_freeze_tasks(bool user_only)
  24. {
  25. struct task_struct *g, *p;
  26. unsigned long end_time;
  27. unsigned int todo;
  28. bool wq_busy = false;
  29. struct timeval start, end;
  30. u64 elapsed_msecs64;
  31. unsigned int elapsed_msecs;
  32. bool wakeup = false;
  33. int sleep_usecs = USEC_PER_MSEC;
  34. #ifdef CONFIG_PM_SLEEP
  35. char suspend_abort[MAX_SUSPEND_ABORT_LEN];
  36. #endif
  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. for_each_process_thread(g, p) {
  45. if (p == current || !freeze_task(p))
  46. continue;
  47. if (!freezer_should_skip(p))
  48. todo++;
  49. }
  50. read_unlock(&tasklist_lock);
  51. if (!user_only) {
  52. wq_busy = freeze_workqueues_busy();
  53. todo += wq_busy;
  54. }
  55. if (!todo || time_after(jiffies, end_time))
  56. break;
  57. if (pm_wakeup_pending()) {
  58. #ifdef CONFIG_PM_SLEEP
  59. pm_get_active_wakeup_sources(suspend_abort,
  60. MAX_SUSPEND_ABORT_LEN);
  61. log_suspend_abort_reason(suspend_abort);
  62. #endif
  63. wakeup = true;
  64. break;
  65. }
  66. /*
  67. * We need to retry, but first give the freezing tasks some
  68. * time to enter the refrigerator. Start with an initial
  69. * 1 ms sleep followed by exponential backoff until 8 ms.
  70. */
  71. usleep_range(sleep_usecs / 2, sleep_usecs);
  72. if (sleep_usecs < 8 * USEC_PER_MSEC)
  73. sleep_usecs *= 2;
  74. }
  75. do_gettimeofday(&end);
  76. elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  77. do_div(elapsed_msecs64, NSEC_PER_MSEC);
  78. elapsed_msecs = elapsed_msecs64;
  79. if (wakeup) {
  80. printk("\n");
  81. printk(KERN_ERR "Freezing of tasks aborted after %d.%03d seconds",
  82. elapsed_msecs / 1000, elapsed_msecs % 1000);
  83. } else if (todo) {
  84. printk("\n");
  85. printk(KERN_ERR "Freezing of tasks failed after %d.%03d seconds"
  86. " (%d tasks refusing to freeze, wq_busy=%d):\n",
  87. elapsed_msecs / 1000, elapsed_msecs % 1000,
  88. todo - wq_busy, wq_busy);
  89. read_lock(&tasklist_lock);
  90. for_each_process_thread(g, p) {
  91. if (p != current && !freezer_should_skip(p)
  92. && freezing(p) && !frozen(p))
  93. sched_show_task(p);
  94. }
  95. read_unlock(&tasklist_lock);
  96. } else {
  97. printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  98. elapsed_msecs % 1000);
  99. }
  100. return todo ? -EBUSY : 0;
  101. }
  102. static bool __check_frozen_processes(void)
  103. {
  104. struct task_struct *g, *p;
  105. for_each_process_thread(g, p)
  106. if (p != current && !freezer_should_skip(p) && !frozen(p))
  107. return false;
  108. return true;
  109. }
  110. /*
  111. * Returns true if all freezable tasks (except for current) are frozen already
  112. */
  113. static bool check_frozen_processes(void)
  114. {
  115. bool ret;
  116. read_lock(&tasklist_lock);
  117. ret = __check_frozen_processes();
  118. read_unlock(&tasklist_lock);
  119. return ret;
  120. }
  121. /**
  122. * freeze_processes - Signal user space processes to enter the refrigerator.
  123. * The current thread will not be frozen. The same process that calls
  124. * freeze_processes must later call thaw_processes.
  125. *
  126. * On success, returns 0. On failure, -errno and system is fully thawed.
  127. */
  128. int freeze_processes(void)
  129. {
  130. int error;
  131. int oom_kills_saved;
  132. error = __usermodehelper_disable(UMH_FREEZING);
  133. if (error)
  134. return error;
  135. /* Make sure this task doesn't get frozen */
  136. current->flags |= PF_SUSPEND_TASK;
  137. if (!pm_freezing)
  138. atomic_inc(&system_freezing_cnt);
  139. pm_wakeup_clear();
  140. printk("Freezing user space processes ... ");
  141. pm_freezing = true;
  142. oom_kills_saved = oom_kills_count();
  143. error = try_to_freeze_tasks(true);
  144. if (!error) {
  145. __usermodehelper_set_disable_depth(UMH_DISABLED);
  146. oom_killer_disable();
  147. /*
  148. * There might have been an OOM kill while we were
  149. * freezing tasks and the killed task might be still
  150. * on the way out so we have to double check for race.
  151. */
  152. if (oom_kills_count() != oom_kills_saved &&
  153. !check_frozen_processes()) {
  154. __usermodehelper_set_disable_depth(UMH_ENABLED);
  155. printk("OOM in progress.");
  156. error = -EBUSY;
  157. } else {
  158. printk("done.");
  159. }
  160. }
  161. printk("\n");
  162. BUG_ON(in_atomic());
  163. if (error)
  164. thaw_processes();
  165. return error;
  166. }
  167. EXPORT_SYMBOL_GPL(freeze_processes);
  168. /**
  169. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  170. *
  171. * On success, returns 0. On failure, -errno and only the kernel threads are
  172. * thawed, so as to give a chance to the caller to do additional cleanups
  173. * (if any) before thawing the userspace tasks. So, it is the responsibility
  174. * of the caller to thaw the userspace tasks, when the time is right.
  175. */
  176. int freeze_kernel_threads(void)
  177. {
  178. int error;
  179. printk("Freezing remaining freezable tasks ... ");
  180. pm_nosig_freezing = true;
  181. error = try_to_freeze_tasks(false);
  182. if (!error)
  183. printk("done.");
  184. printk("\n");
  185. BUG_ON(in_atomic());
  186. if (error)
  187. thaw_kernel_threads();
  188. return error;
  189. }
  190. EXPORT_SYMBOL_GPL(freeze_kernel_threads);
  191. void thaw_processes(void)
  192. {
  193. struct task_struct *g, *p;
  194. struct task_struct *curr = current;
  195. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  196. if (pm_freezing)
  197. atomic_dec(&system_freezing_cnt);
  198. pm_freezing = false;
  199. pm_nosig_freezing = false;
  200. oom_killer_enable();
  201. printk("Restarting tasks ... ");
  202. __usermodehelper_set_disable_depth(UMH_FREEZING);
  203. thaw_workqueues();
  204. read_lock(&tasklist_lock);
  205. for_each_process_thread(g, p) {
  206. /* No other threads should have PF_SUSPEND_TASK set */
  207. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  208. __thaw_task(p);
  209. }
  210. read_unlock(&tasklist_lock);
  211. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  212. curr->flags &= ~PF_SUSPEND_TASK;
  213. usermodehelper_enable();
  214. schedule();
  215. printk("done.\n");
  216. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  217. }
  218. EXPORT_SYMBOL_GPL(thaw_processes);
  219. void thaw_kernel_threads(void)
  220. {
  221. struct task_struct *g, *p;
  222. pm_nosig_freezing = false;
  223. printk("Restarting kernel threads ... ");
  224. thaw_workqueues();
  225. read_lock(&tasklist_lock);
  226. for_each_process_thread(g, p) {
  227. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  228. __thaw_task(p);
  229. }
  230. read_unlock(&tasklist_lock);
  231. schedule();
  232. printk("done.\n");
  233. }
  234. EXPORT_SYMBOL_GPL(thaw_kernel_threads);