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>
26 * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is
27 * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
28 * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING
29 * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of
30 * its ancestors has FREEZING_SELF set.
32 enum freezer_state_flags
{
33 CGROUP_FREEZER_ONLINE
= (1 << 0), /* freezer is fully online */
34 CGROUP_FREEZING_SELF
= (1 << 1), /* this freezer is freezing */
35 CGROUP_FREEZING_PARENT
= (1 << 2), /* the parent freezer is freezing */
36 CGROUP_FROZEN
= (1 << 3), /* this and its descendants frozen */
38 /* mask for all FREEZING flags */
39 CGROUP_FREEZING
= CGROUP_FREEZING_SELF
| CGROUP_FREEZING_PARENT
,
43 struct cgroup_subsys_state css
;
48 static inline struct freezer
*css_freezer(struct cgroup_subsys_state
*css
)
50 return css
? container_of(css
, struct freezer
, css
) : NULL
;
53 static inline struct freezer
*cgroup_freezer(struct cgroup
*cgroup
)
55 return css_freezer(cgroup_css(cgroup
, freezer_subsys_id
));
58 static inline struct freezer
*task_freezer(struct task_struct
*task
)
60 return css_freezer(task_css(task
, freezer_subsys_id
));
63 static struct freezer
*parent_freezer(struct freezer
*freezer
)
65 return css_freezer(css_parent(&freezer
->css
));
68 bool cgroup_freezing(struct task_struct
*task
)
73 ret
= task_freezer(task
)->state
& CGROUP_FREEZING
;
80 * cgroups_write_string() limits the size of freezer state strings to
81 * CGROUP_LOCAL_BUFFER_SIZE
83 static const char *freezer_state_strs(unsigned int state
)
85 if (state
& CGROUP_FROZEN
)
87 if (state
& CGROUP_FREEZING
)
92 struct cgroup_subsys freezer_subsys
;
94 static struct cgroup_subsys_state
*
95 freezer_css_alloc(struct cgroup_subsys_state
*parent_css
)
97 struct freezer
*freezer
;
99 freezer
= kzalloc(sizeof(struct freezer
), GFP_KERNEL
);
101 return ERR_PTR(-ENOMEM
);
103 spin_lock_init(&freezer
->lock
);
104 return &freezer
->css
;
108 * freezer_css_online - commit creation of a freezer css
109 * @css: css being created
111 * We're committing to creation of @css. Mark it online and inherit
112 * parent's freezing state while holding both parent's and our
115 static int freezer_css_online(struct cgroup_subsys_state
*css
)
117 struct freezer
*freezer
= css_freezer(css
);
118 struct freezer
*parent
= parent_freezer(freezer
);
121 * The following double locking and freezing state inheritance
122 * guarantee that @cgroup can never escape ancestors' freezing
123 * states. See cgroup_for_each_descendant_pre() for details.
126 spin_lock_irq(&parent
->lock
);
127 spin_lock_nested(&freezer
->lock
, SINGLE_DEPTH_NESTING
);
129 freezer
->state
|= CGROUP_FREEZER_ONLINE
;
131 if (parent
&& (parent
->state
& CGROUP_FREEZING
)) {
132 freezer
->state
|= CGROUP_FREEZING_PARENT
| CGROUP_FROZEN
;
133 atomic_inc(&system_freezing_cnt
);
136 spin_unlock(&freezer
->lock
);
138 spin_unlock_irq(&parent
->lock
);
144 * freezer_css_offline - initiate destruction of a freezer css
145 * @css: css being destroyed
147 * @css is going away. Mark it dead and decrement system_freezing_count if
148 * it was holding one.
150 static void freezer_css_offline(struct cgroup_subsys_state
*css
)
152 struct freezer
*freezer
= css_freezer(css
);
154 spin_lock_irq(&freezer
->lock
);
156 if (freezer
->state
& CGROUP_FREEZING
)
157 atomic_dec(&system_freezing_cnt
);
161 spin_unlock_irq(&freezer
->lock
);
164 static void freezer_css_free(struct cgroup_subsys_state
*css
)
166 kfree(css_freezer(css
));
170 * Tasks can be migrated into a different freezer anytime regardless of its
171 * current state. freezer_attach() is responsible for making new tasks
172 * conform to the current state.
174 * Freezer state changes and task migration are synchronized via
175 * @freezer->lock. freezer_attach() makes the new tasks conform to the
176 * current state and all following state changes can see the new tasks.
178 static void freezer_attach(struct cgroup_subsys_state
*new_css
,
179 struct cgroup_taskset
*tset
)
181 struct freezer
*freezer
= css_freezer(new_css
);
182 struct task_struct
*task
;
183 bool clear_frozen
= false;
185 spin_lock_irq(&freezer
->lock
);
188 * Make the new tasks conform to the current state of @new_css.
189 * For simplicity, when migrating any task to a FROZEN cgroup, we
190 * revert it to FREEZING and let update_if_frozen() determine the
191 * correct state later.
193 * Tasks in @tset are on @new_css but may not conform to its
194 * current state before executing the following - !frozen tasks may
195 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
197 cgroup_taskset_for_each(task
, new_css
->cgroup
, tset
) {
198 if (!(freezer
->state
& CGROUP_FREEZING
)) {
202 freezer
->state
&= ~CGROUP_FROZEN
;
207 spin_unlock_irq(&freezer
->lock
);
210 * Propagate FROZEN clearing upwards. We may race with
211 * update_if_frozen(), but as long as both work bottom-up, either
212 * update_if_frozen() sees child's FROZEN cleared or we clear the
213 * parent's FROZEN later. No parent w/ !FROZEN children can be
216 while (clear_frozen
&& (freezer
= parent_freezer(freezer
))) {
217 spin_lock_irq(&freezer
->lock
);
218 freezer
->state
&= ~CGROUP_FROZEN
;
219 clear_frozen
= freezer
->state
& CGROUP_FREEZING
;
220 spin_unlock_irq(&freezer
->lock
);
224 static void freezer_fork(struct task_struct
*task
)
226 struct freezer
*freezer
;
229 freezer
= task_freezer(task
);
232 * The root cgroup is non-freezable, so we can skip the
235 if (!parent_freezer(freezer
))
238 spin_lock_irq(&freezer
->lock
);
239 if (freezer
->state
& CGROUP_FREEZING
)
241 spin_unlock_irq(&freezer
->lock
);
247 * update_if_frozen - update whether a cgroup finished freezing
248 * @css: css of interest
250 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
251 * calling this function. If the current state is FREEZING but not FROZEN,
252 * this function checks whether all tasks of this cgroup and the descendant
253 * cgroups finished freezing and, if so, sets FROZEN.
255 * The caller is responsible for grabbing RCU read lock and calling
256 * update_if_frozen() on all descendants prior to invoking this function.
258 * Task states and freezer state might disagree while tasks are being
259 * migrated into or out of @css, so we can't verify task states against
260 * @freezer state here. See freezer_attach() for details.
262 static void update_if_frozen(struct cgroup_subsys_state
*css
)
264 struct freezer
*freezer
= css_freezer(css
);
266 struct cgroup_iter it
;
267 struct task_struct
*task
;
269 WARN_ON_ONCE(!rcu_read_lock_held());
271 spin_lock_irq(&freezer
->lock
);
273 if (!(freezer
->state
& CGROUP_FREEZING
) ||
274 (freezer
->state
& CGROUP_FROZEN
))
277 /* are all (live) children frozen? */
278 cgroup_for_each_child(pos
, css
->cgroup
) {
279 struct freezer
*child
= cgroup_freezer(pos
);
281 if ((child
->state
& CGROUP_FREEZER_ONLINE
) &&
282 !(child
->state
& CGROUP_FROZEN
))
286 /* are all tasks frozen? */
287 cgroup_iter_start(css
->cgroup
, &it
);
289 while ((task
= cgroup_iter_next(css
->cgroup
, &it
))) {
290 if (freezing(task
)) {
292 * freezer_should_skip() indicates that the task
293 * should be skipped when determining freezing
294 * completion. Consider it frozen in addition to
295 * the usual frozen condition.
297 if (!frozen(task
) && !freezer_should_skip(task
))
302 freezer
->state
|= CGROUP_FROZEN
;
304 cgroup_iter_end(css
->cgroup
, &it
);
306 spin_unlock_irq(&freezer
->lock
);
309 static int freezer_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
316 /* update states bottom-up */
317 cgroup_for_each_descendant_post(pos
, css
->cgroup
)
318 update_if_frozen(cgroup_css(pos
, freezer_subsys_id
));
319 update_if_frozen(css
);
323 seq_puts(m
, freezer_state_strs(css_freezer(css
)->state
));
328 static void freeze_cgroup(struct freezer
*freezer
)
330 struct cgroup
*cgroup
= freezer
->css
.cgroup
;
331 struct cgroup_iter it
;
332 struct task_struct
*task
;
334 cgroup_iter_start(cgroup
, &it
);
335 while ((task
= cgroup_iter_next(cgroup
, &it
)))
337 cgroup_iter_end(cgroup
, &it
);
340 static void unfreeze_cgroup(struct freezer
*freezer
)
342 struct cgroup
*cgroup
= freezer
->css
.cgroup
;
343 struct cgroup_iter it
;
344 struct task_struct
*task
;
346 cgroup_iter_start(cgroup
, &it
);
347 while ((task
= cgroup_iter_next(cgroup
, &it
)))
349 cgroup_iter_end(cgroup
, &it
);
353 * freezer_apply_state - apply state change to a single cgroup_freezer
354 * @freezer: freezer to apply state change to
355 * @freeze: whether to freeze or unfreeze
356 * @state: CGROUP_FREEZING_* flag to set or clear
358 * Set or clear @state on @cgroup according to @freeze, and perform
359 * freezing or thawing as necessary.
361 static void freezer_apply_state(struct freezer
*freezer
, bool freeze
,
364 /* also synchronizes against task migration, see freezer_attach() */
365 lockdep_assert_held(&freezer
->lock
);
367 if (!(freezer
->state
& CGROUP_FREEZER_ONLINE
))
371 if (!(freezer
->state
& CGROUP_FREEZING
))
372 atomic_inc(&system_freezing_cnt
);
373 freezer
->state
|= state
;
374 freeze_cgroup(freezer
);
376 bool was_freezing
= freezer
->state
& CGROUP_FREEZING
;
378 freezer
->state
&= ~state
;
380 if (!(freezer
->state
& CGROUP_FREEZING
)) {
382 atomic_dec(&system_freezing_cnt
);
383 freezer
->state
&= ~CGROUP_FROZEN
;
384 unfreeze_cgroup(freezer
);
390 * freezer_change_state - change the freezing state of a cgroup_freezer
391 * @freezer: freezer of interest
392 * @freeze: whether to freeze or thaw
394 * Freeze or thaw @freezer according to @freeze. The operations are
395 * recursive - all descendants of @freezer will be affected.
397 static void freezer_change_state(struct freezer
*freezer
, bool freeze
)
401 /* update @freezer */
402 spin_lock_irq(&freezer
->lock
);
403 freezer_apply_state(freezer
, freeze
, CGROUP_FREEZING_SELF
);
404 spin_unlock_irq(&freezer
->lock
);
407 * Update all its descendants in pre-order traversal. Each
408 * descendant will try to inherit its parent's FREEZING state as
409 * CGROUP_FREEZING_PARENT.
412 cgroup_for_each_descendant_pre(pos
, freezer
->css
.cgroup
) {
413 struct freezer
*pos_f
= cgroup_freezer(pos
);
414 struct freezer
*parent
= parent_freezer(pos_f
);
417 * Our update to @parent->state is already visible which is
418 * all we need. No need to lock @parent. For more info on
419 * synchronization, see freezer_post_create().
421 spin_lock_irq(&pos_f
->lock
);
422 freezer_apply_state(pos_f
, parent
->state
& CGROUP_FREEZING
,
423 CGROUP_FREEZING_PARENT
);
424 spin_unlock_irq(&pos_f
->lock
);
429 static int freezer_write(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
434 if (strcmp(buffer
, freezer_state_strs(0)) == 0)
436 else if (strcmp(buffer
, freezer_state_strs(CGROUP_FROZEN
)) == 0)
441 freezer_change_state(css_freezer(css
), freeze
);
445 static u64
freezer_self_freezing_read(struct cgroup_subsys_state
*css
,
448 struct freezer
*freezer
= css_freezer(css
);
450 return (bool)(freezer
->state
& CGROUP_FREEZING_SELF
);
453 static u64
freezer_parent_freezing_read(struct cgroup_subsys_state
*css
,
456 struct freezer
*freezer
= css_freezer(css
);
458 return (bool)(freezer
->state
& CGROUP_FREEZING_PARENT
);
461 static struct cftype files
[] = {
464 .flags
= CFTYPE_NOT_ON_ROOT
,
465 .read_seq_string
= freezer_read
,
466 .write_string
= freezer_write
,
469 .name
= "self_freezing",
470 .flags
= CFTYPE_NOT_ON_ROOT
,
471 .read_u64
= freezer_self_freezing_read
,
474 .name
= "parent_freezing",
475 .flags
= CFTYPE_NOT_ON_ROOT
,
476 .read_u64
= freezer_parent_freezing_read
,
481 struct cgroup_subsys freezer_subsys
= {
483 .css_alloc
= freezer_css_alloc
,
484 .css_online
= freezer_css_online
,
485 .css_offline
= freezer_css_offline
,
486 .css_free
= freezer_css_free
,
487 .subsys_id
= freezer_subsys_id
,
488 .attach
= freezer_attach
,
489 .fork
= freezer_fork
,
490 .base_cftypes
= files
,