1 /*
2  * cgroup_freezer.c -  control group freezer subsystem
3  *
4  * Copyright IBM Corporation, 2007
5  *
6  * Author : Cedric Le Goater <clg@fr.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2.1 of the GNU Lesser General Public License
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it would be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  */
16 
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/cgroup.h>
20 #include <linux/fs.h>
21 #include <linux/uaccess.h>
22 #include <linux/freezer.h>
23 #include <linux/seq_file.h>
24 #include <linux/mutex.h>
25 #include <linux/cpu.h>
26 
27 /*
28  * A cgroup is freezing if any FREEZING flags are set.  FREEZING_SELF is
29  * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
30  * for "THAWED".  FREEZING_PARENT is set if the parent freezer is FREEZING
31  * for whatever reason.  IOW, a cgroup has FREEZING_PARENT set if one of
32  * its ancestors has FREEZING_SELF set.
33  */
34 enum freezer_state_flags {
35 	CGROUP_FREEZER_ONLINE	= (1 << 0), /* freezer is fully online */
36 	CGROUP_FREEZING_SELF	= (1 << 1), /* this freezer is freezing */
37 	CGROUP_FREEZING_PARENT	= (1 << 2), /* the parent freezer is freezing */
38 	CGROUP_FROZEN		= (1 << 3), /* this and its descendants frozen */
39 
40 	/* mask for all FREEZING flags */
41 	CGROUP_FREEZING		= CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
42 };
43 
44 struct freezer {
45 	struct cgroup_subsys_state	css;
46 	unsigned int			state;
47 };
48 
49 static DEFINE_MUTEX(freezer_mutex);
50 
51 static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
52 {
53 	return css ? container_of(css, struct freezer, css) : NULL;
54 }
55 
56 static inline struct freezer *task_freezer(struct task_struct *task)
57 {
58 	return css_freezer(task_css(task, freezer_cgrp_id));
59 }
60 
61 static struct freezer *parent_freezer(struct freezer *freezer)
62 {
63 	return css_freezer(freezer->css.parent);
64 }
65 
66 bool cgroup_freezing(struct task_struct *task)
67 {
68 	bool ret;
69 
70 	rcu_read_lock();
71 	ret = task_freezer(task)->state & CGROUP_FREEZING;
72 	rcu_read_unlock();
73 
74 	return ret;
75 }
76 
77 static const char *freezer_state_strs(unsigned int state)
78 {
79 	if (state & CGROUP_FROZEN)
80 		return "FROZEN";
81 	if (state & CGROUP_FREEZING)
82 		return "FREEZING";
83 	return "THAWED";
84 };
85 
86 static struct cgroup_subsys_state *
87 freezer_css_alloc(struct cgroup_subsys_state *parent_css)
88 {
89 	struct freezer *freezer;
90 
91 	freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
92 	if (!freezer)
93 		return ERR_PTR(-ENOMEM);
94 
95 	return &freezer->css;
96 }
97 
98 /**
99  * freezer_css_online - commit creation of a freezer css
100  * @css: css being created
101  *
102  * We're committing to creation of @css.  Mark it online and inherit
103  * parent's freezing state while holding both parent's and our
104  * freezer->lock.
105  */
106 static int freezer_css_online(struct cgroup_subsys_state *css)
107 {
108 	struct freezer *freezer = css_freezer(css);
109 	struct freezer *parent = parent_freezer(freezer);
110 
111 	cpus_read_lock();
112 	mutex_lock(&freezer_mutex);
113 
114 	freezer->state |= CGROUP_FREEZER_ONLINE;
115 
116 	if (parent && (parent->state & CGROUP_FREEZING)) {
117 		freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
118 		static_branch_inc_cpuslocked(&freezer_active);
119 	}
120 
121 	mutex_unlock(&freezer_mutex);
122 	cpus_read_unlock();
123 	return 0;
124 }
125 
126 /**
127  * freezer_css_offline - initiate destruction of a freezer css
128  * @css: css being destroyed
129  *
130  * @css is going away.  Mark it dead and decrement system_freezing_count if
131  * it was holding one.
132  */
133 static void freezer_css_offline(struct cgroup_subsys_state *css)
134 {
135 	struct freezer *freezer = css_freezer(css);
136 
137 	cpus_read_lock();
138 	mutex_lock(&freezer_mutex);
139 
140 	if (freezer->state & CGROUP_FREEZING)
141 		static_branch_dec_cpuslocked(&freezer_active);
142 
143 	freezer->state = 0;
144 
145 	mutex_unlock(&freezer_mutex);
146 	cpus_read_unlock();
147 }
148 
149 static void freezer_css_free(struct cgroup_subsys_state *css)
150 {
151 	kfree(css_freezer(css));
152 }
153 
154 /*
155  * Tasks can be migrated into a different freezer anytime regardless of its
156  * current state.  freezer_attach() is responsible for making new tasks
157  * conform to the current state.
158  *
159  * Freezer state changes and task migration are synchronized via
160  * @freezer->lock.  freezer_attach() makes the new tasks conform to the
161  * current state and all following state changes can see the new tasks.
162  */
163 static void freezer_attach(struct cgroup_taskset *tset)
164 {
165 	struct task_struct *task;
166 	struct cgroup_subsys_state *new_css;
167 
168 	mutex_lock(&freezer_mutex);
169 
170 	/*
171 	 * Make the new tasks conform to the current state of @new_css.
172 	 * For simplicity, when migrating any task to a FROZEN cgroup, we
173 	 * revert it to FREEZING and let update_if_frozen() determine the
174 	 * correct state later.
175 	 *
176 	 * Tasks in @tset are on @new_css but may not conform to its
177 	 * current state before executing the following - !frozen tasks may
178 	 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
179 	 */
180 	cgroup_taskset_for_each(task, new_css, tset) {
181 		struct freezer *freezer = css_freezer(new_css);
182 
183 		if (!(freezer->state & CGROUP_FREEZING)) {
184 			__thaw_task(task);
185 		} else {
186 			freeze_task(task);
187 
188 			/* clear FROZEN and propagate upwards */
189 			while (freezer && (freezer->state & CGROUP_FROZEN)) {
190 				freezer->state &= ~CGROUP_FROZEN;
191 				freezer = parent_freezer(freezer);
192 			}
193 		}
194 	}
195 
196 	mutex_unlock(&freezer_mutex);
197 }
198 
199 /**
200  * freezer_fork - cgroup post fork callback
201  * @task: a task which has just been forked
202  *
203  * @task has just been created and should conform to the current state of
204  * the cgroup_freezer it belongs to.  This function may race against
205  * freezer_attach().  Losing to freezer_attach() means that we don't have
206  * to do anything as freezer_attach() will put @task into the appropriate
207  * state.
208  */
209 static void freezer_fork(struct task_struct *task)
210 {
211 	struct freezer *freezer;
212 
213 	/*
214 	 * The root cgroup is non-freezable, so we can skip locking the
215 	 * freezer.  This is safe regardless of race with task migration.
216 	 * If we didn't race or won, skipping is obviously the right thing
217 	 * to do.  If we lost and root is the new cgroup, noop is still the
218 	 * right thing to do.
219 	 */
220 	if (task_css_is_root(task, freezer_cgrp_id))
221 		return;
222 
223 	mutex_lock(&freezer_mutex);
224 	rcu_read_lock();
225 
226 	freezer = task_freezer(task);
227 	if (freezer->state & CGROUP_FREEZING)
228 		freeze_task(task);
229 
230 	rcu_read_unlock();
231 	mutex_unlock(&freezer_mutex);
232 }
233 
234 /**
235  * update_if_frozen - update whether a cgroup finished freezing
236  * @css: css of interest
237  *
238  * Once FREEZING is initiated, transition to FROZEN is lazily updated by
239  * calling this function.  If the current state is FREEZING but not FROZEN,
240  * this function checks whether all tasks of this cgroup and the descendant
241  * cgroups finished freezing and, if so, sets FROZEN.
242  *
243  * The caller is responsible for grabbing RCU read lock and calling
244  * update_if_frozen() on all descendants prior to invoking this function.
245  *
246  * Task states and freezer state might disagree while tasks are being
247  * migrated into or out of @css, so we can't verify task states against
248  * @freezer state here.  See freezer_attach() for details.
249  */
250 static void update_if_frozen(struct cgroup_subsys_state *css)
251 {
252 	struct freezer *freezer = css_freezer(css);
253 	struct cgroup_subsys_state *pos;
254 	struct css_task_iter it;
255 	struct task_struct *task;
256 
257 	lockdep_assert_held(&freezer_mutex);
258 
259 	if (!(freezer->state & CGROUP_FREEZING) ||
260 	    (freezer->state & CGROUP_FROZEN))
261 		return;
262 
263 	/* are all (live) children frozen? */
264 	rcu_read_lock();
265 	css_for_each_child(pos, css) {
266 		struct freezer *child = css_freezer(pos);
267 
268 		if ((child->state & CGROUP_FREEZER_ONLINE) &&
269 		    !(child->state & CGROUP_FROZEN)) {
270 			rcu_read_unlock();
271 			return;
272 		}
273 	}
274 	rcu_read_unlock();
275 
276 	/* are all tasks frozen? */
277 	css_task_iter_start(css, 0, &it);
278 
279 	while ((task = css_task_iter_next(&it))) {
280 		if (freezing(task) && !frozen(task))
281 			goto out_iter_end;
282 	}
283 
284 	freezer->state |= CGROUP_FROZEN;
285 out_iter_end:
286 	css_task_iter_end(&it);
287 }
288 
289 static int freezer_read(struct seq_file *m, void *v)
290 {
291 	struct cgroup_subsys_state *css = seq_css(m), *pos;
292 
293 	mutex_lock(&freezer_mutex);
294 	rcu_read_lock();
295 
296 	/* update states bottom-up */
297 	css_for_each_descendant_post(pos, css) {
298 		if (!css_tryget_online(pos))
299 			continue;
300 		rcu_read_unlock();
301 
302 		update_if_frozen(pos);
303 
304 		rcu_read_lock();
305 		css_put(pos);
306 	}
307 
308 	rcu_read_unlock();
309 	mutex_unlock(&freezer_mutex);
310 
311 	seq_puts(m, freezer_state_strs(css_freezer(css)->state));
312 	seq_putc(m, '\n');
313 	return 0;
314 }
315 
316 static void freeze_cgroup(struct freezer *freezer)
317 {
318 	struct css_task_iter it;
319 	struct task_struct *task;
320 
321 	css_task_iter_start(&freezer->css, 0, &it);
322 	while ((task = css_task_iter_next(&it)))
323 		freeze_task(task);
324 	css_task_iter_end(&it);
325 }
326 
327 static void unfreeze_cgroup(struct freezer *freezer)
328 {
329 	struct css_task_iter it;
330 	struct task_struct *task;
331 
332 	css_task_iter_start(&freezer->css, 0, &it);
333 	while ((task = css_task_iter_next(&it)))
334 		__thaw_task(task);
335 	css_task_iter_end(&it);
336 }
337 
338 /**
339  * freezer_apply_state - apply state change to a single cgroup_freezer
340  * @freezer: freezer to apply state change to
341  * @freeze: whether to freeze or unfreeze
342  * @state: CGROUP_FREEZING_* flag to set or clear
343  *
344  * Set or clear @state on @cgroup according to @freeze, and perform
345  * freezing or thawing as necessary.
346  */
347 static void freezer_apply_state(struct freezer *freezer, bool freeze,
348 				unsigned int state)
349 {
350 	/* also synchronizes against task migration, see freezer_attach() */
351 	lockdep_assert_held(&freezer_mutex);
352 
353 	if (!(freezer->state & CGROUP_FREEZER_ONLINE))
354 		return;
355 
356 	if (freeze) {
357 		if (!(freezer->state & CGROUP_FREEZING))
358 			static_branch_inc_cpuslocked(&freezer_active);
359 		freezer->state |= state;
360 		freeze_cgroup(freezer);
361 	} else {
362 		bool was_freezing = freezer->state & CGROUP_FREEZING;
363 
364 		freezer->state &= ~state;
365 
366 		if (!(freezer->state & CGROUP_FREEZING)) {
367 			freezer->state &= ~CGROUP_FROZEN;
368 			if (was_freezing)
369 				static_branch_dec_cpuslocked(&freezer_active);
370 			unfreeze_cgroup(freezer);
371 		}
372 	}
373 }
374 
375 /**
376  * freezer_change_state - change the freezing state of a cgroup_freezer
377  * @freezer: freezer of interest
378  * @freeze: whether to freeze or thaw
379  *
380  * Freeze or thaw @freezer according to @freeze.  The operations are
381  * recursive - all descendants of @freezer will be affected.
382  */
383 static void freezer_change_state(struct freezer *freezer, bool freeze)
384 {
385 	struct cgroup_subsys_state *pos;
386 
387 	cpus_read_lock();
388 	/*
389 	 * Update all its descendants in pre-order traversal.  Each
390 	 * descendant will try to inherit its parent's FREEZING state as
391 	 * CGROUP_FREEZING_PARENT.
392 	 */
393 	mutex_lock(&freezer_mutex);
394 	rcu_read_lock();
395 	css_for_each_descendant_pre(pos, &freezer->css) {
396 		struct freezer *pos_f = css_freezer(pos);
397 		struct freezer *parent = parent_freezer(pos_f);
398 
399 		if (!css_tryget_online(pos))
400 			continue;
401 		rcu_read_unlock();
402 
403 		if (pos_f == freezer)
404 			freezer_apply_state(pos_f, freeze,
405 					    CGROUP_FREEZING_SELF);
406 		else
407 			freezer_apply_state(pos_f,
408 					    parent->state & CGROUP_FREEZING,
409 					    CGROUP_FREEZING_PARENT);
410 
411 		rcu_read_lock();
412 		css_put(pos);
413 	}
414 	rcu_read_unlock();
415 	mutex_unlock(&freezer_mutex);
416 	cpus_read_unlock();
417 }
418 
419 static ssize_t freezer_write(struct kernfs_open_file *of,
420 			     char *buf, size_t nbytes, loff_t off)
421 {
422 	bool freeze;
423 
424 	buf = strstrip(buf);
425 
426 	if (strcmp(buf, freezer_state_strs(0)) == 0)
427 		freeze = false;
428 	else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0)
429 		freeze = true;
430 	else
431 		return -EINVAL;
432 
433 	freezer_change_state(css_freezer(of_css(of)), freeze);
434 	return nbytes;
435 }
436 
437 static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
438 				      struct cftype *cft)
439 {
440 	struct freezer *freezer = css_freezer(css);
441 
442 	return (bool)(freezer->state & CGROUP_FREEZING_SELF);
443 }
444 
445 static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
446 					struct cftype *cft)
447 {
448 	struct freezer *freezer = css_freezer(css);
449 
450 	return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
451 }
452 
453 static struct cftype files[] = {
454 	{
455 		.name = "state",
456 		.flags = CFTYPE_NOT_ON_ROOT,
457 		.seq_show = freezer_read,
458 		.write = freezer_write,
459 	},
460 	{
461 		.name = "self_freezing",
462 		.flags = CFTYPE_NOT_ON_ROOT,
463 		.read_u64 = freezer_self_freezing_read,
464 	},
465 	{
466 		.name = "parent_freezing",
467 		.flags = CFTYPE_NOT_ON_ROOT,
468 		.read_u64 = freezer_parent_freezing_read,
469 	},
470 	{ }	/* terminate */
471 };
472 
473 struct cgroup_subsys freezer_cgrp_subsys = {
474 	.css_alloc	= freezer_css_alloc,
475 	.css_online	= freezer_css_online,
476 	.css_offline	= freezer_css_offline,
477 	.css_free	= freezer_css_free,
478 	.attach		= freezer_attach,
479 	.fork		= freezer_fork,
480 	.legacy_cftypes	= files,
481 };
482