process.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /*
  22. * Timeout for stopping processes
  23. */
  24. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  25. static int try_to_freeze_tasks(bool user_only)
  26. {
  27. const char *what = user_only ? "user space processes" :
  28. "remaining freezable tasks";
  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. pr_info("Freezing %s\n", what);
  38. start = ktime_get_boottime();
  39. end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
  40. if (!user_only)
  41. freeze_workqueues_begin();
  42. while (true) {
  43. todo = 0;
  44. read_lock(&tasklist_lock);
  45. for_each_process_thread(g, p) {
  46. if (p == current || !freeze_task(p))
  47. continue;
  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 (todo) {
  74. pr_err("Freezing %s %s after %d.%03d seconds "
  75. "(%d tasks refusing to freeze, wq_busy=%d):\n", what,
  76. wakeup ? "aborted" : "failed",
  77. elapsed_msecs / 1000, elapsed_msecs % 1000,
  78. todo - wq_busy, wq_busy);
  79. if (wq_busy)
  80. show_freezable_workqueues();
  81. if (!wakeup || pm_debug_messages_on) {
  82. read_lock(&tasklist_lock);
  83. for_each_process_thread(g, p) {
  84. if (p != current && freezing(p) && !frozen(p))
  85. sched_show_task(p);
  86. }
  87. read_unlock(&tasklist_lock);
  88. }
  89. } else {
  90. pr_info("Freezing %s completed (elapsed %d.%03d seconds)\n",
  91. what, elapsed_msecs / 1000, elapsed_msecs % 1000);
  92. }
  93. return todo ? -EBUSY : 0;
  94. }
  95. /**
  96. * freeze_processes - Signal user space processes to enter the refrigerator.
  97. * The current thread will not be frozen. The same process that calls
  98. * freeze_processes must later call thaw_processes.
  99. *
  100. * On success, returns 0. On failure, -errno and system is fully thawed.
  101. */
  102. int freeze_processes(void)
  103. {
  104. int error;
  105. error = __usermodehelper_disable(UMH_FREEZING);
  106. if (error)
  107. return error;
  108. /* Make sure this task doesn't get frozen */
  109. current->flags |= PF_SUSPEND_TASK;
  110. if (!pm_freezing)
  111. static_branch_inc(&freezer_active);
  112. pm_wakeup_clear(0);
  113. pm_freezing = true;
  114. error = try_to_freeze_tasks(true);
  115. if (!error)
  116. __usermodehelper_set_disable_depth(UMH_DISABLED);
  117. BUG_ON(in_atomic());
  118. /*
  119. * Now that the whole userspace is frozen we need to disable
  120. * the OOM killer to disallow any further interference with
  121. * killable tasks. There is no guarantee oom victims will
  122. * ever reach a point they go away we have to wait with a timeout.
  123. */
  124. if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
  125. error = -EBUSY;
  126. if (error)
  127. thaw_processes();
  128. return error;
  129. }
  130. /**
  131. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  132. *
  133. * On success, returns 0. On failure, -errno and only the kernel threads are
  134. * thawed, so as to give a chance to the caller to do additional cleanups
  135. * (if any) before thawing the userspace tasks. So, it is the responsibility
  136. * of the caller to thaw the userspace tasks, when the time is right.
  137. */
  138. int freeze_kernel_threads(void)
  139. {
  140. int error;
  141. pm_nosig_freezing = true;
  142. error = try_to_freeze_tasks(false);
  143. BUG_ON(in_atomic());
  144. if (error)
  145. thaw_kernel_threads();
  146. return error;
  147. }
  148. void thaw_processes(void)
  149. {
  150. struct task_struct *g, *p;
  151. struct task_struct *curr = current;
  152. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  153. if (pm_freezing)
  154. static_branch_dec(&freezer_active);
  155. pm_freezing = false;
  156. pm_nosig_freezing = false;
  157. oom_killer_enable();
  158. pr_info("Restarting tasks ... ");
  159. __usermodehelper_set_disable_depth(UMH_FREEZING);
  160. thaw_workqueues();
  161. read_lock(&tasklist_lock);
  162. for_each_process_thread(g, p) {
  163. /* No other threads should have PF_SUSPEND_TASK set */
  164. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  165. __thaw_task(p);
  166. }
  167. read_unlock(&tasklist_lock);
  168. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  169. curr->flags &= ~PF_SUSPEND_TASK;
  170. usermodehelper_enable();
  171. schedule();
  172. pr_cont("done.\n");
  173. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  174. }
  175. void thaw_kernel_threads(void)
  176. {
  177. struct task_struct *g, *p;
  178. pm_nosig_freezing = false;
  179. pr_info("Restarting kernel threads ... ");
  180. thaw_workqueues();
  181. read_lock(&tasklist_lock);
  182. for_each_process_thread(g, p) {
  183. if (p->flags & PF_KTHREAD)
  184. __thaw_task(p);
  185. }
  186. read_unlock(&tasklist_lock);
  187. schedule();
  188. pr_cont("done.\n");
  189. }