process.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #define FREEZER_KERNEL_THREADS 0
  18. #define FREEZER_USER_SPACE 1
  19. static inline int freezeable(struct task_struct * p)
  20. {
  21. if ((p == current) ||
  22. (p->flags & PF_NOFREEZE) ||
  23. (p->exit_state != 0))
  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. task_lock(current);
  34. if (freezing(current)) {
  35. frozen_process(current);
  36. task_unlock(current);
  37. } else {
  38. task_unlock(current);
  39. return;
  40. }
  41. save = current->state;
  42. pr_debug("%s entered refrigerator\n", current->comm);
  43. spin_lock_irq(&current->sighand->siglock);
  44. recalc_sigpending(); /* We sent fake signal, clean it up */
  45. spin_unlock_irq(&current->sighand->siglock);
  46. for (;;) {
  47. set_current_state(TASK_UNINTERRUPTIBLE);
  48. if (!frozen(current))
  49. break;
  50. schedule();
  51. }
  52. pr_debug("%s left refrigerator\n", current->comm);
  53. current->state = save;
  54. }
  55. static inline void freeze_process(struct task_struct *p)
  56. {
  57. unsigned long flags;
  58. if (!freezing(p)) {
  59. rmb();
  60. if (!frozen(p)) {
  61. if (p->state == TASK_STOPPED)
  62. force_sig_specific(SIGSTOP, p);
  63. freeze(p);
  64. spin_lock_irqsave(&p->sighand->siglock, flags);
  65. signal_wake_up(p, p->state == TASK_STOPPED);
  66. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  67. }
  68. }
  69. }
  70. static void cancel_freezing(struct task_struct *p)
  71. {
  72. unsigned long flags;
  73. if (freezing(p)) {
  74. pr_debug(" clean up: %s\n", p->comm);
  75. do_not_freeze(p);
  76. spin_lock_irqsave(&p->sighand->siglock, flags);
  77. recalc_sigpending_tsk(p);
  78. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  79. }
  80. }
  81. static inline int is_user_space(struct task_struct *p)
  82. {
  83. return p->mm && !(p->flags & PF_BORROWED_MM);
  84. }
  85. static unsigned int try_to_freeze_tasks(int freeze_user_space)
  86. {
  87. struct task_struct *g, *p;
  88. unsigned long end_time;
  89. unsigned int todo;
  90. end_time = jiffies + TIMEOUT;
  91. do {
  92. todo = 0;
  93. read_lock(&tasklist_lock);
  94. do_each_thread(g, p) {
  95. if (!freezeable(p))
  96. continue;
  97. if (frozen(p))
  98. continue;
  99. if (p->state == TASK_TRACED && frozen(p->parent)) {
  100. cancel_freezing(p);
  101. continue;
  102. }
  103. if (is_user_space(p) == !freeze_user_space)
  104. continue;
  105. freeze_process(p);
  106. if (!freezer_should_skip(p))
  107. todo++;
  108. } while_each_thread(g, p);
  109. read_unlock(&tasklist_lock);
  110. yield(); /* Yield is okay here */
  111. if (todo && time_after(jiffies, end_time))
  112. break;
  113. } while (todo);
  114. if (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. printk("\n");
  121. printk(KERN_ERR "Stopping %s timed out after %d seconds "
  122. "(%d tasks refusing to freeze):\n",
  123. freeze_user_space ? "user space processes" :
  124. "kernel threads",
  125. TIMEOUT / HZ, todo);
  126. read_lock(&tasklist_lock);
  127. do_each_thread(g, p) {
  128. if (is_user_space(p) == !freeze_user_space)
  129. continue;
  130. task_lock(p);
  131. if (freezeable(p) && !frozen(p) &&
  132. !freezer_should_skip(p))
  133. printk(KERN_ERR " %s\n", p->comm);
  134. cancel_freezing(p);
  135. task_unlock(p);
  136. } while_each_thread(g, p);
  137. read_unlock(&tasklist_lock);
  138. }
  139. return todo;
  140. }
  141. /**
  142. * freeze_processes - tell processes to enter the refrigerator
  143. *
  144. * Returns 0 on success, or the number of processes that didn't freeze,
  145. * although they were told to.
  146. */
  147. int freeze_processes(void)
  148. {
  149. unsigned int nr_unfrozen;
  150. printk("Stopping tasks ... ");
  151. nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
  152. if (nr_unfrozen)
  153. return nr_unfrozen;
  154. sys_sync();
  155. nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
  156. if (nr_unfrozen)
  157. return nr_unfrozen;
  158. printk("done.\n");
  159. BUG_ON(in_atomic());
  160. return 0;
  161. }
  162. static void thaw_tasks(int thaw_user_space)
  163. {
  164. struct task_struct *g, *p;
  165. read_lock(&tasklist_lock);
  166. do_each_thread(g, p) {
  167. if (!freezeable(p))
  168. continue;
  169. if (is_user_space(p) == !thaw_user_space)
  170. continue;
  171. thaw_process(p);
  172. } while_each_thread(g, p);
  173. read_unlock(&tasklist_lock);
  174. }
  175. void thaw_processes(void)
  176. {
  177. printk("Restarting tasks ... ");
  178. thaw_tasks(FREEZER_KERNEL_THREADS);
  179. thaw_tasks(FREEZER_USER_SPACE);
  180. schedule();
  181. printk("done.\n");
  182. }
  183. EXPORT_SYMBOL(refrigerator);