process.c 3.7 KB

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