process.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /*
  18. * Timeout for stopping processes
  19. */
  20. #define TIMEOUT (20 * HZ)
  21. static int try_to_freeze_tasks(bool user_only)
  22. {
  23. struct task_struct *g, *p;
  24. unsigned long end_time;
  25. unsigned int todo;
  26. bool wq_busy = false;
  27. struct timeval start, end;
  28. u64 elapsed_csecs64;
  29. unsigned int elapsed_csecs;
  30. bool wakeup = false;
  31. do_gettimeofday(&start);
  32. end_time = jiffies + TIMEOUT;
  33. if (!user_only)
  34. freeze_workqueues_begin();
  35. while (true) {
  36. todo = 0;
  37. read_lock(&tasklist_lock);
  38. do_each_thread(g, p) {
  39. if (p == current || !freeze_task(p))
  40. continue;
  41. /*
  42. * Now that we've done set_freeze_flag, don't
  43. * perturb a task in TASK_STOPPED or TASK_TRACED.
  44. * It is "frozen enough". If the task does wake
  45. * up, it will immediately call try_to_freeze.
  46. *
  47. * Because freeze_task() goes through p's scheduler lock, it's
  48. * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING
  49. * transition can't race with task state testing here.
  50. */
  51. if (!task_is_stopped_or_traced(p) &&
  52. !freezer_should_skip(p))
  53. todo++;
  54. } while_each_thread(g, p);
  55. read_unlock(&tasklist_lock);
  56. if (!user_only) {
  57. wq_busy = freeze_workqueues_busy();
  58. todo += wq_busy;
  59. }
  60. if (!todo || time_after(jiffies, end_time))
  61. break;
  62. if (pm_wakeup_pending()) {
  63. wakeup = true;
  64. break;
  65. }
  66. /*
  67. * We need to retry, but first give the freezing tasks some
  68. * time to enter the regrigerator.
  69. */
  70. msleep(10);
  71. }
  72. do_gettimeofday(&end);
  73. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  74. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  75. elapsed_csecs = elapsed_csecs64;
  76. if (todo) {
  77. printk("\n");
  78. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  79. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  80. wakeup ? "aborted" : "failed",
  81. elapsed_csecs / 100, elapsed_csecs % 100,
  82. todo - wq_busy, wq_busy);
  83. if (!wakeup) {
  84. read_lock(&tasklist_lock);
  85. do_each_thread(g, p) {
  86. if (p != current && !freezer_should_skip(p)
  87. && freezing(p) && !frozen(p))
  88. sched_show_task(p);
  89. } while_each_thread(g, p);
  90. read_unlock(&tasklist_lock);
  91. }
  92. } else {
  93. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  94. elapsed_csecs % 100);
  95. }
  96. return todo ? -EBUSY : 0;
  97. }
  98. /**
  99. * freeze_processes - Signal user space processes to enter the refrigerator.
  100. *
  101. * On success, returns 0. On failure, -errno and system is fully thawed.
  102. */
  103. int freeze_processes(void)
  104. {
  105. int error;
  106. error = __usermodehelper_disable(UMH_FREEZING);
  107. if (error)
  108. return error;
  109. if (!pm_freezing)
  110. atomic_inc(&system_freezing_cnt);
  111. printk("Freezing user space processes ... ");
  112. pm_freezing = true;
  113. error = try_to_freeze_tasks(true);
  114. if (!error) {
  115. printk("done.");
  116. __usermodehelper_set_disable_depth(UMH_DISABLED);
  117. oom_killer_disable();
  118. }
  119. printk("\n");
  120. BUG_ON(in_atomic());
  121. if (error)
  122. thaw_processes();
  123. return error;
  124. }
  125. /**
  126. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  127. *
  128. * On success, returns 0. On failure, -errno and only the kernel threads are
  129. * thawed, so as to give a chance to the caller to do additional cleanups
  130. * (if any) before thawing the userspace tasks. So, it is the responsibility
  131. * of the caller to thaw the userspace tasks, when the time is right.
  132. */
  133. int freeze_kernel_threads(void)
  134. {
  135. int error;
  136. printk("Freezing remaining freezable tasks ... ");
  137. pm_nosig_freezing = true;
  138. error = try_to_freeze_tasks(false);
  139. if (!error)
  140. printk("done.");
  141. printk("\n");
  142. BUG_ON(in_atomic());
  143. if (error)
  144. thaw_kernel_threads();
  145. return error;
  146. }
  147. void thaw_processes(void)
  148. {
  149. struct task_struct *g, *p;
  150. if (pm_freezing)
  151. atomic_dec(&system_freezing_cnt);
  152. pm_freezing = false;
  153. pm_nosig_freezing = false;
  154. oom_killer_enable();
  155. printk("Restarting tasks ... ");
  156. thaw_workqueues();
  157. read_lock(&tasklist_lock);
  158. do_each_thread(g, p) {
  159. __thaw_task(p);
  160. } while_each_thread(g, p);
  161. read_unlock(&tasklist_lock);
  162. usermodehelper_enable();
  163. schedule();
  164. printk("done.\n");
  165. }
  166. void thaw_kernel_threads(void)
  167. {
  168. struct task_struct *g, *p;
  169. pm_nosig_freezing = false;
  170. printk("Restarting kernel threads ... ");
  171. thaw_workqueues();
  172. read_lock(&tasklist_lock);
  173. do_each_thread(g, p) {
  174. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  175. __thaw_task(p);
  176. } while_each_thread(g, p);
  177. read_unlock(&tasklist_lock);
  178. schedule();
  179. printk("done.\n");
  180. }