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
*task_freezer(struct task_struct
*task
)
55 return css_freezer(task_css(task
, freezer_cgrp_id
));
58 static struct freezer
*parent_freezer(struct freezer
*freezer
)
60 return css_freezer(css_parent(&freezer
->css
));
63 bool cgroup_freezing(struct task_struct
*task
)
68 ret
= task_freezer(task
)->state
& CGROUP_FREEZING
;
75 * cgroups_write_string() limits the size of freezer state strings to
76 * CGROUP_LOCAL_BUFFER_SIZE
78 static const char *freezer_state_strs(unsigned int state
)
80 if (state
& CGROUP_FROZEN
)
82 if (state
& CGROUP_FREEZING
)
87 static struct cgroup_subsys_state
*
88 freezer_css_alloc(struct cgroup_subsys_state
*parent_css
)
90 struct freezer
*freezer
;
92 freezer
= kzalloc(sizeof(struct freezer
), GFP_KERNEL
);
94 return ERR_PTR(-ENOMEM
);
96 spin_lock_init(&freezer
->lock
);
101 * freezer_css_online - commit creation of a freezer css
102 * @css: css being created
104 * We're committing to creation of @css. Mark it online and inherit
105 * parent's freezing state while holding both parent's and our
108 static int freezer_css_online(struct cgroup_subsys_state
*css
)
110 struct freezer
*freezer
= css_freezer(css
);
111 struct freezer
*parent
= parent_freezer(freezer
);
114 * The following double locking and freezing state inheritance
115 * guarantee that @cgroup can never escape ancestors' freezing
116 * states. See css_for_each_descendant_pre() for details.
119 spin_lock_irq(&parent
->lock
);
120 spin_lock_nested(&freezer
->lock
, SINGLE_DEPTH_NESTING
);
122 freezer
->state
|= CGROUP_FREEZER_ONLINE
;
124 if (parent
&& (parent
->state
& CGROUP_FREEZING
)) {
125 freezer
->state
|= CGROUP_FREEZING_PARENT
| CGROUP_FROZEN
;
126 atomic_inc(&system_freezing_cnt
);
129 spin_unlock(&freezer
->lock
);
131 spin_unlock_irq(&parent
->lock
);
137 * freezer_css_offline - initiate destruction of a freezer css
138 * @css: css being destroyed
140 * @css is going away. Mark it dead and decrement system_freezing_count if
141 * it was holding one.
143 static void freezer_css_offline(struct cgroup_subsys_state
*css
)
145 struct freezer
*freezer
= css_freezer(css
);
147 spin_lock_irq(&freezer
->lock
);
149 if (freezer
->state
& CGROUP_FREEZING
)
150 atomic_dec(&system_freezing_cnt
);
154 spin_unlock_irq(&freezer
->lock
);
157 static void freezer_css_free(struct cgroup_subsys_state
*css
)
159 kfree(css_freezer(css
));
163 * Tasks can be migrated into a different freezer anytime regardless of its
164 * current state. freezer_attach() is responsible for making new tasks
165 * conform to the current state.
167 * Freezer state changes and task migration are synchronized via
168 * @freezer->lock. freezer_attach() makes the new tasks conform to the
169 * current state and all following state changes can see the new tasks.
171 static void freezer_attach(struct cgroup_subsys_state
*new_css
,
172 struct cgroup_taskset
*tset
)
174 struct freezer
*freezer
= css_freezer(new_css
);
175 struct task_struct
*task
;
176 bool clear_frozen
= false;
178 spin_lock_irq(&freezer
->lock
);
181 * Make the new tasks conform to the current state of @new_css.
182 * For simplicity, when migrating any task to a FROZEN cgroup, we
183 * revert it to FREEZING and let update_if_frozen() determine the
184 * correct state later.
186 * Tasks in @tset are on @new_css but may not conform to its
187 * current state before executing the following - !frozen tasks may
188 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
190 cgroup_taskset_for_each(task
, new_css
, tset
) {
191 if (!(freezer
->state
& CGROUP_FREEZING
)) {
195 freezer
->state
&= ~CGROUP_FROZEN
;
200 spin_unlock_irq(&freezer
->lock
);
203 * Propagate FROZEN clearing upwards. We may race with
204 * update_if_frozen(), but as long as both work bottom-up, either
205 * update_if_frozen() sees child's FROZEN cleared or we clear the
206 * parent's FROZEN later. No parent w/ !FROZEN children can be
209 while (clear_frozen
&& (freezer
= parent_freezer(freezer
))) {
210 spin_lock_irq(&freezer
->lock
);
211 freezer
->state
&= ~CGROUP_FROZEN
;
212 clear_frozen
= freezer
->state
& CGROUP_FREEZING
;
213 spin_unlock_irq(&freezer
->lock
);
217 static void freezer_fork(struct task_struct
*task
)
219 struct freezer
*freezer
;
222 freezer
= task_freezer(task
);
225 * The root cgroup is non-freezable, so we can skip the
228 if (!parent_freezer(freezer
))
231 spin_lock_irq(&freezer
->lock
);
232 if (freezer
->state
& CGROUP_FREEZING
)
234 spin_unlock_irq(&freezer
->lock
);
240 * update_if_frozen - update whether a cgroup finished freezing
241 * @css: css of interest
243 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
244 * calling this function. If the current state is FREEZING but not FROZEN,
245 * this function checks whether all tasks of this cgroup and the descendant
246 * cgroups finished freezing and, if so, sets FROZEN.
248 * The caller is responsible for grabbing RCU read lock and calling
249 * update_if_frozen() on all descendants prior to invoking this function.
251 * Task states and freezer state might disagree while tasks are being
252 * migrated into or out of @css, so we can't verify task states against
253 * @freezer state here. See freezer_attach() for details.
255 static void update_if_frozen(struct cgroup_subsys_state
*css
)
257 struct freezer
*freezer
= css_freezer(css
);
258 struct cgroup_subsys_state
*pos
;
259 struct css_task_iter it
;
260 struct task_struct
*task
;
262 WARN_ON_ONCE(!rcu_read_lock_held());
264 spin_lock_irq(&freezer
->lock
);
266 if (!(freezer
->state
& CGROUP_FREEZING
) ||
267 (freezer
->state
& CGROUP_FROZEN
))
270 /* are all (live) children frozen? */
271 css_for_each_child(pos
, css
) {
272 struct freezer
*child
= css_freezer(pos
);
274 if ((child
->state
& CGROUP_FREEZER_ONLINE
) &&
275 !(child
->state
& CGROUP_FROZEN
))
279 /* are all tasks frozen? */
280 css_task_iter_start(css
, &it
);
282 while ((task
= css_task_iter_next(&it
))) {
283 if (freezing(task
)) {
285 * freezer_should_skip() indicates that the task
286 * should be skipped when determining freezing
287 * completion. Consider it frozen in addition to
288 * the usual frozen condition.
290 if (!frozen(task
) && !freezer_should_skip(task
))
295 freezer
->state
|= CGROUP_FROZEN
;
297 css_task_iter_end(&it
);
299 spin_unlock_irq(&freezer
->lock
);
302 static int freezer_read(struct seq_file
*m
, void *v
)
304 struct cgroup_subsys_state
*css
= seq_css(m
), *pos
;
308 /* update states bottom-up */
309 css_for_each_descendant_post(pos
, css
)
310 update_if_frozen(pos
);
314 seq_puts(m
, freezer_state_strs(css_freezer(css
)->state
));
319 static void freeze_cgroup(struct freezer
*freezer
)
321 struct css_task_iter it
;
322 struct task_struct
*task
;
324 css_task_iter_start(&freezer
->css
, &it
);
325 while ((task
= css_task_iter_next(&it
)))
327 css_task_iter_end(&it
);
330 static void unfreeze_cgroup(struct freezer
*freezer
)
332 struct css_task_iter it
;
333 struct task_struct
*task
;
335 css_task_iter_start(&freezer
->css
, &it
);
336 while ((task
= css_task_iter_next(&it
)))
338 css_task_iter_end(&it
);
342 * freezer_apply_state - apply state change to a single cgroup_freezer
343 * @freezer: freezer to apply state change to
344 * @freeze: whether to freeze or unfreeze
345 * @state: CGROUP_FREEZING_* flag to set or clear
347 * Set or clear @state on @cgroup according to @freeze, and perform
348 * freezing or thawing as necessary.
350 static void freezer_apply_state(struct freezer
*freezer
, bool freeze
,
353 /* also synchronizes against task migration, see freezer_attach() */
354 lockdep_assert_held(&freezer
->lock
);
356 if (!(freezer
->state
& CGROUP_FREEZER_ONLINE
))
360 if (!(freezer
->state
& CGROUP_FREEZING
))
361 atomic_inc(&system_freezing_cnt
);
362 freezer
->state
|= state
;
363 freeze_cgroup(freezer
);
365 bool was_freezing
= freezer
->state
& CGROUP_FREEZING
;
367 freezer
->state
&= ~state
;
369 if (!(freezer
->state
& CGROUP_FREEZING
)) {
371 atomic_dec(&system_freezing_cnt
);
372 freezer
->state
&= ~CGROUP_FROZEN
;
373 unfreeze_cgroup(freezer
);
379 * freezer_change_state - change the freezing state of a cgroup_freezer
380 * @freezer: freezer of interest
381 * @freeze: whether to freeze or thaw
383 * Freeze or thaw @freezer according to @freeze. The operations are
384 * recursive - all descendants of @freezer will be affected.
386 static void freezer_change_state(struct freezer
*freezer
, bool freeze
)
388 struct cgroup_subsys_state
*pos
;
391 * Update all its descendants in pre-order traversal. Each
392 * descendant will try to inherit its parent's FREEZING state as
393 * CGROUP_FREEZING_PARENT.
396 css_for_each_descendant_pre(pos
, &freezer
->css
) {
397 struct freezer
*pos_f
= css_freezer(pos
);
398 struct freezer
*parent
= parent_freezer(pos_f
);
400 spin_lock_irq(&pos_f
->lock
);
402 if (pos_f
== freezer
) {
403 freezer_apply_state(pos_f
, freeze
,
404 CGROUP_FREEZING_SELF
);
407 * Our update to @parent->state is already visible
408 * which is all we need. No need to lock @parent.
409 * For more info on synchronization, see
410 * freezer_post_create().
412 freezer_apply_state(pos_f
,
413 parent
->state
& CGROUP_FREEZING
,
414 CGROUP_FREEZING_PARENT
);
417 spin_unlock_irq(&pos_f
->lock
);
422 static int freezer_write(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
427 if (strcmp(buffer
, freezer_state_strs(0)) == 0)
429 else if (strcmp(buffer
, freezer_state_strs(CGROUP_FROZEN
)) == 0)
434 freezer_change_state(css_freezer(css
), freeze
);
438 static u64
freezer_self_freezing_read(struct cgroup_subsys_state
*css
,
441 struct freezer
*freezer
= css_freezer(css
);
443 return (bool)(freezer
->state
& CGROUP_FREEZING_SELF
);
446 static u64
freezer_parent_freezing_read(struct cgroup_subsys_state
*css
,
449 struct freezer
*freezer
= css_freezer(css
);
451 return (bool)(freezer
->state
& CGROUP_FREEZING_PARENT
);
454 static struct cftype files
[] = {
457 .flags
= CFTYPE_NOT_ON_ROOT
,
458 .seq_show
= freezer_read
,
459 .write_string
= freezer_write
,
462 .name
= "self_freezing",
463 .flags
= CFTYPE_NOT_ON_ROOT
,
464 .read_u64
= freezer_self_freezing_read
,
467 .name
= "parent_freezing",
468 .flags
= CFTYPE_NOT_ON_ROOT
,
469 .read_u64
= freezer_parent_freezing_read
,
474 struct cgroup_subsys freezer_cgrp_subsys
= {
475 .css_alloc
= freezer_css_alloc
,
476 .css_online
= freezer_css_online
,
477 .css_offline
= freezer_css_offline
,
478 .css_free
= freezer_css_free
,
479 .attach
= freezer_attach
,
480 .fork
= freezer_fork
,
481 .base_cftypes
= files
,