blob: 31338cdeafc4749766567bbd41546d3dd885dd96 [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>
Arve Hjønnevåg677f1042008-10-14 16:02:39 -070019#include <linux/wakelock.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 Heo58a69cb2011-02-16 09:25:31 +010026static inline int freezable(struct task_struct * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Oleg Nesterov1065d132007-05-08 00:24:01 -070028 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070030 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 0;
32 return 1;
33}
34
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020035static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080038 unsigned long end_time;
39 unsigned int todo;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020040 bool wq_busy = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070041 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070042 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070043 unsigned int elapsed_csecs;
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020044 bool wakeup = false;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070045
46 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070047
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080048 end_time = jiffies + TIMEOUT;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020049
50 if (!sig_only)
51 freeze_workqueues_begin();
52
Tejun Heobe404f02009-10-08 22:47:30 +020053 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080054 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 read_lock(&tasklist_lock);
56 do_each_thread(g, p) {
Tejun Heo58a69cb2011-02-16 09:25:31 +010057 if (frozen(p) || !freezable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080059
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020060 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070061 continue;
62
Roland McGrath13b1c3d2008-03-03 20:22:05 -080063 /*
64 * Now that we've done set_freeze_flag, don't
65 * perturb a task in TASK_STOPPED or TASK_TRACED.
66 * It is "frozen enough". If the task does wake
67 * up, it will immediately call try_to_freeze.
Tejun Heo8cfe4002010-11-26 23:07:27 +010068 *
69 * Because freeze_task() goes through p's
70 * scheduler lock after setting TIF_FREEZE, it's
71 * guaranteed that either we see TASK_RUNNING or
72 * try_to_stop() after schedule() in ptrace/signal
73 * stop sees TIF_FREEZE.
Roland McGrath13b1c3d2008-03-03 20:22:05 -080074 */
75 if (!task_is_stopped_or_traced(p) &&
76 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070077 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 } while_each_thread(g, p);
79 read_unlock(&tasklist_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +020080
81 if (!sig_only) {
82 wq_busy = freeze_workqueues_busy();
83 todo += wq_busy;
84 }
85
Arve Hjønnevåg677f1042008-10-14 16:02:39 -070086 if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
87 wakeup = 1;
88 break;
89 }
Tejun Heobe404f02009-10-08 22:47:30 +020090 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080091 break;
Tejun Heobe404f02009-10-08 22:47:30 +020092
Rafael J. Wysockia2867e02010-12-03 22:58:31 +010093 if (pm_wakeup_pending()) {
Rafael J. Wysockidbeeec52010-10-04 22:07:32 +020094 wakeup = true;
95 break;
96 }
97
Tejun Heobe404f02009-10-08 22:47:30 +020098 /*
99 * We need to retry, but first give the freezing tasks some
100 * time to enter the regrigerator.
101 */
102 msleep(10);
103 }
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700104
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700105 do_gettimeofday(&end);
106 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
107 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
108 elapsed_csecs = elapsed_csecs64;
109
Pavel Machek6161b2c2005-09-03 15:57:05 -0700110 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800111 /* This does not unfreeze processes that are already frozen
112 * (we have slightly ugly calling convention in that respect,
113 * and caller must call thaw_processes() if something fails),
114 * but it cleans up leftover PF_FREEZE requests.
115 */
Arve Hjønnevåg677f1042008-10-14 16:02:39 -0700116 if(wakeup) {
117 printk("\n");
118 printk(KERN_ERR "Freezing of %s aborted\n",
119 sig_only ? "user space " : "tasks ");
120 }
121 else {
122 printk("\n");
123 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
124 "(%d tasks refusing to freeze, wq_busy=%d):\n",
125 elapsed_csecs / 100, elapsed_csecs % 100,
126 todo - wq_busy, wq_busy);
127 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200128 thaw_workqueues();
129
Pavel Machek6161b2c2005-09-03 15:57:05 -0700130 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800131 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700132 task_lock(p);
Arve Hjønnevåg01e13542009-12-01 20:54:37 -0800133 if (freezing(p) && !freezer_should_skip(p) &&
134 elapsed_csecs > 100)
Xiaotian Feng4f598452010-03-10 22:59:13 +0100135 sched_show_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -0700136 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -0700137 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -0800138 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700139 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700140 } else {
141 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
142 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700143 }
144
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700145 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800146}
147
148/**
149 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800150 */
151int freeze_processes(void)
152{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700153 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800154
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700155 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200156 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700157 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700158 goto Exit;
159 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800160
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700161 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200162 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700163 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700164 goto Exit;
165 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700166
167 oom_killer_disable();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700168 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700170 printk("\n");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700171
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700172 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200175static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct task_struct *g, *p;
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 Heo58a69cb2011-02-16 09:25:31 +0100181 if (!freezable(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800182 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800183
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200184 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800185 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Matt Helsley5a7aadf2010-03-26 23:51:44 +0100187 if (cgroup_freezing_or_frozen(p))
Matt Helsley5a069152008-10-18 20:27:22 -0700188 continue;
189
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700190 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800191 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800193}
194
195void thaw_processes(void)
196{
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700197 oom_killer_enable();
198
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800199 printk("Restarting tasks ... ");
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200200 thaw_workqueues();
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200201 thaw_tasks(true);
202 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800204 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206