2 * cgroup_freezer.c - control group freezer subsystem
4 * Copyright IBM Corporation, 2007
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);
112 mutex_lock(&freezer_mutex);
114 freezer->state |= CGROUP_FREEZER_ONLINE;
116 if (parent && (parent->state & CGROUP_FREEZING)) {
117 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
118 static_branch_inc_cpuslocked(&freezer_active);
121 mutex_unlock(&freezer_mutex);
127 * freezer_css_offline - initiate destruction of a freezer css
128 * @css: css being destroyed
130 * @css is going away. Mark it dead and decrement system_freezing_count if
131 * it was holding one.
133 static void freezer_css_offline(struct cgroup_subsys_state *css)
135 struct freezer *freezer = css_freezer(css);
138 mutex_lock(&freezer_mutex);
140 if (freezer->state & CGROUP_FREEZING)
141 static_branch_dec_cpuslocked(&freezer_active);
145 mutex_unlock(&freezer_mutex);
149 static void freezer_css_free(struct cgroup_subsys_state *css)
151 kfree(css_freezer(css));
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.
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.
163 static void freezer_attach(struct cgroup_taskset *tset)
165 struct task_struct *task;
166 struct cgroup_subsys_state *new_css;
168 mutex_lock(&freezer_mutex);
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.
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.
180 cgroup_taskset_for_each(task, new_css, tset) {
181 struct freezer *freezer = css_freezer(new_css);
183 if (!(freezer->state & CGROUP_FREEZING)) {
188 /* clear FROZEN and propagate upwards */
189 while (freezer && (freezer->state & CGROUP_FROZEN)) {
190 freezer->state &= ~CGROUP_FROZEN;
191 freezer = parent_freezer(freezer);
196 mutex_unlock(&freezer_mutex);
200 * freezer_fork - cgroup post fork callback
201 * @task: a task which has just been forked
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
209 static void freezer_fork(struct task_struct *task)
211 struct freezer *freezer;
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
220 if (task_css_is_root(task, freezer_cgrp_id))
223 mutex_lock(&freezer_mutex);
226 freezer = task_freezer(task);
227 if (freezer->state & CGROUP_FREEZING)
231 mutex_unlock(&freezer_mutex);
235 * update_if_frozen - update whether a cgroup finished freezing
236 * @css: css of interest
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.
243 * The caller is responsible for grabbing RCU read lock and calling
244 * update_if_frozen() on all descendants prior to invoking this function.
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.
250 static void update_if_frozen(struct cgroup_subsys_state *css)
252 struct freezer *freezer = css_freezer(css);
253 struct cgroup_subsys_state *pos;
254 struct css_task_iter it;
255 struct task_struct *task;
257 lockdep_assert_held(&freezer_mutex);
259 if (!(freezer->state & CGROUP_FREEZING) ||
260 (freezer->state & CGROUP_FROZEN))
263 /* are all (live) children frozen? */
265 css_for_each_child(pos, css) {
266 struct freezer *child = css_freezer(pos);
268 if ((child->state & CGROUP_FREEZER_ONLINE) &&
269 !(child->state & CGROUP_FROZEN)) {
276 /* are all tasks frozen? */
277 css_task_iter_start(css, 0, &it);
279 while ((task = css_task_iter_next(&it))) {
280 if (freezing(task) && !frozen(task))
284 freezer->state |= CGROUP_FROZEN;
286 css_task_iter_end(&it);
289 static int freezer_read(struct seq_file *m, void *v)
291 struct cgroup_subsys_state *css = seq_css(m), *pos;
293 mutex_lock(&freezer_mutex);
296 /* update states bottom-up */
297 css_for_each_descendant_post(pos, css) {
298 if (!css_tryget_online(pos))
302 update_if_frozen(pos);
309 mutex_unlock(&freezer_mutex);
311 seq_puts(m, freezer_state_strs(css_freezer(css)->state));
316 static void freeze_cgroup(struct freezer *freezer)
318 struct css_task_iter it;
319 struct task_struct *task;
321 css_task_iter_start(&freezer->css, 0, &it);
322 while ((task = css_task_iter_next(&it)))
324 css_task_iter_end(&it);
327 static void unfreeze_cgroup(struct freezer *freezer)
329 struct css_task_iter it;
330 struct task_struct *task;
332 css_task_iter_start(&freezer->css, 0, &it);
333 while ((task = css_task_iter_next(&it)))
335 css_task_iter_end(&it);
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
344 * Set or clear @state on @cgroup according to @freeze, and perform
345 * freezing or thawing as necessary.
347 static void freezer_apply_state(struct freezer *freezer, bool freeze,
350 /* also synchronizes against task migration, see freezer_attach() */
351 lockdep_assert_held(&freezer_mutex);
353 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
357 if (!(freezer->state & CGROUP_FREEZING))
358 static_branch_inc_cpuslocked(&freezer_active);
359 freezer->state |= state;
360 freeze_cgroup(freezer);
362 bool was_freezing = freezer->state & CGROUP_FREEZING;
364 freezer->state &= ~state;
366 if (!(freezer->state & CGROUP_FREEZING)) {
367 freezer->state &= ~CGROUP_FROZEN;
369 static_branch_dec_cpuslocked(&freezer_active);
370 unfreeze_cgroup(freezer);
376 * freezer_change_state - change the freezing state of a cgroup_freezer
377 * @freezer: freezer of interest
378 * @freeze: whether to freeze or thaw
380 * Freeze or thaw @freezer according to @freeze. The operations are
381 * recursive - all descendants of @freezer will be affected.
383 static void freezer_change_state(struct freezer *freezer, bool freeze)
385 struct cgroup_subsys_state *pos;
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.
393 mutex_lock(&freezer_mutex);
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);
399 if (!css_tryget_online(pos))
403 if (pos_f == freezer)
404 freezer_apply_state(pos_f, freeze,
405 CGROUP_FREEZING_SELF);
407 freezer_apply_state(pos_f,
408 parent->state & CGROUP_FREEZING,
409 CGROUP_FREEZING_PARENT);
415 mutex_unlock(&freezer_mutex);
419 static ssize_t freezer_write(struct kernfs_open_file *of,
420 char *buf, size_t nbytes, loff_t off)
426 if (strcmp(buf, freezer_state_strs(0)) == 0)
428 else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0)
433 freezer_change_state(css_freezer(of_css(of)), freeze);
437 static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
440 struct freezer *freezer = css_freezer(css);
442 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
445 static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
448 struct freezer *freezer = css_freezer(css);
450 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
453 static struct cftype files[] = {
456 .flags = CFTYPE_NOT_ON_ROOT,
457 .seq_show = freezer_read,
458 .write = freezer_write,
461 .name = "self_freezing",
462 .flags = CFTYPE_NOT_ON_ROOT,
463 .read_u64 = freezer_self_freezing_read,
466 .name = "parent_freezing",
467 .flags = CFTYPE_NOT_ON_ROOT,
468 .read_u64 = freezer_parent_freezing_read,
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,