blob: 71ae29052ab6c2275c6f48239b56cdd4be4cba33 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*
20 * Timeout for stopping processes
21 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080022#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static inline int freezeable(struct task_struct * p)
25{
Oleg Nesterov1065d132007-05-08 00:24:01 -070026 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070028 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return 0;
30 return 1;
31}
32
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020033static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080036 unsigned long end_time;
37 unsigned int todo;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070038 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070039 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070040 unsigned int elapsed_csecs;
41
42 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070043
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080044 end_time = jiffies + TIMEOUT;
Tejun Heobe404f02009-10-08 22:47:30 +020045 while (true) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080046 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 read_lock(&tasklist_lock);
48 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070049 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080051
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020052 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070053 continue;
54
Roland McGrath13b1c3d2008-03-03 20:22:05 -080055 /*
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. Wysockiba96a0c2007-05-23 13:57:25 -070063 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 } while_each_thread(g, p);
65 read_unlock(&tasklist_lock);
Tejun Heobe404f02009-10-08 22:47:30 +020066 if (!todo || time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080067 break;
Tejun Heobe404f02009-10-08 22:47:30 +020068
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 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) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080082 /* 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 Cunningham14b5b7c2006-12-06 20:34:26 -080087 printk("\n");
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070088 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080089 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070090 elapsed_csecs / 100, elapsed_csecs % 100, todo);
Pavel Machek6161b2c2005-09-03 15:57:05 -070091 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080092 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070093 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070094 if (freezing(p) && !freezer_should_skip(p))
Xiaotian Feng4f598452010-03-10 22:59:13 +010095 sched_show_task(p);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070096 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070097 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080098 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -070099 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -0700100 } else {
101 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
102 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -0700103 }
104
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700105 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800106}
107
108/**
109 * freeze_processes - tell processes to enter the refrigerator
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. Wysockib842ee52007-10-18 03:04:48 -0700115 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200116 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700117 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700118 goto Exit;
119 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800120
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700121 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200122 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700123 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700124 goto Exit;
125 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700126
127 oom_killer_disable();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700128 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700130 printk("\n");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700131
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700132 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200135static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct task_struct *g, *p;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800140 do_each_thread(g, p) {
141 if (!freezeable(p))
142 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800143
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200144 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800145 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Matt Helsley5a7aadf2010-03-26 23:51:44 +0100147 if (cgroup_freezing_or_frozen(p))
Matt Helsley5a069152008-10-18 20:27:22 -0700148 continue;
149
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700150 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800151 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800153}
154
155void thaw_processes(void)
156{
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700157 oom_killer_enable();
158
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800159 printk("Restarting tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200160 thaw_tasks(true);
161 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800163 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165