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> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 16 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Timeout for stopping processes |
| 20 | */ |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 21 | #define TIMEOUT (20 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 23 | #define FREEZER_KERNEL_THREADS 0 |
| 24 | #define FREEZER_USER_SPACE 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static inline int freezeable(struct task_struct * p) |
| 27 | { |
| 28 | if ((p == current) || |
| 29 | (p->flags & PF_NOFREEZE) || |
| 30 | (p->exit_state == EXIT_ZOMBIE) || |
| 31 | (p->exit_state == EXIT_DEAD) || |
Pavel Machek | 85b6bce | 2006-03-31 02:30:06 -0800 | [diff] [blame] | 32 | (p->state == TASK_STOPPED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return 0; |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | /* Refrigerator is place where frozen processes are stored :-). */ |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 38 | void refrigerator(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | /* Hmm, should we be allowed to suspend when there are realtime |
| 41 | processes around? */ |
| 42 | long save; |
| 43 | save = current->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | pr_debug("%s entered refrigerator\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 46 | frozen_process(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | spin_lock_irq(¤t->sighand->siglock); |
| 48 | recalc_sigpending(); /* We sent fake signal, clean it up */ |
| 49 | spin_unlock_irq(¤t->sighand->siglock); |
| 50 | |
Pavel Machek | 2a23b5d | 2005-09-03 15:56:53 -0700 | [diff] [blame] | 51 | while (frozen(current)) { |
| 52 | current->state = TASK_UNINTERRUPTIBLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | schedule(); |
Pavel Machek | 2a23b5d | 2005-09-03 15:56:53 -0700 | [diff] [blame] | 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | pr_debug("%s left refrigerator\n", current->comm); |
| 56 | current->state = save; |
| 57 | } |
| 58 | |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 59 | static inline void freeze_process(struct task_struct *p) |
| 60 | { |
| 61 | unsigned long flags; |
| 62 | |
| 63 | if (!freezing(p)) { |
| 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 | } |
| 69 | } |
| 70 | |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame] | 71 | static void cancel_freezing(struct task_struct *p) |
| 72 | { |
| 73 | unsigned long flags; |
| 74 | |
| 75 | if (freezing(p)) { |
| 76 | pr_debug(" clean up: %s\n", p->comm); |
| 77 | do_not_freeze(p); |
| 78 | spin_lock_irqsave(&p->sighand->siglock, flags); |
| 79 | recalc_sigpending_tsk(p); |
| 80 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
| 81 | } |
| 82 | } |
| 83 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 84 | static inline int is_user_space(struct task_struct *p) |
| 85 | { |
| 86 | return p->mm && !(p->flags & PF_BORROWED_MM); |
| 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* 0 = success, else # of processes that we failed to stop */ |
| 90 | int freeze_processes(void) |
| 91 | { |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 92 | int todo, nr_user, user_frozen; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 93 | unsigned long start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct task_struct *g, *p; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 95 | |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 96 | printk("Stopping tasks... "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | start_time = jiffies; |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 98 | user_frozen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | do { |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 100 | nr_user = todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | read_lock(&tasklist_lock); |
| 102 | do_each_thread(g, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (!freezeable(p)) |
| 104 | continue; |
Pavel Machek | 1322ad41 | 2005-07-07 17:56:45 -0700 | [diff] [blame] | 105 | if (frozen(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | continue; |
Rafael J. Wysocki | 3eb1b3a | 2006-12-06 20:34:34 -0800 | [diff] [blame] | 107 | if (p->state == TASK_TRACED && |
| 108 | (frozen(p->parent) || |
| 109 | p->parent->state == TASK_STOPPED)) { |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame] | 110 | cancel_freezing(p); |
| 111 | continue; |
| 112 | } |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 113 | if (is_user_space(p)) { |
| 114 | /* Freeze the task unless there is a vfork |
| 115 | * completion pending |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 116 | */ |
| 117 | if (!p->vfork_done) |
| 118 | freeze_process(p); |
| 119 | nr_user++; |
| 120 | } else { |
| 121 | /* Freeze only if the user space is frozen */ |
| 122 | if (user_frozen) |
| 123 | freeze_process(p); |
| 124 | todo++; |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } while_each_thread(g, p); |
| 127 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 128 | todo += nr_user; |
| 129 | if (!user_frozen && !nr_user) { |
| 130 | sys_sync(); |
| 131 | start_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 133 | user_frozen = !nr_user; |
| 134 | yield(); /* Yield is okay here */ |
| 135 | if (todo && time_after(jiffies, start_time + TIMEOUT)) |
| 136 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } while(todo); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 138 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 139 | /* This does not unfreeze processes that are already frozen |
| 140 | * (we have slightly ugly calling convention in that respect, |
| 141 | * and caller must call thaw_processes() if something fails), |
| 142 | * but it cleans up leftover PF_FREEZE requests. |
| 143 | */ |
| 144 | if (todo) { |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 145 | printk("\n"); |
| 146 | printk(KERN_ERR "Stopping tasks timed out " |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 147 | "after %d seconds (%d tasks remaining):\n", |
| 148 | TIMEOUT / HZ, todo); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 149 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 150 | do_each_thread(g, p) { |
| 151 | if (freezeable(p) && !frozen(p)) |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 152 | printk(KERN_ERR " %s\n", p->comm); |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame] | 153 | cancel_freezing(p); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 154 | } while_each_thread(g, p); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 155 | read_unlock(&tasklist_lock); |
| 156 | return todo; |
| 157 | } |
| 158 | |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 159 | printk("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | BUG_ON(in_atomic()); |
| 161 | return 0; |
| 162 | } |
| 163 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 164 | static void thaw_tasks(int thaw_user_space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | struct task_struct *g, *p; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 169 | do_each_thread(g, p) { |
| 170 | if (!freezeable(p)) |
| 171 | continue; |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 172 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 173 | if (is_user_space(p) == !thaw_user_space) |
| 174 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 176 | if (!thaw_process(p)) |
| 177 | printk(KERN_WARNING " Strange, %s not stopped\n", |
| 178 | p->comm ); |
| 179 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame^] | 181 | } |
| 182 | |
| 183 | void thaw_processes(void) |
| 184 | { |
| 185 | printk("Restarting tasks ... "); |
| 186 | thaw_tasks(FREEZER_KERNEL_THREADS); |
| 187 | thaw_tasks(FREEZER_USER_SPACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | schedule(); |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 189 | printk("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | EXPORT_SYMBOL(refrigerator); |