process.c 3.9 KB

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