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>
32 struct cgroup_subsys_state css;
33 enum freezer_state state;
34 spinlock_t lock; /* protects _writes_ to state */
37 static inline struct freezer *cgroup_freezer(
38 struct cgroup *cgroup)
41 cgroup_subsys_state(cgroup, freezer_subsys_id),
45 static inline struct freezer *task_freezer(struct task_struct *task)
47 return container_of(task_subsys_state(task, freezer_subsys_id),
51 bool cgroup_freezing(struct task_struct *task)
53 enum freezer_state state;
57 state = task_freezer(task)->state;
58 ret = state == CGROUP_FREEZING || state == CGROUP_FROZEN;
65 * cgroups_write_string() limits the size of freezer state strings to
66 * CGROUP_LOCAL_BUFFER_SIZE
68 static const char *freezer_state_strs[] = {
76 * Transitions are caused by userspace writes to the freezer.state file.
77 * The values in parenthesis are state labels. The rest are edge labels.
79 * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
81 * | \_______THAWED_______/ |
82 * \__________________________THAWED____________/
85 struct cgroup_subsys freezer_subsys;
87 static struct cgroup_subsys_state *freezer_create(struct cgroup *cgroup)
89 struct freezer *freezer;
91 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
93 return ERR_PTR(-ENOMEM);
95 spin_lock_init(&freezer->lock);
96 freezer->state = CGROUP_THAWED;
100 static void freezer_destroy(struct cgroup *cgroup)
102 struct freezer *freezer = cgroup_freezer(cgroup);
104 if (freezer->state != CGROUP_THAWED)
105 atomic_dec(&system_freezing_cnt);
110 * Tasks can be migrated into a different freezer anytime regardless of its
111 * current state. freezer_attach() is responsible for making new tasks
112 * conform to the current state.
114 * Freezer state changes and task migration are synchronized via
115 * @freezer->lock. freezer_attach() makes the new tasks conform to the
116 * current state and all following state changes can see the new tasks.
118 static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
120 struct freezer *freezer = cgroup_freezer(new_cgrp);
121 struct task_struct *task;
123 spin_lock_irq(&freezer->lock);
126 * Make the new tasks conform to the current state of @new_cgrp.
127 * For simplicity, when migrating any task to a FROZEN cgroup, we
128 * revert it to FREEZING and let update_if_frozen() determine the
129 * correct state later.
131 * Tasks in @tset are on @new_cgrp but may not conform to its
132 * current state before executing the following - !frozen tasks may
133 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
134 * This means that, to determine whether to freeze, one should test
135 * whether the state equals THAWED.
137 cgroup_taskset_for_each(task, new_cgrp, tset) {
138 if (freezer->state == CGROUP_THAWED) {
142 freezer->state = CGROUP_FREEZING;
146 spin_unlock_irq(&freezer->lock);
149 static void freezer_fork(struct task_struct *task)
151 struct freezer *freezer;
154 freezer = task_freezer(task);
157 * The root cgroup is non-freezable, so we can skip the
160 if (!freezer->css.cgroup->parent)
163 spin_lock_irq(&freezer->lock);
165 * @task might have been just migrated into a FROZEN cgroup. Test
166 * equality with THAWED. Read the comment in freezer_attach().
168 if (freezer->state != CGROUP_THAWED)
170 spin_unlock_irq(&freezer->lock);
176 * We change from FREEZING to FROZEN lazily if the cgroup was only
177 * partially frozen when we exitted write. Caller must hold freezer->lock.
179 * Task states and freezer state might disagree while tasks are being
180 * migrated into or out of @cgroup, so we can't verify task states against
181 * @freezer state here. See freezer_attach() for details.
183 static void update_if_frozen(struct cgroup *cgroup, struct freezer *freezer)
185 struct cgroup_iter it;
186 struct task_struct *task;
188 if (freezer->state != CGROUP_FREEZING)
191 cgroup_iter_start(cgroup, &it);
193 while ((task = cgroup_iter_next(cgroup, &it))) {
194 if (freezing(task)) {
196 * freezer_should_skip() indicates that the task
197 * should be skipped when determining freezing
198 * completion. Consider it frozen in addition to
199 * the usual frozen condition.
201 if (!frozen(task) && !freezer_should_skip(task))
206 freezer->state = CGROUP_FROZEN;
208 cgroup_iter_end(cgroup, &it);
211 static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
214 struct freezer *freezer;
215 enum freezer_state state;
217 freezer = cgroup_freezer(cgroup);
218 spin_lock_irq(&freezer->lock);
219 update_if_frozen(cgroup, freezer);
220 state = freezer->state;
221 spin_unlock_irq(&freezer->lock);
223 seq_puts(m, freezer_state_strs[state]);
228 static void freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
230 struct cgroup_iter it;
231 struct task_struct *task;
233 cgroup_iter_start(cgroup, &it);
234 while ((task = cgroup_iter_next(cgroup, &it)))
236 cgroup_iter_end(cgroup, &it);
239 static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
241 struct cgroup_iter it;
242 struct task_struct *task;
244 cgroup_iter_start(cgroup, &it);
245 while ((task = cgroup_iter_next(cgroup, &it)))
247 cgroup_iter_end(cgroup, &it);
250 static void freezer_change_state(struct cgroup *cgroup,
251 enum freezer_state goal_state)
253 struct freezer *freezer = cgroup_freezer(cgroup);
255 /* also synchronizes against task migration, see freezer_attach() */
256 spin_lock_irq(&freezer->lock);
258 switch (goal_state) {
260 if (freezer->state != CGROUP_THAWED)
261 atomic_dec(&system_freezing_cnt);
262 freezer->state = CGROUP_THAWED;
263 unfreeze_cgroup(cgroup, freezer);
266 if (freezer->state == CGROUP_THAWED)
267 atomic_inc(&system_freezing_cnt);
268 freezer->state = CGROUP_FREEZING;
269 freeze_cgroup(cgroup, freezer);
275 spin_unlock_irq(&freezer->lock);
278 static int freezer_write(struct cgroup *cgroup,
282 enum freezer_state goal_state;
284 if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
285 goal_state = CGROUP_THAWED;
286 else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
287 goal_state = CGROUP_FROZEN;
291 freezer_change_state(cgroup, goal_state);
295 static struct cftype files[] = {
298 .flags = CFTYPE_NOT_ON_ROOT,
299 .read_seq_string = freezer_read,
300 .write_string = freezer_write,
305 struct cgroup_subsys freezer_subsys = {
307 .create = freezer_create,
308 .destroy = freezer_destroy,
309 .subsys_id = freezer_subsys_id,
310 .attach = freezer_attach,
311 .fork = freezer_fork,
312 .base_cftypes = files,
315 * freezer subsys doesn't handle hierarchy at all. Frozen state
316 * should be inherited through the hierarchy - if a parent is
317 * frozen, all its children should be frozen. Fix it and remove
320 .broken_hierarchy = true,