process.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /*
  16. * Timeout for stopping processes
  17. */
  18. #define TIMEOUT (20 * HZ)
  19. static inline int freezeable(struct task_struct * p)
  20. {
  21. if ((p == current) ||
  22. (p->flags & PF_NOFREEZE) ||
  23. (p->exit_state != 0))
  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. struct timeval start, end;
  33. u64 elapsed_csecs64;
  34. unsigned int elapsed_csecs;
  35. do_gettimeofday(&start);
  36. end_time = jiffies + TIMEOUT;
  37. while (true) {
  38. todo = 0;
  39. read_lock(&tasklist_lock);
  40. do_each_thread(g, p) {
  41. if (frozen(p) || !freezeable(p))
  42. continue;
  43. if (!freeze_task(p, sig_only))
  44. continue;
  45. /*
  46. * Now that we've done set_freeze_flag, don't
  47. * perturb a task in TASK_STOPPED or TASK_TRACED.
  48. * It is "frozen enough". If the task does wake
  49. * up, it will immediately call try_to_freeze.
  50. */
  51. if (!task_is_stopped_or_traced(p) &&
  52. !freezer_should_skip(p))
  53. todo++;
  54. } while_each_thread(g, p);
  55. read_unlock(&tasklist_lock);
  56. if (!todo || time_after(jiffies, end_time))
  57. break;
  58. /*
  59. * We need to retry, but first give the freezing tasks some
  60. * time to enter the regrigerator.
  61. */
  62. msleep(10);
  63. }
  64. do_gettimeofday(&end);
  65. elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
  66. do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
  67. elapsed_csecs = elapsed_csecs64;
  68. if (todo) {
  69. /* This does not unfreeze processes that are already frozen
  70. * (we have slightly ugly calling convention in that respect,
  71. * and caller must call thaw_processes() if something fails),
  72. * but it cleans up leftover PF_FREEZE requests.
  73. */
  74. printk("\n");
  75. printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
  76. "(%d tasks refusing to freeze):\n",
  77. elapsed_csecs / 100, elapsed_csecs % 100, todo);
  78. read_lock(&tasklist_lock);
  79. do_each_thread(g, p) {
  80. task_lock(p);
  81. if (freezing(p) && !freezer_should_skip(p))
  82. sched_show_task(p);
  83. cancel_freezing(p);
  84. task_unlock(p);
  85. } while_each_thread(g, p);
  86. read_unlock(&tasklist_lock);
  87. } else {
  88. printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
  89. elapsed_csecs % 100);
  90. }
  91. return todo ? -EBUSY : 0;
  92. }
  93. /**
  94. * freeze_processes - tell processes to enter the refrigerator
  95. */
  96. int freeze_processes(void)
  97. {
  98. int error;
  99. printk("Freezing user space processes ... ");
  100. error = try_to_freeze_tasks(true);
  101. if (error)
  102. goto Exit;
  103. printk("done.\n");
  104. printk("Freezing remaining freezable tasks ... ");
  105. error = try_to_freeze_tasks(false);
  106. if (error)
  107. goto Exit;
  108. printk("done.");
  109. oom_killer_disable();
  110. Exit:
  111. BUG_ON(in_atomic());
  112. printk("\n");
  113. return error;
  114. }
  115. static void thaw_tasks(bool nosig_only)
  116. {
  117. struct task_struct *g, *p;
  118. read_lock(&tasklist_lock);
  119. do_each_thread(g, p) {
  120. if (!freezeable(p))
  121. continue;
  122. if (nosig_only && should_send_signal(p))
  123. continue;
  124. if (cgroup_freezing_or_frozen(p))
  125. continue;
  126. thaw_process(p);
  127. } while_each_thread(g, p);
  128. read_unlock(&tasklist_lock);
  129. }
  130. void thaw_processes(void)
  131. {
  132. oom_killer_enable();
  133. printk("Restarting tasks ... ");
  134. thaw_tasks(true);
  135. thaw_tasks(false);
  136. schedule();
  137. printk("done.\n");
  138. }