blob: 0398f7e9ac8174e38a382b13f2f66907a2b48a28 [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>
Tejun Heoe5ced8e2014-05-07 21:31:17 -040024#include <linux/mutex.h>
Matt Helsleydc52ddc2008-10-18 20:27:21 -070025
Tejun Heoef9fe982012-11-09 09:12:30 -080026/*
27 * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is
28 * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
29 * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING
30 * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of
31 * its ancestors has FREEZING_SELF set.
32 */
Tejun Heod6a2fe12012-11-09 09:12:30 -080033enum freezer_state_flags {
Tejun Heo5300a9b2012-11-09 09:12:30 -080034 CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */
Tejun Heoa2252182012-11-09 09:12:30 -080035 CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */
36 CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */
Tejun Heod6a2fe12012-11-09 09:12:30 -080037 CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
Tejun Heoa2252182012-11-09 09:12:30 -080038
39 /* mask for all FREEZING flags */
40 CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
Matt Helsleydc52ddc2008-10-18 20:27:21 -070041};
42
43struct freezer {
Tejun Heobcd66c82012-11-09 09:12:29 -080044 struct cgroup_subsys_state css;
Tejun Heod6a2fe12012-11-09 09:12:30 -080045 unsigned int state;
Matt Helsleydc52ddc2008-10-18 20:27:21 -070046};
47
Tejun Heoe5ced8e2014-05-07 21:31:17 -040048static DEFINE_MUTEX(freezer_mutex);
49
Tejun Heoa7c6d552013-08-08 20:11:23 -040050static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
51{
52 return css ? container_of(css, struct freezer, css) : NULL;
53}
54
Matt Helsleydc52ddc2008-10-18 20:27:21 -070055static inline struct freezer *task_freezer(struct task_struct *task)
56{
Tejun Heo073219e2014-02-08 10:36:58 -050057 return css_freezer(task_css(task, freezer_cgrp_id));
Matt Helsleydc52ddc2008-10-18 20:27:21 -070058}
59
Tejun Heoef9fe982012-11-09 09:12:30 -080060static struct freezer *parent_freezer(struct freezer *freezer)
61{
Tejun Heo63876982013-08-08 20:11:23 -040062 return css_freezer(css_parent(&freezer->css));
Tejun Heoef9fe982012-11-09 09:12:30 -080063}
64
Tejun Heo22b4e112011-11-21 12:32:25 -080065bool cgroup_freezing(struct task_struct *task)
Tomasz Buchertd5de4dd2010-10-27 15:33:32 -070066{
Tejun Heo22b4e112011-11-21 12:32:25 -080067 bool ret;
Tomasz Buchertd5de4dd2010-10-27 15:33:32 -070068
Tejun Heo22b4e112011-11-21 12:32:25 -080069 rcu_read_lock();
Tejun Heod6a2fe12012-11-09 09:12:30 -080070 ret = task_freezer(task)->state & CGROUP_FREEZING;
Tejun Heo22b4e112011-11-21 12:32:25 -080071 rcu_read_unlock();
72
73 return ret;
Matt Helsleydc52ddc2008-10-18 20:27:21 -070074}
75
76/*
77 * cgroups_write_string() limits the size of freezer state strings to
78 * CGROUP_LOCAL_BUFFER_SIZE
79 */
Tejun Heod6a2fe12012-11-09 09:12:30 -080080static const char *freezer_state_strs(unsigned int state)
81{
82 if (state & CGROUP_FROZEN)
83 return "FROZEN";
84 if (state & CGROUP_FREEZING)
85 return "FREEZING";
86 return "THAWED";
Matt Helsleydc52ddc2008-10-18 20:27:21 -070087};
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
Matt Helsleydc52ddc2008-10-18 20:27:21 -070098 return &freezer->css;
99}
100
Tejun Heo5300a9b2012-11-09 09:12:30 -0800101/**
Tejun Heoeb954192013-08-08 20:11:23 -0400102 * freezer_css_online - commit creation of a freezer css
103 * @css: css being created
Tejun Heo5300a9b2012-11-09 09:12:30 -0800104 *
Tejun Heoeb954192013-08-08 20:11:23 -0400105 * We're committing to creation of @css. Mark it online and inherit
Tejun Heoef9fe982012-11-09 09:12:30 -0800106 * parent's freezing state while holding both parent's and our
107 * freezer->lock.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800108 */
Tejun Heoeb954192013-08-08 20:11:23 -0400109static int freezer_css_online(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700110{
Tejun Heoeb954192013-08-08 20:11:23 -0400111 struct freezer *freezer = css_freezer(css);
Tejun Heoef9fe982012-11-09 09:12:30 -0800112 struct freezer *parent = parent_freezer(freezer);
Tejun Heoa3201222011-11-21 12:32:25 -0800113
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400114 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800115
Tejun Heo5300a9b2012-11-09 09:12:30 -0800116 freezer->state |= CGROUP_FREEZER_ONLINE;
Tejun Heoef9fe982012-11-09 09:12:30 -0800117
118 if (parent && (parent->state & CGROUP_FREEZING)) {
119 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
120 atomic_inc(&system_freezing_cnt);
121 }
122
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400123 mutex_unlock(&freezer_mutex);
Tejun Heob1929db2012-11-19 08:13:38 -0800124 return 0;
Tejun Heo5300a9b2012-11-09 09:12:30 -0800125}
126
127/**
Tejun Heoeb954192013-08-08 20:11:23 -0400128 * freezer_css_offline - initiate destruction of a freezer css
129 * @css: css being destroyed
Tejun Heo5300a9b2012-11-09 09:12:30 -0800130 *
Tejun Heoeb954192013-08-08 20:11:23 -0400131 * @css is going away. Mark it dead and decrement system_freezing_count if
132 * it was holding one.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800133 */
Tejun Heoeb954192013-08-08 20:11:23 -0400134static void freezer_css_offline(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800135{
Tejun Heoeb954192013-08-08 20:11:23 -0400136 struct freezer *freezer = css_freezer(css);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800137
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400138 mutex_lock(&freezer_mutex);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800139
Tejun Heod6a2fe12012-11-09 09:12:30 -0800140 if (freezer->state & CGROUP_FREEZING)
Tejun Heoa3201222011-11-21 12:32:25 -0800141 atomic_dec(&system_freezing_cnt);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800142
143 freezer->state = 0;
144
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400145 mutex_unlock(&freezer_mutex);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800146}
147
Tejun Heoeb954192013-08-08 20:11:23 -0400148static void freezer_css_free(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800149{
Tejun Heoeb954192013-08-08 20:11:23 -0400150 kfree(css_freezer(css));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700151}
152
Matt Helsley957a4ee2008-10-18 20:27:22 -0700153/*
Tejun Heoead5c472012-10-16 15:03:15 -0700154 * Tasks can be migrated into a different freezer anytime regardless of its
155 * current state. freezer_attach() is responsible for making new tasks
156 * conform to the current state.
157 *
158 * Freezer state changes and task migration are synchronized via
159 * @freezer->lock. freezer_attach() makes the new tasks conform to the
160 * current state and all following state changes can see the new tasks.
Matt Helsley957a4ee2008-10-18 20:27:22 -0700161 */
Tejun Heoeb954192013-08-08 20:11:23 -0400162static void freezer_attach(struct cgroup_subsys_state *new_css,
163 struct cgroup_taskset *tset)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700164{
Tejun Heoeb954192013-08-08 20:11:23 -0400165 struct freezer *freezer = css_freezer(new_css);
Tejun Heobb9d97b2011-12-12 18:12:21 -0800166 struct task_struct *task;
Tejun Heoef9fe982012-11-09 09:12:30 -0800167 bool clear_frozen = false;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700168
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400169 mutex_lock(&freezer_mutex);
Tejun Heo8755ade2012-10-16 15:03:14 -0700170
Li Zefan80a6a2c2008-10-29 14:00:52 -0700171 /*
Tejun Heoeb954192013-08-08 20:11:23 -0400172 * Make the new tasks conform to the current state of @new_css.
Tejun Heo8755ade2012-10-16 15:03:14 -0700173 * For simplicity, when migrating any task to a FROZEN cgroup, we
174 * revert it to FREEZING and let update_if_frozen() determine the
175 * correct state later.
176 *
Tejun Heoeb954192013-08-08 20:11:23 -0400177 * Tasks in @tset are on @new_css but may not conform to its
Tejun Heo8755ade2012-10-16 15:03:14 -0700178 * current state before executing the following - !frozen tasks may
179 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
Li Zefan80a6a2c2008-10-29 14:00:52 -0700180 */
Tejun Heo924f0d9a2014-02-13 06:58:41 -0500181 cgroup_taskset_for_each(task, tset) {
Tejun Heod6a2fe12012-11-09 09:12:30 -0800182 if (!(freezer->state & CGROUP_FREEZING)) {
Tejun Heo8755ade2012-10-16 15:03:14 -0700183 __thaw_task(task);
184 } else {
185 freeze_task(task);
Tejun Heod6a2fe12012-11-09 09:12:30 -0800186 freezer->state &= ~CGROUP_FROZEN;
Tejun Heoef9fe982012-11-09 09:12:30 -0800187 clear_frozen = true;
Tejun Heo8755ade2012-10-16 15:03:14 -0700188 }
189 }
Matt Helsley957a4ee2008-10-18 20:27:22 -0700190
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400191 /* propagate FROZEN clearing upwards */
Tejun Heoef9fe982012-11-09 09:12:30 -0800192 while (clear_frozen && (freezer = parent_freezer(freezer))) {
Tejun Heoef9fe982012-11-09 09:12:30 -0800193 freezer->state &= ~CGROUP_FROZEN;
194 clear_frozen = freezer->state & CGROUP_FREEZING;
Tejun Heoef9fe982012-11-09 09:12:30 -0800195 }
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400196
197 mutex_unlock(&freezer_mutex);
Ben Blumf780bdb2011-05-26 16:25:19 -0700198}
199
Tejun Heoa60bed22014-02-12 16:07:59 -0500200/**
201 * freezer_fork - cgroup post fork callback
202 * @task: a task which has just been forked
203 *
204 * @task has just been created and should conform to the current state of
205 * the cgroup_freezer it belongs to. This function may race against
206 * freezer_attach(). Losing to freezer_attach() means that we don't have
207 * to do anything as freezer_attach() will put @task into the appropriate
208 * state.
209 */
Li Zefan761b3ef52012-01-31 13:47:36 +0800210static void freezer_fork(struct task_struct *task)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700211{
212 struct freezer *freezer;
213
Li Zefan3b1b3f62008-11-12 13:26:50 -0800214 /*
Tejun Heoa60bed22014-02-12 16:07:59 -0500215 * The root cgroup is non-freezable, so we can skip locking the
216 * freezer. This is safe regardless of race with task migration.
217 * If we didn't race or won, skipping is obviously the right thing
218 * to do. If we lost and root is the new cgroup, noop is still the
219 * right thing to do.
Li Zefan3b1b3f62008-11-12 13:26:50 -0800220 */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400221 if (task_css_is_root(task, freezer_cgrp_id))
222 return;
Li Zefan3b1b3f62008-11-12 13:26:50 -0800223
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400224 mutex_lock(&freezer_mutex);
225 rcu_read_lock();
226
227 freezer = task_freezer(task);
228 if (freezer->state & CGROUP_FREEZING)
Tejun Heo839e3402011-11-21 12:32:26 -0800229 freeze_task(task);
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400230
Tejun Heo5edee612012-10-16 15:03:14 -0700231 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400232 mutex_unlock(&freezer_mutex);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700233}
234
Tejun Heoef9fe982012-11-09 09:12:30 -0800235/**
236 * update_if_frozen - update whether a cgroup finished freezing
Tejun Heo182446d2013-08-08 20:11:24 -0400237 * @css: css of interest
Tejun Heoef9fe982012-11-09 09:12:30 -0800238 *
239 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
240 * calling this function. If the current state is FREEZING but not FROZEN,
241 * this function checks whether all tasks of this cgroup and the descendant
242 * cgroups finished freezing and, if so, sets FROZEN.
243 *
244 * The caller is responsible for grabbing RCU read lock and calling
245 * update_if_frozen() on all descendants prior to invoking this function.
Tejun Heob4d18312012-10-16 15:03:14 -0700246 *
247 * Task states and freezer state might disagree while tasks are being
Tejun Heo182446d2013-08-08 20:11:24 -0400248 * migrated into or out of @css, so we can't verify task states against
Tejun Heoead5c472012-10-16 15:03:15 -0700249 * @freezer state here. See freezer_attach() for details.
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700250 */
Tejun Heo182446d2013-08-08 20:11:24 -0400251static void update_if_frozen(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700252{
Tejun Heo182446d2013-08-08 20:11:24 -0400253 struct freezer *freezer = css_freezer(css);
Tejun Heo492eb212013-08-08 20:11:25 -0400254 struct cgroup_subsys_state *pos;
Tejun Heo72ec7022013-08-08 20:11:26 -0400255 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700256 struct task_struct *task;
Tejun Heob4d18312012-10-16 15:03:14 -0700257
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400258 lockdep_assert_held(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800259
Tejun Heod6a2fe12012-11-09 09:12:30 -0800260 if (!(freezer->state & CGROUP_FREEZING) ||
261 (freezer->state & CGROUP_FROZEN))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400262 return;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700263
Tejun Heoef9fe982012-11-09 09:12:30 -0800264 /* are all (live) children frozen? */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400265 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400266 css_for_each_child(pos, css) {
267 struct freezer *child = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800268
269 if ((child->state & CGROUP_FREEZER_ONLINE) &&
Tejun Heo36e9d2e2014-05-13 11:28:30 -0400270 !(child->state & CGROUP_FROZEN)) {
271 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400272 return;
Tejun Heo36e9d2e2014-05-13 11:28:30 -0400273 }
Tejun Heoef9fe982012-11-09 09:12:30 -0800274 }
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400275 rcu_read_unlock();
Tejun Heoef9fe982012-11-09 09:12:30 -0800276
277 /* are all tasks frozen? */
Tejun Heo72ec7022013-08-08 20:11:26 -0400278 css_task_iter_start(css, &it);
Tejun Heob4d18312012-10-16 15:03:14 -0700279
Tejun Heo72ec7022013-08-08 20:11:26 -0400280 while ((task = css_task_iter_next(&it))) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700281 if (freezing(task)) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700282 /*
283 * freezer_should_skip() indicates that the task
284 * should be skipped when determining freezing
285 * completion. Consider it frozen in addition to
286 * the usual frozen condition.
287 */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200288 if (!frozen(task) && !freezer_should_skip(task))
Tejun Heoef9fe982012-11-09 09:12:30 -0800289 goto out_iter_end;
Tejun Heo3c426d52012-10-16 15:03:14 -0700290 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700291 }
292
Tejun Heod6a2fe12012-11-09 09:12:30 -0800293 freezer->state |= CGROUP_FROZEN;
Tejun Heoef9fe982012-11-09 09:12:30 -0800294out_iter_end:
Tejun Heo72ec7022013-08-08 20:11:26 -0400295 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700296}
297
Tejun Heo2da8ca82013-12-05 12:28:04 -0500298static int freezer_read(struct seq_file *m, void *v)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700299{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500300 struct cgroup_subsys_state *css = seq_css(m), *pos;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700301
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400302 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800303 rcu_read_lock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700304
Tejun Heoef9fe982012-11-09 09:12:30 -0800305 /* update states bottom-up */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400306 css_for_each_descendant_post(pos, css) {
Tejun Heoec903c02014-05-13 12:11:01 -0400307 if (!css_tryget_online(pos))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400308 continue;
309 rcu_read_unlock();
310
Tejun Heo492eb212013-08-08 20:11:25 -0400311 update_if_frozen(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800312
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400313 rcu_read_lock();
314 css_put(pos);
315 }
316
Tejun Heoef9fe982012-11-09 09:12:30 -0800317 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400318 mutex_unlock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800319
Tejun Heo182446d2013-08-08 20:11:24 -0400320 seq_puts(m, freezer_state_strs(css_freezer(css)->state));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700321 seq_putc(m, '\n');
322 return 0;
323}
324
Tejun Heobcd66c82012-11-09 09:12:29 -0800325static void freeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700326{
Tejun Heo72ec7022013-08-08 20:11:26 -0400327 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700328 struct task_struct *task;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700329
Tejun Heo72ec7022013-08-08 20:11:26 -0400330 css_task_iter_start(&freezer->css, &it);
331 while ((task = css_task_iter_next(&it)))
Tejun Heo51f246e2012-10-16 15:03:14 -0700332 freeze_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400333 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700334}
335
Tejun Heobcd66c82012-11-09 09:12:29 -0800336static void unfreeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700337{
Tejun Heo72ec7022013-08-08 20:11:26 -0400338 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700339 struct task_struct *task;
340
Tejun Heo72ec7022013-08-08 20:11:26 -0400341 css_task_iter_start(&freezer->css, &it);
342 while ((task = css_task_iter_next(&it)))
Tejun Heoa5be2d02011-11-21 12:32:23 -0800343 __thaw_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400344 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700345}
346
Tejun Heo04a4ec32012-11-09 09:12:30 -0800347/**
348 * freezer_apply_state - apply state change to a single cgroup_freezer
349 * @freezer: freezer to apply state change to
350 * @freeze: whether to freeze or unfreeze
Tejun Heoa2252182012-11-09 09:12:30 -0800351 * @state: CGROUP_FREEZING_* flag to set or clear
352 *
353 * Set or clear @state on @cgroup according to @freeze, and perform
354 * freezing or thawing as necessary.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800355 */
Tejun Heoa2252182012-11-09 09:12:30 -0800356static void freezer_apply_state(struct freezer *freezer, bool freeze,
357 unsigned int state)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700358{
Tejun Heoead5c472012-10-16 15:03:15 -0700359 /* also synchronizes against task migration, see freezer_attach() */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400360 lockdep_assert_held(&freezer_mutex);
Li Zefan51308ee2008-10-29 14:00:54 -0700361
Tejun Heo5300a9b2012-11-09 09:12:30 -0800362 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
363 return;
364
Tejun Heo04a4ec32012-11-09 09:12:30 -0800365 if (freeze) {
Tejun Heod6a2fe12012-11-09 09:12:30 -0800366 if (!(freezer->state & CGROUP_FREEZING))
Tejun Heoa3201222011-11-21 12:32:25 -0800367 atomic_inc(&system_freezing_cnt);
Tejun Heoa2252182012-11-09 09:12:30 -0800368 freezer->state |= state;
Tejun Heobcd66c82012-11-09 09:12:29 -0800369 freeze_cgroup(freezer);
Tejun Heo04a4ec32012-11-09 09:12:30 -0800370 } else {
Tejun Heoa2252182012-11-09 09:12:30 -0800371 bool was_freezing = freezer->state & CGROUP_FREEZING;
372
373 freezer->state &= ~state;
374
375 if (!(freezer->state & CGROUP_FREEZING)) {
376 if (was_freezing)
377 atomic_dec(&system_freezing_cnt);
378 freezer->state &= ~CGROUP_FROZEN;
379 unfreeze_cgroup(freezer);
380 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700381 }
Tejun Heo04a4ec32012-11-09 09:12:30 -0800382}
Tejun Heo22b4e112011-11-21 12:32:25 -0800383
Tejun Heo04a4ec32012-11-09 09:12:30 -0800384/**
385 * freezer_change_state - change the freezing state of a cgroup_freezer
386 * @freezer: freezer of interest
387 * @freeze: whether to freeze or thaw
388 *
Tejun Heoef9fe982012-11-09 09:12:30 -0800389 * Freeze or thaw @freezer according to @freeze. The operations are
390 * recursive - all descendants of @freezer will be affected.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800391 */
392static void freezer_change_state(struct freezer *freezer, bool freeze)
393{
Tejun Heo492eb212013-08-08 20:11:25 -0400394 struct cgroup_subsys_state *pos;
Tejun Heoef9fe982012-11-09 09:12:30 -0800395
Tejun Heoef9fe982012-11-09 09:12:30 -0800396 /*
397 * Update all its descendants in pre-order traversal. Each
398 * descendant will try to inherit its parent's FREEZING state as
399 * CGROUP_FREEZING_PARENT.
400 */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400401 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800402 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400403 css_for_each_descendant_pre(pos, &freezer->css) {
404 struct freezer *pos_f = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800405 struct freezer *parent = parent_freezer(pos_f);
406
Tejun Heoec903c02014-05-13 12:11:01 -0400407 if (!css_tryget_online(pos))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400408 continue;
409 rcu_read_unlock();
Tejun Heobd8815a2013-08-08 20:11:27 -0400410
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400411 if (pos_f == freezer)
Tejun Heobd8815a2013-08-08 20:11:27 -0400412 freezer_apply_state(pos_f, freeze,
413 CGROUP_FREEZING_SELF);
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400414 else
Tejun Heobd8815a2013-08-08 20:11:27 -0400415 freezer_apply_state(pos_f,
416 parent->state & CGROUP_FREEZING,
417 CGROUP_FREEZING_PARENT);
Tejun Heobd8815a2013-08-08 20:11:27 -0400418
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400419 rcu_read_lock();
420 css_put(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800421 }
422 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400423 mutex_unlock(&freezer_mutex);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700424}
425
Tejun Heo182446d2013-08-08 20:11:24 -0400426static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft,
Tejun Heo4d3bb512014-03-19 10:23:54 -0400427 char *buffer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700428{
Tejun Heo04a4ec32012-11-09 09:12:30 -0800429 bool freeze;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700430
Tejun Heod6a2fe12012-11-09 09:12:30 -0800431 if (strcmp(buffer, freezer_state_strs(0)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800432 freeze = false;
Tejun Heod6a2fe12012-11-09 09:12:30 -0800433 else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800434 freeze = true;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700435 else
Li Zefan3b1b3f62008-11-12 13:26:50 -0800436 return -EINVAL;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700437
Tejun Heo182446d2013-08-08 20:11:24 -0400438 freezer_change_state(css_freezer(css), freeze);
Tejun Heo51f246e2012-10-16 15:03:14 -0700439 return 0;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700440}
441
Tejun Heo182446d2013-08-08 20:11:24 -0400442static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
443 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800444{
Tejun Heo182446d2013-08-08 20:11:24 -0400445 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800446
447 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
448}
449
Tejun Heo182446d2013-08-08 20:11:24 -0400450static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
451 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800452{
Tejun Heo182446d2013-08-08 20:11:24 -0400453 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800454
455 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
456}
457
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700458static struct cftype files[] = {
459 {
460 .name = "state",
Tejun Heo4baf6e32012-04-01 12:09:55 -0700461 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -0500462 .seq_show = freezer_read,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700463 .write_string = freezer_write,
464 },
Tejun Heoa2252182012-11-09 09:12:30 -0800465 {
466 .name = "self_freezing",
467 .flags = CFTYPE_NOT_ON_ROOT,
468 .read_u64 = freezer_self_freezing_read,
469 },
470 {
471 .name = "parent_freezing",
472 .flags = CFTYPE_NOT_ON_ROOT,
473 .read_u64 = freezer_parent_freezing_read,
474 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700475 { } /* terminate */
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700476};
477
Tejun Heo073219e2014-02-08 10:36:58 -0500478struct cgroup_subsys freezer_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800479 .css_alloc = freezer_css_alloc,
480 .css_online = freezer_css_online,
481 .css_offline = freezer_css_offline,
482 .css_free = freezer_css_free,
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};