process.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <linux/wakelock.h>
  18. #include "power.h"
  19. /*
  20. * Timeout for stopping processes
  21. */
  22. #define TIMEOUT (20 * HZ)
  23. static int try_to_freeze_tasks(bool user_only)
  24. {
  25. struct task_struct *g, *p;
  26. struct task_struct *q = NULL;
  27. unsigned long end_time;
  28. unsigned int todo;
  29. bool wq_busy = false;
  30. struct timeval start, end;
  31. u64 elapsed_msecs64;
  32. unsigned int elapsed_msecs;
  33. bool wakeup = false;
  34. int sleep_usecs = USEC_PER_MSEC;
  35. do_gettimeofday(&start);
  36. end_time = jiffies + TIMEOUT;
  37. if (!user_only)
  38. freeze_workqueues_begin();
  39. while (true) {
  40. todo = 0;
  41. read_lock(&tasklist_lock);
  42. do_each_thread(g, p) {
  43. if (p == current || !freeze_task(p))
  44. continue;
  45. /*
  46. * Now that we've done set_freeze_flag, don't
  47. * perturb a task in TASK_STOPPED or TASK_TRACED.
  48. * It is "frozen enough". If the task does wake
  49. * up, it will immediately call try_to_freeze.
  50. *
  51. * Because freeze_task() goes through p's scheduler lock, it's
  52. * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING
  53. * transition can't race with task state testing here.
  54. */
  55. if (!task_is_stopped_or_traced(p) &&
  56. !freezer_should_skip(p)) {
  57. todo++;
  58. q = p;
  59. }
  60. } while_each_thread(g, p);
  61. read_unlock(&tasklist_lock);
  62. if (!user_only) {
  63. wq_busy = freeze_workqueues_busy();
  64. todo += wq_busy;
  65. }
  66. if (!todo || time_after(jiffies, end_time))
  67. break;
  68. if (pm_wakeup_pending()) {
  69. wakeup = true;
  70. break;
  71. }
  72. /*
  73. * We need to retry, but first give the freezing tasks some
  74. * time to enter the refrigerator. Start with an initial
  75. * 1 ms sleep followed by exponential backoff until 8 ms.
  76. */
  77. usleep_range(sleep_usecs / 2, sleep_usecs);
  78. if (sleep_usecs < 8 * USEC_PER_MSEC)
  79. sleep_usecs *= 2;
  80. }
  81. do_gettimeofday(&end);
  82. elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  83. do_div(elapsed_msecs64, NSEC_PER_MSEC);
  84. elapsed_msecs = elapsed_msecs64;
  85. if (todo) {
  86. /* This does not unfreeze processes that are already frozen
  87. * (we have slightly ugly calling convention in that respect,
  88. * and caller must call thaw_processes() if something fails),
  89. * but it cleans up leftover PF_FREEZE requests.
  90. */
  91. if(wakeup) {
  92. printk("\n");
  93. printk(KERN_ERR "Freezing of %s aborted (%d) (%s)\n",
  94. user_only ? "user space " : "tasks ",
  95. q ? q->pid : 0, q ? q->comm : "NONE");
  96. }
  97. else {
  98. printk("\n");
  99. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  100. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  101. wakeup ? "aborted" : "failed",
  102. elapsed_msecs / 1000, elapsed_msecs % 1000,
  103. todo - wq_busy, wq_busy);
  104. }
  105. if (!wakeup) {
  106. read_lock(&tasklist_lock);
  107. do_each_thread(g, p) {
  108. if (p != current && !freezer_should_skip(p)
  109. && freezing(p) && !frozen(p) &&
  110. elapsed_msecs > 1000)
  111. sched_show_task(p);
  112. } while_each_thread(g, p);
  113. read_unlock(&tasklist_lock);
  114. }
  115. } else {
  116. printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  117. elapsed_msecs % 1000);
  118. }
  119. return todo ? -EBUSY : 0;
  120. }
  121. /*
  122. * Returns true if all freezable tasks (except for current) are frozen already
  123. */
  124. static bool check_frozen_processes(void)
  125. {
  126. struct task_struct *g, *p;
  127. bool ret = true;
  128. read_lock(&tasklist_lock);
  129. for_each_process_thread(g, p) {
  130. if (p != current && !freezer_should_skip(p) &&
  131. !frozen(p)) {
  132. ret = false;
  133. goto done;
  134. }
  135. }
  136. done:
  137. read_unlock(&tasklist_lock);
  138. return ret;
  139. }
  140. /**
  141. * freeze_processes - Signal user space processes to enter the refrigerator.
  142. *
  143. * On success, returns 0. On failure, -errno and system is fully thawed.
  144. */
  145. int freeze_processes(void)
  146. {
  147. int error;
  148. int oom_kills_saved;
  149. error = __usermodehelper_disable(UMH_FREEZING);
  150. if (error)
  151. return error;
  152. if (!pm_freezing)
  153. atomic_inc(&system_freezing_cnt);
  154. printk("Freezing user space processes ... ");
  155. pm_freezing = true;
  156. oom_kills_saved = oom_kills_count();
  157. error = try_to_freeze_tasks(true);
  158. if (!error) {
  159. __usermodehelper_set_disable_depth(UMH_DISABLED);
  160. oom_killer_disable();
  161. /*
  162. * There might have been an OOM kill while we were
  163. * freezing tasks and the killed task might be still
  164. * on the way out so we have to double check for race.
  165. */
  166. if (oom_kills_count() != oom_kills_saved &&
  167. !check_frozen_processes()) {
  168. __usermodehelper_set_disable_depth(UMH_ENABLED);
  169. printk("OOM in progress.");
  170. error = -EBUSY;
  171. goto done;
  172. }
  173. printk("done.");
  174. }
  175. done:
  176. printk("\n");
  177. BUG_ON(in_atomic());
  178. if (error)
  179. thaw_processes();
  180. return error;
  181. }
  182. /**
  183. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  184. *
  185. * On success, returns 0. On failure, -errno and only the kernel threads are
  186. * thawed, so as to give a chance to the caller to do additional cleanups
  187. * (if any) before thawing the userspace tasks. So, it is the responsibility
  188. * of the caller to thaw the userspace tasks, when the time is right.
  189. */
  190. int freeze_kernel_threads(void)
  191. {
  192. int error;
  193. printk("Freezing remaining freezable tasks ... ");
  194. pm_nosig_freezing = true;
  195. error = try_to_freeze_tasks(false);
  196. if (!error)
  197. printk("done.");
  198. printk("\n");
  199. BUG_ON(in_atomic());
  200. if (error)
  201. thaw_kernel_threads();
  202. return error;
  203. }
  204. void thaw_processes(void)
  205. {
  206. struct task_struct *g, *p;
  207. if (pm_freezing)
  208. atomic_dec(&system_freezing_cnt);
  209. pm_freezing = false;
  210. pm_nosig_freezing = false;
  211. oom_killer_enable();
  212. printk("Restarting tasks ... ");
  213. __usermodehelper_set_disable_depth(UMH_FREEZING);
  214. thaw_workqueues();
  215. read_lock(&tasklist_lock);
  216. do_each_thread(g, p) {
  217. __thaw_task(p);
  218. } while_each_thread(g, p);
  219. read_unlock(&tasklist_lock);
  220. usermodehelper_enable();
  221. schedule();
  222. printk("done.\n");
  223. }
  224. void thaw_kernel_threads(void)
  225. {
  226. struct task_struct *g, *p;
  227. pm_nosig_freezing = false;
  228. printk("Restarting kernel threads ... ");
  229. thaw_workqueues();
  230. read_lock(&tasklist_lock);
  231. do_each_thread(g, p) {
  232. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  233. __thaw_task(p);
  234. } while_each_thread(g, p);
  235. read_unlock(&tasklist_lock);
  236. schedule();
  237. printk("done.\n");
  238. }