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> |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Timeout for stopping processes |
| 19 | */ |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 20 | #define TIMEOUT (20 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | static inline int freezeable(struct task_struct * p) |
| 24 | { |
| 25 | if ((p == current) || |
| 26 | (p->flags & PF_NOFREEZE) || |
| 27 | (p->exit_state == EXIT_ZOMBIE) || |
| 28 | (p->exit_state == EXIT_DEAD) || |
Pavel Machek | 85b6bce | 2006-03-31 02:30:06 -0800 | [diff] [blame] | 29 | (p->state == TASK_STOPPED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 57 | static inline void freeze_process(struct task_struct *p) |
| 58 | { |
| 59 | unsigned long flags; |
| 60 | |
| 61 | if (!freezing(p)) { |
| 62 | freeze(p); |
| 63 | spin_lock_irqsave(&p->sighand->siglock, flags); |
| 64 | signal_wake_up(p, 0); |
| 65 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 66 | } |
| 67 | } |
| 68 | |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame^] | 69 | static void cancel_freezing(struct task_struct *p) |
| 70 | { |
| 71 | unsigned long flags; |
| 72 | |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* 0 = success, else # of processes that we failed to stop */ |
| 83 | int freeze_processes(void) |
| 84 | { |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 85 | int todo, nr_user, user_frozen; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 86 | unsigned long start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct task_struct *g, *p; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | printk( "Stopping tasks: " ); |
| 90 | start_time = jiffies; |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 91 | user_frozen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | do { |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 93 | nr_user = todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | read_lock(&tasklist_lock); |
| 95 | do_each_thread(g, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!freezeable(p)) |
| 97 | continue; |
Pavel Machek | 1322ad4 | 2005-07-07 17:56:45 -0700 | [diff] [blame] | 98 | if (frozen(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | continue; |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame^] | 100 | if (p->state == TASK_TRACED && frozen(p->parent)) { |
| 101 | cancel_freezing(p); |
| 102 | continue; |
| 103 | } |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 104 | if (p->mm && !(p->flags & PF_BORROWED_MM)) { |
| 105 | /* The task is a user-space one. |
| 106 | * Freeze it unless there's a vfork completion |
| 107 | * pending |
| 108 | */ |
| 109 | if (!p->vfork_done) |
| 110 | freeze_process(p); |
| 111 | nr_user++; |
| 112 | } else { |
| 113 | /* Freeze only if the user space is frozen */ |
| 114 | if (user_frozen) |
| 115 | freeze_process(p); |
| 116 | todo++; |
| 117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } while_each_thread(g, p); |
| 119 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 120 | todo += nr_user; |
| 121 | if (!user_frozen && !nr_user) { |
| 122 | sys_sync(); |
| 123 | start_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 125 | user_frozen = !nr_user; |
| 126 | yield(); /* Yield is okay here */ |
| 127 | if (todo && time_after(jiffies, start_time + TIMEOUT)) |
| 128 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } while(todo); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 130 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 131 | /* This does not unfreeze processes that are already frozen |
| 132 | * (we have slightly ugly calling convention in that respect, |
| 133 | * and caller must call thaw_processes() if something fails), |
| 134 | * but it cleans up leftover PF_FREEZE requests. |
| 135 | */ |
| 136 | if (todo) { |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 137 | printk( "\n" ); |
| 138 | printk(KERN_ERR " stopping tasks timed out " |
| 139 | "after %d seconds (%d tasks remaining):\n", |
| 140 | TIMEOUT / HZ, todo); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 141 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 142 | do_each_thread(g, p) { |
| 143 | if (freezeable(p) && !frozen(p)) |
| 144 | printk(KERN_ERR " %s\n", p->comm); |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame^] | 145 | cancel_freezing(p); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 146 | } while_each_thread(g, p); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 147 | read_unlock(&tasklist_lock); |
| 148 | return todo; |
| 149 | } |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | printk( "|\n" ); |
| 152 | BUG_ON(in_atomic()); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | void thaw_processes(void) |
| 157 | { |
| 158 | struct task_struct *g, *p; |
| 159 | |
| 160 | printk( "Restarting tasks..." ); |
| 161 | read_lock(&tasklist_lock); |
| 162 | do_each_thread(g, p) { |
| 163 | if (!freezeable(p)) |
| 164 | continue; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 165 | if (!thaw_process(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | printk(KERN_INFO " Strange, %s not stopped\n", p->comm ); |
| 167 | } while_each_thread(g, p); |
| 168 | |
| 169 | read_unlock(&tasklist_lock); |
| 170 | schedule(); |
| 171 | printk( " done\n" ); |
| 172 | } |
| 173 | |
| 174 | EXPORT_SYMBOL(refrigerator); |