process.c 5.5 KB

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