process.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /*
  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. struct task_struct *g, *p;
  29. unsigned long end_time;
  30. unsigned int todo;
  31. bool wq_busy = false;
  32. ktime_t start, end, elapsed;
  33. unsigned int elapsed_msecs;
  34. bool wakeup = false;
  35. int sleep_usecs = USEC_PER_MSEC;
  36. start = ktime_get_boottime();
  37. end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
  38. if (!user_only)
  39. freeze_workqueues_begin();
  40. while (true) {
  41. todo = 0;
  42. read_lock(&tasklist_lock);
  43. for_each_process_thread(g, p) {
  44. if (p == current || !freeze_task(p))
  45. continue;
  46. if (!freezer_should_skip(p))
  47. todo++;
  48. }
  49. read_unlock(&tasklist_lock);
  50. if (!user_only) {
  51. wq_busy = freeze_workqueues_busy();
  52. todo += wq_busy;
  53. }
  54. if (!todo || time_after(jiffies, end_time))
  55. break;
  56. if (pm_wakeup_pending()) {
  57. wakeup = true;
  58. break;
  59. }
  60. /*
  61. * We need to retry, but first give the freezing tasks some
  62. * time to enter the refrigerator. Start with an initial
  63. * 1 ms sleep followed by exponential backoff until 8 ms.
  64. */
  65. usleep_range(sleep_usecs / 2, sleep_usecs);
  66. if (sleep_usecs < 8 * USEC_PER_MSEC)
  67. sleep_usecs *= 2;
  68. }
  69. end = ktime_get_boottime();
  70. elapsed = ktime_sub(end, start);
  71. elapsed_msecs = ktime_to_ms(elapsed);
  72. if (todo) {
  73. pr_cont("\n");
  74. pr_err("Freezing of tasks %s after %d.%03d seconds "
  75. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  76. wakeup ? "aborted" : "failed",
  77. elapsed_msecs / 1000, elapsed_msecs % 1000,
  78. todo - wq_busy, wq_busy);
  79. if (wq_busy)
  80. show_workqueue_state();
  81. if (!wakeup) {
  82. read_lock(&tasklist_lock);
  83. for_each_process_thread(g, p) {
  84. if (p != current && !freezer_should_skip(p)
  85. && freezing(p) && !frozen(p))
  86. sched_show_task(p);
  87. }
  88. read_unlock(&tasklist_lock);
  89. }
  90. } else {
  91. pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  92. elapsed_msecs % 1000);
  93. }
  94. return todo ? -EBUSY : 0;
  95. }
  96. /**
  97. * freeze_processes - Signal user space processes to enter the refrigerator.
  98. * The current thread will not be frozen. The same process that calls
  99. * freeze_processes must later call thaw_processes.
  100. *
  101. * On success, returns 0. On failure, -errno and system is fully thawed.
  102. */
  103. int freeze_processes(void)
  104. {
  105. int error;
  106. error = __usermodehelper_disable(UMH_FREEZING);
  107. if (error)
  108. return error;
  109. /* Make sure this task doesn't get frozen */
  110. current->flags |= PF_SUSPEND_TASK;
  111. if (!pm_freezing)
  112. atomic_inc(&system_freezing_cnt);
  113. pm_wakeup_clear(true);
  114. pr_info("Freezing user space processes ... ");
  115. pm_freezing = true;
  116. error = try_to_freeze_tasks(true);
  117. if (!error) {
  118. __usermodehelper_set_disable_depth(UMH_DISABLED);
  119. pr_cont("done.");
  120. }
  121. pr_cont("\n");
  122. BUG_ON(in_atomic());
  123. /*
  124. * Now that the whole userspace is frozen we need to disbale
  125. * the OOM killer to disallow any further interference with
  126. * killable tasks. There is no guarantee oom victims will
  127. * ever reach a point they go away we have to wait with a timeout.
  128. */
  129. if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
  130. error = -EBUSY;
  131. if (error)
  132. thaw_processes();
  133. return error;
  134. }
  135. /**
  136. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  137. *
  138. * On success, returns 0. On failure, -errno and only the kernel threads are
  139. * thawed, so as to give a chance to the caller to do additional cleanups
  140. * (if any) before thawing the userspace tasks. So, it is the responsibility
  141. * of the caller to thaw the userspace tasks, when the time is right.
  142. */
  143. int freeze_kernel_threads(void)
  144. {
  145. int error;
  146. pr_info("Freezing remaining freezable tasks ... ");
  147. pm_nosig_freezing = true;
  148. error = try_to_freeze_tasks(false);
  149. if (!error)
  150. pr_cont("done.");
  151. pr_cont("\n");
  152. BUG_ON(in_atomic());
  153. if (error)
  154. thaw_kernel_threads();
  155. return error;
  156. }
  157. void thaw_processes(void)
  158. {
  159. struct task_struct *g, *p;
  160. struct task_struct *curr = current;
  161. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  162. if (pm_freezing)
  163. atomic_dec(&system_freezing_cnt);
  164. pm_freezing = false;
  165. pm_nosig_freezing = false;
  166. oom_killer_enable();
  167. pr_info("Restarting tasks ... ");
  168. __usermodehelper_set_disable_depth(UMH_FREEZING);
  169. thaw_workqueues();
  170. cpuset_wait_for_hotplug();
  171. read_lock(&tasklist_lock);
  172. for_each_process_thread(g, p) {
  173. /* No other threads should have PF_SUSPEND_TASK set */
  174. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  175. __thaw_task(p);
  176. }
  177. read_unlock(&tasklist_lock);
  178. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  179. curr->flags &= ~PF_SUSPEND_TASK;
  180. usermodehelper_enable();
  181. schedule();
  182. pr_cont("done.\n");
  183. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  184. }
  185. void thaw_kernel_threads(void)
  186. {
  187. struct task_struct *g, *p;
  188. pm_nosig_freezing = false;
  189. pr_info("Restarting kernel threads ... ");
  190. thaw_workqueues();
  191. read_lock(&tasklist_lock);
  192. for_each_process_thread(g, p) {
  193. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  194. __thaw_task(p);
  195. }
  196. read_unlock(&tasklist_lock);
  197. schedule();
  198. pr_cont("done.\n");
  199. }