process.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 sig_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 (!sig_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, sig_only))
  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
  47. * scheduler lock after setting TIF_FREEZE, it's
  48. * guaranteed that either we see TASK_RUNNING or
  49. * try_to_stop() after schedule() in ptrace/signal
  50. * stop sees TIF_FREEZE.
  51. */
  52. if (!task_is_stopped_or_traced(p) &&
  53. !freezer_should_skip(p))
  54. todo++;
  55. } while_each_thread(g, p);
  56. read_unlock(&tasklist_lock);
  57. if (!sig_only) {
  58. wq_busy = freeze_workqueues_busy();
  59. todo += wq_busy;
  60. }
  61. if (!todo || time_after(jiffies, end_time))
  62. break;
  63. if (pm_wakeup_pending()) {
  64. wakeup = true;
  65. break;
  66. }
  67. /*
  68. * We need to retry, but first give the freezing tasks some
  69. * time to enter the regrigerator.
  70. */
  71. msleep(10);
  72. }
  73. do_gettimeofday(&end);
  74. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  75. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  76. elapsed_csecs = elapsed_csecs64;
  77. if (todo) {
  78. printk("\n");
  79. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  80. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  81. wakeup ? "aborted" : "failed",
  82. elapsed_csecs / 100, elapsed_csecs % 100,
  83. todo - wq_busy, wq_busy);
  84. read_lock(&tasklist_lock);
  85. do_each_thread(g, p) {
  86. if (!wakeup && !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. } 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. printk("Freezing user space processes ... ");
  106. error = try_to_freeze_tasks(true);
  107. if (!error) {
  108. printk("done.");
  109. oom_killer_disable();
  110. }
  111. printk("\n");
  112. BUG_ON(in_atomic());
  113. if (error)
  114. thaw_processes();
  115. return error;
  116. }
  117. /**
  118. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  119. *
  120. * On success, returns 0. On failure, -errno and system is fully thawed.
  121. */
  122. int freeze_kernel_threads(void)
  123. {
  124. int error;
  125. printk("Freezing remaining freezable tasks ... ");
  126. error = try_to_freeze_tasks(false);
  127. if (!error)
  128. printk("done.");
  129. printk("\n");
  130. BUG_ON(in_atomic());
  131. if (error)
  132. thaw_processes();
  133. return error;
  134. }
  135. void thaw_processes(void)
  136. {
  137. struct task_struct *g, *p;
  138. oom_killer_enable();
  139. printk("Restarting tasks ... ");
  140. thaw_workqueues();
  141. read_lock(&tasklist_lock);
  142. do_each_thread(g, p) {
  143. if (cgroup_freezing_or_frozen(p))
  144. continue;
  145. __thaw_task(p);
  146. } while_each_thread(g, p);
  147. read_unlock(&tasklist_lock);
  148. schedule();
  149. printk("done.\n");
  150. }