process.c 4.5 KB

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