process.c 3.4 KB

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