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> |
Arve Hjønnevåg | 677f104 | 2008-10-14 16:02:39 -0700 | [diff] [blame] | 19 | #include <linux/wakelock.h> |
Bryan Huntsman | d074fa2 | 2011-11-16 13:52:50 -0800 | [diff] [blame] | 20 | #include "power.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Timeout for stopping processes |
| 24 | */ |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 25 | #define TIMEOUT (20 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 27 | static inline int freezable(struct task_struct * p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Oleg Nesterov | 1065d13 | 2007-05-08 00:24:01 -0700 | [diff] [blame] | 29 | if ((p == current) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | (p->flags & PF_NOFREEZE) || |
Oleg Nesterov | 1065d13 | 2007-05-08 00:24:01 -0700 | [diff] [blame] | 31 | (p->exit_state != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | return 1; |
| 34 | } |
| 35 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 36 | static int try_to_freeze_tasks(bool sig_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct task_struct *g, *p; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 39 | unsigned long end_time; |
| 40 | unsigned int todo; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 41 | bool wq_busy = false; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 42 | struct timeval start, end; |
David Howells | f0af566 | 2008-07-23 21:28:44 -0700 | [diff] [blame] | 43 | u64 elapsed_csecs64; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 44 | unsigned int elapsed_csecs; |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 45 | bool wakeup = false; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 46 | |
| 47 | do_gettimeofday(&start); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 48 | |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 49 | end_time = jiffies + TIMEOUT; |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 50 | |
| 51 | if (!sig_only) |
| 52 | freeze_workqueues_begin(); |
| 53 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 54 | while (true) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 55 | todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | read_lock(&tasklist_lock); |
| 57 | do_each_thread(g, p) { |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 58 | if (frozen(p) || !freezable(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | continue; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 60 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 61 | if (!freeze_task(p, sig_only)) |
Rafael J. Wysocki | d5d8c59 | 2007-10-18 03:04:46 -0700 | [diff] [blame] | 62 | continue; |
| 63 | |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 64 | /* |
| 65 | * Now that we've done set_freeze_flag, don't |
| 66 | * perturb a task in TASK_STOPPED or TASK_TRACED. |
| 67 | * It is "frozen enough". If the task does wake |
| 68 | * up, it will immediately call try_to_freeze. |
Tejun Heo | 8cfe400 | 2010-11-26 23:07:27 +0100 | [diff] [blame] | 69 | * |
| 70 | * Because freeze_task() goes through p's |
| 71 | * scheduler lock after setting TIF_FREEZE, it's |
| 72 | * guaranteed that either we see TASK_RUNNING or |
| 73 | * try_to_stop() after schedule() in ptrace/signal |
| 74 | * stop sees TIF_FREEZE. |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 75 | */ |
| 76 | if (!task_is_stopped_or_traced(p) && |
| 77 | !freezer_should_skip(p)) |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 78 | todo++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } while_each_thread(g, p); |
| 80 | read_unlock(&tasklist_lock); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 81 | |
| 82 | if (!sig_only) { |
| 83 | wq_busy = freeze_workqueues_busy(); |
| 84 | todo += wq_busy; |
| 85 | } |
| 86 | |
Arve Hjønnevåg | 677f104 | 2008-10-14 16:02:39 -0700 | [diff] [blame] | 87 | if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) { |
| 88 | wakeup = 1; |
| 89 | break; |
| 90 | } |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 91 | if (!todo || time_after(jiffies, end_time)) |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 92 | break; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 93 | |
Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 94 | if (pm_wakeup_pending()) { |
Rafael J. Wysocki | dbeeec5 | 2010-10-04 22:07:32 +0200 | [diff] [blame] | 95 | wakeup = true; |
| 96 | break; |
| 97 | } |
| 98 | |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 99 | /* |
| 100 | * We need to retry, but first give the freezing tasks some |
| 101 | * time to enter the regrigerator. |
| 102 | */ |
| 103 | msleep(10); |
| 104 | } |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 105 | |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 106 | do_gettimeofday(&end); |
| 107 | elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start); |
| 108 | do_div(elapsed_csecs64, NSEC_PER_SEC / 100); |
| 109 | elapsed_csecs = elapsed_csecs64; |
| 110 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 111 | if (todo) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 112 | /* This does not unfreeze processes that are already frozen |
| 113 | * (we have slightly ugly calling convention in that respect, |
| 114 | * and caller must call thaw_processes() if something fails), |
| 115 | * but it cleans up leftover PF_FREEZE requests. |
| 116 | */ |
Arve Hjønnevåg | 677f104 | 2008-10-14 16:02:39 -0700 | [diff] [blame] | 117 | if(wakeup) { |
| 118 | printk("\n"); |
| 119 | printk(KERN_ERR "Freezing of %s aborted\n", |
| 120 | sig_only ? "user space " : "tasks "); |
| 121 | } |
| 122 | else { |
| 123 | printk("\n"); |
| 124 | printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds " |
| 125 | "(%d tasks refusing to freeze, wq_busy=%d):\n", |
| 126 | elapsed_csecs / 100, elapsed_csecs % 100, |
| 127 | todo - wq_busy, wq_busy); |
| 128 | } |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 129 | thaw_workqueues(); |
| 130 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 131 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 132 | do_each_thread(g, p) { |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 133 | task_lock(p); |
Arve Hjønnevåg | 01e1354 | 2009-12-01 20:54:37 -0800 | [diff] [blame] | 134 | if (freezing(p) && !freezer_should_skip(p) && |
| 135 | elapsed_csecs > 100) |
Xiaotian Feng | 4f59845 | 2010-03-10 22:59:13 +0100 | [diff] [blame] | 136 | sched_show_task(p); |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame] | 137 | cancel_freezing(p); |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 138 | task_unlock(p); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 139 | } while_each_thread(g, p); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 140 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 141 | } else { |
| 142 | printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100, |
| 143 | elapsed_csecs % 100); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 146 | return todo ? -EBUSY : 0; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * freeze_processes - tell processes to enter the refrigerator |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 151 | */ |
| 152 | int freeze_processes(void) |
| 153 | { |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 154 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 155 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 156 | printk("Freezing user space processes ... "); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 157 | error = try_to_freeze_tasks(true); |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 158 | if (error) |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 159 | goto Exit; |
| 160 | printk("done.\n"); |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 161 | |
Bryan Huntsman | d074fa2 | 2011-11-16 13:52:50 -0800 | [diff] [blame] | 162 | error = suspend_sys_sync_wait(); |
| 163 | if (error) |
| 164 | goto Exit; |
| 165 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 166 | printk("Freezing remaining freezable tasks ... "); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 167 | error = try_to_freeze_tasks(false); |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 168 | if (error) |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 169 | goto Exit; |
| 170 | printk("done."); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 171 | |
| 172 | oom_killer_disable(); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 173 | Exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | BUG_ON(in_atomic()); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 175 | printk("\n"); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 176 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 177 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 180 | static void thaw_tasks(bool nosig_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | struct task_struct *g, *p; |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 185 | do_each_thread(g, p) { |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 186 | if (!freezable(p)) |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 187 | continue; |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 188 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 189 | if (nosig_only && should_send_signal(p)) |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 190 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Matt Helsley | 5a7aadf | 2010-03-26 23:51:44 +0100 | [diff] [blame] | 192 | if (cgroup_freezing_or_frozen(p)) |
Matt Helsley | 5a06915 | 2008-10-18 20:27:22 -0700 | [diff] [blame] | 193 | continue; |
| 194 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 195 | thaw_process(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 | } |
| 199 | |
| 200 | void thaw_processes(void) |
| 201 | { |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 202 | oom_killer_enable(); |
| 203 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 204 | printk("Restarting tasks ... "); |
Tejun Heo | a0a1a5f | 2010-06-29 10:07:12 +0200 | [diff] [blame] | 205 | thaw_workqueues(); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 206 | thaw_tasks(true); |
| 207 | thaw_tasks(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | schedule(); |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 209 | printk("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |