xref: /linux/kernel/power/process.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
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/oom.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19 
20 /*
21  * Timeout for stopping processes
22  */
23 #define TIMEOUT	(20 * HZ)
24 
25 static inline int freezeable(struct task_struct * p)
26 {
27 	if ((p == current) ||
28 	    (p->flags & PF_NOFREEZE) ||
29 	    (p->exit_state != 0))
30 		return 0;
31 	return 1;
32 }
33 
34 static int try_to_freeze_tasks(bool sig_only)
35 {
36 	struct task_struct *g, *p;
37 	unsigned long end_time;
38 	unsigned int todo;
39 	bool wq_busy = false;
40 	struct timeval start, end;
41 	u64 elapsed_csecs64;
42 	unsigned int elapsed_csecs;
43 
44 	do_gettimeofday(&start);
45 
46 	end_time = jiffies + TIMEOUT;
47 
48 	if (!sig_only)
49 		freeze_workqueues_begin();
50 
51 	while (true) {
52 		todo = 0;
53 		read_lock(&tasklist_lock);
54 		do_each_thread(g, p) {
55 			if (frozen(p) || !freezeable(p))
56 				continue;
57 
58 			if (!freeze_task(p, sig_only))
59 				continue;
60 
61 			/*
62 			 * Now that we've done set_freeze_flag, don't
63 			 * perturb a task in TASK_STOPPED or TASK_TRACED.
64 			 * It is "frozen enough".  If the task does wake
65 			 * up, it will immediately call try_to_freeze.
66 			 */
67 			if (!task_is_stopped_or_traced(p) &&
68 			    !freezer_should_skip(p))
69 				todo++;
70 		} while_each_thread(g, p);
71 		read_unlock(&tasklist_lock);
72 
73 		if (!sig_only) {
74 			wq_busy = freeze_workqueues_busy();
75 			todo += wq_busy;
76 		}
77 
78 		if (!todo || time_after(jiffies, end_time))
79 			break;
80 
81 		/*
82 		 * We need to retry, but first give the freezing tasks some
83 		 * time to enter the regrigerator.
84 		 */
85 		msleep(10);
86 	}
87 
88 	do_gettimeofday(&end);
89 	elapsed_csecs64 = timeval_to_ns(&end) - timeval_to_ns(&start);
90 	do_div(elapsed_csecs64, NSEC_PER_SEC / 100);
91 	elapsed_csecs = elapsed_csecs64;
92 
93 	if (todo) {
94 		/* This does not unfreeze processes that are already frozen
95 		 * (we have slightly ugly calling convention in that respect,
96 		 * and caller must call thaw_processes() if something fails),
97 		 * but it cleans up leftover PF_FREEZE requests.
98 		 */
99 		printk("\n");
100 		printk(KERN_ERR "Freezing of tasks failed after %d.%02d seconds "
101 		       "(%d tasks refusing to freeze, wq_busy=%d):\n",
102 		       elapsed_csecs / 100, elapsed_csecs % 100,
103 		       todo - wq_busy, wq_busy);
104 
105 		thaw_workqueues();
106 
107 		read_lock(&tasklist_lock);
108 		do_each_thread(g, p) {
109 			task_lock(p);
110 			if (freezing(p) && !freezer_should_skip(p))
111 				sched_show_task(p);
112 			cancel_freezing(p);
113 			task_unlock(p);
114 		} while_each_thread(g, p);
115 		read_unlock(&tasklist_lock);
116 	} else {
117 		printk("(elapsed %d.%02d seconds) ", elapsed_csecs / 100,
118 			elapsed_csecs % 100);
119 	}
120 
121 	return todo ? -EBUSY : 0;
122 }
123 
124 /**
125  *	freeze_processes - tell processes to enter the refrigerator
126  */
127 int freeze_processes(void)
128 {
129 	int error;
130 
131 	printk("Freezing user space processes ... ");
132 	error = try_to_freeze_tasks(true);
133 	if (error)
134 		goto Exit;
135 	printk("done.\n");
136 
137 	printk("Freezing remaining freezable tasks ... ");
138 	error = try_to_freeze_tasks(false);
139 	if (error)
140 		goto Exit;
141 	printk("done.");
142 
143 	oom_killer_disable();
144  Exit:
145 	BUG_ON(in_atomic());
146 	printk("\n");
147 
148 	return error;
149 }
150 
151 static void thaw_tasks(bool nosig_only)
152 {
153 	struct task_struct *g, *p;
154 
155 	read_lock(&tasklist_lock);
156 	do_each_thread(g, p) {
157 		if (!freezeable(p))
158 			continue;
159 
160 		if (nosig_only && should_send_signal(p))
161 			continue;
162 
163 		if (cgroup_freezing_or_frozen(p))
164 			continue;
165 
166 		thaw_process(p);
167 	} while_each_thread(g, p);
168 	read_unlock(&tasklist_lock);
169 }
170 
171 void thaw_processes(void)
172 {
173 	oom_killer_enable();
174 
175 	printk("Restarting tasks ... ");
176 	thaw_workqueues();
177 	thaw_tasks(true);
178 	thaw_tasks(false);
179 	schedule();
180 	printk("done.\n");
181 }
182 
183