process.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 inline int freezable(struct task_struct * p)
  21. {
  22. if ((p == current) ||
  23. (p->flags & PF_NOFREEZE))
  24. return 0;
  25. return 1;
  26. }
  27. static int try_to_freeze_tasks(bool sig_only)
  28. {
  29. struct task_struct *g, *p;
  30. unsigned long end_time;
  31. unsigned int todo;
  32. bool wq_busy = false;
  33. struct timeval start, end;
  34. u64 elapsed_csecs64;
  35. unsigned int elapsed_csecs;
  36. bool wakeup = false;
  37. do_gettimeofday(&start);
  38. end_time = jiffies + TIMEOUT;
  39. if (!sig_only)
  40. freeze_workqueues_begin();
  41. while (true) {
  42. todo = 0;
  43. read_lock(&tasklist_lock);
  44. do_each_thread(g, p) {
  45. if (frozen(p) || !freezable(p))
  46. continue;
  47. if (!freeze_task(p, sig_only))
  48. continue;
  49. /*
  50. * Now that we've done set_freeze_flag, don't
  51. * perturb a task in TASK_STOPPED or TASK_TRACED.
  52. * It is "frozen enough". If the task does wake
  53. * up, it will immediately call try_to_freeze.
  54. *
  55. * Because freeze_task() goes through p's
  56. * scheduler lock after setting TIF_FREEZE, it's
  57. * guaranteed that either we see TASK_RUNNING or
  58. * try_to_stop() after schedule() in ptrace/signal
  59. * stop sees TIF_FREEZE.
  60. */
  61. if (!task_is_stopped_or_traced(p) &&
  62. !freezer_should_skip(p))
  63. todo++;
  64. } while_each_thread(g, p);
  65. read_unlock(&tasklist_lock);
  66. if (!sig_only) {
  67. wq_busy = freeze_workqueues_busy();
  68. todo += wq_busy;
  69. }
  70. if (!todo || time_after(jiffies, end_time))
  71. break;
  72. if (pm_wakeup_pending()) {
  73. wakeup = true;
  74. break;
  75. }
  76. /*
  77. * We need to retry, but first give the freezing tasks some
  78. * time to enter the regrigerator.
  79. */
  80. msleep(10);
  81. }
  82. do_gettimeofday(&end);
  83. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  84. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  85. elapsed_csecs = elapsed_csecs64;
  86. if (todo) {
  87. /* This does not unfreeze processes that are already frozen
  88. * (we have slightly ugly calling convention in that respect,
  89. * and caller must call thaw_processes() if something fails),
  90. * but it cleans up leftover PF_FREEZE requests.
  91. */
  92. printk("\n");
  93. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  94. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  95. wakeup ? "aborted" : "failed",
  96. elapsed_csecs / 100, elapsed_csecs % 100,
  97. todo - wq_busy, wq_busy);
  98. thaw_workqueues();
  99. read_lock(&tasklist_lock);
  100. do_each_thread(g, p) {
  101. if (!wakeup && freezing(p) && !freezer_should_skip(p))
  102. sched_show_task(p);
  103. cancel_freezing(p);
  104. } while_each_thread(g, p);
  105. read_unlock(&tasklist_lock);
  106. } else {
  107. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  108. elapsed_csecs % 100);
  109. }
  110. return todo ? -EBUSY : 0;
  111. }
  112. /**
  113. * freeze_processes - Signal user space processes to enter the refrigerator.
  114. */
  115. int freeze_processes(void)
  116. {
  117. int error;
  118. printk("Freezing user space processes ... ");
  119. error = try_to_freeze_tasks(true);
  120. if (!error) {
  121. printk("done.");
  122. oom_killer_disable();
  123. }
  124. printk("\n");
  125. BUG_ON(in_atomic());
  126. return error;
  127. }
  128. /**
  129. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  130. */
  131. int freeze_kernel_threads(void)
  132. {
  133. int error;
  134. printk("Freezing remaining freezable tasks ... ");
  135. error = try_to_freeze_tasks(false);
  136. if (!error)
  137. printk("done.");
  138. printk("\n");
  139. BUG_ON(in_atomic());
  140. return error;
  141. }
  142. void thaw_processes(void)
  143. {
  144. struct task_struct *g, *p;
  145. oom_killer_enable();
  146. printk("Restarting tasks ... ");
  147. thaw_workqueues();
  148. read_lock(&tasklist_lock);
  149. do_each_thread(g, p) {
  150. if (!freezable(p))
  151. continue;
  152. if (cgroup_freezing_or_frozen(p))
  153. continue;
  154. __thaw_task(p);
  155. } while_each_thread(g, p);
  156. read_unlock(&tasklist_lock);
  157. schedule();
  158. printk("done.\n");
  159. }