usb: added usb-host functionality for MSM devices amd Leo config file
[htc-linux.git] / kernel / cgroup_freezer.c
bloba9b6bc3fa44a9430b9cb404f1d1b3ef70aafce34
1 /*
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/module.h>
18 #include <linux/cgroup.h>
19 #include <linux/fs.h>
20 #include <linux/uaccess.h>
21 #include <linux/freezer.h>
22 #include <linux/seq_file.h>
24 enum freezer_state {
25 CGROUP_THAWED = 0,
26 CGROUP_FREEZING,
27 CGROUP_FROZEN,
30 struct freezer {
31 struct cgroup_subsys_state css;
32 enum freezer_state state;
33 spinlock_t lock; /* protects _writes_ to state */
36 static inline struct freezer *cgroup_freezer(
37 struct cgroup *cgroup)
39 return container_of(
40 cgroup_subsys_state(cgroup, freezer_subsys_id),
41 struct freezer, css);
44 static inline struct freezer *task_freezer(struct task_struct *task)
46 return container_of(task_subsys_state(task, freezer_subsys_id),
47 struct freezer, css);
50 int cgroup_freezing_or_frozen(struct task_struct *task)
52 struct freezer *freezer;
53 enum freezer_state state;
55 task_lock(task);
56 freezer = task_freezer(task);
57 if (!freezer->css.cgroup->parent)
58 state = CGROUP_THAWED; /* root cgroup can't be frozen */
59 else
60 state = freezer->state;
61 task_unlock(task);
63 return (state == CGROUP_FREEZING) || (state == CGROUP_FROZEN);
67 * cgroups_write_string() limits the size of freezer state strings to
68 * CGROUP_LOCAL_BUFFER_SIZE
70 static const char *freezer_state_strs[] = {
71 "THAWED",
72 "FREEZING",
73 "FROZEN",
77 * State diagram
78 * Transitions are caused by userspace writes to the freezer.state file.
79 * The values in parenthesis are state labels. The rest are edge labels.
81 * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
82 * ^ ^ | |
83 * | \_______THAWED_______/ |
84 * \__________________________THAWED____________/
87 struct cgroup_subsys freezer_subsys;
89 /* Locks taken and their ordering
90 * ------------------------------
91 * css_set_lock
92 * cgroup_mutex (AKA cgroup_lock)
93 * task->alloc_lock (AKA task_lock)
94 * freezer->lock
95 * task->sighand->siglock
97 * cgroup code forces css_set_lock to be taken before task->alloc_lock
99 * freezer_create(), freezer_destroy():
100 * cgroup_mutex [ by cgroup core ]
102 * can_attach():
103 * cgroup_mutex
105 * cgroup_frozen():
106 * task->alloc_lock (to get task's cgroup)
108 * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
109 * task->alloc_lock (to get task's cgroup)
110 * freezer->lock
111 * sighand->siglock (if the cgroup is freezing)
113 * freezer_read():
114 * cgroup_mutex
115 * freezer->lock
116 * read_lock css_set_lock (cgroup iterator start)
118 * freezer_write() (freeze):
119 * cgroup_mutex
120 * freezer->lock
121 * read_lock css_set_lock (cgroup iterator start)
122 * sighand->siglock
124 * freezer_write() (unfreeze):
125 * cgroup_mutex
126 * freezer->lock
127 * read_lock css_set_lock (cgroup iterator start)
128 * task->alloc_lock (to prevent races with freeze_task())
129 * sighand->siglock
131 static struct cgroup_subsys_state *freezer_create(struct cgroup_subsys *ss,
132 struct cgroup *cgroup)
134 struct freezer *freezer;
136 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
137 if (!freezer)
138 return ERR_PTR(-ENOMEM);
140 spin_lock_init(&freezer->lock);
141 freezer->state = CGROUP_THAWED;
142 return &freezer->css;
145 static void freezer_destroy(struct cgroup_subsys *ss,
146 struct cgroup *cgroup)
148 kfree(cgroup_freezer(cgroup));
151 /* Task is frozen or will freeze immediately when next it gets woken */
152 static bool is_task_frozen_enough(struct task_struct *task)
154 return frozen(task) ||
155 (task_is_stopped_or_traced(task) && freezing(task));
159 * The call to cgroup_lock() in the freezer.state write method prevents
160 * a write to that file racing against an attach, and hence the
161 * can_attach() result will remain valid until the attach completes.
163 static int freezer_can_attach(struct cgroup_subsys *ss,
164 struct cgroup *new_cgroup,
165 struct task_struct *task, bool threadgroup)
167 struct freezer *freezer;
169 if ((current != task) && (!capable(CAP_SYS_ADMIN))) {
170 const struct cred *cred = current_cred(), *tcred;
172 tcred = __task_cred(task);
173 if (cred->euid != tcred->uid && cred->euid != tcred->suid)
174 return -EPERM;
178 * Anything frozen can't move or be moved to/from.
180 * Since orig_freezer->state == FROZEN means that @task has been
181 * frozen, so it's sufficient to check the latter condition.
184 if (is_task_frozen_enough(task))
185 return -EBUSY;
187 freezer = cgroup_freezer(new_cgroup);
188 if (freezer->state == CGROUP_FROZEN)
189 return -EBUSY;
191 if (threadgroup) {
192 struct task_struct *c;
194 rcu_read_lock();
195 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
196 if (is_task_frozen_enough(c)) {
197 rcu_read_unlock();
198 return -EBUSY;
201 rcu_read_unlock();
204 return 0;
207 static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
209 struct freezer *freezer;
212 * No lock is needed, since the task isn't on tasklist yet,
213 * so it can't be moved to another cgroup, which means the
214 * freezer won't be removed and will be valid during this
215 * function call.
217 freezer = task_freezer(task);
220 * The root cgroup is non-freezable, so we can skip the
221 * following check.
223 if (!freezer->css.cgroup->parent)
224 return;
226 spin_lock_irq(&freezer->lock);
227 BUG_ON(freezer->state == CGROUP_FROZEN);
229 /* Locking avoids race with FREEZING -> THAWED transitions. */
230 if (freezer->state == CGROUP_FREEZING)
231 freeze_task(task, true);
232 spin_unlock_irq(&freezer->lock);
236 * caller must hold freezer->lock
238 static void update_freezer_state(struct cgroup *cgroup,
239 struct freezer *freezer)
241 struct cgroup_iter it;
242 struct task_struct *task;
243 unsigned int nfrozen = 0, ntotal = 0;
245 cgroup_iter_start(cgroup, &it);
246 while ((task = cgroup_iter_next(cgroup, &it))) {
247 ntotal++;
248 if (is_task_frozen_enough(task))
249 nfrozen++;
253 * Transition to FROZEN when no new tasks can be added ensures
254 * that we never exist in the FROZEN state while there are unfrozen
255 * tasks.
257 if (nfrozen == ntotal)
258 freezer->state = CGROUP_FROZEN;
259 else if (nfrozen > 0)
260 freezer->state = CGROUP_FREEZING;
261 else
262 freezer->state = CGROUP_THAWED;
263 cgroup_iter_end(cgroup, &it);
266 static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
267 struct seq_file *m)
269 struct freezer *freezer;
270 enum freezer_state state;
272 if (!cgroup_lock_live_group(cgroup))
273 return -ENODEV;
275 freezer = cgroup_freezer(cgroup);
276 spin_lock_irq(&freezer->lock);
277 state = freezer->state;
278 if (state == CGROUP_FREEZING) {
279 /* We change from FREEZING to FROZEN lazily if the cgroup was
280 * only partially frozen when we exitted write. */
281 update_freezer_state(cgroup, freezer);
282 state = freezer->state;
284 spin_unlock_irq(&freezer->lock);
285 cgroup_unlock();
287 seq_puts(m, freezer_state_strs[state]);
288 seq_putc(m, '\n');
289 return 0;
292 static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
294 struct cgroup_iter it;
295 struct task_struct *task;
296 unsigned int num_cant_freeze_now = 0;
298 freezer->state = CGROUP_FREEZING;
299 cgroup_iter_start(cgroup, &it);
300 while ((task = cgroup_iter_next(cgroup, &it))) {
301 if (!freeze_task(task, true))
302 continue;
303 if (is_task_frozen_enough(task))
304 continue;
305 if (!freezing(task) && !freezer_should_skip(task))
306 num_cant_freeze_now++;
308 cgroup_iter_end(cgroup, &it);
310 return num_cant_freeze_now ? -EBUSY : 0;
313 static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
315 struct cgroup_iter it;
316 struct task_struct *task;
318 cgroup_iter_start(cgroup, &it);
319 while ((task = cgroup_iter_next(cgroup, &it))) {
320 thaw_process(task);
322 cgroup_iter_end(cgroup, &it);
324 freezer->state = CGROUP_THAWED;
327 static int freezer_change_state(struct cgroup *cgroup,
328 enum freezer_state goal_state)
330 struct freezer *freezer;
331 int retval = 0;
333 freezer = cgroup_freezer(cgroup);
335 spin_lock_irq(&freezer->lock);
337 update_freezer_state(cgroup, freezer);
338 if (goal_state == freezer->state)
339 goto out;
341 switch (goal_state) {
342 case CGROUP_THAWED:
343 unfreeze_cgroup(cgroup, freezer);
344 break;
345 case CGROUP_FROZEN:
346 retval = try_to_freeze_cgroup(cgroup, freezer);
347 break;
348 default:
349 BUG();
351 out:
352 spin_unlock_irq(&freezer->lock);
354 return retval;
357 static int freezer_write(struct cgroup *cgroup,
358 struct cftype *cft,
359 const char *buffer)
361 int retval;
362 enum freezer_state goal_state;
364 if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
365 goal_state = CGROUP_THAWED;
366 else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
367 goal_state = CGROUP_FROZEN;
368 else
369 return -EINVAL;
371 if (!cgroup_lock_live_group(cgroup))
372 return -ENODEV;
373 retval = freezer_change_state(cgroup, goal_state);
374 cgroup_unlock();
375 return retval;
378 static struct cftype files[] = {
380 .name = "state",
381 .read_seq_string = freezer_read,
382 .write_string = freezer_write,
386 static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
388 if (!cgroup->parent)
389 return 0;
390 return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
393 struct cgroup_subsys freezer_subsys = {
394 .name = "freezer",
395 .create = freezer_create,
396 .destroy = freezer_destroy,
397 .populate = freezer_populate,
398 .subsys_id = freezer_subsys_id,
399 .can_attach = freezer_can_attach,
400 .attach = NULL,
401 .fork = freezer_fork,
402 .exit = NULL,