process.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include <linux/interrupt.h>
  9. #include <linux/oom.h>
  10. #include <linux/suspend.h>
  11. #include <linux/module.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/sched/task.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/freezer.h>
  16. #include <linux/delay.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/kmod.h>
  19. #include <trace/events/power.h>
  20. #include <linux/cpuset.h>
  21. #include <trace/hooks/power.h>
  22. /*
  23. * Timeout for stopping processes
  24. */
  25. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  26. static int try_to_freeze_tasks(bool user_only)
  27. {
  28. const char *what = user_only ? "user space processes" :
  29. "remaining freezable tasks";
  30. struct task_struct *g, *p;
  31. unsigned long end_time;
  32. unsigned int todo;
  33. bool wq_busy = false;
  34. ktime_t start, end, elapsed;
  35. unsigned int elapsed_msecs;
  36. bool wakeup = false;
  37. int sleep_usecs = USEC_PER_MSEC;
  38. pr_info("Freezing %s\n", what);
  39. start = ktime_get_boottime();
  40. end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
  41. if (!user_only)
  42. freeze_workqueues_begin();
  43. while (true) {
  44. todo = 0;
  45. read_lock(&tasklist_lock);
  46. for_each_process_thread(g, p) {
  47. if (p == current || !freeze_task(p))
  48. continue;
  49. todo++;
  50. }
  51. read_unlock(&tasklist_lock);
  52. if (!user_only) {
  53. wq_busy = freeze_workqueues_busy();
  54. todo += wq_busy;
  55. }
  56. if (!todo || time_after(jiffies, end_time))
  57. break;
  58. if (pm_wakeup_pending()) {
  59. wakeup = true;
  60. break;
  61. }
  62. /*
  63. * We need to retry, but first give the freezing tasks some
  64. * time to enter the refrigerator. Start with an initial
  65. * 1 ms sleep followed by exponential backoff until 8 ms.
  66. */
  67. usleep_range(sleep_usecs / 2, sleep_usecs);
  68. if (sleep_usecs < 8 * USEC_PER_MSEC)
  69. sleep_usecs *= 2;
  70. }
  71. end = ktime_get_boottime();
  72. elapsed = ktime_sub(end, start);
  73. elapsed_msecs = ktime_to_ms(elapsed);
  74. if (wakeup) {
  75. pr_cont("\n");
  76. pr_err("Freezing of tasks aborted after %d.%03d seconds",
  77. elapsed_msecs / 1000, elapsed_msecs % 1000);
  78. } else if (todo) {
  79. pr_cont("\n");
  80. pr_err("Freezing of tasks failed after %d.%03d seconds"
  81. " (%d tasks refusing to freeze, wq_busy=%d):\n",
  82. elapsed_msecs / 1000, elapsed_msecs % 1000,
  83. todo - wq_busy, wq_busy);
  84. if (wq_busy)
  85. show_all_workqueues();
  86. if (pm_debug_messages_on) {
  87. read_lock(&tasklist_lock);
  88. for_each_process_thread(g, p) {
  89. if (p != current && 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("Freezing %s completed (elapsed %d.%03d seconds)\n",
  99. what, elapsed_msecs / 1000, 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. pr_info("Disabling usermodehelper ... ");
  114. error = __usermodehelper_disable(UMH_FREEZING);
  115. if (error)
  116. return error;
  117. pr_cont("done.\n");
  118. /* Make sure this task doesn't get frozen */
  119. current->flags |= PF_SUSPEND_TASK;
  120. if (!pm_freezing)
  121. static_branch_inc(&freezer_active);
  122. pm_wakeup_clear(0);
  123. pm_freezing = true;
  124. error = try_to_freeze_tasks(true);
  125. if (!error)
  126. __usermodehelper_set_disable_depth(UMH_DISABLED);
  127. BUG_ON(in_atomic());
  128. /*
  129. * Now that the whole userspace is frozen we need to disable
  130. * the OOM killer to disallow any further interference with
  131. * killable tasks. There is no guarantee oom victims will
  132. * ever reach a point they go away we have to wait with a timeout.
  133. */
  134. if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
  135. error = -EBUSY;
  136. if (error)
  137. thaw_processes();
  138. return error;
  139. }
  140. /**
  141. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  142. *
  143. * On success, returns 0. On failure, -errno and only the kernel threads are
  144. * thawed, so as to give a chance to the caller to do additional cleanups
  145. * (if any) before thawing the userspace tasks. So, it is the responsibility
  146. * of the caller to thaw the userspace tasks, when the time is right.
  147. */
  148. int freeze_kernel_threads(void)
  149. {
  150. int error;
  151. pm_nosig_freezing = true;
  152. error = try_to_freeze_tasks(false);
  153. BUG_ON(in_atomic());
  154. if (error)
  155. thaw_kernel_threads();
  156. return error;
  157. }
  158. void thaw_processes(void)
  159. {
  160. struct task_struct *g, *p;
  161. struct task_struct *curr = current;
  162. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  163. if (pm_freezing)
  164. static_branch_dec(&freezer_active);
  165. pm_freezing = false;
  166. pm_nosig_freezing = false;
  167. oom_killer_enable();
  168. pr_info("Restarting tasks ... ");
  169. __usermodehelper_set_disable_depth(UMH_FREEZING);
  170. thaw_workqueues();
  171. cpuset_wait_for_hotplug();
  172. read_lock(&tasklist_lock);
  173. for_each_process_thread(g, p) {
  174. /* No other threads should have PF_SUSPEND_TASK set */
  175. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  176. __thaw_task(p);
  177. }
  178. read_unlock(&tasklist_lock);
  179. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  180. curr->flags &= ~PF_SUSPEND_TASK;
  181. usermodehelper_enable();
  182. schedule();
  183. pr_cont("done.\n");
  184. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  185. }
  186. void thaw_kernel_threads(void)
  187. {
  188. struct task_struct *g, *p;
  189. pm_nosig_freezing = false;
  190. pr_info("Restarting kernel threads ... ");
  191. thaw_workqueues();
  192. read_lock(&tasklist_lock);
  193. for_each_process_thread(g, p) {
  194. if (p->flags & PF_KTHREAD)
  195. __thaw_task(p);
  196. }
  197. read_unlock(&tasklist_lock);
  198. schedule();
  199. pr_cont("done.\n");
  200. }