blob: ca634019497a5cf8b035872bdaf8edcfaefd8b88 [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>
12#include <linux/suspend.h>
13#include <linux/module.h>
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080014#include <linux/syscalls.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080015#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * Timeout for stopping processes
19 */
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080020#define TIMEOUT (20 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static inline int freezeable(struct task_struct * p)
23{
Oleg Nesterov1065d132007-05-08 00:24:01 -070024 if ((p == current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 (p->flags & PF_NOFREEZE) ||
Oleg Nesterov1065d132007-05-08 00:24:01 -070026 (p->exit_state != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return 0;
28 return 1;
29}
30
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020031static int try_to_freeze_tasks(bool sig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct task_struct *g, *p;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080034 unsigned long end_time;
35 unsigned int todo;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070036 struct timeval start, end;
David Howellsf0af5662008-07-23 21:28:44 -070037 u64 elapsed_csecs64;
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070038 unsigned int elapsed_csecs;
39
40 do_gettimeofday(&start);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070041
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080042 end_time = jiffies + TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 do {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080044 todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 read_lock(&tasklist_lock);
46 do_each_thread(g, p) {
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070047 if (frozen(p) || !freezeable(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 continue;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080049
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +020050 if (!freeze_task(p, sig_only))
Rafael J. Wysockid5d8c592007-10-18 03:04:46 -070051 continue;
52
Roland McGrath13b1c3d2008-03-03 20:22:05 -080053 /*
54 * Now that we've done set_freeze_flag, don't
55 * perturb a task in TASK_STOPPED or TASK_TRACED.
56 * It is "frozen enough". If the task does wake
57 * up, it will immediately call try_to_freeze.
58 */
59 if (!task_is_stopped_or_traced(p) &&
60 !freezer_should_skip(p))
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -070061 todo++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 } while_each_thread(g, p);
63 read_unlock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080064 yield(); /* Yield is okay here */
Rafael J. Wysockic2cf7d82007-07-19 01:47:35 -070065 if (time_after(jiffies, end_time))
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080066 break;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080067 } while (todo);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070068
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070069 do_gettimeofday(&end);
70 elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
71 do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
72 elapsed_csecs = elapsed_csecs64;
73
Pavel Machek6161b2c2005-09-03 15:57:05 -070074 if (todo) {
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080075 /* This does not unfreeze processes that are already frozen
76 * (we have slightly ugly calling convention in that respect,
77 * and caller must call thaw_processes() if something fails),
78 * but it cleans up leftover PF_FREEZE requests.
79 */
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080080 printk("\n");
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070081 printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -080082 "(%d tasks refusing to freeze):\n",
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070083 elapsed_csecs / 100, elapsed_csecs % 100, todo);
Andrew Morton328616e2007-07-19 01:47:26 -070084 show_state();
Pavel Machek6161b2c2005-09-03 15:57:05 -070085 read_lock(&tasklist_lock);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080086 do_each_thread(g, p) {
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070087 task_lock(p);
Rafael J. Wysocki0c1eecf2007-07-19 01:47:33 -070088 if (freezing(p) && !freezer_should_skip(p))
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -080089 printk(KERN_ERR " %s\n", p->comm);
Rafael J. Wysockia7ef7872006-08-05 12:13:42 -070090 cancel_freezing(p);
Rafael J. Wysocki33e1c282007-05-23 13:57:24 -070091 task_unlock(p);
Rafael J. Wysocki02aaeb92006-03-23 03:00:04 -080092 } while_each_thread(g, p);
Pavel Machek6161b2c2005-09-03 15:57:05 -070093 read_unlock(&tasklist_lock);
Rafael J. Wysocki438e2ce2007-10-18 03:04:49 -070094 } else {
95 printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
96 elapsed_csecs % 100);
Pavel Machek6161b2c2005-09-03 15:57:05 -070097 }
98
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -070099 return todo ? -EBUSY : 0;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800100}
101
102/**
103 * freeze_processes - tell processes to enter the refrigerator
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800104 */
105int freeze_processes(void)
106{
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700107 int error;
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800108
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700109 printk("Freezing user space processes ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200110 error = try_to_freeze_tasks(true);
Rafael J. Wysockie7cd8a72007-07-19 01:47:34 -0700111 if (error)
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700112 goto Exit;
113 printk("done.\n");
Rafael J. Wysocki11b2ce22006-12-06 20:34:40 -0800114
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700115 printk("Freezing remaining freezable tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200116 error = try_to_freeze_tasks(false);
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.");
120 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 BUG_ON(in_atomic());
Rafael J. Wysockib842ee52007-10-18 03:04:48 -0700122 printk("\n");
123 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200126static void thaw_tasks(bool nosig_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct task_struct *g, *p;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 read_lock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800131 do_each_thread(g, p) {
132 if (!freezeable(p))
133 continue;
Nigel Cunninghamff395932006-12-06 20:34:28 -0800134
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200135 if (nosig_only && should_send_signal(p))
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800136 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Matt Helsley5a069152008-10-18 20:27:22 -0700138 if (cgroup_frozen(p))
139 continue;
140
Rafael J. Wysockiba96a0c2007-05-23 13:57:25 -0700141 thaw_process(p);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800142 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 read_unlock(&tasklist_lock);
Rafael J. Wysockia9b6f562006-12-06 20:34:37 -0800144}
145
146void thaw_processes(void)
147{
148 printk("Restarting tasks ... ");
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200149 thaw_tasks(true);
150 thaw_tasks(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 schedule();
Nigel Cunningham14b5b7c2006-12-06 20:34:26 -0800152 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154