process.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
  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_msecs64;
  29. unsigned int elapsed_msecs;
  30. bool wakeup = false;
  31. int sleep_usecs = USEC_PER_MSEC;
  32. do_gettimeofday(&start);
  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. do_each_thread(g, p) {
  40. if (p == current || !freeze_task(p))
  41. continue;
  42. if (!freezer_should_skip(p))
  43. todo++;
  44. } while_each_thread(g, p);
  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. do_gettimeofday(&end);
  66. elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  67. do_div(elapsed_msecs64, NSEC_PER_MSEC);
  68. elapsed_msecs = elapsed_msecs64;
  69. if (todo) {
  70. printk("\n");
  71. printk(KERN_ERR "Freezing of tasks %s after %d.%03d seconds "
  72. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  73. wakeup ? "aborted" : "failed",
  74. elapsed_msecs / 1000, elapsed_msecs % 1000,
  75. todo - wq_busy, wq_busy);
  76. if (!wakeup) {
  77. read_lock(&tasklist_lock);
  78. do_each_thread(g, p) {
  79. if (p != current && !freezer_should_skip(p)
  80. && freezing(p) && !frozen(p))
  81. sched_show_task(p);
  82. } while_each_thread(g, p);
  83. read_unlock(&tasklist_lock);
  84. }
  85. } else {
  86. printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
  87. elapsed_msecs % 1000);
  88. }
  89. return todo ? -EBUSY : 0;
  90. }
  91. /**
  92. * freeze_processes - Signal user space processes to enter the refrigerator.
  93. *
  94. * On success, returns 0. On failure, -errno and system is fully thawed.
  95. */
  96. int freeze_processes(void)
  97. {
  98. int error;
  99. error = __usermodehelper_disable(UMH_FREEZING);
  100. if (error)
  101. return error;
  102. if (!pm_freezing)
  103. atomic_inc(&system_freezing_cnt);
  104. printk("Freezing user space processes ... ");
  105. pm_freezing = true;
  106. error = try_to_freeze_tasks(true);
  107. if (!error) {
  108. printk("done.");
  109. __usermodehelper_set_disable_depth(UMH_DISABLED);
  110. oom_killer_disable();
  111. }
  112. printk("\n");
  113. BUG_ON(in_atomic());
  114. if (error)
  115. thaw_processes();
  116. return error;
  117. }
  118. /**
  119. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  120. *
  121. * On success, returns 0. On failure, -errno and only the kernel threads are
  122. * thawed, so as to give a chance to the caller to do additional cleanups
  123. * (if any) before thawing the userspace tasks. So, it is the responsibility
  124. * of the caller to thaw the userspace tasks, when the time is right.
  125. */
  126. int freeze_kernel_threads(void)
  127. {
  128. int error;
  129. printk("Freezing remaining freezable tasks ... ");
  130. pm_nosig_freezing = true;
  131. error = try_to_freeze_tasks(false);
  132. if (!error)
  133. printk("done.");
  134. printk("\n");
  135. BUG_ON(in_atomic());
  136. if (error)
  137. thaw_kernel_threads();
  138. return error;
  139. }
  140. void thaw_processes(void)
  141. {
  142. struct task_struct *g, *p;
  143. if (pm_freezing)
  144. atomic_dec(&system_freezing_cnt);
  145. pm_freezing = false;
  146. pm_nosig_freezing = false;
  147. oom_killer_enable();
  148. printk("Restarting tasks ... ");
  149. thaw_workqueues();
  150. read_lock(&tasklist_lock);
  151. do_each_thread(g, p) {
  152. __thaw_task(p);
  153. } while_each_thread(g, p);
  154. read_unlock(&tasklist_lock);
  155. usermodehelper_enable();
  156. schedule();
  157. printk("done.\n");
  158. }
  159. void thaw_kernel_threads(void)
  160. {
  161. struct task_struct *g, *p;
  162. pm_nosig_freezing = false;
  163. printk("Restarting kernel threads ... ");
  164. thaw_workqueues();
  165. read_lock(&tasklist_lock);
  166. do_each_thread(g, p) {
  167. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  168. __thaw_task(p);
  169. } while_each_thread(g, p);
  170. read_unlock(&tasklist_lock);
  171. schedule();
  172. printk("done.\n");
  173. }