process.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. if (!freezer_should_skip(p))
  42. todo++;
  43. } while_each_thread(g, p);
  44. read_unlock(&tasklist_lock);
  45. if (!user_only) {
  46. wq_busy = freeze_workqueues_busy();
  47. todo += wq_busy;
  48. }
  49. if (!todo || time_after(jiffies, end_time))
  50. break;
  51. if (pm_wakeup_pending()) {
  52. wakeup = true;
  53. break;
  54. }
  55. /*
  56. * We need to retry, but first give the freezing tasks some
  57. * time to enter the refrigerator.
  58. */
  59. msleep(10);
  60. }
  61. do_gettimeofday(&end);
  62. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  63. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  64. elapsed_csecs = elapsed_csecs64;
  65. if (todo) {
  66. printk("\n");
  67. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  68. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  69. wakeup ? "aborted" : "failed",
  70. elapsed_csecs / 100, elapsed_csecs % 100,
  71. todo - wq_busy, wq_busy);
  72. if (!wakeup) {
  73. read_lock(&tasklist_lock);
  74. do_each_thread(g, p) {
  75. if (p != current && !freezer_should_skip(p)
  76. && freezing(p) && !frozen(p))
  77. sched_show_task(p);
  78. } while_each_thread(g, p);
  79. read_unlock(&tasklist_lock);
  80. }
  81. } else {
  82. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  83. elapsed_csecs % 100);
  84. }
  85. return todo ? -EBUSY : 0;
  86. }
  87. /**
  88. * freeze_processes - Signal user space processes to enter the refrigerator.
  89. *
  90. * On success, returns 0. On failure, -errno and system is fully thawed.
  91. */
  92. int freeze_processes(void)
  93. {
  94. int error;
  95. error = __usermodehelper_disable(UMH_FREEZING);
  96. if (error)
  97. return error;
  98. if (!pm_freezing)
  99. atomic_inc(&system_freezing_cnt);
  100. printk("Freezing user space processes ... ");
  101. pm_freezing = true;
  102. error = try_to_freeze_tasks(true);
  103. if (!error) {
  104. printk("done.");
  105. __usermodehelper_set_disable_depth(UMH_DISABLED);
  106. oom_killer_disable();
  107. }
  108. printk("\n");
  109. BUG_ON(in_atomic());
  110. if (error)
  111. thaw_processes();
  112. return error;
  113. }
  114. /**
  115. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  116. *
  117. * On success, returns 0. On failure, -errno and only the kernel threads are
  118. * thawed, so as to give a chance to the caller to do additional cleanups
  119. * (if any) before thawing the userspace tasks. So, it is the responsibility
  120. * of the caller to thaw the userspace tasks, when the time is right.
  121. */
  122. int freeze_kernel_threads(void)
  123. {
  124. int error;
  125. printk("Freezing remaining freezable tasks ... ");
  126. pm_nosig_freezing = true;
  127. error = try_to_freeze_tasks(false);
  128. if (!error)
  129. printk("done.");
  130. printk("\n");
  131. BUG_ON(in_atomic());
  132. if (error)
  133. thaw_kernel_threads();
  134. return error;
  135. }
  136. void thaw_processes(void)
  137. {
  138. struct task_struct *g, *p;
  139. if (pm_freezing)
  140. atomic_dec(&system_freezing_cnt);
  141. pm_freezing = false;
  142. pm_nosig_freezing = false;
  143. oom_killer_enable();
  144. printk("Restarting tasks ... ");
  145. thaw_workqueues();
  146. read_lock(&tasklist_lock);
  147. do_each_thread(g, p) {
  148. __thaw_task(p);
  149. } while_each_thread(g, p);
  150. read_unlock(&tasklist_lock);
  151. usermodehelper_enable();
  152. schedule();
  153. printk("done.\n");
  154. }
  155. void thaw_kernel_threads(void)
  156. {
  157. struct task_struct *g, *p;
  158. pm_nosig_freezing = false;
  159. printk("Restarting kernel threads ... ");
  160. thaw_workqueues();
  161. read_lock(&tasklist_lock);
  162. do_each_thread(g, p) {
  163. if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
  164. __thaw_task(p);
  165. } while_each_thread(g, p);
  166. read_unlock(&tasklist_lock);
  167. schedule();
  168. printk("done.\n");
  169. }