• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include <linux/interrupt.h>
12 #include <linux/suspend.h>
13 #include <linux/module.h>
14 #include <linux/syscalls.h>
15 #include <linux/freezer.h>
16 #include <linux/wakelock.h>
17 
18 /*
19  * Timeout for stopping processes
20  */
21 #define TIMEOUT	(20 * HZ)
22 
freezeable(struct task_struct * p)23 static inline int freezeable(struct task_struct * p)
24 {
25 	if ((p == current) ||
26 	    (p->flags & PF_NOFREEZE) ||
27 	    (p->exit_state != 0))
28 		return 0;
29 	return 1;
30 }
31 
try_to_freeze_tasks(bool sig_only)32 static int try_to_freeze_tasks(bool sig_only)
33 {
34 	struct task_struct *g, *p;
35 	unsigned long end_time;
36 	unsigned int todo;
37 	struct timeval start, end;
38 	u64 elapsed_csecs64;
39 	unsigned int elapsed_csecs;
40 	unsigned int wakeup = 0;
41 
42 	do_gettimeofday(&start);
43 
44 	end_time = jiffies + TIMEOUT;
45 	do {
46 		todo = 0;
47 		read_lock(&tasklist_lock);
48 		do_each_thread(g, p) {
49 			if (frozen(p) || !freezeable(p))
50 				continue;
51 
52 			if (!freeze_task(p, sig_only))
53 				continue;
54 
55 			/*
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))
63 				todo++;
64 		} while_each_thread(g, p);
65 		read_unlock(&tasklist_lock);
66 		yield();			/* Yield is okay here */
67 		if (todo && has_wake_lock(WAKE_LOCK_SUSPEND)) {
68 			wakeup = 1;
69 			break;
70 		}
71 		if (time_after(jiffies, end_time))
72 			break;
73 	} while (todo);
74 
75 	do_gettimeofday(&end);
76 	elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
77 	do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
78 	elapsed_csecs = elapsed_csecs64;
79 
80 	if (todo) {
81 		/* This does not unfreeze processes that are already frozen
82 		 * (we have slightly ugly calling convention in that respect,
83 		 * and caller must call thaw_processes() if something fails),
84 		 * but it cleans up leftover PF_FREEZE requests.
85 		 */
86 		if(wakeup) {
87 			printk("\n");
88 			printk(KERN_ERR "Freezing of %s aborted\n",
89 					sig_only ? "user space " : "tasks ");
90 		}
91 		else {
92 			printk("\n");
93 			printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
94 					"(%d tasks refusing to freeze):\n",
95 					elapsed_csecs / 100, elapsed_csecs % 100, todo);
96 			show_state();
97 		}
98 		read_lock(&tasklist_lock);
99 		do_each_thread(g, p) {
100 			task_lock(p);
101 			if (freezing(p) && !freezer_should_skip(p))
102 				printk(KERN_ERR " %s\n", p->comm);
103 			cancel_freezing(p);
104 			task_unlock(p);
105 		} while_each_thread(g, p);
106 		read_unlock(&tasklist_lock);
107 	} else {
108 		printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
109 			elapsed_csecs % 100);
110 	}
111 
112 	return todo ? -EBUSY : 0;
113 }
114 
115 /**
116  *	freeze_processes - tell processes to enter the refrigerator
117  */
freeze_processes(void)118 int freeze_processes(void)
119 {
120 	int error;
121 
122 	printk("Freezing user space processes ... ");
123 	error = try_to_freeze_tasks(true);
124 	if (error)
125 		goto Exit;
126 	printk("done.\n");
127 
128 	printk("Freezing remaining freezable tasks ... ");
129 	error = try_to_freeze_tasks(false);
130 	if (error)
131 		goto Exit;
132 	printk("done.");
133  Exit:
134 	BUG_ON(in_atomic());
135 	printk("\n");
136 	return error;
137 }
138 
thaw_tasks(bool nosig_only)139 static void thaw_tasks(bool nosig_only)
140 {
141 	struct task_struct *g, *p;
142 
143 	read_lock(&tasklist_lock);
144 	do_each_thread(g, p) {
145 		if (!freezeable(p))
146 			continue;
147 
148 		if (nosig_only && should_send_signal(p))
149 			continue;
150 
151 		if (cgroup_frozen(p))
152 			continue;
153 
154 		thaw_process(p);
155 	} while_each_thread(g, p);
156 	read_unlock(&tasklist_lock);
157 }
158 
thaw_processes(void)159 void thaw_processes(void)
160 {
161 	printk("Restarting tasks ... ");
162 	thaw_tasks(true);
163 	thaw_tasks(false);
164 	schedule();
165 	printk("done.\n");
166 }
167 
168