cgroup_freezer.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * cgroup_freezer.c -  control group freezer subsystem
 *
 * Copyright IBM Corporation, 2007
 *
 * Author : Cedric Le Goater <clg@fr.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

17
#include <linux/export.h>
18
#include <linux/slab.h>
19 20 21 22 23 24
#include <linux/cgroup.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/freezer.h>
#include <linux/seq_file.h>

25 26 27
enum freezer_state_flags {
	CGROUP_FREEZING		= (1 << 1), /* this freezer is freezing */
	CGROUP_FROZEN		= (1 << 3), /* this and its descendants frozen */
28 29 30
};

struct freezer {
T
Tejun Heo 已提交
31
	struct cgroup_subsys_state	css;
32
	unsigned int			state;
T
Tejun Heo 已提交
33
	spinlock_t			lock;
34 35
};

T
Tejun Heo 已提交
36
static inline struct freezer *cgroup_freezer(struct cgroup *cgroup)
37
{
T
Tejun Heo 已提交
38 39
	return container_of(cgroup_subsys_state(cgroup, freezer_subsys_id),
			    struct freezer, css);
40 41 42 43 44 45 46 47
}

static inline struct freezer *task_freezer(struct task_struct *task)
{
	return container_of(task_subsys_state(task, freezer_subsys_id),
			    struct freezer, css);
}

48
bool cgroup_freezing(struct task_struct *task)
49
{
50
	bool ret;
51

52
	rcu_read_lock();
53
	ret = task_freezer(task)->state & CGROUP_FREEZING;
54 55 56
	rcu_read_unlock();

	return ret;
57 58 59 60 61 62
}

/*
 * cgroups_write_string() limits the size of freezer state strings to
 * CGROUP_LOCAL_BUFFER_SIZE
 */
63 64 65 66 67 68 69
static const char *freezer_state_strs(unsigned int state)
{
	if (state & CGROUP_FROZEN)
		return "FROZEN";
	if (state & CGROUP_FREEZING)
		return "FREEZING";
	return "THAWED";
70 71 72 73 74 75 76
};

/*
 * State diagram
 * Transitions are caused by userspace writes to the freezer.state file.
 * The values in parenthesis are state labels. The rest are edge labels.
 *
77 78 79 80
 * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
 *    ^ ^                    |                     |
 *    | \_______THAWED_______/                     |
 *    \__________________________THAWED____________/
81 82 83 84
 */

struct cgroup_subsys freezer_subsys;

85
static struct cgroup_subsys_state *freezer_create(struct cgroup *cgroup)
86 87 88 89 90 91 92 93 94 95 96
{
	struct freezer *freezer;

	freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
	if (!freezer)
		return ERR_PTR(-ENOMEM);

	spin_lock_init(&freezer->lock);
	return &freezer->css;
}

97
static void freezer_destroy(struct cgroup *cgroup)
98
{
99 100
	struct freezer *freezer = cgroup_freezer(cgroup);

101
	if (freezer->state & CGROUP_FREEZING)
102 103
		atomic_dec(&system_freezing_cnt);
	kfree(freezer);
104 105
}

106
/*
107 108 109 110 111 112 113
 * Tasks can be migrated into a different freezer anytime regardless of its
 * current state.  freezer_attach() is responsible for making new tasks
 * conform to the current state.
 *
 * Freezer state changes and task migration are synchronized via
 * @freezer->lock.  freezer_attach() makes the new tasks conform to the
 * current state and all following state changes can see the new tasks.
114
 */
115
static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
116
{
117
	struct freezer *freezer = cgroup_freezer(new_cgrp);
118
	struct task_struct *task;
119

120 121
	spin_lock_irq(&freezer->lock);

122
	/*
123 124 125 126 127 128 129 130
	 * Make the new tasks conform to the current state of @new_cgrp.
	 * For simplicity, when migrating any task to a FROZEN cgroup, we
	 * revert it to FREEZING and let update_if_frozen() determine the
	 * correct state later.
	 *
	 * Tasks in @tset are on @new_cgrp but may not conform to its
	 * current state before executing the following - !frozen tasks may
	 * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
131
	 */
132
	cgroup_taskset_for_each(task, new_cgrp, tset) {
133
		if (!(freezer->state & CGROUP_FREEZING)) {
134 135 136
			__thaw_task(task);
		} else {
			freeze_task(task);
137
			freezer->state &= ~CGROUP_FROZEN;
138 139
		}
	}
140

141
	spin_unlock_irq(&freezer->lock);
142 143
}

144
static void freezer_fork(struct task_struct *task)
145 146 147
{
	struct freezer *freezer;

148
	rcu_read_lock();
149 150
	freezer = task_freezer(task);

151 152 153 154 155
	/*
	 * The root cgroup is non-freezable, so we can skip the
	 * following check.
	 */
	if (!freezer->css.cgroup->parent)
156
		goto out;
157

158
	spin_lock_irq(&freezer->lock);
159
	if (freezer->state & CGROUP_FREEZING)
160
		freeze_task(task);
161
	spin_unlock_irq(&freezer->lock);
162 163
out:
	rcu_read_unlock();
164 165 166
}

/*
167 168 169 170
 * We change from FREEZING to FROZEN lazily if the cgroup was only
 * partially frozen when we exitted write.  Caller must hold freezer->lock.
 *
 * Task states and freezer state might disagree while tasks are being
171 172
 * migrated into or out of @cgroup, so we can't verify task states against
 * @freezer state here.  See freezer_attach() for details.
173
 */
T
Tejun Heo 已提交
174
static void update_if_frozen(struct freezer *freezer)
175
{
T
Tejun Heo 已提交
176
	struct cgroup *cgroup = freezer->css.cgroup;
177 178
	struct cgroup_iter it;
	struct task_struct *task;
179

180 181
	if (!(freezer->state & CGROUP_FREEZING) ||
	    (freezer->state & CGROUP_FROZEN))
182
		return;
183 184

	cgroup_iter_start(cgroup, &it);
185

186
	while ((task = cgroup_iter_next(cgroup, &it))) {
187 188 189 190 191 192 193
		if (freezing(task)) {
			/*
			 * freezer_should_skip() indicates that the task
			 * should be skipped when determining freezing
			 * completion.  Consider it frozen in addition to
			 * the usual frozen condition.
			 */
194
			if (!frozen(task) && !freezer_should_skip(task))
195
				goto notyet;
196
		}
197 198
	}

199
	freezer->state |= CGROUP_FROZEN;
200
notyet:
201 202 203 204 205 206
	cgroup_iter_end(cgroup, &it);
}

static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
			struct seq_file *m)
{
T
Tejun Heo 已提交
207
	struct freezer *freezer = cgroup_freezer(cgroup);
208
	unsigned int state;
209 210

	spin_lock_irq(&freezer->lock);
T
Tejun Heo 已提交
211
	update_if_frozen(freezer);
212 213 214
	state = freezer->state;
	spin_unlock_irq(&freezer->lock);

215
	seq_puts(m, freezer_state_strs(state));
216 217 218 219
	seq_putc(m, '\n');
	return 0;
}

T
Tejun Heo 已提交
220
static void freeze_cgroup(struct freezer *freezer)
221
{
T
Tejun Heo 已提交
222
	struct cgroup *cgroup = freezer->css.cgroup;
223 224 225 226
	struct cgroup_iter it;
	struct task_struct *task;

	cgroup_iter_start(cgroup, &it);
227 228
	while ((task = cgroup_iter_next(cgroup, &it)))
		freeze_task(task);
229 230 231
	cgroup_iter_end(cgroup, &it);
}

T
Tejun Heo 已提交
232
static void unfreeze_cgroup(struct freezer *freezer)
233
{
T
Tejun Heo 已提交
234
	struct cgroup *cgroup = freezer->css.cgroup;
235 236 237 238
	struct cgroup_iter it;
	struct task_struct *task;

	cgroup_iter_start(cgroup, &it);
239 240
	while ((task = cgroup_iter_next(cgroup, &it)))
		__thaw_task(task);
241 242 243
	cgroup_iter_end(cgroup, &it);
}

244 245 246 247 248 249
/**
 * freezer_apply_state - apply state change to a single cgroup_freezer
 * @freezer: freezer to apply state change to
 * @freeze: whether to freeze or unfreeze
 */
static void freezer_apply_state(struct freezer *freezer, bool freeze)
250
{
251
	/* also synchronizes against task migration, see freezer_attach() */
252
	lockdep_assert_held(&freezer->lock);
253

254
	if (freeze) {
255
		if (!(freezer->state & CGROUP_FREEZING))
256
			atomic_inc(&system_freezing_cnt);
257
		freezer->state |= CGROUP_FREEZING;
T
Tejun Heo 已提交
258
		freeze_cgroup(freezer);
259
	} else {
260
		if (freezer->state & CGROUP_FREEZING)
261
			atomic_dec(&system_freezing_cnt);
262
		freezer->state &= ~(CGROUP_FREEZING | CGROUP_FROZEN);
263
		unfreeze_cgroup(freezer);
264
	}
265
}
266

267 268 269 270 271 272 273 274 275 276 277 278
/**
 * freezer_change_state - change the freezing state of a cgroup_freezer
 * @freezer: freezer of interest
 * @freeze: whether to freeze or thaw
 *
 * Freeze or thaw @cgroup according to @freeze.
 */
static void freezer_change_state(struct freezer *freezer, bool freeze)
{
	/* update @freezer */
	spin_lock_irq(&freezer->lock);
	freezer_apply_state(freezer, freeze);
279 280 281
	spin_unlock_irq(&freezer->lock);
}

T
Tejun Heo 已提交
282
static int freezer_write(struct cgroup *cgroup, struct cftype *cft,
283 284
			 const char *buffer)
{
285
	bool freeze;
286

287
	if (strcmp(buffer, freezer_state_strs(0)) == 0)
288
		freeze = false;
289
	else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
290
		freeze = true;
291
	else
292
		return -EINVAL;
293

294
	freezer_change_state(cgroup_freezer(cgroup), freeze);
295
	return 0;
296 297 298 299 300
}

static struct cftype files[] = {
	{
		.name = "state",
301
		.flags = CFTYPE_NOT_ON_ROOT,
302 303 304
		.read_seq_string = freezer_read,
		.write_string = freezer_write,
	},
305
	{ }	/* terminate */
306 307 308 309 310 311 312
};

struct cgroup_subsys freezer_subsys = {
	.name		= "freezer",
	.create		= freezer_create,
	.destroy	= freezer_destroy,
	.subsys_id	= freezer_subsys_id,
313
	.attach		= freezer_attach,
314
	.fork		= freezer_fork,
315
	.base_cftypes	= files,
316 317 318 319 320 321 322 323

	/*
	 * freezer subsys doesn't handle hierarchy at all.  Frozen state
	 * should be inherited through the hierarchy - if a parent is
	 * frozen, all its children should be frozen.  Fix it and remove
	 * the following.
	 */
	.broken_hierarchy = true,
324
};