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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Timeout for stopping processes |
| 21 | */ |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 22 | #define TIMEOUT (20 * HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static inline int freezeable(struct task_struct * p) |
| 25 | { |
Oleg Nesterov | 1065d13 | 2007-05-08 00:24:01 -0700 | [diff] [blame] | 26 | if ((p == current) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | (p->flags & PF_NOFREEZE) || |
Oleg Nesterov | 1065d13 | 2007-05-08 00:24:01 -0700 | [diff] [blame] | 28 | (p->exit_state != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | return 0; |
| 30 | return 1; |
| 31 | } |
| 32 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 33 | static int try_to_freeze_tasks(bool sig_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct task_struct *g, *p; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 36 | unsigned long end_time; |
| 37 | unsigned int todo; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 38 | struct timeval start, end; |
David Howells | f0af566 | 2008-07-23 21:28:44 -0700 | [diff] [blame] | 39 | u64 elapsed_csecs64; |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 40 | unsigned int elapsed_csecs; |
| 41 | |
| 42 | do_gettimeofday(&start); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 43 | |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 44 | end_time = jiffies + TIMEOUT; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 45 | while (true) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 46 | todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | read_lock(&tasklist_lock); |
| 48 | do_each_thread(g, p) { |
Rafael J. Wysocki | 0c1eecf | 2007-07-19 01:47:33 -0700 | [diff] [blame] | 49 | if (frozen(p) || !freezeable(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | continue; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 51 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 52 | if (!freeze_task(p, sig_only)) |
Rafael J. Wysocki | d5d8c59 | 2007-10-18 03:04:46 -0700 | [diff] [blame] | 53 | continue; |
| 54 | |
Roland McGrath | 13b1c3d | 2008-03-03 20:22:05 -0800 | [diff] [blame] | 55 | /* |
| 56 | * Now that we've done set_freeze_flag, don't |
| 57 | * perturb a task in TASK_STOPPED or TASK_TRACED. |
| 58 | * It is "frozen enough". If the task does wake |
| 59 | * up, it will immediately call try_to_freeze. |
| 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 | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 66 | if (!todo || time_after(jiffies, end_time)) |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 67 | break; |
Tejun Heo | be404f0 | 2009-10-08 22:47:30 +0200 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * We need to retry, but first give the freezing tasks some |
| 71 | * time to enter the regrigerator. |
| 72 | */ |
| 73 | msleep(10); |
| 74 | } |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 75 | |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 76 | do_gettimeofday(&end); |
| 77 | elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start); |
| 78 | do_div(elapsed_csecs64, NSEC_PER_SEC / 100); |
| 79 | elapsed_csecs = elapsed_csecs64; |
| 80 | |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 81 | if (todo) { |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 82 | /* This does not unfreeze processes that are already frozen |
| 83 | * (we have slightly ugly calling convention in that respect, |
| 84 | * and caller must call thaw_processes() if something fails), |
| 85 | * but it cleans up leftover PF_FREEZE requests. |
| 86 | */ |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 87 | printk("\n"); |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 88 | printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds " |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 89 | "(%d tasks refusing to freeze):\n", |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 90 | elapsed_csecs / 100, elapsed_csecs % 100, todo); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 91 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 92 | do_each_thread(g, p) { |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 93 | task_lock(p); |
Rafael J. Wysocki | 0c1eecf | 2007-07-19 01:47:33 -0700 | [diff] [blame] | 94 | if (freezing(p) && !freezer_should_skip(p)) |
Xiaotian Feng | 4f59845 | 2010-03-10 22:59:13 +0100 | [diff] [blame^] | 95 | sched_show_task(p); |
Rafael J. Wysocki | a7ef787 | 2006-08-05 12:13:42 -0700 | [diff] [blame] | 96 | cancel_freezing(p); |
Rafael J. Wysocki | 33e1c28 | 2007-05-23 13:57:24 -0700 | [diff] [blame] | 97 | task_unlock(p); |
Rafael J. Wysocki | 02aaeb9 | 2006-03-23 03:00:04 -0800 | [diff] [blame] | 98 | } while_each_thread(g, p); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 99 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | 438e2ce | 2007-10-18 03:04:49 -0700 | [diff] [blame] | 100 | } else { |
| 101 | printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100, |
| 102 | elapsed_csecs % 100); |
Pavel Machek | 6161b2c | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 105 | return todo ? -EBUSY : 0; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * freeze_processes - tell processes to enter the refrigerator |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 110 | */ |
| 111 | int freeze_processes(void) |
| 112 | { |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 113 | int error; |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 114 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 115 | printk("Freezing user space processes ... "); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 116 | error = try_to_freeze_tasks(true); |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 117 | if (error) |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 118 | goto Exit; |
| 119 | printk("done.\n"); |
Rafael J. Wysocki | 11b2ce2 | 2006-12-06 20:34:40 -0800 | [diff] [blame] | 120 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 121 | printk("Freezing remaining freezable tasks ... "); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 122 | error = try_to_freeze_tasks(false); |
Rafael J. Wysocki | e7cd8a7 | 2007-07-19 01:47:34 -0700 | [diff] [blame] | 123 | if (error) |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 124 | goto Exit; |
| 125 | printk("done."); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 126 | |
| 127 | oom_killer_disable(); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 128 | Exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | BUG_ON(in_atomic()); |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 130 | printk("\n"); |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 131 | |
Rafael J. Wysocki | b842ee5 | 2007-10-18 03:04:48 -0700 | [diff] [blame] | 132 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 135 | static void thaw_tasks(bool nosig_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | struct task_struct *g, *p; |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | read_lock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 140 | do_each_thread(g, p) { |
| 141 | if (!freezeable(p)) |
| 142 | continue; |
Nigel Cunningham | ff39593 | 2006-12-06 20:34:28 -0800 | [diff] [blame] | 143 | |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 144 | if (nosig_only && should_send_signal(p)) |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 145 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Matt Helsley | 5a06915 | 2008-10-18 20:27:22 -0700 | [diff] [blame] | 147 | if (cgroup_frozen(p)) |
| 148 | continue; |
| 149 | |
Rafael J. Wysocki | ba96a0c | 2007-05-23 13:57:25 -0700 | [diff] [blame] | 150 | thaw_process(p); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 151 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | read_unlock(&tasklist_lock); |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void thaw_processes(void) |
| 156 | { |
Rafael J. Wysocki | 7f33d49 | 2009-06-16 15:32:41 -0700 | [diff] [blame] | 157 | oom_killer_enable(); |
| 158 | |
Rafael J. Wysocki | a9b6f56 | 2006-12-06 20:34:37 -0800 | [diff] [blame] | 159 | printk("Restarting tasks ... "); |
Rafael J. Wysocki | ebb12db | 2008-06-11 22:04:29 +0200 | [diff] [blame] | 160 | thaw_tasks(true); |
| 161 | thaw_tasks(false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | schedule(); |
Nigel Cunningham | 14b5b7c | 2006-12-06 20:34:26 -0800 | [diff] [blame] | 163 | printk("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |