process.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 freezeable(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) || !freezeable(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. if (!task_is_stopped_or_traced(p) &&
  57. !freezer_should_skip(p))
  58. todo++;
  59. } while_each_thread(g, p);
  60. read_unlock(&tasklist_lock);
  61. if (!sig_only) {
  62. wq_busy = freeze_workqueues_busy();
  63. todo += wq_busy;
  64. }
  65. if (!todo || time_after(jiffies, end_time))
  66. break;
  67. if (!pm_check_wakeup_events()) {
  68. wakeup = true;
  69. break;
  70. }
  71. /*
  72. * We need to retry, but first give the freezing tasks some
  73. * time to enter the regrigerator.
  74. */
  75. msleep(10);
  76. }
  77. do_gettimeofday(&end);
  78. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  79. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  80. elapsed_csecs = elapsed_csecs64;
  81. if (todo) {
  82. /* This does not unfreeze processes that are already frozen
  83. * (we have slightly ugly calling convention in that respect,
  84. * and caller must call thaw_processes() if something fails),
  85. * but it cleans up leftover PF_FREEZE requests.
  86. */
  87. printk("\n");
  88. printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
  89. "(%d tasks refusing to freeze, wq_busy=%d):\n",
  90. wakeup ? "aborted" : "failed",
  91. elapsed_csecs / 100, elapsed_csecs % 100,
  92. todo - wq_busy, wq_busy);
  93. thaw_workqueues();
  94. read_lock(&tasklist_lock);
  95. do_each_thread(g, p) {
  96. task_lock(p);
  97. if (!wakeup && freezing(p) && !freezer_should_skip(p))
  98. sched_show_task(p);
  99. cancel_freezing(p);
  100. task_unlock(p);
  101. } while_each_thread(g, p);
  102. read_unlock(&tasklist_lock);
  103. } else {
  104. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  105. elapsed_csecs % 100);
  106. }
  107. return todo ? -EBUSY : 0;
  108. }
  109. /**
  110. * freeze_processes - tell processes to enter the refrigerator
  111. */
  112. int freeze_processes(void)
  113. {
  114. int error;
  115. printk("Freezing user space processes ... ");
  116. error = try_to_freeze_tasks(true);
  117. if (error)
  118. goto Exit;
  119. printk("done.\n");
  120. printk("Freezing remaining freezable tasks ... ");
  121. error = try_to_freeze_tasks(false);
  122. if (error)
  123. goto Exit;
  124. printk("done.");
  125. oom_killer_disable();
  126. Exit:
  127. BUG_ON(in_atomic());
  128. printk("\n");
  129. return error;
  130. }
  131. static void thaw_tasks(bool nosig_only)
  132. {
  133. struct task_struct *g, *p;
  134. read_lock(&tasklist_lock);
  135. do_each_thread(g, p) {
  136. if (!freezeable(p))
  137. continue;
  138. if (nosig_only && should_send_signal(p))
  139. continue;
  140. if (cgroup_freezing_or_frozen(p))
  141. continue;
  142. thaw_process(p);
  143. } while_each_thread(g, p);
  144. read_unlock(&tasklist_lock);
  145. }
  146. void thaw_processes(void)
  147. {
  148. oom_killer_enable();
  149. printk("Restarting tasks ... ");
  150. thaw_workqueues();
  151. thaw_tasks(true);
  152. thaw_tasks(false);
  153. schedule();
  154. printk("done.\n");
  155. }