process.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. task_lock(p);
  102. if (!wakeup && freezing(p) && !freezer_should_skip(p))
  103. sched_show_task(p);
  104. cancel_freezing(p);
  105. task_unlock(p);
  106. } while_each_thread(g, p);
  107. read_unlock(&tasklist_lock);
  108. } else {
  109. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  110. elapsed_csecs % 100);
  111. }
  112. return todo ? -EBUSY : 0;
  113. }
  114. /**
  115. * freeze_processes - Signal user space processes to enter the refrigerator.
  116. */
  117. int freeze_processes(void)
  118. {
  119. int error;
  120. printk("Freezing user space processes ... ");
  121. error = try_to_freeze_tasks(true);
  122. if (!error) {
  123. printk("done.");
  124. oom_killer_disable();
  125. }
  126. printk("\n");
  127. BUG_ON(in_atomic());
  128. return error;
  129. }
  130. /**
  131. * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
  132. */
  133. int freeze_kernel_threads(void)
  134. {
  135. int error;
  136. printk("Freezing remaining freezable tasks ... ");
  137. error = try_to_freeze_tasks(false);
  138. if (!error)
  139. printk("done.");
  140. printk("\n");
  141. BUG_ON(in_atomic());
  142. return error;
  143. }
  144. void thaw_processes(void)
  145. {
  146. struct task_struct *g, *p;
  147. oom_killer_enable();
  148. printk("Restarting tasks ... ");
  149. thaw_workqueues();
  150. read_lock(&tasklist_lock);
  151. do_each_thread(g, p) {
  152. if (!freezable(p))
  153. continue;
  154. if (cgroup_freezing_or_frozen(p))
  155. continue;
  156. __thaw_task(p);
  157. } while_each_thread(g, p);
  158. read_unlock(&tasklist_lock);
  159. schedule();
  160. printk("done.\n");
  161. }