process.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* 0 = success, else # of processes that we failed to stop */
  58. int freeze_processes(void)
  59. {
  60. int todo, nr_user, user_frozen;
  61. unsigned long start_time;
  62. struct task_struct *g, *p;
  63. unsigned long flags;
  64. printk( "Stopping tasks: " );
  65. start_time = jiffies;
  66. user_frozen = 0;
  67. do {
  68. nr_user = todo = 0;
  69. read_lock(&tasklist_lock);
  70. do_each_thread(g, p) {
  71. if (!freezeable(p))
  72. continue;
  73. if (frozen(p))
  74. continue;
  75. if (p->mm && !(p->flags & PF_BORROWED_MM)) {
  76. /* The task is a user-space one.
  77. * Freeze it unless there's a vfork completion
  78. * pending
  79. */
  80. if (!p->vfork_done)
  81. freeze_process(p);
  82. nr_user++;
  83. } else {
  84. /* Freeze only if the user space is frozen */
  85. if (user_frozen)
  86. freeze_process(p);
  87. todo++;
  88. }
  89. } while_each_thread(g, p);
  90. read_unlock(&tasklist_lock);
  91. todo += nr_user;
  92. if (!user_frozen && !nr_user) {
  93. sys_sync();
  94. start_time = jiffies;
  95. }
  96. user_frozen = !nr_user;
  97. yield(); /* Yield is okay here */
  98. if (todo && time_after(jiffies, start_time + TIMEOUT))
  99. break;
  100. } while(todo);
  101. /* This does not unfreeze processes that are already frozen
  102. * (we have slightly ugly calling convention in that respect,
  103. * and caller must call thaw_processes() if something fails),
  104. * but it cleans up leftover PF_FREEZE requests.
  105. */
  106. if (todo) {
  107. printk( "\n" );
  108. printk(KERN_ERR " stopping tasks timed out "
  109. "after %d seconds (%d tasks remaining):\n",
  110. TIMEOUT / HZ, todo);
  111. read_lock(&tasklist_lock);
  112. do_each_thread(g, p) {
  113. if (freezeable(p) && !frozen(p))
  114. printk(KERN_ERR " %s\n", p->comm);
  115. if (freezing(p)) {
  116. pr_debug(" clean up: %s\n", p->comm);
  117. p->flags &= ~PF_FREEZE;
  118. spin_lock_irqsave(&p->sighand->siglock, flags);
  119. recalc_sigpending_tsk(p);
  120. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  121. }
  122. } while_each_thread(g, p);
  123. read_unlock(&tasklist_lock);
  124. return todo;
  125. }
  126. printk( "|\n" );
  127. BUG_ON(in_atomic());
  128. return 0;
  129. }
  130. void thaw_processes(void)
  131. {
  132. struct task_struct *g, *p;
  133. printk( "Restarting tasks..." );
  134. read_lock(&tasklist_lock);
  135. do_each_thread(g, p) {
  136. if (!freezeable(p))
  137. continue;
  138. if (!thaw_process(p))
  139. printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
  140. } while_each_thread(g, p);
  141. read_unlock(&tasklist_lock);
  142. schedule();
  143. printk( " done\n" );
  144. }
  145. EXPORT_SYMBOL(refrigerator);