blob: 6c3154e477f601deb7588317b9cbc6d7267cbed2 [file] [log] [blame]
Matt Helsleydc52ddc2008-10-18 20:27:21 -07001/*
2 * cgroup_freezer.c - control group freezer subsystem
3 *
4 * Copyright IBM Corporation, 2007
5 *
6 * Author : Cedric Le Goater <clg@fr.ibm.com>
7 *
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.
11 *
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.
15 */
16
Paul Gortmaker9984de12011-05-23 14:51:41 -040017#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Matt Helsleydc52ddc2008-10-18 20:27:21 -070019#include <linux/cgroup.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
22#include <linux/freezer.h>
23#include <linux/seq_file.h>
24
Tejun Heoef9fe982012-11-09 09:12:30 -080025/*
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.
31 */
Tejun Heod6a2fe12012-11-09 09:12:30 -080032enum freezer_state_flags {
Tejun Heo5300a9b2012-11-09 09:12:30 -080033 CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */
Tejun Heoa2252182012-11-09 09:12:30 -080034 CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */
35 CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */
Tejun Heod6a2fe12012-11-09 09:12:30 -080036 CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
Tejun Heoa2252182012-11-09 09:12:30 -080037
38 /* mask for all FREEZING flags */
39 CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
Matt Helsleydc52ddc2008-10-18 20:27:21 -070040};
41
42struct freezer {
Tejun Heobcd66c82012-11-09 09:12:29 -080043 struct cgroup_subsys_state css;
Tejun Heod6a2fe12012-11-09 09:12:30 -080044 unsigned int state;
Tejun Heobcd66c82012-11-09 09:12:29 -080045 spinlock_t lock;
Matt Helsleydc52ddc2008-10-18 20:27:21 -070046};
47
Tejun Heoa7c6d552013-08-08 20:11:23 -040048static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
49{
50 return css ? container_of(css, struct freezer, css) : NULL;
51}
52
Matt Helsleydc52ddc2008-10-18 20:27:21 -070053static inline struct freezer *task_freezer(struct task_struct *task)
54{
Tejun Heoa7c6d552013-08-08 20:11:23 -040055 return css_freezer(task_css(task, freezer_subsys_id));
Matt Helsleydc52ddc2008-10-18 20:27:21 -070056}
57
Tejun Heoef9fe982012-11-09 09:12:30 -080058static struct freezer *parent_freezer(struct freezer *freezer)
59{
Tejun Heo63876982013-08-08 20:11:23 -040060 return css_freezer(css_parent(&freezer->css));
Tejun Heoef9fe982012-11-09 09:12:30 -080061}
62
Tejun Heo22b4e112011-11-21 12:32:25 -080063bool cgroup_freezing(struct task_struct *task)
Tomasz Buchertd5de4dd2010-10-27 15:33:32 -070064{
Tejun Heo22b4e112011-11-21 12:32:25 -080065 bool ret;
Tomasz Buchertd5de4dd2010-10-27 15:33:32 -070066
Tejun Heo22b4e112011-11-21 12:32:25 -080067 rcu_read_lock();
Tejun Heod6a2fe12012-11-09 09:12:30 -080068 ret = task_freezer(task)->state & CGROUP_FREEZING;
Tejun Heo22b4e112011-11-21 12:32:25 -080069 rcu_read_unlock();
70
71 return ret;
Matt Helsleydc52ddc2008-10-18 20:27:21 -070072}
73
74/*
75 * cgroups_write_string() limits the size of freezer state strings to
76 * CGROUP_LOCAL_BUFFER_SIZE
77 */
Tejun Heod6a2fe12012-11-09 09:12:30 -080078static const char *freezer_state_strs(unsigned int state)
79{
80 if (state & CGROUP_FROZEN)
81 return "FROZEN";
82 if (state & CGROUP_FREEZING)
83 return "FREEZING";
84 return "THAWED";
Matt Helsleydc52ddc2008-10-18 20:27:21 -070085};
86
Matt Helsleydc52ddc2008-10-18 20:27:21 -070087struct cgroup_subsys freezer_subsys;
88
Tejun Heoeb954192013-08-08 20:11:23 -040089static struct cgroup_subsys_state *
90freezer_css_alloc(struct cgroup_subsys_state *parent_css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -070091{
92 struct freezer *freezer;
93
94 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
95 if (!freezer)
96 return ERR_PTR(-ENOMEM);
97
98 spin_lock_init(&freezer->lock);
Matt Helsleydc52ddc2008-10-18 20:27:21 -070099 return &freezer->css;
100}
101
Tejun Heo5300a9b2012-11-09 09:12:30 -0800102/**
Tejun Heoeb954192013-08-08 20:11:23 -0400103 * freezer_css_online - commit creation of a freezer css
104 * @css: css being created
Tejun Heo5300a9b2012-11-09 09:12:30 -0800105 *
Tejun Heoeb954192013-08-08 20:11:23 -0400106 * We're committing to creation of @css. Mark it online and inherit
Tejun Heoef9fe982012-11-09 09:12:30 -0800107 * parent's freezing state while holding both parent's and our
108 * freezer->lock.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800109 */
Tejun Heoeb954192013-08-08 20:11:23 -0400110static int freezer_css_online(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700111{
Tejun Heoeb954192013-08-08 20:11:23 -0400112 struct freezer *freezer = css_freezer(css);
Tejun Heoef9fe982012-11-09 09:12:30 -0800113 struct freezer *parent = parent_freezer(freezer);
Tejun Heoa3201222011-11-21 12:32:25 -0800114
Tejun Heoef9fe982012-11-09 09:12:30 -0800115 /*
116 * The following double locking and freezing state inheritance
117 * guarantee that @cgroup can never escape ancestors' freezing
Tejun Heo492eb212013-08-08 20:11:25 -0400118 * states. See css_for_each_descendant_pre() for details.
Tejun Heoef9fe982012-11-09 09:12:30 -0800119 */
120 if (parent)
121 spin_lock_irq(&parent->lock);
122 spin_lock_nested(&freezer->lock, SINGLE_DEPTH_NESTING);
123
Tejun Heo5300a9b2012-11-09 09:12:30 -0800124 freezer->state |= CGROUP_FREEZER_ONLINE;
Tejun Heoef9fe982012-11-09 09:12:30 -0800125
126 if (parent && (parent->state & CGROUP_FREEZING)) {
127 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
128 atomic_inc(&system_freezing_cnt);
129 }
130
131 spin_unlock(&freezer->lock);
132 if (parent)
133 spin_unlock_irq(&parent->lock);
Tejun Heob1929db2012-11-19 08:13:38 -0800134
135 return 0;
Tejun Heo5300a9b2012-11-09 09:12:30 -0800136}
137
138/**
Tejun Heoeb954192013-08-08 20:11:23 -0400139 * freezer_css_offline - initiate destruction of a freezer css
140 * @css: css being destroyed
Tejun Heo5300a9b2012-11-09 09:12:30 -0800141 *
Tejun Heoeb954192013-08-08 20:11:23 -0400142 * @css is going away. Mark it dead and decrement system_freezing_count if
143 * it was holding one.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800144 */
Tejun Heoeb954192013-08-08 20:11:23 -0400145static void freezer_css_offline(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800146{
Tejun Heoeb954192013-08-08 20:11:23 -0400147 struct freezer *freezer = css_freezer(css);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800148
149 spin_lock_irq(&freezer->lock);
150
Tejun Heod6a2fe12012-11-09 09:12:30 -0800151 if (freezer->state & CGROUP_FREEZING)
Tejun Heoa3201222011-11-21 12:32:25 -0800152 atomic_dec(&system_freezing_cnt);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800153
154 freezer->state = 0;
155
156 spin_unlock_irq(&freezer->lock);
157}
158
Tejun Heoeb954192013-08-08 20:11:23 -0400159static void freezer_css_free(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800160{
Tejun Heoeb954192013-08-08 20:11:23 -0400161 kfree(css_freezer(css));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700162}
163
Matt Helsley957a4ee2008-10-18 20:27:22 -0700164/*
Tejun Heoead5c472012-10-16 15:03:15 -0700165 * Tasks can be migrated into a different freezer anytime regardless of its
166 * current state. freezer_attach() is responsible for making new tasks
167 * conform to the current state.
168 *
169 * Freezer state changes and task migration are synchronized via
170 * @freezer->lock. freezer_attach() makes the new tasks conform to the
171 * current state and all following state changes can see the new tasks.
Matt Helsley957a4ee2008-10-18 20:27:22 -0700172 */
Tejun Heoeb954192013-08-08 20:11:23 -0400173static void freezer_attach(struct cgroup_subsys_state *new_css,
174 struct cgroup_taskset *tset)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700175{
Tejun Heoeb954192013-08-08 20:11:23 -0400176 struct freezer *freezer = css_freezer(new_css);
Tejun Heobb9d97b2011-12-12 18:12:21 -0800177 struct task_struct *task;
Tejun Heoef9fe982012-11-09 09:12:30 -0800178 bool clear_frozen = false;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700179
Tejun Heo8755ade2012-10-16 15:03:14 -0700180 spin_lock_irq(&freezer->lock);
181
Li Zefan80a6a2c2008-10-29 14:00:52 -0700182 /*
Tejun Heoeb954192013-08-08 20:11:23 -0400183 * Make the new tasks conform to the current state of @new_css.
Tejun Heo8755ade2012-10-16 15:03:14 -0700184 * For simplicity, when migrating any task to a FROZEN cgroup, we
185 * revert it to FREEZING and let update_if_frozen() determine the
186 * correct state later.
187 *
Tejun Heoeb954192013-08-08 20:11:23 -0400188 * Tasks in @tset are on @new_css but may not conform to its
Tejun Heo8755ade2012-10-16 15:03:14 -0700189 * current state before executing the following - !frozen tasks may
190 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
Li Zefan80a6a2c2008-10-29 14:00:52 -0700191 */
Tejun Heod99c8722013-08-08 20:11:27 -0400192 cgroup_taskset_for_each(task, new_css, tset) {
Tejun Heod6a2fe12012-11-09 09:12:30 -0800193 if (!(freezer->state & CGROUP_FREEZING)) {
Tejun Heo8755ade2012-10-16 15:03:14 -0700194 __thaw_task(task);
195 } else {
196 freeze_task(task);
Tejun Heod6a2fe12012-11-09 09:12:30 -0800197 freezer->state &= ~CGROUP_FROZEN;
Tejun Heoef9fe982012-11-09 09:12:30 -0800198 clear_frozen = true;
Tejun Heo8755ade2012-10-16 15:03:14 -0700199 }
200 }
Matt Helsley957a4ee2008-10-18 20:27:22 -0700201
Tejun Heo8755ade2012-10-16 15:03:14 -0700202 spin_unlock_irq(&freezer->lock);
Tejun Heoef9fe982012-11-09 09:12:30 -0800203
204 /*
205 * Propagate FROZEN clearing upwards. We may race with
206 * update_if_frozen(), but as long as both work bottom-up, either
207 * update_if_frozen() sees child's FROZEN cleared or we clear the
208 * parent's FROZEN later. No parent w/ !FROZEN children can be
209 * left FROZEN.
210 */
211 while (clear_frozen && (freezer = parent_freezer(freezer))) {
212 spin_lock_irq(&freezer->lock);
213 freezer->state &= ~CGROUP_FROZEN;
214 clear_frozen = freezer->state & CGROUP_FREEZING;
215 spin_unlock_irq(&freezer->lock);
216 }
Ben Blumf780bdb2011-05-26 16:25:19 -0700217}
218
Li Zefan761b3ef52012-01-31 13:47:36 +0800219static void freezer_fork(struct task_struct *task)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700220{
221 struct freezer *freezer;
222
Paul E. McKenney8b46f882010-04-21 13:02:08 -0700223 rcu_read_lock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700224 freezer = task_freezer(task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700225
Li Zefan3b1b3f62008-11-12 13:26:50 -0800226 /*
227 * The root cgroup is non-freezable, so we can skip the
228 * following check.
229 */
Tejun Heo63876982013-08-08 20:11:23 -0400230 if (!parent_freezer(freezer))
Tejun Heo5edee612012-10-16 15:03:14 -0700231 goto out;
Li Zefan3b1b3f62008-11-12 13:26:50 -0800232
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700233 spin_lock_irq(&freezer->lock);
Tejun Heod6a2fe12012-11-09 09:12:30 -0800234 if (freezer->state & CGROUP_FREEZING)
Tejun Heo839e3402011-11-21 12:32:26 -0800235 freeze_task(task);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700236 spin_unlock_irq(&freezer->lock);
Tejun Heo5edee612012-10-16 15:03:14 -0700237out:
238 rcu_read_unlock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700239}
240
Tejun Heoef9fe982012-11-09 09:12:30 -0800241/**
242 * update_if_frozen - update whether a cgroup finished freezing
Tejun Heo182446d2013-08-08 20:11:24 -0400243 * @css: css of interest
Tejun Heoef9fe982012-11-09 09:12:30 -0800244 *
245 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
246 * calling this function. If the current state is FREEZING but not FROZEN,
247 * this function checks whether all tasks of this cgroup and the descendant
248 * cgroups finished freezing and, if so, sets FROZEN.
249 *
250 * The caller is responsible for grabbing RCU read lock and calling
251 * update_if_frozen() on all descendants prior to invoking this function.
Tejun Heob4d18312012-10-16 15:03:14 -0700252 *
253 * Task states and freezer state might disagree while tasks are being
Tejun Heo182446d2013-08-08 20:11:24 -0400254 * migrated into or out of @css, so we can't verify task states against
Tejun Heoead5c472012-10-16 15:03:15 -0700255 * @freezer state here. See freezer_attach() for details.
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700256 */
Tejun Heo182446d2013-08-08 20:11:24 -0400257static void update_if_frozen(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700258{
Tejun Heo182446d2013-08-08 20:11:24 -0400259 struct freezer *freezer = css_freezer(css);
Tejun Heo492eb212013-08-08 20:11:25 -0400260 struct cgroup_subsys_state *pos;
Tejun Heo72ec7022013-08-08 20:11:26 -0400261 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700262 struct task_struct *task;
Tejun Heob4d18312012-10-16 15:03:14 -0700263
Tejun Heoef9fe982012-11-09 09:12:30 -0800264 WARN_ON_ONCE(!rcu_read_lock_held());
265
266 spin_lock_irq(&freezer->lock);
267
Tejun Heod6a2fe12012-11-09 09:12:30 -0800268 if (!(freezer->state & CGROUP_FREEZING) ||
269 (freezer->state & CGROUP_FROZEN))
Tejun Heoef9fe982012-11-09 09:12:30 -0800270 goto out_unlock;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700271
Tejun Heoef9fe982012-11-09 09:12:30 -0800272 /* are all (live) children frozen? */
Tejun Heo492eb212013-08-08 20:11:25 -0400273 css_for_each_child(pos, css) {
274 struct freezer *child = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800275
276 if ((child->state & CGROUP_FREEZER_ONLINE) &&
277 !(child->state & CGROUP_FROZEN))
278 goto out_unlock;
279 }
280
281 /* are all tasks frozen? */
Tejun Heo72ec7022013-08-08 20:11:26 -0400282 css_task_iter_start(css, &it);
Tejun Heob4d18312012-10-16 15:03:14 -0700283
Tejun Heo72ec7022013-08-08 20:11:26 -0400284 while ((task = css_task_iter_next(&it))) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700285 if (freezing(task)) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700286 /*
287 * freezer_should_skip() indicates that the task
288 * should be skipped when determining freezing
289 * completion. Consider it frozen in addition to
290 * the usual frozen condition.
291 */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200292 if (!frozen(task) && !freezer_should_skip(task))
Tejun Heoef9fe982012-11-09 09:12:30 -0800293 goto out_iter_end;
Tejun Heo3c426d52012-10-16 15:03:14 -0700294 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700295 }
296
Tejun Heod6a2fe12012-11-09 09:12:30 -0800297 freezer->state |= CGROUP_FROZEN;
Tejun Heoef9fe982012-11-09 09:12:30 -0800298out_iter_end:
Tejun Heo72ec7022013-08-08 20:11:26 -0400299 css_task_iter_end(&it);
Tejun Heoef9fe982012-11-09 09:12:30 -0800300out_unlock:
301 spin_unlock_irq(&freezer->lock);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700302}
303
Tejun Heo2da8ca82013-12-05 12:28:04 -0500304static int freezer_read(struct seq_file *m, void *v)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700305{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500306 struct cgroup_subsys_state *css = seq_css(m), *pos;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700307
Tejun Heoef9fe982012-11-09 09:12:30 -0800308 rcu_read_lock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700309
Tejun Heoef9fe982012-11-09 09:12:30 -0800310 /* update states bottom-up */
Tejun Heo492eb212013-08-08 20:11:25 -0400311 css_for_each_descendant_post(pos, css)
312 update_if_frozen(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800313
314 rcu_read_unlock();
315
Tejun Heo182446d2013-08-08 20:11:24 -0400316 seq_puts(m, freezer_state_strs(css_freezer(css)->state));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700317 seq_putc(m, '\n');
318 return 0;
319}
320
Tejun Heobcd66c82012-11-09 09:12:29 -0800321static void freeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700322{
Tejun Heo72ec7022013-08-08 20:11:26 -0400323 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700324 struct task_struct *task;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700325
Tejun Heo72ec7022013-08-08 20:11:26 -0400326 css_task_iter_start(&freezer->css, &it);
327 while ((task = css_task_iter_next(&it)))
Tejun Heo51f246e2012-10-16 15:03:14 -0700328 freeze_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400329 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700330}
331
Tejun Heobcd66c82012-11-09 09:12:29 -0800332static void unfreeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700333{
Tejun Heo72ec7022013-08-08 20:11:26 -0400334 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700335 struct task_struct *task;
336
Tejun Heo72ec7022013-08-08 20:11:26 -0400337 css_task_iter_start(&freezer->css, &it);
338 while ((task = css_task_iter_next(&it)))
Tejun Heoa5be2d02011-11-21 12:32:23 -0800339 __thaw_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400340 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700341}
342
Tejun Heo04a4ec32012-11-09 09:12:30 -0800343/**
344 * freezer_apply_state - apply state change to a single cgroup_freezer
345 * @freezer: freezer to apply state change to
346 * @freeze: whether to freeze or unfreeze
Tejun Heoa2252182012-11-09 09:12:30 -0800347 * @state: CGROUP_FREEZING_* flag to set or clear
348 *
349 * Set or clear @state on @cgroup according to @freeze, and perform
350 * freezing or thawing as necessary.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800351 */
Tejun Heoa2252182012-11-09 09:12:30 -0800352static void freezer_apply_state(struct freezer *freezer, bool freeze,
353 unsigned int state)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700354{
Tejun Heoead5c472012-10-16 15:03:15 -0700355 /* also synchronizes against task migration, see freezer_attach() */
Tejun Heo04a4ec32012-11-09 09:12:30 -0800356 lockdep_assert_held(&freezer->lock);
Li Zefan51308ee2008-10-29 14:00:54 -0700357
Tejun Heo5300a9b2012-11-09 09:12:30 -0800358 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
359 return;
360
Tejun Heo04a4ec32012-11-09 09:12:30 -0800361 if (freeze) {
Tejun Heod6a2fe12012-11-09 09:12:30 -0800362 if (!(freezer->state & CGROUP_FREEZING))
Tejun Heoa3201222011-11-21 12:32:25 -0800363 atomic_inc(&system_freezing_cnt);
Tejun Heoa2252182012-11-09 09:12:30 -0800364 freezer->state |= state;
Tejun Heobcd66c82012-11-09 09:12:29 -0800365 freeze_cgroup(freezer);
Tejun Heo04a4ec32012-11-09 09:12:30 -0800366 } else {
Tejun Heoa2252182012-11-09 09:12:30 -0800367 bool was_freezing = freezer->state & CGROUP_FREEZING;
368
369 freezer->state &= ~state;
370
371 if (!(freezer->state & CGROUP_FREEZING)) {
372 if (was_freezing)
373 atomic_dec(&system_freezing_cnt);
374 freezer->state &= ~CGROUP_FROZEN;
375 unfreeze_cgroup(freezer);
376 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700377 }
Tejun Heo04a4ec32012-11-09 09:12:30 -0800378}
Tejun Heo22b4e112011-11-21 12:32:25 -0800379
Tejun Heo04a4ec32012-11-09 09:12:30 -0800380/**
381 * freezer_change_state - change the freezing state of a cgroup_freezer
382 * @freezer: freezer of interest
383 * @freeze: whether to freeze or thaw
384 *
Tejun Heoef9fe982012-11-09 09:12:30 -0800385 * Freeze or thaw @freezer according to @freeze. The operations are
386 * recursive - all descendants of @freezer will be affected.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800387 */
388static void freezer_change_state(struct freezer *freezer, bool freeze)
389{
Tejun Heo492eb212013-08-08 20:11:25 -0400390 struct cgroup_subsys_state *pos;
Tejun Heoef9fe982012-11-09 09:12:30 -0800391
Tejun Heoef9fe982012-11-09 09:12:30 -0800392 /*
393 * Update all its descendants in pre-order traversal. Each
394 * descendant will try to inherit its parent's FREEZING state as
395 * CGROUP_FREEZING_PARENT.
396 */
397 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400398 css_for_each_descendant_pre(pos, &freezer->css) {
399 struct freezer *pos_f = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800400 struct freezer *parent = parent_freezer(pos_f);
401
Tejun Heoef9fe982012-11-09 09:12:30 -0800402 spin_lock_irq(&pos_f->lock);
Tejun Heobd8815a2013-08-08 20:11:27 -0400403
404 if (pos_f == freezer) {
405 freezer_apply_state(pos_f, freeze,
406 CGROUP_FREEZING_SELF);
407 } else {
408 /*
409 * Our update to @parent->state is already visible
410 * which is all we need. No need to lock @parent.
411 * For more info on synchronization, see
412 * freezer_post_create().
413 */
414 freezer_apply_state(pos_f,
415 parent->state & CGROUP_FREEZING,
416 CGROUP_FREEZING_PARENT);
417 }
418
Tejun Heoef9fe982012-11-09 09:12:30 -0800419 spin_unlock_irq(&pos_f->lock);
420 }
421 rcu_read_unlock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700422}
423
Tejun Heo182446d2013-08-08 20:11:24 -0400424static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700425 const char *buffer)
426{
Tejun Heo04a4ec32012-11-09 09:12:30 -0800427 bool freeze;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700428
Tejun Heod6a2fe12012-11-09 09:12:30 -0800429 if (strcmp(buffer, freezer_state_strs(0)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800430 freeze = false;
Tejun Heod6a2fe12012-11-09 09:12:30 -0800431 else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800432 freeze = true;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700433 else
Li Zefan3b1b3f62008-11-12 13:26:50 -0800434 return -EINVAL;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700435
Tejun Heo182446d2013-08-08 20:11:24 -0400436 freezer_change_state(css_freezer(css), freeze);
Tejun Heo51f246e2012-10-16 15:03:14 -0700437 return 0;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700438}
439
Tejun Heo182446d2013-08-08 20:11:24 -0400440static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
441 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800442{
Tejun Heo182446d2013-08-08 20:11:24 -0400443 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800444
445 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
446}
447
Tejun Heo182446d2013-08-08 20:11:24 -0400448static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
449 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800450{
Tejun Heo182446d2013-08-08 20:11:24 -0400451 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800452
453 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
454}
455
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700456static struct cftype files[] = {
457 {
458 .name = "state",
Tejun Heo4baf6e32012-04-01 12:09:55 -0700459 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -0500460 .seq_show = freezer_read,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700461 .write_string = freezer_write,
462 },
Tejun Heoa2252182012-11-09 09:12:30 -0800463 {
464 .name = "self_freezing",
465 .flags = CFTYPE_NOT_ON_ROOT,
466 .read_u64 = freezer_self_freezing_read,
467 },
468 {
469 .name = "parent_freezing",
470 .flags = CFTYPE_NOT_ON_ROOT,
471 .read_u64 = freezer_parent_freezing_read,
472 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700473 { } /* terminate */
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700474};
475
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700476struct cgroup_subsys freezer_subsys = {
477 .name = "freezer",
Tejun Heo92fb9742012-11-19 08:13:38 -0800478 .css_alloc = freezer_css_alloc,
479 .css_online = freezer_css_online,
480 .css_offline = freezer_css_offline,
481 .css_free = freezer_css_free,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700482 .subsys_id = freezer_subsys_id,
Tejun Heo8755ade2012-10-16 15:03:14 -0700483 .attach = freezer_attach,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700484 .fork = freezer_fork,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700485 .base_cftypes = files,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700486};