process.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #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 == EXIT_ZOMBIE) ||
  23. (p->exit_state == EXIT_DEAD) ||
  24. (p->state == TASK_STOPPED))
  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. 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 &&
  86. (frozen(p->parent) ||
  87. p->parent->state == TASK_STOPPED)) {
  88. cancel_freezing(p);
  89. continue;
  90. }
  91. if (p->mm && !(p->flags & PF_BORROWED_MM)) {
  92. /* The task is a user-space one.
  93. * Freeze it unless there's a vfork completion
  94. * pending
  95. */
  96. if (!p->vfork_done)
  97. freeze_process(p);
  98. nr_user++;
  99. } else {
  100. /* Freeze only if the user space is frozen */
  101. if (user_frozen)
  102. freeze_process(p);
  103. todo++;
  104. }
  105. } while_each_thread(g, p);
  106. read_unlock(&tasklist_lock);
  107. todo += nr_user;
  108. if (!user_frozen && !nr_user) {
  109. sys_sync();
  110. start_time = jiffies;
  111. }
  112. user_frozen = !nr_user;
  113. yield(); /* Yield is okay here */
  114. if (todo && time_after(jiffies, start_time + TIMEOUT))
  115. break;
  116. } while(todo);
  117. /* This does not unfreeze processes that are already frozen
  118. * (we have slightly ugly calling convention in that respect,
  119. * and caller must call thaw_processes() if something fails),
  120. * but it cleans up leftover PF_FREEZE requests.
  121. */
  122. if (todo) {
  123. printk("\n");
  124. printk(KERN_ERR "Stopping tasks timed out "
  125. "after %d seconds (%d tasks remaining):\n",
  126. TIMEOUT / HZ, todo);
  127. read_lock(&tasklist_lock);
  128. do_each_thread(g, p) {
  129. if (freezeable(p) && !frozen(p))
  130. printk(KERN_ERR " %s\n", p->comm);
  131. cancel_freezing(p);
  132. } while_each_thread(g, p);
  133. read_unlock(&tasklist_lock);
  134. return todo;
  135. }
  136. printk("done.\n");
  137. BUG_ON(in_atomic());
  138. return 0;
  139. }
  140. void thaw_some_processes(int all)
  141. {
  142. struct task_struct *g, *p;
  143. int pass = 0; /* Pass 0 = Kernel space, 1 = Userspace */
  144. printk("Restarting tasks... ");
  145. read_lock(&tasklist_lock);
  146. do {
  147. do_each_thread(g, p) {
  148. /*
  149. * is_user = 0 if kernel thread or borrowed mm,
  150. * 1 otherwise.
  151. */
  152. int is_user = !!(p->mm && !(p->flags & PF_BORROWED_MM));
  153. if (!freezeable(p) || (is_user != pass))
  154. continue;
  155. if (!thaw_process(p))
  156. printk(KERN_INFO
  157. "Strange, %s not stopped\n", p->comm);
  158. } while_each_thread(g, p);
  159. pass++;
  160. } while (pass < 2 && all);
  161. read_unlock(&tasklist_lock);
  162. schedule();
  163. printk("done.\n");
  164. }
  165. EXPORT_SYMBOL(refrigerator);