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/module.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 int cgroup_frozen(struct task_struct *task)
53 struct freezer *freezer;
54 enum freezer_state state;
57 freezer = task_freezer(task);
58 state = freezer->state;
61 return 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 /* Locks taken and their ordering
88 * ------------------------------
90 * cgroup_mutex (AKA cgroup_lock)
91 * task->alloc_lock (AKA task_lock)
93 * task->sighand->siglock
95 * cgroup code forces css_set_lock to be taken before task->alloc_lock
97 * freezer_create(), freezer_destroy():
98 * cgroup_mutex [ by cgroup core ]
104 * task->alloc_lock (to get task's cgroup)
106 * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
107 * task->alloc_lock (to get task's cgroup)
109 * sighand->siglock (if the cgroup is freezing)
114 * read_lock css_set_lock (cgroup iterator start)
116 * freezer_write() (freeze):
119 * read_lock css_set_lock (cgroup iterator start)
122 * freezer_write() (unfreeze):
125 * read_lock css_set_lock (cgroup iterator start)
126 * task->alloc_lock (to prevent races with freeze_task())
129 static struct cgroup_subsys_state *freezer_create(struct cgroup_subsys *ss,
130 struct cgroup *cgroup)
132 struct freezer *freezer;
134 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
136 return ERR_PTR(-ENOMEM);
138 spin_lock_init(&freezer->lock);
139 freezer->state = CGROUP_THAWED;
140 return &freezer->css;
143 static void freezer_destroy(struct cgroup_subsys *ss,
144 struct cgroup *cgroup)
146 kfree(cgroup_freezer(cgroup));
149 /* Task is frozen or will freeze immediately when next it gets woken */
150 static bool is_task_frozen_enough(struct task_struct *task)
152 return frozen(task) ||
153 (task_is_stopped_or_traced(task) && freezing(task));
157 * The call to cgroup_lock() in the freezer.state write method prevents
158 * a write to that file racing against an attach, and hence the
159 * can_attach() result will remain valid until the attach completes.
161 static int freezer_can_attach(struct cgroup_subsys *ss,
162 struct cgroup *new_cgroup,
163 struct task_struct *task, bool threadgroup)
165 struct freezer *freezer;
168 * Anything frozen can't move or be moved to/from.
170 * Since orig_freezer->state == FROZEN means that @task has been
171 * frozen, so it's sufficient to check the latter condition.
174 if (is_task_frozen_enough(task))
177 freezer = cgroup_freezer(new_cgroup);
178 if (freezer->state == CGROUP_FROZEN)
182 struct task_struct *c;
185 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
186 if (is_task_frozen_enough(c)) {
197 static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
199 struct freezer *freezer;
202 * No lock is needed, since the task isn't on tasklist yet,
203 * so it can't be moved to another cgroup, which means the
204 * freezer won't be removed and will be valid during this
207 freezer = task_freezer(task);
210 * The root cgroup is non-freezable, so we can skip the
213 if (!freezer->css.cgroup->parent)
216 spin_lock_irq(&freezer->lock);
217 BUG_ON(freezer->state == CGROUP_FROZEN);
219 /* Locking avoids race with FREEZING -> THAWED transitions. */
220 if (freezer->state == CGROUP_FREEZING)
221 freeze_task(task, true);
222 spin_unlock_irq(&freezer->lock);
226 * caller must hold freezer->lock
228 static void update_freezer_state(struct cgroup *cgroup,
229 struct freezer *freezer)
231 struct cgroup_iter it;
232 struct task_struct *task;
233 unsigned int nfrozen = 0, ntotal = 0;
235 cgroup_iter_start(cgroup, &it);
236 while ((task = cgroup_iter_next(cgroup, &it))) {
238 if (is_task_frozen_enough(task))
243 * Transition to FROZEN when no new tasks can be added ensures
244 * that we never exist in the FROZEN state while there are unfrozen
247 if (nfrozen == ntotal)
248 freezer->state = CGROUP_FROZEN;
249 else if (nfrozen > 0)
250 freezer->state = CGROUP_FREEZING;
252 freezer->state = CGROUP_THAWED;
253 cgroup_iter_end(cgroup, &it);
256 static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
259 struct freezer *freezer;
260 enum freezer_state state;
262 if (!cgroup_lock_live_group(cgroup))
265 freezer = cgroup_freezer(cgroup);
266 spin_lock_irq(&freezer->lock);
267 state = freezer->state;
268 if (state == CGROUP_FREEZING) {
269 /* We change from FREEZING to FROZEN lazily if the cgroup was
270 * only partially frozen when we exitted write. */
271 update_freezer_state(cgroup, freezer);
272 state = freezer->state;
274 spin_unlock_irq(&freezer->lock);
277 seq_puts(m, freezer_state_strs[state]);
282 static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
284 struct cgroup_iter it;
285 struct task_struct *task;
286 unsigned int num_cant_freeze_now = 0;
288 freezer->state = CGROUP_FREEZING;
289 cgroup_iter_start(cgroup, &it);
290 while ((task = cgroup_iter_next(cgroup, &it))) {
291 if (!freeze_task(task, true))
293 if (is_task_frozen_enough(task))
295 if (!freezing(task) && !freezer_should_skip(task))
296 num_cant_freeze_now++;
298 cgroup_iter_end(cgroup, &it);
300 return num_cant_freeze_now ? -EBUSY : 0;
303 static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
305 struct cgroup_iter it;
306 struct task_struct *task;
308 cgroup_iter_start(cgroup, &it);
309 while ((task = cgroup_iter_next(cgroup, &it))) {
312 cgroup_iter_end(cgroup, &it);
314 freezer->state = CGROUP_THAWED;
317 static int freezer_change_state(struct cgroup *cgroup,
318 enum freezer_state goal_state)
320 struct freezer *freezer;
323 freezer = cgroup_freezer(cgroup);
325 spin_lock_irq(&freezer->lock);
327 update_freezer_state(cgroup, freezer);
328 if (goal_state == freezer->state)
331 switch (goal_state) {
333 unfreeze_cgroup(cgroup, freezer);
336 retval = try_to_freeze_cgroup(cgroup, freezer);
342 spin_unlock_irq(&freezer->lock);
347 static int freezer_write(struct cgroup *cgroup,
352 enum freezer_state goal_state;
354 if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
355 goal_state = CGROUP_THAWED;
356 else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
357 goal_state = CGROUP_FROZEN;
361 if (!cgroup_lock_live_group(cgroup))
363 retval = freezer_change_state(cgroup, goal_state);
368 static struct cftype files[] = {
371 .read_seq_string = freezer_read,
372 .write_string = freezer_write,
376 static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
380 return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
383 struct cgroup_subsys freezer_subsys = {
385 .create = freezer_create,
386 .destroy = freezer_destroy,
387 .populate = freezer_populate,
388 .subsys_id = freezer_subsys_id,
389 .can_attach = freezer_can_attach,
391 .fork = freezer_fork,