process.c 4.1 KB

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