xref: /linux/kernel/power/process.c (revision 757dea93e136b219af09d3cd56a81063fdbdef1a)
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/smp_lock.h>
12 #include <linux/interrupt.h>
13 #include <linux/suspend.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/freezer.h>
17 
18 /*
19  * Timeout for stopping processes
20  */
21 #define TIMEOUT	(20 * HZ)
22 
23 #define FREEZER_KERNEL_THREADS 0
24 #define FREEZER_USER_SPACE 1
25 
26 static inline int freezeable(struct task_struct * p)
27 {
28 	if ((p == current) ||
29 	    (p->flags & PF_NOFREEZE) ||
30 	    (p->exit_state != 0))
31 		return 0;
32 	return 1;
33 }
34 
35 /* Refrigerator is place where frozen processes are stored :-). */
36 void refrigerator(void)
37 {
38 	/* Hmm, should we be allowed to suspend when there are realtime
39 	   processes around? */
40 	long save;
41 	save = current->state;
42 	pr_debug("%s entered refrigerator\n", current->comm);
43 
44 	frozen_process(current);
45 	spin_lock_irq(&current->sighand->siglock);
46 	recalc_sigpending(); /* We sent fake signal, clean it up */
47 	spin_unlock_irq(&current->sighand->siglock);
48 
49 	for (;;) {
50 		set_current_state(TASK_UNINTERRUPTIBLE);
51 		if (!frozen(current))
52 			break;
53 		schedule();
54 	}
55 	pr_debug("%s left refrigerator\n", current->comm);
56 	current->state = save;
57 }
58 
59 static inline void freeze_process(struct task_struct *p)
60 {
61 	unsigned long flags;
62 
63 	if (!freezing(p)) {
64 		rmb();
65 		if (!frozen(p)) {
66 			if (p->state == TASK_STOPPED)
67 				force_sig_specific(SIGSTOP, p);
68 
69 			freeze(p);
70 			spin_lock_irqsave(&p->sighand->siglock, flags);
71 			signal_wake_up(p, p->state == TASK_STOPPED);
72 			spin_unlock_irqrestore(&p->sighand->siglock, flags);
73 		}
74 	}
75 }
76 
77 static void cancel_freezing(struct task_struct *p)
78 {
79 	unsigned long flags;
80 
81 	if (freezing(p)) {
82 		pr_debug("  clean up: %s\n", p->comm);
83 		do_not_freeze(p);
84 		spin_lock_irqsave(&p->sighand->siglock, flags);
85 		recalc_sigpending_tsk(p);
86 		spin_unlock_irqrestore(&p->sighand->siglock, flags);
87 	}
88 }
89 
90 static inline int is_user_space(struct task_struct *p)
91 {
92 	return p->mm && !(p->flags & PF_BORROWED_MM);
93 }
94 
95 static unsigned int try_to_freeze_tasks(int freeze_user_space)
96 {
97 	struct task_struct *g, *p;
98 	unsigned long end_time;
99 	unsigned int todo;
100 
101 	end_time = jiffies + TIMEOUT;
102 	do {
103 		todo = 0;
104 		read_lock(&tasklist_lock);
105 		do_each_thread(g, p) {
106 			if (!freezeable(p))
107 				continue;
108 
109 			if (frozen(p))
110 				continue;
111 
112 			if (p->state == TASK_TRACED && frozen(p->parent)) {
113 				cancel_freezing(p);
114 				continue;
115 			}
116 			if (is_user_space(p)) {
117 				if (!freeze_user_space)
118 					continue;
119 
120 				/* Freeze the task unless there is a vfork
121 				 * completion pending
122 				 */
123 				if (!p->vfork_done)
124 					freeze_process(p);
125 			} else {
126 				if (freeze_user_space)
127 					continue;
128 
129 				freeze_process(p);
130 			}
131 			todo++;
132 		} while_each_thread(g, p);
133 		read_unlock(&tasklist_lock);
134 		yield();			/* Yield is okay here */
135 		if (todo && time_after(jiffies, end_time))
136 			break;
137 	} while (todo);
138 
139 	if (todo) {
140 		/* This does not unfreeze processes that are already frozen
141 		 * (we have slightly ugly calling convention in that respect,
142 		 * and caller must call thaw_processes() if something fails),
143 		 * but it cleans up leftover PF_FREEZE requests.
144 		 */
145 		printk("\n");
146 		printk(KERN_ERR "Stopping %s timed out after %d seconds "
147 				"(%d tasks refusing to freeze):\n",
148 				freeze_user_space ? "user space processes" :
149 					"kernel threads",
150 				TIMEOUT / HZ, todo);
151 		read_lock(&tasklist_lock);
152 		do_each_thread(g, p) {
153 			if (is_user_space(p) == !freeze_user_space)
154 				continue;
155 
156 			if (freezeable(p) && !frozen(p))
157 				printk(KERN_ERR " %s\n", p->comm);
158 
159 			cancel_freezing(p);
160 		} while_each_thread(g, p);
161 		read_unlock(&tasklist_lock);
162 	}
163 
164 	return todo;
165 }
166 
167 /**
168  *	freeze_processes - tell processes to enter the refrigerator
169  *
170  *	Returns 0 on success, or the number of processes that didn't freeze,
171  *	although they were told to.
172  */
173 int freeze_processes(void)
174 {
175 	unsigned int nr_unfrozen;
176 
177 	printk("Stopping tasks ... ");
178 	nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
179 	if (nr_unfrozen)
180 		return nr_unfrozen;
181 
182 	sys_sync();
183 	nr_unfrozen = try_to_freeze_tasks(FREEZER_KERNEL_THREADS);
184 	if (nr_unfrozen)
185 		return nr_unfrozen;
186 
187 	printk("done.\n");
188 	BUG_ON(in_atomic());
189 	return 0;
190 }
191 
192 static void thaw_tasks(int thaw_user_space)
193 {
194 	struct task_struct *g, *p;
195 
196 	read_lock(&tasklist_lock);
197 	do_each_thread(g, p) {
198 		if (!freezeable(p))
199 			continue;
200 
201 		if (is_user_space(p) == !thaw_user_space)
202 			continue;
203 
204 		if (!thaw_process(p))
205 			printk(KERN_WARNING " Strange, %s not stopped\n",
206 				p->comm );
207 	} while_each_thread(g, p);
208 	read_unlock(&tasklist_lock);
209 }
210 
211 void thaw_processes(void)
212 {
213 	printk("Restarting tasks ... ");
214 	thaw_tasks(FREEZER_KERNEL_THREADS);
215 	thaw_tasks(FREEZER_USER_SPACE);
216 	schedule();
217 	printk("done.\n");
218 }
219 
220 EXPORT_SYMBOL(refrigerator);
221