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) && !task_is_stopped_or_traced(task) &&
202 !freezer_should_skip(task))
207 freezer->state = CGROUP_FROZEN;
209 cgroup_iter_end(cgroup, &it);
212 static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
215 struct freezer *freezer;
216 enum freezer_state state;
218 freezer = cgroup_freezer(cgroup);
219 spin_lock_irq(&freezer->lock);
220 update_if_frozen(cgroup, freezer);
221 state = freezer->state;
222 spin_unlock_irq(&freezer->lock);
224 seq_puts(m, freezer_state_strs[state]);
229 static void freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
231 struct cgroup_iter it;
232 struct task_struct *task;
234 cgroup_iter_start(cgroup, &it);
235 while ((task = cgroup_iter_next(cgroup, &it)))
237 cgroup_iter_end(cgroup, &it);
240 static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
242 struct cgroup_iter it;
243 struct task_struct *task;
245 cgroup_iter_start(cgroup, &it);
246 while ((task = cgroup_iter_next(cgroup, &it)))
248 cgroup_iter_end(cgroup, &it);
251 static void freezer_change_state(struct cgroup *cgroup,
252 enum freezer_state goal_state)
254 struct freezer *freezer = cgroup_freezer(cgroup);
256 /* also synchronizes against task migration, see freezer_attach() */
257 spin_lock_irq(&freezer->lock);
259 switch (goal_state) {
261 if (freezer->state != CGROUP_THAWED)
262 atomic_dec(&system_freezing_cnt);
263 freezer->state = CGROUP_THAWED;
264 unfreeze_cgroup(cgroup, freezer);
267 if (freezer->state == CGROUP_THAWED)
268 atomic_inc(&system_freezing_cnt);
269 freezer->state = CGROUP_FREEZING;
270 freeze_cgroup(cgroup, freezer);
276 spin_unlock_irq(&freezer->lock);
279 static int freezer_write(struct cgroup *cgroup,
283 enum freezer_state goal_state;
285 if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
286 goal_state = CGROUP_THAWED;
287 else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
288 goal_state = CGROUP_FROZEN;
292 freezer_change_state(cgroup, goal_state);
296 static struct cftype files[] = {
299 .flags = CFTYPE_NOT_ON_ROOT,
300 .read_seq_string = freezer_read,
301 .write_string = freezer_write,
306 struct cgroup_subsys freezer_subsys = {
308 .create = freezer_create,
309 .destroy = freezer_destroy,
310 .subsys_id = freezer_subsys_id,
311 .attach = freezer_attach,
312 .fork = freezer_fork,
313 .base_cftypes = files,
316 * freezer subsys doesn't handle hierarchy at all. Frozen state
317 * should be inherited through the hierarchy - if a parent is
318 * frozen, all its children should be frozen. Fix it and remove
321 .broken_hierarchy = true,