process.c 4.3 KB

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