process.c 5.6 KB

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