2 * cgroup_freezer.c - control group freezer subsystem
4 * Copyright IBM Corporation, 2007
6 * Author : Cedric Le Goater <clg@fr.ibm.com>
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.
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.
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/cgroup.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>
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.
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 */
40 /* mask for all FREEZING flags */
41 CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
45 struct cgroup_subsys_state css;
49 static DEFINE_MUTEX(freezer_mutex);
51 static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
53 return css ? container_of(css, struct freezer, css) : NULL;
56 static inline struct freezer *task_freezer(struct task_struct *task)
58 return css_freezer(task_css(task, freezer_cgrp_id));
61 static struct freezer *parent_freezer(struct freezer *freezer)
63 return css_freezer(freezer->css.parent);
66 bool cgroup_freezing(struct task_struct *task)
71 ret = task_freezer(task)->state & CGROUP_FREEZING;
77 static const char *freezer_state_strs(unsigned int state)
79 if (state & CGROUP_FROZEN)
81 if (state & CGROUP_FREEZING)
86 static struct cgroup_subsys_state *
87 freezer_css_alloc(struct cgroup_subsys_state *parent_css)
89 struct freezer *freezer;
91 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
93 return ERR_PTR(-ENOMEM);
99 * freezer_css_online - commit creation of a freezer css
100 * @css: css being created
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
106 static int freezer_css_online(struct cgroup_subsys_state *css)
108 struct freezer *freezer = css_freezer(css);
109 struct freezer *parent = parent_freezer(freezer);
111 mutex_lock(&freezer_mutex);
113 freezer->state |= CGROUP_FREEZER_ONLINE;
115 if (parent && (parent->state & CGROUP_FREEZING)) {
116 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
117 static_branch_inc(&freezer_active);
120 mutex_unlock(&freezer_mutex);
125 * freezer_css_offline - initiate destruction of a freezer css
126 * @css: css being destroyed
128 * @css is going away. Mark it dead and decrement system_freezing_count if
129 * it was holding one.
131 static void freezer_css_offline(struct cgroup_subsys_state *css)
133 struct freezer *freezer = css_freezer(css);
135 mutex_lock(&freezer_mutex);
137 if (freezer->state & CGROUP_FREEZING)
138 static_branch_dec(&freezer_active);
142 mutex_unlock(&freezer_mutex);
145 static void freezer_css_free(struct cgroup_subsys_state *css)
147 kfree(css_freezer(css));
151 * Tasks can be migrated into a different freezer anytime regardless of its
152 * current state. freezer_attach() is responsible for making new tasks
153 * conform to the current state.
155 * Freezer state changes and task migration are synchronized via
156 * @freezer->lock. freezer_attach() makes the new tasks conform to the
157 * current state and all following state changes can see the new tasks.
159 static void freezer_attach(struct cgroup_taskset *tset)
161 struct task_struct *task;
162 struct cgroup_subsys_state *new_css;
164 mutex_lock(&freezer_mutex);
167 * Make the new tasks conform to the current state of @new_css.
168 * For simplicity, when migrating any task to a FROZEN cgroup, we
169 * revert it to FREEZING and let update_if_frozen() determine the
170 * correct state later.
172 * Tasks in @tset are on @new_css but may not conform to its
173 * current state before executing the following - !frozen tasks may
174 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
176 cgroup_taskset_for_each(task, new_css, tset) {
177 struct freezer *freezer = css_freezer(new_css);
179 if (!(freezer->state & CGROUP_FREEZING)) {
184 /* clear FROZEN and propagate upwards */
185 while (freezer && (freezer->state & CGROUP_FROZEN)) {
186 freezer->state &= ~CGROUP_FROZEN;
187 freezer = parent_freezer(freezer);
192 mutex_unlock(&freezer_mutex);
196 * freezer_fork - cgroup post fork callback
197 * @task: a task which has just been forked
199 * @task has just been created and should conform to the current state of
200 * the cgroup_freezer it belongs to. This function may race against
201 * freezer_attach(). Losing to freezer_attach() means that we don't have
202 * to do anything as freezer_attach() will put @task into the appropriate
205 static void freezer_fork(struct task_struct *task)
207 struct freezer *freezer;
210 * The root cgroup is non-freezable, so we can skip locking the
211 * freezer. This is safe regardless of race with task migration.
212 * If we didn't race or won, skipping is obviously the right thing
213 * to do. If we lost and root is the new cgroup, noop is still the
216 if (task_css_is_root(task, freezer_cgrp_id))
219 mutex_lock(&freezer_mutex);
222 freezer = task_freezer(task);
223 if (freezer->state & CGROUP_FREEZING)
227 mutex_unlock(&freezer_mutex);
231 * update_if_frozen - update whether a cgroup finished freezing
232 * @css: css of interest
234 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
235 * calling this function. If the current state is FREEZING but not FROZEN,
236 * this function checks whether all tasks of this cgroup and the descendant
237 * cgroups finished freezing and, if so, sets FROZEN.
239 * The caller is responsible for grabbing RCU read lock and calling
240 * update_if_frozen() on all descendants prior to invoking this function.
242 * Task states and freezer state might disagree while tasks are being
243 * migrated into or out of @css, so we can't verify task states against
244 * @freezer state here. See freezer_attach() for details.
246 static void update_if_frozen(struct cgroup_subsys_state *css)
248 struct freezer *freezer = css_freezer(css);
249 struct cgroup_subsys_state *pos;
250 struct css_task_iter it;
251 struct task_struct *task;
253 lockdep_assert_held(&freezer_mutex);
255 if (!(freezer->state & CGROUP_FREEZING) ||
256 (freezer->state & CGROUP_FROZEN))
259 /* are all (live) children frozen? */
261 css_for_each_child(pos, css) {
262 struct freezer *child = css_freezer(pos);
264 if ((child->state & CGROUP_FREEZER_ONLINE) &&
265 !(child->state & CGROUP_FROZEN)) {
272 /* are all tasks frozen? */
273 css_task_iter_start(css, 0, &it);
275 while ((task = css_task_iter_next(&it))) {
276 if (freezing(task) && !frozen(task))
280 freezer->state |= CGROUP_FROZEN;
282 css_task_iter_end(&it);
285 static int freezer_read(struct seq_file *m, void *v)
287 struct cgroup_subsys_state *css = seq_css(m), *pos;
289 mutex_lock(&freezer_mutex);
292 /* update states bottom-up */
293 css_for_each_descendant_post(pos, css) {
294 if (!css_tryget_online(pos))
298 update_if_frozen(pos);
305 mutex_unlock(&freezer_mutex);
307 seq_puts(m, freezer_state_strs(css_freezer(css)->state));
312 static void freeze_cgroup(struct freezer *freezer)
314 struct css_task_iter it;
315 struct task_struct *task;
317 css_task_iter_start(&freezer->css, 0, &it);
318 while ((task = css_task_iter_next(&it)))
320 css_task_iter_end(&it);
323 static void unfreeze_cgroup(struct freezer *freezer)
325 struct css_task_iter it;
326 struct task_struct *task;
328 css_task_iter_start(&freezer->css, 0, &it);
329 while ((task = css_task_iter_next(&it)))
331 css_task_iter_end(&it);
335 * freezer_apply_state - apply state change to a single cgroup_freezer
336 * @freezer: freezer to apply state change to
337 * @freeze: whether to freeze or unfreeze
338 * @state: CGROUP_FREEZING_* flag to set or clear
340 * Set or clear @state on @cgroup according to @freeze, and perform
341 * freezing or thawing as necessary.
343 static void freezer_apply_state(struct freezer *freezer, bool freeze,
346 /* also synchronizes against task migration, see freezer_attach() */
347 lockdep_assert_held(&freezer_mutex);
349 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
353 if (!(freezer->state & CGROUP_FREEZING))
354 static_branch_inc_cpuslocked(&freezer_active);
355 freezer->state |= state;
356 freeze_cgroup(freezer);
358 bool was_freezing = freezer->state & CGROUP_FREEZING;
360 freezer->state &= ~state;
362 if (!(freezer->state & CGROUP_FREEZING)) {
363 freezer->state &= ~CGROUP_FROZEN;
365 static_branch_dec_cpuslocked(&freezer_active);
366 unfreeze_cgroup(freezer);
372 * freezer_change_state - change the freezing state of a cgroup_freezer
373 * @freezer: freezer of interest
374 * @freeze: whether to freeze or thaw
376 * Freeze or thaw @freezer according to @freeze. The operations are
377 * recursive - all descendants of @freezer will be affected.
379 static void freezer_change_state(struct freezer *freezer, bool freeze)
381 struct cgroup_subsys_state *pos;
385 * Update all its descendants in pre-order traversal. Each
386 * descendant will try to inherit its parent's FREEZING state as
387 * CGROUP_FREEZING_PARENT.
389 mutex_lock(&freezer_mutex);
391 css_for_each_descendant_pre(pos, &freezer->css) {
392 struct freezer *pos_f = css_freezer(pos);
393 struct freezer *parent = parent_freezer(pos_f);
395 if (!css_tryget_online(pos))
399 if (pos_f == freezer)
400 freezer_apply_state(pos_f, freeze,
401 CGROUP_FREEZING_SELF);
403 freezer_apply_state(pos_f,
404 parent->state & CGROUP_FREEZING,
405 CGROUP_FREEZING_PARENT);
411 mutex_unlock(&freezer_mutex);
415 static ssize_t freezer_write(struct kernfs_open_file *of,
416 char *buf, size_t nbytes, loff_t off)
422 if (strcmp(buf, freezer_state_strs(0)) == 0)
424 else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0)
429 freezer_change_state(css_freezer(of_css(of)), freeze);
433 static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
436 struct freezer *freezer = css_freezer(css);
438 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
441 static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
444 struct freezer *freezer = css_freezer(css);
446 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
449 static struct cftype files[] = {
452 .flags = CFTYPE_NOT_ON_ROOT,
453 .seq_show = freezer_read,
454 .write = freezer_write,
457 .name = "self_freezing",
458 .flags = CFTYPE_NOT_ON_ROOT,
459 .read_u64 = freezer_self_freezing_read,
462 .name = "parent_freezing",
463 .flags = CFTYPE_NOT_ON_ROOT,
464 .read_u64 = freezer_parent_freezing_read,
469 struct cgroup_subsys freezer_cgrp_subsys = {
470 .css_alloc = freezer_css_alloc,
471 .css_online = freezer_css_online,
472 .css_offline = freezer_css_offline,
473 .css_free = freezer_css_free,
474 .attach = freezer_attach,
475 .fork = freezer_fork,
476 .legacy_cftypes = files,