process.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. struct timeval start, end;
  29. u64 elapsed_msecs64;
  30. unsigned int elapsed_msecs;
  31. bool wakeup = false;
  32. int sleep_usecs = USEC_PER_MSEC;
  33. do_gettimeofday(&start);
  34. end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
  35. if (!user_only)
  36. freeze_workqueues_begin();
  37. while (true) {
  38. todo = 0;
  39. read_lock(&tasklist_lock);
  40. for_each_process_thread(g, p) {
  41. if (p == current || !freeze_task(p))
  42. continue;
  43. if (!freezer_should_skip(p))
  44. todo++;
  45. }
  46. read_unlock(&tasklist_lock);
  47. if (!user_only) {
  48. wq_busy = freeze_workqueues_busy();
  49. todo += wq_busy;
  50. }
  51. if (!todo || time_after(jiffies, end_time))
  52. break;
  53. if (pm_wakeup_pending()) {
  54. wakeup = true;
  55. break;
  56. }
  57. /*
  58. * We need to retry, but first give the freezing tasks some
  59. * time to enter the refrigerator. Start with an initial
  60. * 1 ms sleep followed by exponential backoff until 8 ms.
  61. */
  62. usleep_range(sleep_usecs / 2, sleep_usecs);
  63. if (sleep_usecs < 8 * USEC_PER_MSEC)
  64. sleep_usecs *= 2;
  65. }
  66. do_gettimeofday(&end);
  67. elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  68. do_div(elapsed_msecs64, NSEC_PER_MSEC);
  69. elapsed_msecs = elapsed_msecs64;
  70. if (todo) {
  71. printk("\n");
  72. printk(KERN_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 (!wakeup) {
  78. read_lock(&tasklist_lock);
  79. for_each_process_thread(g, p) {
  80. if (p != current && !freezer_should_skip(p)
  81. && freezing(p) && !frozen(p))
  82. sched_show_task(p);
  83. }
  84. read_unlock(&tasklist_lock);
  85. }
  86. } else {
  87. printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  88. elapsed_msecs % 1000);
  89. }
  90. return todo ? -EBUSY : 0;
  91. }
  92. static bool __check_frozen_processes(void)
  93. {
  94. struct task_struct *g, *p;
  95. for_each_process_thread(g, p)
  96. if (p != current && !freezer_should_skip(p) && !frozen(p))
  97. return false;
  98. return true;
  99. }
  100. /*
  101. * Returns true if all freezable tasks (except for current) are frozen already
  102. */
  103. static bool check_frozen_processes(void)
  104. {
  105. bool ret;
  106. read_lock(&tasklist_lock);
  107. ret = __check_frozen_processes();
  108. read_unlock(&tasklist_lock);
  109. return ret;
  110. }
  111. /**
  112. * freeze_processes - Signal user space processes to enter the refrigerator.
  113. * The current thread will not be frozen. The same process that calls
  114. * freeze_processes must later call thaw_processes.
  115. *
  116. * On success, returns 0. On failure, -errno and system is fully thawed.
  117. */
  118. int freeze_processes(void)
  119. {
  120. int error;
  121. int oom_kills_saved;
  122. error = __usermodehelper_disable(UMH_FREEZING);
  123. if (error)
  124. return error;
  125. /* Make sure this task doesn't get frozen */
  126. current->flags |= PF_SUSPEND_TASK;
  127. if (!pm_freezing)
  128. atomic_inc(&system_freezing_cnt);
  129. pm_wakeup_clear();
  130. printk("Freezing user space processes ... ");
  131. pm_freezing = true;
  132. oom_kills_saved = oom_kills_count();
  133. error = try_to_freeze_tasks(true);
  134. if (!error) {
  135. __usermodehelper_set_disable_depth(UMH_DISABLED);
  136. oom_killer_disable();
  137. /*
  138. * There might have been an OOM kill while we were
  139. * freezing tasks and the killed task might be still
  140. * on the way out so we have to double check for race.
  141. */
  142. if (oom_kills_count() != oom_kills_saved &&
  143. !check_frozen_processes()) {
  144. __usermodehelper_set_disable_depth(UMH_ENABLED);
  145. printk("OOM in progress.");
  146. error = -EBUSY;
  147. } else {
  148. printk("done.");
  149. }
  150. }
  151. printk("\n");
  152. BUG_ON(in_atomic());
  153. if (error)
  154. thaw_processes();
  155. return error;
  156. }
  157. /**
  158. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  159. *
  160. * On success, returns 0. On failure, -errno and only the kernel threads are
  161. * thawed, so as to give a chance to the caller to do additional cleanups
  162. * (if any) before thawing the userspace tasks. So, it is the responsibility
  163. * of the caller to thaw the userspace tasks, when the time is right.
  164. */
  165. int freeze_kernel_threads(void)
  166. {
  167. int error;
  168. printk("Freezing remaining freezable tasks ... ");
  169. pm_nosig_freezing = true;
  170. error = try_to_freeze_tasks(false);
  171. if (!error)
  172. printk("done.");
  173. printk("\n");
  174. BUG_ON(in_atomic());
  175. if (error)
  176. thaw_kernel_threads();
  177. return error;
  178. }
  179. void thaw_processes(void)
  180. {
  181. struct task_struct *g, *p;
  182. struct task_struct *curr = current;
  183. trace_suspend_resume(TPS("thaw_processes"), 0, true);
  184. if (pm_freezing)
  185. atomic_dec(&system_freezing_cnt);
  186. pm_freezing = false;
  187. pm_nosig_freezing = false;
  188. oom_killer_enable();
  189. printk("Restarting tasks ... ");
  190. __usermodehelper_set_disable_depth(UMH_FREEZING);
  191. thaw_workqueues();
  192. read_lock(&tasklist_lock);
  193. for_each_process_thread(g, p) {
  194. /* No other threads should have PF_SUSPEND_TASK set */
  195. WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
  196. __thaw_task(p);
  197. }
  198. read_unlock(&tasklist_lock);
  199. WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
  200. curr->flags &= ~PF_SUSPEND_TASK;
  201. usermodehelper_enable();
  202. schedule();
  203. printk("done.\n");
  204. trace_suspend_resume(TPS("thaw_processes"), 0, false);
  205. }
  206. void thaw_kernel_threads(void)
  207. {
  208. struct task_struct *g, *p;
  209. pm_nosig_freezing = false;
  210. printk("Restarting kernel threads ... ");
  211. thaw_workqueues();
  212. read_lock(&tasklist_lock);
  213. for_each_process_thread(g, p) {
  214. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  215. __thaw_task(p);
  216. }
  217. read_unlock(&tasklist_lock);
  218. schedule();
  219. printk("done.\n");
  220. }