blob: 1b72d56edce5e2d9253c2c03f203e81aa76bd16c [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 Heo5c9d5352014-05-16 13:22:48 -040062 return css_freezer(freezer->css.parent);
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
Tejun Heod6a2fe12012-11-09 09:12:30 -080076static const char *freezer_state_strs(unsigned int state)
77{
78 if (state & CGROUP_FROZEN)
79 return "FROZEN";
80 if (state & CGROUP_FREEZING)
81 return "FREEZING";
82 return "THAWED";
Matt Helsleydc52ddc2008-10-18 20:27:21 -070083};
84
Tejun Heoeb954192013-08-08 20:11:23 -040085static struct cgroup_subsys_state *
86freezer_css_alloc(struct cgroup_subsys_state *parent_css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -070087{
88 struct freezer *freezer;
89
90 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
91 if (!freezer)
92 return ERR_PTR(-ENOMEM);
93
Matt Helsleydc52ddc2008-10-18 20:27:21 -070094 return &freezer->css;
95}
96
Tejun Heo5300a9b2012-11-09 09:12:30 -080097/**
Tejun Heoeb954192013-08-08 20:11:23 -040098 * freezer_css_online - commit creation of a freezer css
99 * @css: css being created
Tejun Heo5300a9b2012-11-09 09:12:30 -0800100 *
Tejun Heoeb954192013-08-08 20:11:23 -0400101 * We're committing to creation of @css. Mark it online and inherit
Tejun Heoef9fe982012-11-09 09:12:30 -0800102 * parent's freezing state while holding both parent's and our
103 * freezer->lock.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800104 */
Tejun Heoeb954192013-08-08 20:11:23 -0400105static int freezer_css_online(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700106{
Tejun Heoeb954192013-08-08 20:11:23 -0400107 struct freezer *freezer = css_freezer(css);
Tejun Heoef9fe982012-11-09 09:12:30 -0800108 struct freezer *parent = parent_freezer(freezer);
Tejun Heoa3201222011-11-21 12:32:25 -0800109
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400110 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800111
Tejun Heo5300a9b2012-11-09 09:12:30 -0800112 freezer->state |= CGROUP_FREEZER_ONLINE;
Tejun Heoef9fe982012-11-09 09:12:30 -0800113
114 if (parent && (parent->state & CGROUP_FREEZING)) {
115 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
116 atomic_inc(&system_freezing_cnt);
117 }
118
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400119 mutex_unlock(&freezer_mutex);
Tejun Heob1929db2012-11-19 08:13:38 -0800120 return 0;
Tejun Heo5300a9b2012-11-09 09:12:30 -0800121}
122
123/**
Tejun Heoeb954192013-08-08 20:11:23 -0400124 * freezer_css_offline - initiate destruction of a freezer css
125 * @css: css being destroyed
Tejun Heo5300a9b2012-11-09 09:12:30 -0800126 *
Tejun Heoeb954192013-08-08 20:11:23 -0400127 * @css is going away. Mark it dead and decrement system_freezing_count if
128 * it was holding one.
Tejun Heo5300a9b2012-11-09 09:12:30 -0800129 */
Tejun Heoeb954192013-08-08 20:11:23 -0400130static void freezer_css_offline(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800131{
Tejun Heoeb954192013-08-08 20:11:23 -0400132 struct freezer *freezer = css_freezer(css);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800133
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400134 mutex_lock(&freezer_mutex);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800135
Tejun Heod6a2fe12012-11-09 09:12:30 -0800136 if (freezer->state & CGROUP_FREEZING)
Tejun Heoa3201222011-11-21 12:32:25 -0800137 atomic_dec(&system_freezing_cnt);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800138
139 freezer->state = 0;
140
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400141 mutex_unlock(&freezer_mutex);
Tejun Heo5300a9b2012-11-09 09:12:30 -0800142}
143
Tejun Heoeb954192013-08-08 20:11:23 -0400144static void freezer_css_free(struct cgroup_subsys_state *css)
Tejun Heo5300a9b2012-11-09 09:12:30 -0800145{
Tejun Heoeb954192013-08-08 20:11:23 -0400146 kfree(css_freezer(css));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700147}
148
Matt Helsley957a4ee2008-10-18 20:27:22 -0700149/*
Tejun Heoead5c472012-10-16 15:03:15 -0700150 * Tasks can be migrated into a different freezer anytime regardless of its
151 * current state. freezer_attach() is responsible for making new tasks
152 * conform to the current state.
153 *
154 * Freezer state changes and task migration are synchronized via
155 * @freezer->lock. freezer_attach() makes the new tasks conform to the
156 * current state and all following state changes can see the new tasks.
Matt Helsley957a4ee2008-10-18 20:27:22 -0700157 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500158static void freezer_attach(struct cgroup_taskset *tset)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700159{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800160 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500161 struct cgroup_subsys_state *new_css;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700162
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400163 mutex_lock(&freezer_mutex);
Tejun Heo8755ade2012-10-16 15:03:14 -0700164
Li Zefan80a6a2c2008-10-29 14:00:52 -0700165 /*
Tejun Heoeb954192013-08-08 20:11:23 -0400166 * Make the new tasks conform to the current state of @new_css.
Tejun Heo8755ade2012-10-16 15:03:14 -0700167 * For simplicity, when migrating any task to a FROZEN cgroup, we
168 * revert it to FREEZING and let update_if_frozen() determine the
169 * correct state later.
170 *
Tejun Heoeb954192013-08-08 20:11:23 -0400171 * Tasks in @tset are on @new_css but may not conform to its
Tejun Heo8755ade2012-10-16 15:03:14 -0700172 * current state before executing the following - !frozen tasks may
173 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
Li Zefan80a6a2c2008-10-29 14:00:52 -0700174 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500175 cgroup_taskset_for_each(task, new_css, tset) {
Tejun Heo599c9632015-12-03 10:18:21 -0500176 struct freezer *freezer = css_freezer(new_css);
177
Tejun Heod6a2fe12012-11-09 09:12:30 -0800178 if (!(freezer->state & CGROUP_FREEZING)) {
Tejun Heo8755ade2012-10-16 15:03:14 -0700179 __thaw_task(task);
180 } else {
181 freeze_task(task);
Tejun Heo599c9632015-12-03 10:18:21 -0500182 /* clear FROZEN and propagate upwards */
183 while (freezer && (freezer->state & CGROUP_FROZEN)) {
184 freezer->state &= ~CGROUP_FROZEN;
185 freezer = parent_freezer(freezer);
186 }
Tejun Heo8755ade2012-10-16 15:03:14 -0700187 }
188 }
Matt Helsley957a4ee2008-10-18 20:27:22 -0700189
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400190 mutex_unlock(&freezer_mutex);
Ben Blumf780bdb2011-05-26 16:25:19 -0700191}
192
Tejun Heoa60bed22014-02-12 16:07:59 -0500193/**
194 * freezer_fork - cgroup post fork callback
195 * @task: a task which has just been forked
196 *
197 * @task has just been created and should conform to the current state of
198 * the cgroup_freezer it belongs to. This function may race against
199 * freezer_attach(). Losing to freezer_attach() means that we don't have
200 * to do anything as freezer_attach() will put @task into the appropriate
201 * state.
202 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500203static void freezer_fork(struct task_struct *task)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700204{
205 struct freezer *freezer;
206
Li Zefan3b1b3f62008-11-12 13:26:50 -0800207 /*
Tejun Heoa60bed22014-02-12 16:07:59 -0500208 * The root cgroup is non-freezable, so we can skip locking the
209 * freezer. This is safe regardless of race with task migration.
210 * If we didn't race or won, skipping is obviously the right thing
211 * to do. If we lost and root is the new cgroup, noop is still the
212 * right thing to do.
Li Zefan3b1b3f62008-11-12 13:26:50 -0800213 */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400214 if (task_css_is_root(task, freezer_cgrp_id))
215 return;
Li Zefan3b1b3f62008-11-12 13:26:50 -0800216
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400217 mutex_lock(&freezer_mutex);
218 rcu_read_lock();
219
220 freezer = task_freezer(task);
221 if (freezer->state & CGROUP_FREEZING)
Tejun Heo839e3402011-11-21 12:32:26 -0800222 freeze_task(task);
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400223
Tejun Heo5edee612012-10-16 15:03:14 -0700224 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400225 mutex_unlock(&freezer_mutex);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700226}
227
Tejun Heoef9fe982012-11-09 09:12:30 -0800228/**
229 * update_if_frozen - update whether a cgroup finished freezing
Tejun Heo182446d2013-08-08 20:11:24 -0400230 * @css: css of interest
Tejun Heoef9fe982012-11-09 09:12:30 -0800231 *
232 * Once FREEZING is initiated, transition to FROZEN is lazily updated by
233 * calling this function. If the current state is FREEZING but not FROZEN,
234 * this function checks whether all tasks of this cgroup and the descendant
235 * cgroups finished freezing and, if so, sets FROZEN.
236 *
237 * The caller is responsible for grabbing RCU read lock and calling
238 * update_if_frozen() on all descendants prior to invoking this function.
Tejun Heob4d18312012-10-16 15:03:14 -0700239 *
240 * Task states and freezer state might disagree while tasks are being
Tejun Heo182446d2013-08-08 20:11:24 -0400241 * migrated into or out of @css, so we can't verify task states against
Tejun Heoead5c472012-10-16 15:03:15 -0700242 * @freezer state here. See freezer_attach() for details.
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700243 */
Tejun Heo182446d2013-08-08 20:11:24 -0400244static void update_if_frozen(struct cgroup_subsys_state *css)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700245{
Tejun Heo182446d2013-08-08 20:11:24 -0400246 struct freezer *freezer = css_freezer(css);
Tejun Heo492eb212013-08-08 20:11:25 -0400247 struct cgroup_subsys_state *pos;
Tejun Heo72ec7022013-08-08 20:11:26 -0400248 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700249 struct task_struct *task;
Tejun Heob4d18312012-10-16 15:03:14 -0700250
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400251 lockdep_assert_held(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800252
Tejun Heod6a2fe12012-11-09 09:12:30 -0800253 if (!(freezer->state & CGROUP_FREEZING) ||
254 (freezer->state & CGROUP_FROZEN))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400255 return;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700256
Tejun Heoef9fe982012-11-09 09:12:30 -0800257 /* are all (live) children frozen? */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400258 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400259 css_for_each_child(pos, css) {
260 struct freezer *child = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800261
262 if ((child->state & CGROUP_FREEZER_ONLINE) &&
Tejun Heo36e9d2e2014-05-13 11:28:30 -0400263 !(child->state & CGROUP_FROZEN)) {
264 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400265 return;
Tejun Heo36e9d2e2014-05-13 11:28:30 -0400266 }
Tejun Heoef9fe982012-11-09 09:12:30 -0800267 }
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400268 rcu_read_unlock();
Tejun Heoef9fe982012-11-09 09:12:30 -0800269
270 /* are all tasks frozen? */
Tejun Heo72ec7022013-08-08 20:11:26 -0400271 css_task_iter_start(css, &it);
Tejun Heob4d18312012-10-16 15:03:14 -0700272
Tejun Heo72ec7022013-08-08 20:11:26 -0400273 while ((task = css_task_iter_next(&it))) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700274 if (freezing(task)) {
Tejun Heo3c426d52012-10-16 15:03:14 -0700275 /*
276 * freezer_should_skip() indicates that the task
277 * should be skipped when determining freezing
278 * completion. Consider it frozen in addition to
279 * the usual frozen condition.
280 */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +0200281 if (!frozen(task) && !freezer_should_skip(task))
Tejun Heoef9fe982012-11-09 09:12:30 -0800282 goto out_iter_end;
Tejun Heo3c426d52012-10-16 15:03:14 -0700283 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700284 }
285
Tejun Heod6a2fe12012-11-09 09:12:30 -0800286 freezer->state |= CGROUP_FROZEN;
Tejun Heoef9fe982012-11-09 09:12:30 -0800287out_iter_end:
Tejun Heo72ec7022013-08-08 20:11:26 -0400288 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700289}
290
Tejun Heo2da8ca82013-12-05 12:28:04 -0500291static int freezer_read(struct seq_file *m, void *v)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700292{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500293 struct cgroup_subsys_state *css = seq_css(m), *pos;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700294
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400295 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800296 rcu_read_lock();
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700297
Tejun Heoef9fe982012-11-09 09:12:30 -0800298 /* update states bottom-up */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400299 css_for_each_descendant_post(pos, css) {
Tejun Heoec903c02014-05-13 12:11:01 -0400300 if (!css_tryget_online(pos))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400301 continue;
302 rcu_read_unlock();
303
Tejun Heo492eb212013-08-08 20:11:25 -0400304 update_if_frozen(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800305
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400306 rcu_read_lock();
307 css_put(pos);
308 }
309
Tejun Heoef9fe982012-11-09 09:12:30 -0800310 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400311 mutex_unlock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800312
Tejun Heo182446d2013-08-08 20:11:24 -0400313 seq_puts(m, freezer_state_strs(css_freezer(css)->state));
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700314 seq_putc(m, '\n');
315 return 0;
316}
317
Tejun Heobcd66c82012-11-09 09:12:29 -0800318static void freeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700319{
Tejun Heo72ec7022013-08-08 20:11:26 -0400320 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700321 struct task_struct *task;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700322
Tejun Heo72ec7022013-08-08 20:11:26 -0400323 css_task_iter_start(&freezer->css, &it);
324 while ((task = css_task_iter_next(&it)))
Tejun Heo51f246e2012-10-16 15:03:14 -0700325 freeze_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400326 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700327}
328
Tejun Heobcd66c82012-11-09 09:12:29 -0800329static void unfreeze_cgroup(struct freezer *freezer)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700330{
Tejun Heo72ec7022013-08-08 20:11:26 -0400331 struct css_task_iter it;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700332 struct task_struct *task;
333
Tejun Heo72ec7022013-08-08 20:11:26 -0400334 css_task_iter_start(&freezer->css, &it);
335 while ((task = css_task_iter_next(&it)))
Tejun Heoa5be2d02011-11-21 12:32:23 -0800336 __thaw_task(task);
Tejun Heo72ec7022013-08-08 20:11:26 -0400337 css_task_iter_end(&it);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700338}
339
Tejun Heo04a4ec32012-11-09 09:12:30 -0800340/**
341 * freezer_apply_state - apply state change to a single cgroup_freezer
342 * @freezer: freezer to apply state change to
343 * @freeze: whether to freeze or unfreeze
Tejun Heoa2252182012-11-09 09:12:30 -0800344 * @state: CGROUP_FREEZING_* flag to set or clear
345 *
346 * Set or clear @state on @cgroup according to @freeze, and perform
347 * freezing or thawing as necessary.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800348 */
Tejun Heoa2252182012-11-09 09:12:30 -0800349static void freezer_apply_state(struct freezer *freezer, bool freeze,
350 unsigned int state)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700351{
Tejun Heoead5c472012-10-16 15:03:15 -0700352 /* also synchronizes against task migration, see freezer_attach() */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400353 lockdep_assert_held(&freezer_mutex);
Li Zefan51308ee2008-10-29 14:00:54 -0700354
Tejun Heo5300a9b2012-11-09 09:12:30 -0800355 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
356 return;
357
Tejun Heo04a4ec32012-11-09 09:12:30 -0800358 if (freeze) {
Tejun Heod6a2fe12012-11-09 09:12:30 -0800359 if (!(freezer->state & CGROUP_FREEZING))
Tejun Heoa3201222011-11-21 12:32:25 -0800360 atomic_inc(&system_freezing_cnt);
Tejun Heoa2252182012-11-09 09:12:30 -0800361 freezer->state |= state;
Tejun Heobcd66c82012-11-09 09:12:29 -0800362 freeze_cgroup(freezer);
Tejun Heo04a4ec32012-11-09 09:12:30 -0800363 } else {
Tejun Heoa2252182012-11-09 09:12:30 -0800364 bool was_freezing = freezer->state & CGROUP_FREEZING;
365
366 freezer->state &= ~state;
367
368 if (!(freezer->state & CGROUP_FREEZING)) {
369 if (was_freezing)
370 atomic_dec(&system_freezing_cnt);
371 freezer->state &= ~CGROUP_FROZEN;
372 unfreeze_cgroup(freezer);
373 }
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700374 }
Tejun Heo04a4ec32012-11-09 09:12:30 -0800375}
Tejun Heo22b4e112011-11-21 12:32:25 -0800376
Tejun Heo04a4ec32012-11-09 09:12:30 -0800377/**
378 * freezer_change_state - change the freezing state of a cgroup_freezer
379 * @freezer: freezer of interest
380 * @freeze: whether to freeze or thaw
381 *
Tejun Heoef9fe982012-11-09 09:12:30 -0800382 * Freeze or thaw @freezer according to @freeze. The operations are
383 * recursive - all descendants of @freezer will be affected.
Tejun Heo04a4ec32012-11-09 09:12:30 -0800384 */
385static void freezer_change_state(struct freezer *freezer, bool freeze)
386{
Tejun Heo492eb212013-08-08 20:11:25 -0400387 struct cgroup_subsys_state *pos;
Tejun Heoef9fe982012-11-09 09:12:30 -0800388
Tejun Heoef9fe982012-11-09 09:12:30 -0800389 /*
390 * Update all its descendants in pre-order traversal. Each
391 * descendant will try to inherit its parent's FREEZING state as
392 * CGROUP_FREEZING_PARENT.
393 */
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400394 mutex_lock(&freezer_mutex);
Tejun Heoef9fe982012-11-09 09:12:30 -0800395 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -0400396 css_for_each_descendant_pre(pos, &freezer->css) {
397 struct freezer *pos_f = css_freezer(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800398 struct freezer *parent = parent_freezer(pos_f);
399
Tejun Heoec903c02014-05-13 12:11:01 -0400400 if (!css_tryget_online(pos))
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400401 continue;
402 rcu_read_unlock();
Tejun Heobd8815a2013-08-08 20:11:27 -0400403
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400404 if (pos_f == freezer)
Tejun Heobd8815a2013-08-08 20:11:27 -0400405 freezer_apply_state(pos_f, freeze,
406 CGROUP_FREEZING_SELF);
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400407 else
Tejun Heobd8815a2013-08-08 20:11:27 -0400408 freezer_apply_state(pos_f,
409 parent->state & CGROUP_FREEZING,
410 CGROUP_FREEZING_PARENT);
Tejun Heobd8815a2013-08-08 20:11:27 -0400411
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400412 rcu_read_lock();
413 css_put(pos);
Tejun Heoef9fe982012-11-09 09:12:30 -0800414 }
415 rcu_read_unlock();
Tejun Heoe5ced8e2014-05-07 21:31:17 -0400416 mutex_unlock(&freezer_mutex);
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700417}
418
Tejun Heo451af5042014-05-13 12:16:21 -0400419static ssize_t freezer_write(struct kernfs_open_file *of,
420 char *buf, size_t nbytes, loff_t off)
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700421{
Tejun Heo04a4ec32012-11-09 09:12:30 -0800422 bool freeze;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700423
Tejun Heo451af5042014-05-13 12:16:21 -0400424 buf = strstrip(buf);
425
426 if (strcmp(buf, freezer_state_strs(0)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800427 freeze = false;
Tejun Heo451af5042014-05-13 12:16:21 -0400428 else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0)
Tejun Heo04a4ec32012-11-09 09:12:30 -0800429 freeze = true;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700430 else
Li Zefan3b1b3f62008-11-12 13:26:50 -0800431 return -EINVAL;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700432
Tejun Heo451af5042014-05-13 12:16:21 -0400433 freezer_change_state(css_freezer(of_css(of)), freeze);
434 return nbytes;
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700435}
436
Tejun Heo182446d2013-08-08 20:11:24 -0400437static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
438 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800439{
Tejun Heo182446d2013-08-08 20:11:24 -0400440 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800441
442 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
443}
444
Tejun Heo182446d2013-08-08 20:11:24 -0400445static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
446 struct cftype *cft)
Tejun Heoa2252182012-11-09 09:12:30 -0800447{
Tejun Heo182446d2013-08-08 20:11:24 -0400448 struct freezer *freezer = css_freezer(css);
Tejun Heoa2252182012-11-09 09:12:30 -0800449
450 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
451}
452
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700453static struct cftype files[] = {
454 {
455 .name = "state",
Tejun Heo4baf6e32012-04-01 12:09:55 -0700456 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -0500457 .seq_show = freezer_read,
Tejun Heo451af5042014-05-13 12:16:21 -0400458 .write = freezer_write,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700459 },
Tejun Heoa2252182012-11-09 09:12:30 -0800460 {
461 .name = "self_freezing",
462 .flags = CFTYPE_NOT_ON_ROOT,
463 .read_u64 = freezer_self_freezing_read,
464 },
465 {
466 .name = "parent_freezing",
467 .flags = CFTYPE_NOT_ON_ROOT,
468 .read_u64 = freezer_parent_freezing_read,
469 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700470 { } /* terminate */
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700471};
472
Tejun Heo073219e2014-02-08 10:36:58 -0500473struct cgroup_subsys freezer_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800474 .css_alloc = freezer_css_alloc,
475 .css_online = freezer_css_online,
476 .css_offline = freezer_css_offline,
477 .css_free = freezer_css_free,
Tejun Heo8755ade2012-10-16 15:03:14 -0700478 .attach = freezer_attach,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700479 .fork = freezer_fork,
Tejun Heo55779642014-07-15 11:05:09 -0400480 .legacy_cftypes = files,
Matt Helsleydc52ddc2008-10-18 20:27:21 -0700481};