Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/power/process.c - Functions for starting/stopping processes on |
| 3 | * suspend transitions. |
| 4 | * |
| 5 | * Originally from swsusp. |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | #undef DEBUG |
| 10 | |
| 11 | #include <linux/smp_lock.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/suspend.h> |
| 14 | #include <linux/module.h> |
| 15 | |
| 16 | /* |
| 17 | * Timeout for stopping processes |
| 18 | */ |
| 19 | #define TIMEOUT (6 * HZ) |
| 20 | |
| 21 | |
| 22 | static inline int freezeable(struct task_struct * p) |
| 23 | { |
| 24 | if ((p == current) || |
| 25 | (p->flags & PF_NOFREEZE) || |
| 26 | (p->exit_state == EXIT_ZOMBIE) || |
| 27 | (p->exit_state == EXIT_DEAD) || |
| 28 | (p->state == TASK_STOPPED) || |
| 29 | (p->state == TASK_TRACED)) |
| 30 | return 0; |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | /* Refrigerator is place where frozen processes are stored :-). */ |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 35 | void refrigerator(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | /* Hmm, should we be allowed to suspend when there are realtime |
| 38 | processes around? */ |
| 39 | long save; |
| 40 | save = current->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | pr_debug("%s entered refrigerator\n", current->comm); |
| 42 | printk("="); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 44 | frozen_process(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | spin_lock_irq(¤t->sighand->siglock); |
| 46 | recalc_sigpending(); /* We sent fake signal, clean it up */ |
| 47 | spin_unlock_irq(¤t->sighand->siglock); |
| 48 | |
Pavel Machek | 2a23b5d | 2005-09-03 15:56:53 -0700 | [diff] [blame] | 49 | while (frozen(current)) { |
| 50 | current->state = TASK_UNINTERRUPTIBLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | schedule(); |
Pavel Machek | 2a23b5d | 2005-09-03 15:56:53 -0700 | [diff] [blame] | 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | pr_debug("%s left refrigerator\n", current->comm); |
| 54 | current->state = save; |
| 55 | } |
| 56 | |
| 57 | /* 0 = success, else # of processes that we failed to stop */ |
| 58 | int freeze_processes(void) |
| 59 | { |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 60 | int todo; |
| 61 | unsigned long start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct task_struct *g, *p; |
Pavel Machek | 1322ad4 | 2005-07-07 17:56:45 -0700 | [diff] [blame] | 63 | unsigned long flags; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | printk( "Stopping tasks: " ); |
| 66 | start_time = jiffies; |
| 67 | do { |
| 68 | todo = 0; |
| 69 | read_lock(&tasklist_lock); |
| 70 | do_each_thread(g, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (!freezeable(p)) |
| 72 | continue; |
Pavel Machek | 1322ad4 | 2005-07-07 17:56:45 -0700 | [diff] [blame] | 73 | if (frozen(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | continue; |
| 75 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 76 | freeze(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | spin_lock_irqsave(&p->sighand->siglock, flags); |
| 78 | signal_wake_up(p, 0); |
| 79 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 80 | todo++; |
| 81 | } while_each_thread(g, p); |
| 82 | read_unlock(&tasklist_lock); |
| 83 | yield(); /* Yield is okay here */ |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 84 | if (todo && time_after(jiffies, start_time + TIMEOUT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | printk( "\n" ); |
| 86 | printk(KERN_ERR " stopping tasks failed (%d tasks remaining)\n", todo ); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 87 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | } while(todo); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 90 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 91 | /* This does not unfreeze processes that are already frozen |
| 92 | * (we have slightly ugly calling convention in that respect, |
| 93 | * and caller must call thaw_processes() if something fails), |
| 94 | * but it cleans up leftover PF_FREEZE requests. |
| 95 | */ |
| 96 | if (todo) { |
| 97 | read_lock(&tasklist_lock); |
| 98 | do_each_thread(g, p) |
| 99 | if (freezing(p)) { |
| 100 | pr_debug(" clean up: %s\n", p->comm); |
| 101 | p->flags &= ~PF_FREEZE; |
| 102 | spin_lock_irqsave(&p->sighand->siglock, flags); |
| 103 | recalc_sigpending_tsk(p); |
| 104 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 105 | } |
| 106 | while_each_thread(g, p); |
| 107 | read_unlock(&tasklist_lock); |
| 108 | return todo; |
| 109 | } |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | printk( "|\n" ); |
| 112 | BUG_ON(in_atomic()); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | void thaw_processes(void) |
| 117 | { |
| 118 | struct task_struct *g, *p; |
| 119 | |
| 120 | printk( "Restarting tasks..." ); |
| 121 | read_lock(&tasklist_lock); |
| 122 | do_each_thread(g, p) { |
| 123 | if (!freezeable(p)) |
| 124 | continue; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 125 | if (!thaw_process(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | printk(KERN_INFO " Strange, %s not stopped\n", p->comm ); |
| 127 | } while_each_thread(g, p); |
| 128 | |
| 129 | read_unlock(&tasklist_lock); |
| 130 | schedule(); |
| 131 | printk( " done\n" ); |
| 132 | } |
| 133 | |
| 134 | EXPORT_SYMBOL(refrigerator); |