process.c 3.8 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/smp_lock.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/suspend.h>
  11. #include <linux/module.h>
  12. #include <linux/syscalls.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 == EXIT_ZOMBIE) ||
  22. (p->exit_state == EXIT_DEAD) ||
  23. (p->state == TASK_STOPPED) ||
  24. (p->state == TASK_TRACED))
  25. return 0;
  26. return 1;
  27. }
  28. /* Refrigerator is place where frozen processes are stored :-). */
  29. void refrigerator(void)
  30. {
  31. /* Hmm, should we be allowed to suspend when there are realtime
  32. processes around? */
  33. long save;
  34. save = current->state;
  35. pr_debug("%s entered refrigerator\n", current->comm);
  36. printk("=");
  37. frozen_process(current);
  38. spin_lock_irq(&current->sighand->siglock);
  39. recalc_sigpending(); /* We sent fake signal, clean it up */
  40. spin_unlock_irq(&current->sighand->siglock);
  41. while (frozen(current)) {
  42. current->state = TASK_UNINTERRUPTIBLE;
  43. schedule();
  44. }
  45. pr_debug("%s left refrigerator\n", current->comm);
  46. current->state = save;
  47. }
  48. static inline void freeze_process(struct task_struct *p)
  49. {
  50. unsigned long flags;
  51. if (!freezing(p)) {
  52. freeze(p);
  53. spin_lock_irqsave(&p->sighand->siglock, flags);
  54. signal_wake_up(p, 0);
  55. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  56. }
  57. }
  58. /* 0 = success, else # of processes that we failed to stop */
  59. int freeze_processes(void)
  60. {
  61. int todo, nr_user, user_frozen;
  62. unsigned long start_time;
  63. struct task_struct *g, *p;
  64. unsigned long flags;
  65. printk( "Stopping tasks: " );
  66. start_time = jiffies;
  67. user_frozen = 0;
  68. do {
  69. nr_user = todo = 0;
  70. read_lock(&tasklist_lock);
  71. do_each_thread(g, p) {
  72. if (!freezeable(p))
  73. continue;
  74. if (frozen(p))
  75. continue;
  76. if (p->mm && !(p->flags & PF_BORROWED_MM)) {
  77. /* The task is a user-space one.
  78. * Freeze it unless there's a vfork completion
  79. * pending
  80. */
  81. if (!p->vfork_done)
  82. freeze_process(p);
  83. nr_user++;
  84. } else {
  85. /* Freeze only if the user space is frozen */
  86. if (user_frozen)
  87. freeze_process(p);
  88. todo++;
  89. }
  90. } while_each_thread(g, p);
  91. read_unlock(&tasklist_lock);
  92. todo += nr_user;
  93. if (!user_frozen && !nr_user) {
  94. sys_sync();
  95. start_time = jiffies;
  96. }
  97. user_frozen = !nr_user;
  98. yield(); /* Yield is okay here */
  99. if (todo && time_after(jiffies, start_time + TIMEOUT))
  100. break;
  101. } while(todo);
  102. /* This does not unfreeze processes that are already frozen
  103. * (we have slightly ugly calling convention in that respect,
  104. * and caller must call thaw_processes() if something fails),
  105. * but it cleans up leftover PF_FREEZE requests.
  106. */
  107. if (todo) {
  108. printk( "\n" );
  109. printk(KERN_ERR " stopping tasks timed out "
  110. "after %d seconds (%d tasks remaining):\n",
  111. TIMEOUT / HZ, todo);
  112. read_lock(&tasklist_lock);
  113. do_each_thread(g, p) {
  114. if (freezeable(p) && !frozen(p))
  115. printk(KERN_ERR " %s\n", p->comm);
  116. if (freezing(p)) {
  117. pr_debug(" clean up: %s\n", p->comm);
  118. p->flags &= ~PF_FREEZE;
  119. spin_lock_irqsave(&p->sighand->siglock, flags);
  120. recalc_sigpending_tsk(p);
  121. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  122. }
  123. } while_each_thread(g, p);
  124. read_unlock(&tasklist_lock);
  125. return todo;
  126. }
  127. printk( "|\n" );
  128. BUG_ON(in_atomic());
  129. return 0;
  130. }
  131. void thaw_processes(void)
  132. {
  133. struct task_struct *g, *p;
  134. printk( "Restarting tasks..." );
  135. read_lock(&tasklist_lock);
  136. do_each_thread(g, p) {
  137. if (!freezeable(p))
  138. continue;
  139. if (!thaw_process(p))
  140. printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
  141. } while_each_thread(g, p);
  142. read_unlock(&tasklist_lock);
  143. schedule();
  144. printk( " done\n" );
  145. }
  146. EXPORT_SYMBOL(refrigerator);