blob: cc2e55373b68cc3f32e83fbf036200c30a14f343 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/*
19 * Timeout for stopping processes
20 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080021#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static inline int freezeable(struct task_struct * p)
24{
Oleg Nesterov1065d132007-05-08 00:24:01 -070025 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070027 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return 0;
29 return 1;
30}
31
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020032static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080035 unsigned long end_time;
36 unsigned int todo;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070037 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070038 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070039 unsigned int elapsed_csecs;
40
41 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070042
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080043 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 do {
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) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070048 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080050
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020051 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070052 continue;
53
Roland McGrath13b1c3d2008-03-03 20:22:05 -080054 /*
55 * Now that we've done set_freeze_flag, don't
56 * perturb a task in TASK_STOPPED or TASK_TRACED.
57 * It is "frozen enough". If the task does wake
58 * up, it will immediately call try_to_freeze.
59 */
60 if (!task_is_stopped_or_traced(p) &&
61 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070062 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 } while_each_thread(g, p);
64 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080065 yield(); /* Yield is okay here */
Rafael J. Wysockic2cf7d82007-07-19 01:47:35 -070066 if (time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080067 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080068 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070069
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070070 do_gettimeofday(&end);
71 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
72 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
73 elapsed_csecs = elapsed_csecs64;
74
Pavel Machek6161b2c2005-09-03 15:57:05 -070075 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080076 /* This does not unfreeze processes that are already frozen
77 * (we have slightly ugly calling convention in that respect,
78 * and caller must call thaw_processes() if something fails),
79 * but it cleans up leftover PF_FREEZE requests.
80 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080081 printk("\n");
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070082 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080083 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070084 elapsed_csecs / 100, elapsed_csecs % 100, todo);
Andrew Morton328616e2007-07-19 01:47:26 -070085 show_state();
Pavel Machek6161b2c2005-09-03 15:57:05 -070086 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080087 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070088 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070089 if (freezing(p) && !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080090 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070091 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070092 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080093 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -070094 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070095 } else {
96 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
97 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -070098 }
99
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700100 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800101}
102
103/**
104 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800105 */
106int freeze_processes(void)
107{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700108 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800109
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700110 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200111 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700112 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700113 goto Exit;
114 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800115
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700116 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200117 error = try_to_freeze_tasks(false);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700118 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700119 goto Exit;
120 printk("done.");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700121
122 oom_killer_disable();
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700123 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700125 printk("\n");
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700126
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700127 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200130static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct task_struct *g, *p;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800135 do_each_thread(g, p) {
136 if (!freezeable(p))
137 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800138
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200139 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800140 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Matt Helsley5a069152008-10-18 20:27:22 -0700142 if (cgroup_frozen(p))
143 continue;
144
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700145 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800146 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800148}
149
150void thaw_processes(void)
151{
Rafael J. Wysocki7f33d492009-06-16 15:32:41 -0700152 oom_killer_enable();
153
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800154 printk("Restarting tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200155 thaw_tasks(true);
156 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800158 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160