process.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/power/process.c - Functions for starting/stopping processes on
  4. * suspend transitions.
  5. *
  6. * Originally from swsusp.
  7. */
  8. #undef DEBUG
  9. #include <linux/interrupt.h>
  10. #include <linux/oom.h>
  11. #include <linux/suspend.h>
  12. #include <linux/module.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/sched/task.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/freezer.h>
  17. #include <linux/delay.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/kmod.h>
  20. #include <trace/events/power.h>
  21. #include <linux/cpuset.h>
  22. #include <trace/hooks/power.h>
  23. /*
  24. * Timeout for stopping processes
  25. */
  26. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  27. static int try_to_freeze_tasks(bool user_only)
  28. {
  29. struct task_struct *g, *p;
  30. unsigned long end_time;
  31. unsigned int todo;
  32. bool wq_busy = false;
  33. ktime_t start, end, elapsed;
  34. unsigned int elapsed_msecs;
  35. bool wakeup = false;
  36. int sleep_usecs = USEC_PER_MSEC;
  37. start = ktime_get_boottime();
  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. wakeup = true;
  59. break;
  60. }
  61. /*
  62. * We need to retry, but first give the freezing tasks some
  63. * time to enter the refrigerator. Start with an initial
  64. * 1 ms sleep followed by exponential backoff until 8 ms.
  65. */
  66. usleep_range(sleep_usecs / 2, sleep_usecs);
  67. if (sleep_usecs < 8 * USEC_PER_MSEC)
  68. sleep_usecs *= 2;
  69. }
  70. end = ktime_get_boottime();
  71. elapsed = ktime_sub(end, start);
  72. elapsed_msecs = ktime_to_ms(elapsed);
  73. if (wakeup) {
  74. pr_cont("\n");
  75. pr_err("Freezing of tasks aborted after %d.%03d seconds",
  76. elapsed_msecs / 1000, elapsed_msecs % 1000);
  77. } else if (todo) {
  78. pr_cont("\n");
  79. pr_err("Freezing of tasks failed after %d.%03d seconds"
  80. " (%d tasks refusing to freeze, wq_busy=%d):\n",
  81. elapsed_msecs / 1000, elapsed_msecs % 1000,
  82. todo - wq_busy, wq_busy);
  83. if (wq_busy)
  84. show_workqueue_state();
  85. if (pm_debug_messages_on) {
  86. read_lock(&tasklist_lock);
  87. for_each_process_thread(g, p) {
  88. if (p != current && !freezer_should_skip(p)
  89. && freezing(p) && !frozen(p)) {
  90. sched_show_task(p);
  91. trace_android_vh_try_to_freeze_todo_unfrozen(p);
  92. }
  93. }
  94. read_unlock(&tasklist_lock);
  95. }
  96. trace_android_vh_try_to_freeze_todo(todo, elapsed_msecs, wq_busy);
  97. } else {
  98. pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  99. elapsed_msecs % 1000);
  100. }
  101. return todo ? -EBUSY : 0;
  102. }
  103. /**
  104. * freeze_processes - Signal user space processes to enter the refrigerator.
  105. * The current thread will not be frozen. The same process that calls
  106. * freeze_processes must later call thaw_processes.
  107. *
  108. * On success, returns 0. On failure, -errno and system is fully thawed.
  109. */
  110. int freeze_processes(void)
  111. {
  112. int error;
  113. error = __usermodehelper_disable(UMH_FREEZING);
  114. if (error)
  115. return error;
  116. /* Make sure this task doesn't get frozen */
  117. current->flags |= PF_SUSPEND_TASK;
  118. if (!pm_freezing)
  119. atomic_inc(&system_freezing_cnt);
  120. pm_wakeup_clear(0);
  121. pr_info("Freezing user space processes ... ");
  122. pm_freezing = true;
  123. error = try_to_freeze_tasks(true);
  124. if (!error) {
  125. __usermodehelper_set_disable_depth(UMH_DISABLED);
  126. pr_cont("done.");
  127. }
  128. pr_cont("\n");
  129. BUG_ON(in_atomic());
  130. /*
  131. * Now that the whole userspace is frozen we need to disable
  132. * the OOM killer to disallow any further interference with
  133. * killable tasks. There is no guarantee oom victims will
  134. * ever reach a point they go away we have to wait with a timeout.
  135. */
  136. if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
  137. error = -EBUSY;
  138. if (error)
  139. thaw_processes();
  140. return error;
  141. }
  142. /**
  143. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  144. *
  145. * On success, returns 0. On failure, -errno and only the kernel threads are
  146. * thawed, so as to give a chance to the caller to do additional cleanups
  147. * (if any) before thawing the userspace tasks. So, it is the responsibility
  148. * of the caller to thaw the userspace tasks, when the time is right.
  149. */
  150. int freeze_kernel_threads(void)
  151. {
  152. int error;
  153. pr_info("Freezing remaining freezable tasks ... ");
  154. pm_nosig_freezing = true;
  155. error = try_to_freeze_tasks(false);
  156. if (!error)
  157. pr_cont("done.");
  158. pr_cont("\n");
  159. BUG_ON(in_atomic());
  160. if (error)
  161. thaw_kernel_threads();
  162. return error;
  163. }
  164. void thaw_processes(void)
  165. {
  166. struct task_struct *g, *p;
  167. struct task_struct *curr = current;
  168. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  169. if (pm_freezing)
  170. atomic_dec(&system_freezing_cnt);
  171. pm_freezing = false;
  172. pm_nosig_freezing = false;
  173. oom_killer_enable();
  174. pr_info("Restarting tasks ... ");
  175. __usermodehelper_set_disable_depth(UMH_FREEZING);
  176. thaw_workqueues();
  177. cpuset_wait_for_hotplug();
  178. read_lock(&tasklist_lock);
  179. for_each_process_thread(g, p) {
  180. /* No other threads should have PF_SUSPEND_TASK set */
  181. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  182. __thaw_task(p);
  183. }
  184. read_unlock(&tasklist_lock);
  185. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  186. curr->flags &= ~PF_SUSPEND_TASK;
  187. usermodehelper_enable();
  188. schedule();
  189. pr_cont("done.\n");
  190. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  191. }
  192. void thaw_kernel_threads(void)
  193. {
  194. struct task_struct *g, *p;
  195. pm_nosig_freezing = false;
  196. pr_info("Restarting kernel threads ... ");
  197. thaw_workqueues();
  198. read_lock(&tasklist_lock);
  199. for_each_process_thread(g, p) {
  200. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  201. __thaw_task(p);
  202. }
  203. read_unlock(&tasklist_lock);
  204. schedule();
  205. pr_cont("done.\n");
  206. }