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> |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame^] | 20 | #include <trace/events/power.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Timeout for stopping processes |
| 24 | */ |
Li Fei | 957d128 | 2013-02-01 08:56:03 +0000 | [diff] [blame] | 25 | unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 27 | static int try_to_freeze_tasks(bool user_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct task_struct *g, *p; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 30 | unsigned long end_time; |
| 31 | unsigned int todo; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 32 | bool wq_busy = false; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 33 | struct timeval start, end; |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 34 | u64 elapsed_msecs64; |
| 35 | unsigned int elapsed_msecs; |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 36 | bool wakeup = false; |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 37 | int sleep_usecs = USEC_PER_MSEC; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 38 | |
| 39 | do_gettimeofday(&start); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 40 | |
Li Fei | 957d128 | 2013-02-01 08:56:03 +0000 | [diff] [blame] | 41 | end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 42 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 43 | if (!user_only) |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 44 | freeze_workqueues_begin(); |
| 45 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 46 | while (true) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 47 | todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | read_lock(&tasklist_lock); |
| 49 | do_each_thread(g, p) { |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 50 | if (p == current || !freeze_task(p)) |
Rafael J. Wysocki | d5d8c59 | 2007-10-18 03:04:46 -0700 | [diff] [blame] | 51 | continue; |
| 52 | |
Oleg Nesterov | 5d8f72b | 2012-10-26 19:46:06 +0200 | [diff] [blame] | 53 | if (!freezer_should_skip(p)) |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 54 | todo++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } while_each_thread(g, p); |
| 56 | read_unlock(&tasklist_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 57 | |
Tejun Heo | 839e340 | 2011-11-21 12:32:26 -0800 | [diff] [blame] | 58 | if (!user_only) { |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 59 | wq_busy = freeze_workqueues_busy(); |
| 60 | todo += wq_busy; |
| 61 | } |
| 62 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 63 | if (!todo || time_after(jiffies, end_time)) |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 64 | break; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 65 | |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 66 | if (pm_wakeup_pending()) { |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 67 | wakeup = true; |
| 68 | break; |
| 69 | } |
| 70 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 71 | /* |
| 72 | * We need to retry, but first give the freezing tasks some |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 73 | * time to enter the refrigerator. Start with an initial |
| 74 | * 1 ms sleep followed by exponential backoff until 8 ms. |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 75 | */ |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 76 | usleep_range(sleep_usecs / 2, sleep_usecs); |
| 77 | if (sleep_usecs < 8 * USEC_PER_MSEC) |
| 78 | sleep_usecs *= 2; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 79 | } |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 80 | |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 81 | do_gettimeofday(&end); |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 82 | elapsed_msecs64 = timeval_to_ns(&end) - timeval_to_ns(&start); |
| 83 | do_div(elapsed_msecs64, NSEC_PER_MSEC); |
| 84 | elapsed_msecs = elapsed_msecs64; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 85 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 86 | if (todo) { |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 87 | printk("\n"); |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 88 | printk(KERN_ERR "Freezing of tasks %s after %d.%03d seconds " |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 89 | "(%d tasks refusing to freeze, wq_busy=%d):\n", |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 90 | wakeup ? "aborted" : "failed", |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 91 | elapsed_msecs / 1000, elapsed_msecs % 1000, |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 92 | todo - wq_busy, wq_busy); |
| 93 | |
Rafael J. Wysocki | 6c83b48 | 2012-02-11 00:00:34 +0100 | [diff] [blame] | 94 | if (!wakeup) { |
| 95 | read_lock(&tasklist_lock); |
| 96 | do_each_thread(g, p) { |
| 97 | if (p != current && !freezer_should_skip(p) |
| 98 | && freezing(p) && !frozen(p)) |
| 99 | sched_show_task(p); |
| 100 | } while_each_thread(g, p); |
| 101 | read_unlock(&tasklist_lock); |
| 102 | } |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 103 | } else { |
Colin Cross | 18ad0c6 | 2013-05-06 23:50:10 +0000 | [diff] [blame] | 104 | printk("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000, |
| 105 | elapsed_msecs % 1000); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 108 | return todo ? -EBUSY : 0; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 112 | * freeze_processes - Signal user space processes to enter the refrigerator. |
Colin Cross | 2b44c4d | 2013-07-24 17:41:33 -0700 | [diff] [blame] | 113 | * The current thread will not be frozen. The same process that calls |
| 114 | * freeze_processes must later call thaw_processes. |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 115 | * |
| 116 | * 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] | 117 | */ |
| 118 | int freeze_processes(void) |
| 119 | { |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 120 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 121 | |
Rafael J. Wysocki | 247bc03 | 2012-03-28 23:30:28 +0200 | [diff] [blame] | 122 | error = __usermodehelper_disable(UMH_FREEZING); |
Rafael J. Wysocki | 1e73203 | 2012-03-28 23:30:21 +0200 | [diff] [blame] | 123 | if (error) |
| 124 | return error; |
| 125 | |
Colin Cross | 2b44c4d | 2013-07-24 17:41:33 -0700 | [diff] [blame] | 126 | /* Make sure this task doesn't get frozen */ |
| 127 | current->flags |= PF_SUSPEND_TASK; |
| 128 | |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 129 | if (!pm_freezing) |
| 130 | atomic_inc(&system_freezing_cnt); |
| 131 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 132 | printk("Freezing user space processes ... "); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 133 | pm_freezing = true; |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 134 | error = try_to_freeze_tasks(true); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 135 | if (!error) { |
| 136 | printk("done."); |
Rafael J. Wysocki | 247bc03 | 2012-03-28 23:30:28 +0200 | [diff] [blame] | 137 | __usermodehelper_set_disable_depth(UMH_DISABLED); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 138 | oom_killer_disable(); |
| 139 | } |
| 140 | printk("\n"); |
| 141 | BUG_ON(in_atomic()); |
| 142 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 143 | if (error) |
| 144 | thaw_processes(); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 145 | return error; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator. |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 150 | * |
Srivatsa S. Bhat | 379e0be | 2012-02-03 22:22:41 +0100 | [diff] [blame] | 151 | * On success, returns 0. On failure, -errno and only the kernel threads are |
| 152 | * thawed, so as to give a chance to the caller to do additional cleanups |
| 153 | * (if any) before thawing the userspace tasks. So, it is the responsibility |
| 154 | * 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] | 155 | */ |
| 156 | int freeze_kernel_threads(void) |
| 157 | { |
| 158 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 159 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 160 | printk("Freezing remaining freezable tasks ... "); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 161 | pm_nosig_freezing = true; |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 162 | error = try_to_freeze_tasks(false); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 163 | if (!error) |
| 164 | printk("done."); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 165 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 166 | printk("\n"); |
Rafael J. Wysocki | 2aede85 | 2011-09-26 20:32:27 +0200 | [diff] [blame] | 167 | BUG_ON(in_atomic()); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 168 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 169 | if (error) |
Srivatsa S. Bhat | 379e0be | 2012-02-03 22:22:41 +0100 | [diff] [blame] | 170 | thaw_kernel_threads(); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 171 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Tejun Heo | 6cd8ded | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 174 | void thaw_processes(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct task_struct *g, *p; |
Colin Cross | 2b44c4d | 2013-07-24 17:41:33 -0700 | [diff] [blame] | 177 | struct task_struct *curr = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame^] | 179 | trace_suspend_resume(TPS("thaw_processes"), 0, true); |
Tejun Heo | a320122 | 2011-11-21 12:32:25 -0800 | [diff] [blame] | 180 | if (pm_freezing) |
| 181 | atomic_dec(&system_freezing_cnt); |
| 182 | pm_freezing = false; |
| 183 | pm_nosig_freezing = false; |
| 184 | |
Tejun Heo | 6cd8ded | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 185 | oom_killer_enable(); |
| 186 | |
| 187 | printk("Restarting tasks ... "); |
| 188 | |
| 189 | thaw_workqueues(); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 192 | do_each_thread(g, p) { |
Colin Cross | 2b44c4d | 2013-07-24 17:41:33 -0700 | [diff] [blame] | 193 | /* No other threads should have PF_SUSPEND_TASK set */ |
| 194 | WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK)); |
Tejun Heo | a5be2d0 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 195 | __thaw_task(p); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 196 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 198 | |
Colin Cross | 2b44c4d | 2013-07-24 17:41:33 -0700 | [diff] [blame] | 199 | WARN_ON(!(curr->flags & PF_SUSPEND_TASK)); |
| 200 | curr->flags &= ~PF_SUSPEND_TASK; |
| 201 | |
Rafael J. Wysocki | 1e73203 | 2012-03-28 23:30:21 +0200 | [diff] [blame] | 202 | usermodehelper_enable(); |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | schedule(); |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 205 | printk("done.\n"); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame^] | 206 | trace_suspend_resume(TPS("thaw_processes"), 0, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Rafael J. Wysocki | 181e9bd | 2012-01-29 20:35:52 +0100 | [diff] [blame] | 209 | void thaw_kernel_threads(void) |
| 210 | { |
| 211 | struct task_struct *g, *p; |
| 212 | |
| 213 | pm_nosig_freezing = false; |
| 214 | printk("Restarting kernel threads ... "); |
| 215 | |
| 216 | thaw_workqueues(); |
| 217 | |
| 218 | read_lock(&tasklist_lock); |
| 219 | do_each_thread(g, p) { |
| 220 | if (p->flags & (PF_KTHREAD | PF_WQ_WORKER)) |
| 221 | __thaw_task(p); |
| 222 | } while_each_thread(g, p); |
| 223 | read_unlock(&tasklist_lock); |
| 224 | |
| 225 | schedule(); |
| 226 | printk("done.\n"); |
| 227 | } |