blob: d5a258b60c6fd71aed065a0dd2ea5acadddc991f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/interrupt.h>
Alexey Dobriyan1a8670a2009-09-21 17:03:09 -070012#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/suspend.h>
14#include <linux/module.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080015#include <linux/syscalls.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080016#include <linux/freezer.h>
Tejun Heobe404f02009-10-08 22:47:30 +020017#include <linux/delay.h>
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020018#include <linux/workqueue.h>
Rafael J. Wysocki1e732032012-03-28 23:30:21 +020019#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/*
22 * Timeout for stopping processes
23 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080024#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Tejun Heo839e3402011-11-21 12:32:26 -080026static int try_to_freeze_tasks(bool user_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080029 unsigned long end_time;
30 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020031 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070032 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070033 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070034 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020035 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070036
37 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070038
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080039 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020040
Tejun Heo839e3402011-11-21 12:32:26 -080041 if (!user_only)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020042 freeze_workqueues_begin();
43
Tejun Heobe404f02009-10-08 22:47:30 +020044 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080045 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 read_lock(&tasklist_lock);
47 do_each_thread(g, p) {
Tejun Heo839e3402011-11-21 12:32:26 -080048 if (p == current || !freeze_task(p))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070049 continue;
50
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +020051 if (!freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070052 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 } while_each_thread(g, p);
54 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020055
Tejun Heo839e3402011-11-21 12:32:26 -080056 if (!user_only) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020057 wq_busy = freeze_workqueues_busy();
58 todo += wq_busy;
59 }
60
Tejun Heobe404f02009-10-08 22:47:30 +020061 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080062 break;
Tejun Heobe404f02009-10-08 22:47:30 +020063
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010064 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020065 wakeup = true;
66 break;
67 }
68
Tejun Heobe404f02009-10-08 22:47:30 +020069 /*
70 * We need to retry, but first give the freezing tasks some
Sedat Dilek5834ec32012-08-23 02:47:13 +020071 * time to enter the refrigerator.
Tejun Heobe404f02009-10-08 22:47:30 +020072 */
73 msleep(10);
74 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070075
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070076 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 Machek6161b2c2005-09-03 15:57:05 -070081 if (todo) {
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080082 printk("\n");
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020083 printk(KERN_ERR "Freezing of tasks %s after %d.%02d seconds "
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020084 "(%d tasks refusing to freeze, wq_busy=%d):\n",
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020085 wakeup ? "aborted" : "failed",
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020086 elapsed_csecs / 100, elapsed_csecs % 100,
87 todo - wq_busy, wq_busy);
88
Rafael J. Wysocki6c83b482012-02-11 00:00:34 +010089 if (!wakeup) {
90 read_lock(&tasklist_lock);
91 do_each_thread(g, p) {
92 if (p != current && !freezer_should_skip(p)
93 && freezing(p) && !frozen(p))
94 sched_show_task(p);
95 } while_each_thread(g, p);
96 read_unlock(&tasklist_lock);
97 }
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070098 } else {
99 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
100 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700101 }
102
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700103 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800104}
105
106/**
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200107 * freeze_processes - Signal user space processes to enter the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800108 *
109 * On success, returns 0. On failure, -errno and system is fully thawed.
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800110 */
111int freeze_processes(void)
112{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700113 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800114
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200115 error = __usermodehelper_disable(UMH_FREEZING);
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200116 if (error)
117 return error;
118
Tejun Heoa3201222011-11-21 12:32:25 -0800119 if (!pm_freezing)
120 atomic_inc(&system_freezing_cnt);
121
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700122 printk("Freezing user space processes ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800123 pm_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200124 error = try_to_freeze_tasks(true);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200125 if (!error) {
126 printk("done.");
Rafael J. Wysocki247bc032012-03-28 23:30:28 +0200127 __usermodehelper_set_disable_depth(UMH_DISABLED);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200128 oom_killer_disable();
129 }
130 printk("\n");
131 BUG_ON(in_atomic());
132
Tejun Heo03afed82011-11-21 12:32:24 -0800133 if (error)
134 thaw_processes();
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200135 return error;
136}
137
138/**
139 * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
Tejun Heo03afed82011-11-21 12:32:24 -0800140 *
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100141 * On success, returns 0. On failure, -errno and only the kernel threads are
142 * thawed, so as to give a chance to the caller to do additional cleanups
143 * (if any) before thawing the userspace tasks. So, it is the responsibility
144 * of the caller to thaw the userspace tasks, when the time is right.
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200145 */
146int freeze_kernel_threads(void)
147{
148 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800149
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700150 printk("Freezing remaining freezable tasks ... ");
Tejun Heoa3201222011-11-21 12:32:25 -0800151 pm_nosig_freezing = true;
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200152 error = try_to_freeze_tasks(false);
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200153 if (!error)
154 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700155
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700156 printk("\n");
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200157 BUG_ON(in_atomic());
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700158
Tejun Heo03afed82011-11-21 12:32:24 -0800159 if (error)
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100160 thaw_kernel_threads();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700161 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800164void thaw_processes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct task_struct *g, *p;
167
Tejun Heoa3201222011-11-21 12:32:25 -0800168 if (pm_freezing)
169 atomic_dec(&system_freezing_cnt);
170 pm_freezing = false;
171 pm_nosig_freezing = false;
172
Tejun Heo6cd8ded2011-11-21 12:32:23 -0800173 oom_killer_enable();
174
175 printk("Restarting tasks ... ");
176
177 thaw_workqueues();
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800180 do_each_thread(g, p) {
Tejun Heoa5be2d02011-11-21 12:32:23 -0800181 __thaw_task(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800182 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800184
Rafael J. Wysocki1e732032012-03-28 23:30:21 +0200185 usermodehelper_enable();
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800188 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Rafael J. Wysocki181e9bd2012-01-29 20:35:52 +0100191void thaw_kernel_threads(void)
192{
193 struct task_struct *g, *p;
194
195 pm_nosig_freezing = false;
196 printk("Restarting kernel threads ... ");
197
198 thaw_workqueues();
199
200 read_lock(&tasklist_lock);
201 do_each_thread(g, p) {
202 if (p->flags & (PF_KTHREAD | PF_WQ_WORKER))
203 __thaw_task(p);
204 } while_each_thread(g, p);
205 read_unlock(&tasklist_lock);
206
207 schedule();
208 printk("done.\n");
209}