process.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*
  13. * Timeout for stopping processes
  14. */
  15. #define TIMEOUT (6 * HZ)
  16. static inline int freezeable(struct task_struct * p)
  17. {
  18. if ((p == current) ||
  19. (p->flags & PF_NOFREEZE) ||
  20. (p->exit_state == EXIT_ZOMBIE) ||
  21. (p->exit_state == EXIT_DEAD) ||
  22. (p->state == TASK_STOPPED) ||
  23. (p->state == TASK_TRACED))
  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. /* 0 = success, else # of processes that we failed to stop */
  48. int freeze_processes(void)
  49. {
  50. int todo;
  51. unsigned long start_time;
  52. struct task_struct *g, *p;
  53. unsigned long flags;
  54. printk( "Stopping tasks: " );
  55. start_time = jiffies;
  56. do {
  57. todo = 0;
  58. read_lock(&tasklist_lock);
  59. do_each_thread(g, p) {
  60. if (!freezeable(p))
  61. continue;
  62. if (frozen(p))
  63. continue;
  64. freeze(p);
  65. spin_lock_irqsave(&p->sighand->siglock, flags);
  66. signal_wake_up(p, 0);
  67. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  68. todo++;
  69. } while_each_thread(g, p);
  70. read_unlock(&tasklist_lock);
  71. yield(); /* Yield is okay here */
  72. if (todo && time_after(jiffies, start_time + TIMEOUT)) {
  73. printk( "\n" );
  74. printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo );
  75. break;
  76. }
  77. } while(todo);
  78. /* This does not unfreeze processes that are already frozen
  79. * (we have slightly ugly calling convention in that respect,
  80. * and caller must call thaw_processes() if something fails),
  81. * but it cleans up leftover PF_FREEZE requests.
  82. */
  83. if (todo) {
  84. read_lock(&tasklist_lock);
  85. do_each_thread(g, p)
  86. if (freezing(p)) {
  87. pr_debug(" clean up: %s\n", p->comm);
  88. p->flags &= ~PF_FREEZE;
  89. spin_lock_irqsave(&p->sighand->siglock, flags);
  90. recalc_sigpending_tsk(p);
  91. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  92. }
  93. while_each_thread(g, p);
  94. read_unlock(&tasklist_lock);
  95. return todo;
  96. }
  97. printk( "|\n" );
  98. BUG_ON(in_atomic());
  99. return 0;
  100. }
  101. void thaw_processes(void)
  102. {
  103. struct task_struct *g, *p;
  104. printk( "Restarting tasks..." );
  105. read_lock(&tasklist_lock);
  106. do_each_thread(g, p) {
  107. if (!freezeable(p))
  108. continue;
  109. if (!thaw_process(p))
  110. printk(KERN_INFO " Strange, %s not stopped\n", p->comm );
  111. } while_each_thread(g, p);
  112. read_unlock(&tasklist_lock);
  113. schedule();
  114. printk( " done\n" );
  115. }
  116. EXPORT_SYMBOL(refrigerator);