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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Alexey Dobriyan | 1a8670a | 2009-09-21 17:03:09 -0700 | [diff] [blame] | 12 | #include <linux/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 17 | #include <linux/delay.h> |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 18 | #include <linux/workqueue.h> |
Rafael J. Wysocki | 1e73203 | 2012-03-28 23:30:21 +0200 | [diff] [blame] | 19 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * Timeout for stopping processes |
| 23 | */ |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 24 | #define TIMEOUT (20 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 26 | static int try_to_freeze_tasks(bool user_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct task_struct *g, *p; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 29 | unsigned long end_time; |
| 30 | unsigned int todo; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 31 | bool wq_busy = false; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 32 | struct timeval start, end; |
David Howells | f0af566 | 2008-07-23 21:28:44 -0700 | [diff] [blame] | 33 | u64 elapsed_csecs64; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 34 | unsigned int elapsed_csecs; |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 35 | bool wakeup = false; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 36 | |
| 37 | do_gettimeofday(&start); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 38 | |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 39 | end_time = jiffies + TIMEOUT; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 40 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 41 | if (!user_only) |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 42 | freeze_workqueues_begin(); |
| 43 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 44 | while (true) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 45 | todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | read_lock(&tasklist_lock); |
| 47 | do_each_thread(g, p) { |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 48 | if (p == current || !freeze_task(p)) |
Rafael J. Wysocki | d5d8c59 | 2007-10-18 03:04:46 -0700 | [diff] [blame] | 49 | continue; |
| 50 | |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 51 | /* |
| 52 | * Now that we've done set_freeze_flag, don't |
| 53 | * perturb a task in TASK_STOPPED or TASK_TRACED. |
| 54 | * It is "frozen enough". If the task does wake |
| 55 | * up, it will immediately call try_to_freeze. |
Tejun Heo | 8cfe400 | 2010-11-26 23:07:27 +0100 | [diff] [blame] | 56 | * |
Marcos Paulo de Souza | 37f08be | 2012-02-21 23:57:47 +0100 | [diff] [blame] | 57 | * Because freeze_task() goes through p's scheduler lock, it's |
| 58 | * guaranteed that TASK_STOPPED/TRACED -> TASK_RUNNING |
| 59 | * transition can't race with task state testing here. |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 60 | */ |
| 61 | if (!task_is_stopped_or_traced(p) && |
| 62 | !freezer_should_skip(p)) |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 63 | todo++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } while_each_thread(g, p); |
| 65 | read_unlock(&tasklist_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 66 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 67 | if (!user_only) { |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 68 | wq_busy = freeze_workqueues_busy(); |
| 69 | todo += wq_busy; |
| 70 | } |
| 71 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 72 | if (!todo || time_after(jiffies, end_time)) |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 73 | break; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 74 | |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 75 | if (pm_wakeup_pending()) { |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 76 | wakeup = true; |
| 77 | break; |
| 78 | } |
| 79 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 80 | /* |
| 81 | * We need to retry, but first give the freezing tasks some |
Sedat Dilek | 5834ec3 | 2012-08-23 02:47:13 +0200 | [diff] [blame^] | 82 | * time to enter the refrigerator. |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 83 | */ |
| 84 | msleep(10); |
| 85 | } |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 86 | |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 87 | do_gettimeofday(&end); |
| 88 | elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start); |
| 89 | do_div(elapsed_csecs64, NSEC_PER_SEC / 100); |
| 90 | elapsed_csecs = elapsed_csecs64; |
| 91 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 92 | if (todo) { |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 93 | printk("\n"); |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 94 | printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds " |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 95 | "(%d tasks refusing to freeze, wq_busy=%d):\n", |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 96 | wakeup ? "aborted" : "failed", |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 97 | elapsed_csecs / 100, elapsed_csecs % 100, |
| 98 | todo - wq_busy, wq_busy); |
| 99 | |
Rafael J. Wysocki | 6c83b48 | 2012-02-11 00:00:34 +0100 | [diff] [blame] | 100 | if (!wakeup) { |
| 101 | read_lock(&tasklist_lock); |
| 102 | do_each_thread(g, p) { |
| 103 | if (p != current && !freezer_should_skip(p) |
| 104 | && freezing(p) && !frozen(p)) |
| 105 | sched_show_task(p); |
| 106 | } while_each_thread(g, p); |
| 107 | read_unlock(&tasklist_lock); |
| 108 | } |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 109 | } else { |
| 110 | printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100, |
| 111 | elapsed_csecs % 100); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 114 | return todo ? -EBUSY : 0; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 118 | * freeze_processes - Signal user space processes to enter the refrigerator. |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 119 | * |
| 120 | * On success, returns 0. On failure, -errno and system is fully thawed. |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 121 | */ |
| 122 | int freeze_processes(void) |
| 123 | { |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 124 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 125 | |
Rafael J. Wysocki | 247bc03 | 2012-03-28 23:30:28 +0200 | [diff] [blame] | 126 | error = __usermodehelper_disable(UMH_FREEZING); |
Rafael J. Wysocki | 1e73203 | 2012-03-28 23:30:21 +0200 | [diff] [blame] | 127 | if (error) |
| 128 | return error; |
| 129 | |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 130 | if (!pm_freezing) |
| 131 | atomic_inc(&system_freezing_cnt); |
| 132 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 133 | printk("Freezing user space processes ... "); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 134 | pm_freezing = true; |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 135 | error = try_to_freeze_tasks(true); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 136 | if (!error) { |
| 137 | printk("done."); |
Rafael J. Wysocki | 247bc03 | 2012-03-28 23:30:28 +0200 | [diff] [blame] | 138 | __usermodehelper_set_disable_depth(UMH_DISABLED); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 139 | oom_killer_disable(); |
| 140 | } |
| 141 | printk("\n"); |
| 142 | BUG_ON(in_atomic()); |
| 143 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 144 | if (error) |
| 145 | thaw_processes(); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 146 | return error; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator. |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 151 | * |
Srivatsa S. Bhat | 379e0be | 2012-02-03 22:22:41 +0100 | [diff] [blame] | 152 | * On success, returns 0. On failure, -errno and only the kernel threads are |
| 153 | * thawed, so as to give a chance to the caller to do additional cleanups |
| 154 | * (if any) before thawing the userspace tasks. So, it is the responsibility |
| 155 | * of the caller to thaw the userspace tasks, when the time is right. |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 156 | */ |
| 157 | int freeze_kernel_threads(void) |
| 158 | { |
| 159 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 160 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 161 | printk("Freezing remaining freezable tasks ... "); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 162 | pm_nosig_freezing = true; |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 163 | error = try_to_freeze_tasks(false); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 164 | if (!error) |
| 165 | printk("done."); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 166 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 167 | printk("\n"); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 168 | BUG_ON(in_atomic()); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 169 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 170 | if (error) |
Srivatsa S. Bhat | 379e0be | 2012-02-03 22:22:41 +0100 | [diff] [blame] | 171 | thaw_kernel_threads(); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 172 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Tejun Heo | 6cd8ded | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 175 | void thaw_processes(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | struct task_struct *g, *p; |
| 178 | |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 179 | if (pm_freezing) |
| 180 | atomic_dec(&system_freezing_cnt); |
| 181 | pm_freezing = false; |
| 182 | pm_nosig_freezing = false; |
| 183 | |
Tejun Heo | 6cd8ded | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 184 | oom_killer_enable(); |
| 185 | |
| 186 | printk("Restarting tasks ... "); |
| 187 | |
| 188 | thaw_workqueues(); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 191 | do_each_thread(g, p) { |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 192 | __thaw_task(p); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 193 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 195 | |
Rafael J. Wysocki | 1e73203 | 2012-03-28 23:30:21 +0200 | [diff] [blame] | 196 | usermodehelper_enable(); |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | schedule(); |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 199 | printk("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Rafael J. Wysocki | 181e9bd | 2012-01-29 20:35:52 +0100 | [diff] [blame] | 202 | void thaw_kernel_threads(void) |
| 203 | { |
| 204 | struct task_struct *g, *p; |
| 205 | |
| 206 | pm_nosig_freezing = false; |
| 207 | printk("Restarting kernel threads ... "); |
| 208 | |
| 209 | thaw_workqueues(); |
| 210 | |
| 211 | read_lock(&tasklist_lock); |
| 212 | do_each_thread(g, p) { |
| 213 | if (p->flags & (PF_KTHREAD | PF_WQ_WORKER)) |
| 214 | __thaw_task(p); |
| 215 | } while_each_thread(g, p); |
| 216 | read_unlock(&tasklist_lock); |
| 217 | |
| 218 | schedule(); |
| 219 | printk("done.\n"); |
| 220 | } |