process.c 3.3 KB

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