cgroups: documentation updates
[linux-2.6-block.git] / kernel / cgroup.c
CommitLineData
ddbcc7e8 1/*
ddbcc7e8
PM
2 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
a424316c 34#include <linux/proc_fs.h>
ddbcc7e8
PM
35#include <linux/rcupdate.h>
36#include <linux/sched.h>
817929ec 37#include <linux/backing-dev.h>
ddbcc7e8
PM
38#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
bbcb81d0 43#include <linux/sort.h>
81a6a5cd 44#include <linux/kmod.h>
846c7bb0
BS
45#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
472b1053 47#include <linux/hash.h>
3f8206d4 48#include <linux/namei.h>
846c7bb0 49
ddbcc7e8
PM
50#include <asm/atomic.h>
51
81a6a5cd
PM
52static DEFINE_MUTEX(cgroup_mutex);
53
ddbcc7e8
PM
54/* Generate an array of cgroup subsystem pointers */
55#define SUBSYS(_x) &_x ## _subsys,
56
57static struct cgroup_subsys *subsys[] = {
58#include <linux/cgroup_subsys.h>
59};
60
61/*
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
64 * hierarchy
65 */
66struct cgroupfs_root {
67 struct super_block *sb;
68
69 /*
70 * The bitmask of subsystems intended to be attached to this
71 * hierarchy
72 */
73 unsigned long subsys_bits;
74
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits;
77
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list;
80
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup;
83
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups;
86
87 /* A list running through the mounted hierarchies */
88 struct list_head root_list;
89
90 /* Hierarchy-specific flags */
91 unsigned long flags;
81a6a5cd 92
e788e066 93 /* The path to use for release notifications. */
81a6a5cd 94 char release_agent_path[PATH_MAX];
ddbcc7e8
PM
95};
96
97
98/*
99 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
100 * subsystems that are otherwise unattached - it never has more than a
101 * single cgroup, and all tasks are part of that cgroup.
102 */
103static struct cgroupfs_root rootnode;
104
105/* The list of hierarchy roots */
106
107static LIST_HEAD(roots);
817929ec 108static int root_count;
ddbcc7e8
PM
109
110/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
111#define dummytop (&rootnode.top_cgroup)
112
113/* This flag indicates whether tasks in the fork and exit paths should
a043e3b2
LZ
114 * check for fork/exit handlers to call. This avoids us having to do
115 * extra work in the fork/exit path if none of the subsystems need to
116 * be called.
ddbcc7e8 117 */
8947f9d5 118static int need_forkexit_callback __read_mostly;
ddbcc7e8 119
ddbcc7e8 120/* convenient tests for these bits */
bd89aabc 121inline int cgroup_is_removed(const struct cgroup *cgrp)
ddbcc7e8 122{
bd89aabc 123 return test_bit(CGRP_REMOVED, &cgrp->flags);
ddbcc7e8
PM
124}
125
126/* bits in struct cgroupfs_root flags field */
127enum {
128 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
129};
130
e9685a03 131static int cgroup_is_releasable(const struct cgroup *cgrp)
81a6a5cd
PM
132{
133 const int bits =
bd89aabc
PM
134 (1 << CGRP_RELEASABLE) |
135 (1 << CGRP_NOTIFY_ON_RELEASE);
136 return (cgrp->flags & bits) == bits;
81a6a5cd
PM
137}
138
e9685a03 139static int notify_on_release(const struct cgroup *cgrp)
81a6a5cd 140{
bd89aabc 141 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
81a6a5cd
PM
142}
143
ddbcc7e8
PM
144/*
145 * for_each_subsys() allows you to iterate on each subsystem attached to
146 * an active hierarchy
147 */
148#define for_each_subsys(_root, _ss) \
149list_for_each_entry(_ss, &_root->subsys_list, sibling)
150
151/* for_each_root() allows you to iterate across the active hierarchies */
152#define for_each_root(_root) \
153list_for_each_entry(_root, &roots, root_list)
154
81a6a5cd
PM
155/* the list of cgroups eligible for automatic release. Protected by
156 * release_list_lock */
157static LIST_HEAD(release_list);
158static DEFINE_SPINLOCK(release_list_lock);
159static void cgroup_release_agent(struct work_struct *work);
160static DECLARE_WORK(release_agent_work, cgroup_release_agent);
bd89aabc 161static void check_for_release(struct cgroup *cgrp);
81a6a5cd 162
817929ec
PM
163/* Link structure for associating css_set objects with cgroups */
164struct cg_cgroup_link {
165 /*
166 * List running through cg_cgroup_links associated with a
167 * cgroup, anchored on cgroup->css_sets
168 */
bd89aabc 169 struct list_head cgrp_link_list;
817929ec
PM
170 /*
171 * List running through cg_cgroup_links pointing at a
172 * single css_set object, anchored on css_set->cg_links
173 */
174 struct list_head cg_link_list;
175 struct css_set *cg;
176};
177
178/* The default css_set - used by init and its children prior to any
179 * hierarchies being mounted. It contains a pointer to the root state
180 * for each subsystem. Also used to anchor the list of css_sets. Not
181 * reference-counted, to improve performance when child cgroups
182 * haven't been created.
183 */
184
185static struct css_set init_css_set;
186static struct cg_cgroup_link init_css_set_link;
187
188/* css_set_lock protects the list of css_set objects, and the
189 * chain of tasks off each css_set. Nests outside task->alloc_lock
190 * due to cgroup_iter_start() */
191static DEFINE_RWLOCK(css_set_lock);
192static int css_set_count;
193
472b1053
LZ
194/* hash table for cgroup groups. This improves the performance to
195 * find an existing css_set */
196#define CSS_SET_HASH_BITS 7
197#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
198static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
199
200static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
201{
202 int i;
203 int index;
204 unsigned long tmp = 0UL;
205
206 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
207 tmp += (unsigned long)css[i];
208 tmp = (tmp >> 16) ^ tmp;
209
210 index = hash_long(tmp, CSS_SET_HASH_BITS);
211
212 return &css_set_table[index];
213}
214
817929ec
PM
215/* We don't maintain the lists running through each css_set to its
216 * task until after the first call to cgroup_iter_start(). This
217 * reduces the fork()/exit() overhead for people who have cgroups
218 * compiled into their kernel but not actually in use */
8947f9d5 219static int use_task_css_set_links __read_mostly;
817929ec
PM
220
221/* When we create or destroy a css_set, the operation simply
222 * takes/releases a reference count on all the cgroups referenced
223 * by subsystems in this css_set. This can end up multiple-counting
224 * some cgroups, but that's OK - the ref-count is just a
225 * busy/not-busy indicator; ensuring that we only count each cgroup
226 * once would require taking a global lock to ensure that no
b4f48b63
PM
227 * subsystems moved between hierarchies while we were doing so.
228 *
229 * Possible TODO: decide at boot time based on the number of
230 * registered subsystems and the number of CPUs or NUMA nodes whether
231 * it's better for performance to ref-count every subsystem, or to
232 * take a global lock and only add one ref count to each hierarchy.
233 */
817929ec
PM
234
235/*
236 * unlink a css_set from the list and free it
237 */
81a6a5cd 238static void unlink_css_set(struct css_set *cg)
b4f48b63 239{
71cbb949
KM
240 struct cg_cgroup_link *link;
241 struct cg_cgroup_link *saved_link;
242
472b1053 243 hlist_del(&cg->hlist);
817929ec 244 css_set_count--;
71cbb949
KM
245
246 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
247 cg_link_list) {
817929ec 248 list_del(&link->cg_link_list);
bd89aabc 249 list_del(&link->cgrp_link_list);
817929ec
PM
250 kfree(link);
251 }
81a6a5cd
PM
252}
253
146aa1bd 254static void __put_css_set(struct css_set *cg, int taskexit)
81a6a5cd
PM
255{
256 int i;
146aa1bd
LJ
257 /*
258 * Ensure that the refcount doesn't hit zero while any readers
259 * can see it. Similar to atomic_dec_and_lock(), but for an
260 * rwlock
261 */
262 if (atomic_add_unless(&cg->refcount, -1, 1))
263 return;
264 write_lock(&css_set_lock);
265 if (!atomic_dec_and_test(&cg->refcount)) {
266 write_unlock(&css_set_lock);
267 return;
268 }
81a6a5cd 269 unlink_css_set(cg);
146aa1bd 270 write_unlock(&css_set_lock);
81a6a5cd
PM
271
272 rcu_read_lock();
273 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
bd89aabc
PM
274 struct cgroup *cgrp = cg->subsys[i]->cgroup;
275 if (atomic_dec_and_test(&cgrp->count) &&
276 notify_on_release(cgrp)) {
81a6a5cd 277 if (taskexit)
bd89aabc
PM
278 set_bit(CGRP_RELEASABLE, &cgrp->flags);
279 check_for_release(cgrp);
81a6a5cd
PM
280 }
281 }
282 rcu_read_unlock();
817929ec 283 kfree(cg);
b4f48b63
PM
284}
285
817929ec
PM
286/*
287 * refcounted get/put for css_set objects
288 */
289static inline void get_css_set(struct css_set *cg)
290{
146aa1bd 291 atomic_inc(&cg->refcount);
817929ec
PM
292}
293
294static inline void put_css_set(struct css_set *cg)
295{
146aa1bd 296 __put_css_set(cg, 0);
817929ec
PM
297}
298
81a6a5cd
PM
299static inline void put_css_set_taskexit(struct css_set *cg)
300{
146aa1bd 301 __put_css_set(cg, 1);
81a6a5cd
PM
302}
303
817929ec
PM
304/*
305 * find_existing_css_set() is a helper for
306 * find_css_set(), and checks to see whether an existing
472b1053 307 * css_set is suitable.
817929ec
PM
308 *
309 * oldcg: the cgroup group that we're using before the cgroup
310 * transition
311 *
bd89aabc 312 * cgrp: the cgroup that we're moving into
817929ec
PM
313 *
314 * template: location in which to build the desired set of subsystem
315 * state objects for the new cgroup group
316 */
817929ec
PM
317static struct css_set *find_existing_css_set(
318 struct css_set *oldcg,
bd89aabc 319 struct cgroup *cgrp,
817929ec 320 struct cgroup_subsys_state *template[])
b4f48b63
PM
321{
322 int i;
bd89aabc 323 struct cgroupfs_root *root = cgrp->root;
472b1053
LZ
324 struct hlist_head *hhead;
325 struct hlist_node *node;
326 struct css_set *cg;
817929ec
PM
327
328 /* Built the set of subsystem state objects that we want to
329 * see in the new css_set */
330 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 331 if (root->subsys_bits & (1UL << i)) {
817929ec
PM
332 /* Subsystem is in this hierarchy. So we want
333 * the subsystem state from the new
334 * cgroup */
bd89aabc 335 template[i] = cgrp->subsys[i];
817929ec
PM
336 } else {
337 /* Subsystem is not in this hierarchy, so we
338 * don't want to change the subsystem state */
339 template[i] = oldcg->subsys[i];
340 }
341 }
342
472b1053
LZ
343 hhead = css_set_hash(template);
344 hlist_for_each_entry(cg, node, hhead, hlist) {
817929ec
PM
345 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
346 /* All subsystems matched */
347 return cg;
348 }
472b1053 349 }
817929ec
PM
350
351 /* No existing cgroup group matched */
352 return NULL;
353}
354
36553434
LZ
355static void free_cg_links(struct list_head *tmp)
356{
357 struct cg_cgroup_link *link;
358 struct cg_cgroup_link *saved_link;
359
360 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
361 list_del(&link->cgrp_link_list);
362 kfree(link);
363 }
364}
365
817929ec
PM
366/*
367 * allocate_cg_links() allocates "count" cg_cgroup_link structures
bd89aabc 368 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
817929ec
PM
369 * success or a negative error
370 */
817929ec
PM
371static int allocate_cg_links(int count, struct list_head *tmp)
372{
373 struct cg_cgroup_link *link;
374 int i;
375 INIT_LIST_HEAD(tmp);
376 for (i = 0; i < count; i++) {
377 link = kmalloc(sizeof(*link), GFP_KERNEL);
378 if (!link) {
36553434 379 free_cg_links(tmp);
817929ec
PM
380 return -ENOMEM;
381 }
bd89aabc 382 list_add(&link->cgrp_link_list, tmp);
817929ec
PM
383 }
384 return 0;
385}
386
817929ec
PM
387/*
388 * find_css_set() takes an existing cgroup group and a
389 * cgroup object, and returns a css_set object that's
390 * equivalent to the old group, but with the given cgroup
391 * substituted into the appropriate hierarchy. Must be called with
392 * cgroup_mutex held
393 */
817929ec 394static struct css_set *find_css_set(
bd89aabc 395 struct css_set *oldcg, struct cgroup *cgrp)
817929ec
PM
396{
397 struct css_set *res;
398 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
399 int i;
400
401 struct list_head tmp_cg_links;
402 struct cg_cgroup_link *link;
403
472b1053
LZ
404 struct hlist_head *hhead;
405
817929ec
PM
406 /* First see if we already have a cgroup group that matches
407 * the desired set */
7e9abd89 408 read_lock(&css_set_lock);
bd89aabc 409 res = find_existing_css_set(oldcg, cgrp, template);
817929ec
PM
410 if (res)
411 get_css_set(res);
7e9abd89 412 read_unlock(&css_set_lock);
817929ec
PM
413
414 if (res)
415 return res;
416
417 res = kmalloc(sizeof(*res), GFP_KERNEL);
418 if (!res)
419 return NULL;
420
421 /* Allocate all the cg_cgroup_link objects that we'll need */
422 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
423 kfree(res);
424 return NULL;
425 }
426
146aa1bd 427 atomic_set(&res->refcount, 1);
817929ec
PM
428 INIT_LIST_HEAD(&res->cg_links);
429 INIT_LIST_HEAD(&res->tasks);
472b1053 430 INIT_HLIST_NODE(&res->hlist);
817929ec
PM
431
432 /* Copy the set of subsystem state objects generated in
433 * find_existing_css_set() */
434 memcpy(res->subsys, template, sizeof(res->subsys));
435
436 write_lock(&css_set_lock);
437 /* Add reference counts and links from the new css_set. */
438 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
bd89aabc 439 struct cgroup *cgrp = res->subsys[i]->cgroup;
817929ec 440 struct cgroup_subsys *ss = subsys[i];
bd89aabc 441 atomic_inc(&cgrp->count);
817929ec
PM
442 /*
443 * We want to add a link once per cgroup, so we
444 * only do it for the first subsystem in each
445 * hierarchy
446 */
447 if (ss->root->subsys_list.next == &ss->sibling) {
448 BUG_ON(list_empty(&tmp_cg_links));
449 link = list_entry(tmp_cg_links.next,
450 struct cg_cgroup_link,
bd89aabc
PM
451 cgrp_link_list);
452 list_del(&link->cgrp_link_list);
453 list_add(&link->cgrp_link_list, &cgrp->css_sets);
817929ec
PM
454 link->cg = res;
455 list_add(&link->cg_link_list, &res->cg_links);
456 }
457 }
458 if (list_empty(&rootnode.subsys_list)) {
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
bd89aabc
PM
461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &dummytop->css_sets);
817929ec
PM
464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
466 }
467
468 BUG_ON(!list_empty(&tmp_cg_links));
469
817929ec 470 css_set_count++;
472b1053
LZ
471
472 /* Add this cgroup group to the hash table */
473 hhead = css_set_hash(res->subsys);
474 hlist_add_head(&res->hlist, hhead);
475
817929ec
PM
476 write_unlock(&css_set_lock);
477
478 return res;
b4f48b63
PM
479}
480
ddbcc7e8
PM
481/*
482 * There is one global cgroup mutex. We also require taking
483 * task_lock() when dereferencing a task's cgroup subsys pointers.
484 * See "The task_lock() exception", at the end of this comment.
485 *
486 * A task must hold cgroup_mutex to modify cgroups.
487 *
488 * Any task can increment and decrement the count field without lock.
489 * So in general, code holding cgroup_mutex can't rely on the count
490 * field not changing. However, if the count goes to zero, then only
956db3ca 491 * cgroup_attach_task() can increment it again. Because a count of zero
ddbcc7e8
PM
492 * means that no tasks are currently attached, therefore there is no
493 * way a task attached to that cgroup can fork (the other way to
494 * increment the count). So code holding cgroup_mutex can safely
495 * assume that if the count is zero, it will stay zero. Similarly, if
496 * a task holds cgroup_mutex on a cgroup with zero count, it
497 * knows that the cgroup won't be removed, as cgroup_rmdir()
498 * needs that mutex.
499 *
ddbcc7e8
PM
500 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
501 * (usually) take cgroup_mutex. These are the two most performance
502 * critical pieces of code here. The exception occurs on cgroup_exit(),
503 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
504 * is taken, and if the cgroup count is zero, a usermode call made
a043e3b2
LZ
505 * to the release agent with the name of the cgroup (path relative to
506 * the root of cgroup file system) as the argument.
ddbcc7e8
PM
507 *
508 * A cgroup can only be deleted if both its 'count' of using tasks
509 * is zero, and its list of 'children' cgroups is empty. Since all
510 * tasks in the system use _some_ cgroup, and since there is always at
511 * least one task in the system (init, pid == 1), therefore, top_cgroup
512 * always has either children cgroups and/or using tasks. So we don't
513 * need a special hack to ensure that top_cgroup cannot be deleted.
514 *
515 * The task_lock() exception
516 *
517 * The need for this exception arises from the action of
956db3ca 518 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
a043e3b2 519 * another. It does so using cgroup_mutex, however there are
ddbcc7e8
PM
520 * several performance critical places that need to reference
521 * task->cgroup without the expense of grabbing a system global
522 * mutex. Therefore except as noted below, when dereferencing or, as
956db3ca 523 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
ddbcc7e8
PM
524 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
525 * the task_struct routinely used for such matters.
526 *
527 * P.S. One more locking exception. RCU is used to guard the
956db3ca 528 * update of a tasks cgroup pointer by cgroup_attach_task()
ddbcc7e8
PM
529 */
530
ddbcc7e8
PM
531/**
532 * cgroup_lock - lock out any changes to cgroup structures
533 *
534 */
ddbcc7e8
PM
535void cgroup_lock(void)
536{
537 mutex_lock(&cgroup_mutex);
538}
539
540/**
541 * cgroup_unlock - release lock on cgroup changes
542 *
543 * Undo the lock taken in a previous cgroup_lock() call.
544 */
ddbcc7e8
PM
545void cgroup_unlock(void)
546{
547 mutex_unlock(&cgroup_mutex);
548}
549
550/*
551 * A couple of forward declarations required, due to cyclic reference loop:
552 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
553 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
554 * -> cgroup_mkdir.
555 */
556
557static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
558static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
bd89aabc 559static int cgroup_populate_dir(struct cgroup *cgrp);
ddbcc7e8 560static struct inode_operations cgroup_dir_inode_operations;
a424316c
PM
561static struct file_operations proc_cgroupstats_operations;
562
563static struct backing_dev_info cgroup_backing_dev_info = {
e4ad08fe 564 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
a424316c 565};
ddbcc7e8
PM
566
567static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
568{
569 struct inode *inode = new_inode(sb);
ddbcc7e8
PM
570
571 if (inode) {
572 inode->i_mode = mode;
76aac0e9
DH
573 inode->i_uid = current_fsuid();
574 inode->i_gid = current_fsgid();
ddbcc7e8
PM
575 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
576 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
577 }
578 return inode;
579}
580
4fca88c8
KH
581/*
582 * Call subsys's pre_destroy handler.
583 * This is called before css refcnt check.
584 */
4fca88c8
KH
585static void cgroup_call_pre_destroy(struct cgroup *cgrp)
586{
587 struct cgroup_subsys *ss;
588 for_each_subsys(cgrp->root, ss)
589 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
590 ss->pre_destroy(ss, cgrp);
591 return;
592}
593
ddbcc7e8
PM
594static void cgroup_diput(struct dentry *dentry, struct inode *inode)
595{
596 /* is dentry a directory ? if so, kfree() associated cgroup */
597 if (S_ISDIR(inode->i_mode)) {
bd89aabc 598 struct cgroup *cgrp = dentry->d_fsdata;
8dc4f3e1 599 struct cgroup_subsys *ss;
bd89aabc 600 BUG_ON(!(cgroup_is_removed(cgrp)));
81a6a5cd
PM
601 /* It's possible for external users to be holding css
602 * reference counts on a cgroup; css_put() needs to
603 * be able to access the cgroup after decrementing
604 * the reference count in order to know if it needs to
605 * queue the cgroup to be handled by the release
606 * agent */
607 synchronize_rcu();
8dc4f3e1
PM
608
609 mutex_lock(&cgroup_mutex);
610 /*
611 * Release the subsystem state objects.
612 */
613 for_each_subsys(cgrp->root, ss) {
614 if (cgrp->subsys[ss->subsys_id])
615 ss->destroy(ss, cgrp);
616 }
617
618 cgrp->root->number_of_cgroups--;
619 mutex_unlock(&cgroup_mutex);
620
621 /* Drop the active superblock reference that we took when we
622 * created the cgroup */
623 deactivate_super(cgrp->root->sb);
624
bd89aabc 625 kfree(cgrp);
ddbcc7e8
PM
626 }
627 iput(inode);
628}
629
630static void remove_dir(struct dentry *d)
631{
632 struct dentry *parent = dget(d->d_parent);
633
634 d_delete(d);
635 simple_rmdir(parent->d_inode, d);
636 dput(parent);
637}
638
639static void cgroup_clear_directory(struct dentry *dentry)
640{
641 struct list_head *node;
642
643 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
644 spin_lock(&dcache_lock);
645 node = dentry->d_subdirs.next;
646 while (node != &dentry->d_subdirs) {
647 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
648 list_del_init(node);
649 if (d->d_inode) {
650 /* This should never be called on a cgroup
651 * directory with child cgroups */
652 BUG_ON(d->d_inode->i_mode & S_IFDIR);
653 d = dget_locked(d);
654 spin_unlock(&dcache_lock);
655 d_delete(d);
656 simple_unlink(dentry->d_inode, d);
657 dput(d);
658 spin_lock(&dcache_lock);
659 }
660 node = dentry->d_subdirs.next;
661 }
662 spin_unlock(&dcache_lock);
663}
664
665/*
666 * NOTE : the dentry must have been dget()'ed
667 */
668static void cgroup_d_remove_dir(struct dentry *dentry)
669{
670 cgroup_clear_directory(dentry);
671
672 spin_lock(&dcache_lock);
673 list_del_init(&dentry->d_u.d_child);
674 spin_unlock(&dcache_lock);
675 remove_dir(dentry);
676}
677
678static int rebind_subsystems(struct cgroupfs_root *root,
679 unsigned long final_bits)
680{
681 unsigned long added_bits, removed_bits;
bd89aabc 682 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
683 int i;
684
685 removed_bits = root->actual_subsys_bits & ~final_bits;
686 added_bits = final_bits & ~root->actual_subsys_bits;
687 /* Check that any added subsystems are currently free */
688 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 689 unsigned long bit = 1UL << i;
ddbcc7e8
PM
690 struct cgroup_subsys *ss = subsys[i];
691 if (!(bit & added_bits))
692 continue;
693 if (ss->root != &rootnode) {
694 /* Subsystem isn't free */
695 return -EBUSY;
696 }
697 }
698
699 /* Currently we don't handle adding/removing subsystems when
700 * any child cgroups exist. This is theoretically supportable
701 * but involves complex error handling, so it's being left until
702 * later */
307257cf 703 if (root->number_of_cgroups > 1)
ddbcc7e8
PM
704 return -EBUSY;
705
706 /* Process each subsystem */
707 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
708 struct cgroup_subsys *ss = subsys[i];
709 unsigned long bit = 1UL << i;
710 if (bit & added_bits) {
711 /* We're binding this subsystem to this hierarchy */
bd89aabc 712 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
713 BUG_ON(!dummytop->subsys[i]);
714 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
bd89aabc
PM
715 cgrp->subsys[i] = dummytop->subsys[i];
716 cgrp->subsys[i]->cgroup = cgrp;
ddbcc7e8
PM
717 list_add(&ss->sibling, &root->subsys_list);
718 rcu_assign_pointer(ss->root, root);
719 if (ss->bind)
bd89aabc 720 ss->bind(ss, cgrp);
ddbcc7e8
PM
721
722 } else if (bit & removed_bits) {
723 /* We're removing this subsystem */
bd89aabc
PM
724 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
725 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
ddbcc7e8
PM
726 if (ss->bind)
727 ss->bind(ss, dummytop);
728 dummytop->subsys[i]->cgroup = dummytop;
bd89aabc 729 cgrp->subsys[i] = NULL;
ddbcc7e8
PM
730 rcu_assign_pointer(subsys[i]->root, &rootnode);
731 list_del(&ss->sibling);
732 } else if (bit & final_bits) {
733 /* Subsystem state should already exist */
bd89aabc 734 BUG_ON(!cgrp->subsys[i]);
ddbcc7e8
PM
735 } else {
736 /* Subsystem state shouldn't exist */
bd89aabc 737 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
738 }
739 }
740 root->subsys_bits = root->actual_subsys_bits = final_bits;
741 synchronize_rcu();
742
743 return 0;
744}
745
746static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
747{
748 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
749 struct cgroup_subsys *ss;
750
751 mutex_lock(&cgroup_mutex);
752 for_each_subsys(root, ss)
753 seq_printf(seq, ",%s", ss->name);
754 if (test_bit(ROOT_NOPREFIX, &root->flags))
755 seq_puts(seq, ",noprefix");
81a6a5cd
PM
756 if (strlen(root->release_agent_path))
757 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
ddbcc7e8
PM
758 mutex_unlock(&cgroup_mutex);
759 return 0;
760}
761
762struct cgroup_sb_opts {
763 unsigned long subsys_bits;
764 unsigned long flags;
81a6a5cd 765 char *release_agent;
ddbcc7e8
PM
766};
767
768/* Convert a hierarchy specifier into a bitmask of subsystems and
769 * flags. */
770static int parse_cgroupfs_options(char *data,
771 struct cgroup_sb_opts *opts)
772{
773 char *token, *o = data ?: "all";
774
775 opts->subsys_bits = 0;
776 opts->flags = 0;
81a6a5cd 777 opts->release_agent = NULL;
ddbcc7e8
PM
778
779 while ((token = strsep(&o, ",")) != NULL) {
780 if (!*token)
781 return -EINVAL;
782 if (!strcmp(token, "all")) {
8bab8dde
PM
783 /* Add all non-disabled subsystems */
784 int i;
785 opts->subsys_bits = 0;
786 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
787 struct cgroup_subsys *ss = subsys[i];
788 if (!ss->disabled)
789 opts->subsys_bits |= 1ul << i;
790 }
ddbcc7e8
PM
791 } else if (!strcmp(token, "noprefix")) {
792 set_bit(ROOT_NOPREFIX, &opts->flags);
81a6a5cd
PM
793 } else if (!strncmp(token, "release_agent=", 14)) {
794 /* Specifying two release agents is forbidden */
795 if (opts->release_agent)
796 return -EINVAL;
797 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
798 if (!opts->release_agent)
799 return -ENOMEM;
800 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
801 opts->release_agent[PATH_MAX - 1] = 0;
ddbcc7e8
PM
802 } else {
803 struct cgroup_subsys *ss;
804 int i;
805 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
806 ss = subsys[i];
807 if (!strcmp(token, ss->name)) {
8bab8dde
PM
808 if (!ss->disabled)
809 set_bit(i, &opts->subsys_bits);
ddbcc7e8
PM
810 break;
811 }
812 }
813 if (i == CGROUP_SUBSYS_COUNT)
814 return -ENOENT;
815 }
816 }
817
818 /* We can't have an empty hierarchy */
819 if (!opts->subsys_bits)
820 return -EINVAL;
821
822 return 0;
823}
824
825static int cgroup_remount(struct super_block *sb, int *flags, char *data)
826{
827 int ret = 0;
828 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 829 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
830 struct cgroup_sb_opts opts;
831
bd89aabc 832 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
833 mutex_lock(&cgroup_mutex);
834
835 /* See what subsystems are wanted */
836 ret = parse_cgroupfs_options(data, &opts);
837 if (ret)
838 goto out_unlock;
839
840 /* Don't allow flags to change at remount */
841 if (opts.flags != root->flags) {
842 ret = -EINVAL;
843 goto out_unlock;
844 }
845
846 ret = rebind_subsystems(root, opts.subsys_bits);
847
848 /* (re)populate subsystem files */
849 if (!ret)
bd89aabc 850 cgroup_populate_dir(cgrp);
ddbcc7e8 851
81a6a5cd
PM
852 if (opts.release_agent)
853 strcpy(root->release_agent_path, opts.release_agent);
ddbcc7e8 854 out_unlock:
81a6a5cd
PM
855 if (opts.release_agent)
856 kfree(opts.release_agent);
ddbcc7e8 857 mutex_unlock(&cgroup_mutex);
bd89aabc 858 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
859 return ret;
860}
861
862static struct super_operations cgroup_ops = {
863 .statfs = simple_statfs,
864 .drop_inode = generic_delete_inode,
865 .show_options = cgroup_show_options,
866 .remount_fs = cgroup_remount,
867};
868
cc31edce
PM
869static void init_cgroup_housekeeping(struct cgroup *cgrp)
870{
871 INIT_LIST_HEAD(&cgrp->sibling);
872 INIT_LIST_HEAD(&cgrp->children);
873 INIT_LIST_HEAD(&cgrp->css_sets);
874 INIT_LIST_HEAD(&cgrp->release_list);
875 init_rwsem(&cgrp->pids_mutex);
876}
ddbcc7e8
PM
877static void init_cgroup_root(struct cgroupfs_root *root)
878{
bd89aabc 879 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
880 INIT_LIST_HEAD(&root->subsys_list);
881 INIT_LIST_HEAD(&root->root_list);
882 root->number_of_cgroups = 1;
bd89aabc
PM
883 cgrp->root = root;
884 cgrp->top_cgroup = cgrp;
cc31edce 885 init_cgroup_housekeeping(cgrp);
ddbcc7e8
PM
886}
887
888static int cgroup_test_super(struct super_block *sb, void *data)
889{
890 struct cgroupfs_root *new = data;
891 struct cgroupfs_root *root = sb->s_fs_info;
892
893 /* First check subsystems */
894 if (new->subsys_bits != root->subsys_bits)
895 return 0;
896
897 /* Next check flags */
898 if (new->flags != root->flags)
899 return 0;
900
901 return 1;
902}
903
904static int cgroup_set_super(struct super_block *sb, void *data)
905{
906 int ret;
907 struct cgroupfs_root *root = data;
908
909 ret = set_anon_super(sb, NULL);
910 if (ret)
911 return ret;
912
913 sb->s_fs_info = root;
914 root->sb = sb;
915
916 sb->s_blocksize = PAGE_CACHE_SIZE;
917 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
918 sb->s_magic = CGROUP_SUPER_MAGIC;
919 sb->s_op = &cgroup_ops;
920
921 return 0;
922}
923
924static int cgroup_get_rootdir(struct super_block *sb)
925{
926 struct inode *inode =
927 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
928 struct dentry *dentry;
929
930 if (!inode)
931 return -ENOMEM;
932
ddbcc7e8
PM
933 inode->i_fop = &simple_dir_operations;
934 inode->i_op = &cgroup_dir_inode_operations;
935 /* directories start off with i_nlink == 2 (for "." entry) */
936 inc_nlink(inode);
937 dentry = d_alloc_root(inode);
938 if (!dentry) {
939 iput(inode);
940 return -ENOMEM;
941 }
942 sb->s_root = dentry;
943 return 0;
944}
945
946static int cgroup_get_sb(struct file_system_type *fs_type,
947 int flags, const char *unused_dev_name,
948 void *data, struct vfsmount *mnt)
949{
950 struct cgroup_sb_opts opts;
951 int ret = 0;
952 struct super_block *sb;
953 struct cgroupfs_root *root;
28fd5dfc 954 struct list_head tmp_cg_links;
ddbcc7e8
PM
955
956 /* First find the desired set of subsystems */
957 ret = parse_cgroupfs_options(data, &opts);
81a6a5cd
PM
958 if (ret) {
959 if (opts.release_agent)
960 kfree(opts.release_agent);
ddbcc7e8 961 return ret;
81a6a5cd 962 }
ddbcc7e8
PM
963
964 root = kzalloc(sizeof(*root), GFP_KERNEL);
f7770738
LZ
965 if (!root) {
966 if (opts.release_agent)
967 kfree(opts.release_agent);
ddbcc7e8 968 return -ENOMEM;
f7770738 969 }
ddbcc7e8
PM
970
971 init_cgroup_root(root);
972 root->subsys_bits = opts.subsys_bits;
973 root->flags = opts.flags;
81a6a5cd
PM
974 if (opts.release_agent) {
975 strcpy(root->release_agent_path, opts.release_agent);
976 kfree(opts.release_agent);
977 }
ddbcc7e8
PM
978
979 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
980
981 if (IS_ERR(sb)) {
982 kfree(root);
983 return PTR_ERR(sb);
984 }
985
986 if (sb->s_fs_info != root) {
987 /* Reusing an existing superblock */
988 BUG_ON(sb->s_root == NULL);
989 kfree(root);
990 root = NULL;
991 } else {
992 /* New superblock */
bd89aabc 993 struct cgroup *cgrp = &root->top_cgroup;
817929ec 994 struct inode *inode;
28fd5dfc 995 int i;
ddbcc7e8
PM
996
997 BUG_ON(sb->s_root != NULL);
998
999 ret = cgroup_get_rootdir(sb);
1000 if (ret)
1001 goto drop_new_super;
817929ec 1002 inode = sb->s_root->d_inode;
ddbcc7e8 1003
817929ec 1004 mutex_lock(&inode->i_mutex);
ddbcc7e8
PM
1005 mutex_lock(&cgroup_mutex);
1006
817929ec
PM
1007 /*
1008 * We're accessing css_set_count without locking
1009 * css_set_lock here, but that's OK - it can only be
1010 * increased by someone holding cgroup_lock, and
1011 * that's us. The worst that can happen is that we
1012 * have some link structures left over
1013 */
1014 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1015 if (ret) {
1016 mutex_unlock(&cgroup_mutex);
1017 mutex_unlock(&inode->i_mutex);
1018 goto drop_new_super;
1019 }
1020
ddbcc7e8
PM
1021 ret = rebind_subsystems(root, root->subsys_bits);
1022 if (ret == -EBUSY) {
1023 mutex_unlock(&cgroup_mutex);
817929ec 1024 mutex_unlock(&inode->i_mutex);
20ca9b3f 1025 goto free_cg_links;
ddbcc7e8
PM
1026 }
1027
1028 /* EBUSY should be the only error here */
1029 BUG_ON(ret);
1030
1031 list_add(&root->root_list, &roots);
817929ec 1032 root_count++;
ddbcc7e8
PM
1033
1034 sb->s_root->d_fsdata = &root->top_cgroup;
1035 root->top_cgroup.dentry = sb->s_root;
1036
817929ec
PM
1037 /* Link the top cgroup in this hierarchy into all
1038 * the css_set objects */
1039 write_lock(&css_set_lock);
28fd5dfc
LZ
1040 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1041 struct hlist_head *hhead = &css_set_table[i];
1042 struct hlist_node *node;
817929ec 1043 struct css_set *cg;
28fd5dfc
LZ
1044
1045 hlist_for_each_entry(cg, node, hhead, hlist) {
1046 struct cg_cgroup_link *link;
1047
1048 BUG_ON(list_empty(&tmp_cg_links));
1049 link = list_entry(tmp_cg_links.next,
1050 struct cg_cgroup_link,
1051 cgrp_link_list);
1052 list_del(&link->cgrp_link_list);
1053 link->cg = cg;
1054 list_add(&link->cgrp_link_list,
1055 &root->top_cgroup.css_sets);
1056 list_add(&link->cg_link_list, &cg->cg_links);
1057 }
1058 }
817929ec
PM
1059 write_unlock(&css_set_lock);
1060
1061 free_cg_links(&tmp_cg_links);
1062
bd89aabc
PM
1063 BUG_ON(!list_empty(&cgrp->sibling));
1064 BUG_ON(!list_empty(&cgrp->children));
ddbcc7e8
PM
1065 BUG_ON(root->number_of_cgroups != 1);
1066
bd89aabc 1067 cgroup_populate_dir(cgrp);
817929ec 1068 mutex_unlock(&inode->i_mutex);
ddbcc7e8
PM
1069 mutex_unlock(&cgroup_mutex);
1070 }
1071
1072 return simple_set_mnt(mnt, sb);
1073
20ca9b3f
LZ
1074 free_cg_links:
1075 free_cg_links(&tmp_cg_links);
ddbcc7e8
PM
1076 drop_new_super:
1077 up_write(&sb->s_umount);
1078 deactivate_super(sb);
1079 return ret;
1080}
1081
1082static void cgroup_kill_sb(struct super_block *sb) {
1083 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 1084 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8 1085 int ret;
71cbb949
KM
1086 struct cg_cgroup_link *link;
1087 struct cg_cgroup_link *saved_link;
ddbcc7e8
PM
1088
1089 BUG_ON(!root);
1090
1091 BUG_ON(root->number_of_cgroups != 1);
bd89aabc
PM
1092 BUG_ON(!list_empty(&cgrp->children));
1093 BUG_ON(!list_empty(&cgrp->sibling));
ddbcc7e8
PM
1094
1095 mutex_lock(&cgroup_mutex);
1096
1097 /* Rebind all subsystems back to the default hierarchy */
1098 ret = rebind_subsystems(root, 0);
1099 /* Shouldn't be able to fail ... */
1100 BUG_ON(ret);
1101
817929ec
PM
1102 /*
1103 * Release all the links from css_sets to this hierarchy's
1104 * root cgroup
1105 */
1106 write_lock(&css_set_lock);
71cbb949
KM
1107
1108 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1109 cgrp_link_list) {
817929ec 1110 list_del(&link->cg_link_list);
bd89aabc 1111 list_del(&link->cgrp_link_list);
817929ec
PM
1112 kfree(link);
1113 }
1114 write_unlock(&css_set_lock);
1115
1116 if (!list_empty(&root->root_list)) {
ddbcc7e8 1117 list_del(&root->root_list);
817929ec
PM
1118 root_count--;
1119 }
ddbcc7e8
PM
1120 mutex_unlock(&cgroup_mutex);
1121
1122 kfree(root);
1123 kill_litter_super(sb);
1124}
1125
1126static struct file_system_type cgroup_fs_type = {
1127 .name = "cgroup",
1128 .get_sb = cgroup_get_sb,
1129 .kill_sb = cgroup_kill_sb,
1130};
1131
bd89aabc 1132static inline struct cgroup *__d_cgrp(struct dentry *dentry)
ddbcc7e8
PM
1133{
1134 return dentry->d_fsdata;
1135}
1136
1137static inline struct cftype *__d_cft(struct dentry *dentry)
1138{
1139 return dentry->d_fsdata;
1140}
1141
a043e3b2
LZ
1142/**
1143 * cgroup_path - generate the path of a cgroup
1144 * @cgrp: the cgroup in question
1145 * @buf: the buffer to write the path into
1146 * @buflen: the length of the buffer
1147 *
1148 * Called with cgroup_mutex held. Writes path of cgroup into buf.
ddbcc7e8
PM
1149 * Returns 0 on success, -errno on error.
1150 */
bd89aabc 1151int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
ddbcc7e8
PM
1152{
1153 char *start;
1154
bd89aabc 1155 if (cgrp == dummytop) {
ddbcc7e8
PM
1156 /*
1157 * Inactive subsystems have no dentry for their root
1158 * cgroup
1159 */
1160 strcpy(buf, "/");
1161 return 0;
1162 }
1163
1164 start = buf + buflen;
1165
1166 *--start = '\0';
1167 for (;;) {
bd89aabc 1168 int len = cgrp->dentry->d_name.len;
ddbcc7e8
PM
1169 if ((start -= len) < buf)
1170 return -ENAMETOOLONG;
bd89aabc
PM
1171 memcpy(start, cgrp->dentry->d_name.name, len);
1172 cgrp = cgrp->parent;
1173 if (!cgrp)
ddbcc7e8 1174 break;
bd89aabc 1175 if (!cgrp->parent)
ddbcc7e8
PM
1176 continue;
1177 if (--start < buf)
1178 return -ENAMETOOLONG;
1179 *start = '/';
1180 }
1181 memmove(buf, start, buf + buflen - start);
1182 return 0;
1183}
1184
bbcb81d0
PM
1185/*
1186 * Return the first subsystem attached to a cgroup's hierarchy, and
1187 * its subsystem id.
1188 */
1189
bd89aabc 1190static void get_first_subsys(const struct cgroup *cgrp,
bbcb81d0
PM
1191 struct cgroup_subsys_state **css, int *subsys_id)
1192{
bd89aabc 1193 const struct cgroupfs_root *root = cgrp->root;
bbcb81d0
PM
1194 const struct cgroup_subsys *test_ss;
1195 BUG_ON(list_empty(&root->subsys_list));
1196 test_ss = list_entry(root->subsys_list.next,
1197 struct cgroup_subsys, sibling);
1198 if (css) {
bd89aabc 1199 *css = cgrp->subsys[test_ss->subsys_id];
bbcb81d0
PM
1200 BUG_ON(!*css);
1201 }
1202 if (subsys_id)
1203 *subsys_id = test_ss->subsys_id;
1204}
1205
a043e3b2
LZ
1206/**
1207 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1208 * @cgrp: the cgroup the task is attaching to
1209 * @tsk: the task to be attached
bbcb81d0 1210 *
a043e3b2
LZ
1211 * Call holding cgroup_mutex. May take task_lock of
1212 * the task 'tsk' during call.
bbcb81d0 1213 */
956db3ca 1214int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
bbcb81d0
PM
1215{
1216 int retval = 0;
1217 struct cgroup_subsys *ss;
bd89aabc 1218 struct cgroup *oldcgrp;
817929ec
PM
1219 struct css_set *cg = tsk->cgroups;
1220 struct css_set *newcg;
bd89aabc 1221 struct cgroupfs_root *root = cgrp->root;
bbcb81d0
PM
1222 int subsys_id;
1223
bd89aabc 1224 get_first_subsys(cgrp, NULL, &subsys_id);
bbcb81d0
PM
1225
1226 /* Nothing to do if the task is already in that cgroup */
bd89aabc
PM
1227 oldcgrp = task_cgroup(tsk, subsys_id);
1228 if (cgrp == oldcgrp)
bbcb81d0
PM
1229 return 0;
1230
1231 for_each_subsys(root, ss) {
1232 if (ss->can_attach) {
bd89aabc 1233 retval = ss->can_attach(ss, cgrp, tsk);
e18f6318 1234 if (retval)
bbcb81d0 1235 return retval;
bbcb81d0
PM
1236 }
1237 }
1238
817929ec
PM
1239 /*
1240 * Locate or allocate a new css_set for this task,
1241 * based on its final set of cgroups
1242 */
bd89aabc 1243 newcg = find_css_set(cg, cgrp);
e18f6318 1244 if (!newcg)
817929ec 1245 return -ENOMEM;
817929ec 1246
bbcb81d0
PM
1247 task_lock(tsk);
1248 if (tsk->flags & PF_EXITING) {
1249 task_unlock(tsk);
817929ec 1250 put_css_set(newcg);
bbcb81d0
PM
1251 return -ESRCH;
1252 }
817929ec 1253 rcu_assign_pointer(tsk->cgroups, newcg);
bbcb81d0
PM
1254 task_unlock(tsk);
1255
817929ec
PM
1256 /* Update the css_set linked lists if we're using them */
1257 write_lock(&css_set_lock);
1258 if (!list_empty(&tsk->cg_list)) {
1259 list_del(&tsk->cg_list);
1260 list_add(&tsk->cg_list, &newcg->tasks);
1261 }
1262 write_unlock(&css_set_lock);
1263
bbcb81d0 1264 for_each_subsys(root, ss) {
e18f6318 1265 if (ss->attach)
bd89aabc 1266 ss->attach(ss, cgrp, oldcgrp, tsk);
bbcb81d0 1267 }
bd89aabc 1268 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
bbcb81d0 1269 synchronize_rcu();
817929ec 1270 put_css_set(cg);
bbcb81d0
PM
1271 return 0;
1272}
1273
1274/*
af351026
PM
1275 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1276 * held. May take task_lock of task
bbcb81d0 1277 */
af351026 1278static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
bbcb81d0 1279{
bbcb81d0 1280 struct task_struct *tsk;
c69e8d9c 1281 const struct cred *cred = current_cred(), *tcred;
bbcb81d0
PM
1282 int ret;
1283
bbcb81d0
PM
1284 if (pid) {
1285 rcu_read_lock();
73507f33 1286 tsk = find_task_by_vpid(pid);
bbcb81d0
PM
1287 if (!tsk || tsk->flags & PF_EXITING) {
1288 rcu_read_unlock();
1289 return -ESRCH;
1290 }
bbcb81d0 1291
c69e8d9c
DH
1292 tcred = __task_cred(tsk);
1293 if (cred->euid &&
1294 cred->euid != tcred->uid &&
1295 cred->euid != tcred->suid) {
1296 rcu_read_unlock();
bbcb81d0
PM
1297 return -EACCES;
1298 }
c69e8d9c
DH
1299 get_task_struct(tsk);
1300 rcu_read_unlock();
bbcb81d0
PM
1301 } else {
1302 tsk = current;
1303 get_task_struct(tsk);
1304 }
1305
956db3ca 1306 ret = cgroup_attach_task(cgrp, tsk);
bbcb81d0
PM
1307 put_task_struct(tsk);
1308 return ret;
1309}
1310
af351026
PM
1311static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1312{
1313 int ret;
1314 if (!cgroup_lock_live_group(cgrp))
1315 return -ENODEV;
1316 ret = attach_task_by_pid(cgrp, pid);
1317 cgroup_unlock();
1318 return ret;
1319}
1320
ddbcc7e8 1321/* The various types of files and directories in a cgroup file system */
ddbcc7e8
PM
1322enum cgroup_filetype {
1323 FILE_ROOT,
1324 FILE_DIR,
1325 FILE_TASKLIST,
81a6a5cd 1326 FILE_NOTIFY_ON_RELEASE,
81a6a5cd 1327 FILE_RELEASE_AGENT,
ddbcc7e8
PM
1328};
1329
e788e066
PM
1330/**
1331 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1332 * @cgrp: the cgroup to be checked for liveness
1333 *
84eea842
PM
1334 * On success, returns true; the lock should be later released with
1335 * cgroup_unlock(). On failure returns false with no lock held.
e788e066 1336 */
84eea842 1337bool cgroup_lock_live_group(struct cgroup *cgrp)
e788e066
PM
1338{
1339 mutex_lock(&cgroup_mutex);
1340 if (cgroup_is_removed(cgrp)) {
1341 mutex_unlock(&cgroup_mutex);
1342 return false;
1343 }
1344 return true;
1345}
1346
1347static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1348 const char *buffer)
1349{
1350 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1351 if (!cgroup_lock_live_group(cgrp))
1352 return -ENODEV;
1353 strcpy(cgrp->root->release_agent_path, buffer);
84eea842 1354 cgroup_unlock();
e788e066
PM
1355 return 0;
1356}
1357
1358static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1359 struct seq_file *seq)
1360{
1361 if (!cgroup_lock_live_group(cgrp))
1362 return -ENODEV;
1363 seq_puts(seq, cgrp->root->release_agent_path);
1364 seq_putc(seq, '\n');
84eea842 1365 cgroup_unlock();
e788e066
PM
1366 return 0;
1367}
1368
84eea842
PM
1369/* A buffer size big enough for numbers or short strings */
1370#define CGROUP_LOCAL_BUFFER_SIZE 64
1371
e73d2c61 1372static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
f4c753b7
PM
1373 struct file *file,
1374 const char __user *userbuf,
1375 size_t nbytes, loff_t *unused_ppos)
355e0c48 1376{
84eea842 1377 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
355e0c48 1378 int retval = 0;
355e0c48
PM
1379 char *end;
1380
1381 if (!nbytes)
1382 return -EINVAL;
1383 if (nbytes >= sizeof(buffer))
1384 return -E2BIG;
1385 if (copy_from_user(buffer, userbuf, nbytes))
1386 return -EFAULT;
1387
1388 buffer[nbytes] = 0; /* nul-terminate */
b7269dfc 1389 strstrip(buffer);
e73d2c61
PM
1390 if (cft->write_u64) {
1391 u64 val = simple_strtoull(buffer, &end, 0);
1392 if (*end)
1393 return -EINVAL;
1394 retval = cft->write_u64(cgrp, cft, val);
1395 } else {
1396 s64 val = simple_strtoll(buffer, &end, 0);
1397 if (*end)
1398 return -EINVAL;
1399 retval = cft->write_s64(cgrp, cft, val);
1400 }
355e0c48
PM
1401 if (!retval)
1402 retval = nbytes;
1403 return retval;
1404}
1405
db3b1497
PM
1406static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1407 struct file *file,
1408 const char __user *userbuf,
1409 size_t nbytes, loff_t *unused_ppos)
1410{
84eea842 1411 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
db3b1497
PM
1412 int retval = 0;
1413 size_t max_bytes = cft->max_write_len;
1414 char *buffer = local_buffer;
1415
1416 if (!max_bytes)
1417 max_bytes = sizeof(local_buffer) - 1;
1418 if (nbytes >= max_bytes)
1419 return -E2BIG;
1420 /* Allocate a dynamic buffer if we need one */
1421 if (nbytes >= sizeof(local_buffer)) {
1422 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1423 if (buffer == NULL)
1424 return -ENOMEM;
1425 }
5a3eb9f6
LZ
1426 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1427 retval = -EFAULT;
1428 goto out;
1429 }
db3b1497
PM
1430
1431 buffer[nbytes] = 0; /* nul-terminate */
1432 strstrip(buffer);
1433 retval = cft->write_string(cgrp, cft, buffer);
1434 if (!retval)
1435 retval = nbytes;
5a3eb9f6 1436out:
db3b1497
PM
1437 if (buffer != local_buffer)
1438 kfree(buffer);
1439 return retval;
1440}
1441
ddbcc7e8
PM
1442static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1443 size_t nbytes, loff_t *ppos)
1444{
1445 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1446 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1447
8dc4f3e1 1448 if (!cft || cgroup_is_removed(cgrp))
ddbcc7e8 1449 return -ENODEV;
355e0c48 1450 if (cft->write)
bd89aabc 1451 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1452 if (cft->write_u64 || cft->write_s64)
1453 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
db3b1497
PM
1454 if (cft->write_string)
1455 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
d447ea2f
PE
1456 if (cft->trigger) {
1457 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1458 return ret ? ret : nbytes;
1459 }
355e0c48 1460 return -EINVAL;
ddbcc7e8
PM
1461}
1462
f4c753b7
PM
1463static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1464 struct file *file,
1465 char __user *buf, size_t nbytes,
1466 loff_t *ppos)
ddbcc7e8 1467{
84eea842 1468 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
f4c753b7 1469 u64 val = cft->read_u64(cgrp, cft);
ddbcc7e8
PM
1470 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1471
1472 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1473}
1474
e73d2c61
PM
1475static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1476 struct file *file,
1477 char __user *buf, size_t nbytes,
1478 loff_t *ppos)
1479{
84eea842 1480 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
e73d2c61
PM
1481 s64 val = cft->read_s64(cgrp, cft);
1482 int len = sprintf(tmp, "%lld\n", (long long) val);
1483
1484 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1485}
1486
ddbcc7e8
PM
1487static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1488 size_t nbytes, loff_t *ppos)
1489{
1490 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1491 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1492
8dc4f3e1 1493 if (!cft || cgroup_is_removed(cgrp))
ddbcc7e8
PM
1494 return -ENODEV;
1495
1496 if (cft->read)
bd89aabc 1497 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
f4c753b7
PM
1498 if (cft->read_u64)
1499 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1500 if (cft->read_s64)
1501 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
ddbcc7e8
PM
1502 return -EINVAL;
1503}
1504
91796569
PM
1505/*
1506 * seqfile ops/methods for returning structured data. Currently just
1507 * supports string->u64 maps, but can be extended in future.
1508 */
1509
1510struct cgroup_seqfile_state {
1511 struct cftype *cft;
1512 struct cgroup *cgroup;
1513};
1514
1515static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1516{
1517 struct seq_file *sf = cb->state;
1518 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1519}
1520
1521static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1522{
1523 struct cgroup_seqfile_state *state = m->private;
1524 struct cftype *cft = state->cft;
29486df3
SH
1525 if (cft->read_map) {
1526 struct cgroup_map_cb cb = {
1527 .fill = cgroup_map_add,
1528 .state = m,
1529 };
1530 return cft->read_map(state->cgroup, cft, &cb);
1531 }
1532 return cft->read_seq_string(state->cgroup, cft, m);
91796569
PM
1533}
1534
96930a63 1535static int cgroup_seqfile_release(struct inode *inode, struct file *file)
91796569
PM
1536{
1537 struct seq_file *seq = file->private_data;
1538 kfree(seq->private);
1539 return single_release(inode, file);
1540}
1541
1542static struct file_operations cgroup_seqfile_operations = {
1543 .read = seq_read,
e788e066 1544 .write = cgroup_file_write,
91796569
PM
1545 .llseek = seq_lseek,
1546 .release = cgroup_seqfile_release,
1547};
1548
ddbcc7e8
PM
1549static int cgroup_file_open(struct inode *inode, struct file *file)
1550{
1551 int err;
1552 struct cftype *cft;
1553
1554 err = generic_file_open(inode, file);
1555 if (err)
1556 return err;
1557
1558 cft = __d_cft(file->f_dentry);
1559 if (!cft)
1560 return -ENODEV;
29486df3 1561 if (cft->read_map || cft->read_seq_string) {
91796569
PM
1562 struct cgroup_seqfile_state *state =
1563 kzalloc(sizeof(*state), GFP_USER);
1564 if (!state)
1565 return -ENOMEM;
1566 state->cft = cft;
1567 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1568 file->f_op = &cgroup_seqfile_operations;
1569 err = single_open(file, cgroup_seqfile_show, state);
1570 if (err < 0)
1571 kfree(state);
1572 } else if (cft->open)
ddbcc7e8
PM
1573 err = cft->open(inode, file);
1574 else
1575 err = 0;
1576
1577 return err;
1578}
1579
1580static int cgroup_file_release(struct inode *inode, struct file *file)
1581{
1582 struct cftype *cft = __d_cft(file->f_dentry);
1583 if (cft->release)
1584 return cft->release(inode, file);
1585 return 0;
1586}
1587
1588/*
1589 * cgroup_rename - Only allow simple rename of directories in place.
1590 */
1591static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1592 struct inode *new_dir, struct dentry *new_dentry)
1593{
1594 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1595 return -ENOTDIR;
1596 if (new_dentry->d_inode)
1597 return -EEXIST;
1598 if (old_dir != new_dir)
1599 return -EIO;
1600 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1601}
1602
1603static struct file_operations cgroup_file_operations = {
1604 .read = cgroup_file_read,
1605 .write = cgroup_file_write,
1606 .llseek = generic_file_llseek,
1607 .open = cgroup_file_open,
1608 .release = cgroup_file_release,
1609};
1610
1611static struct inode_operations cgroup_dir_inode_operations = {
1612 .lookup = simple_lookup,
1613 .mkdir = cgroup_mkdir,
1614 .rmdir = cgroup_rmdir,
1615 .rename = cgroup_rename,
1616};
1617
1618static int cgroup_create_file(struct dentry *dentry, int mode,
1619 struct super_block *sb)
1620{
1621 static struct dentry_operations cgroup_dops = {
1622 .d_iput = cgroup_diput,
1623 };
1624
1625 struct inode *inode;
1626
1627 if (!dentry)
1628 return -ENOENT;
1629 if (dentry->d_inode)
1630 return -EEXIST;
1631
1632 inode = cgroup_new_inode(mode, sb);
1633 if (!inode)
1634 return -ENOMEM;
1635
1636 if (S_ISDIR(mode)) {
1637 inode->i_op = &cgroup_dir_inode_operations;
1638 inode->i_fop = &simple_dir_operations;
1639
1640 /* start off with i_nlink == 2 (for "." entry) */
1641 inc_nlink(inode);
1642
1643 /* start with the directory inode held, so that we can
1644 * populate it without racing with another mkdir */
817929ec 1645 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
ddbcc7e8
PM
1646 } else if (S_ISREG(mode)) {
1647 inode->i_size = 0;
1648 inode->i_fop = &cgroup_file_operations;
1649 }
1650 dentry->d_op = &cgroup_dops;
1651 d_instantiate(dentry, inode);
1652 dget(dentry); /* Extra count - pin the dentry in core */
1653 return 0;
1654}
1655
1656/*
a043e3b2
LZ
1657 * cgroup_create_dir - create a directory for an object.
1658 * @cgrp: the cgroup we create the directory for. It must have a valid
1659 * ->parent field. And we are going to fill its ->dentry field.
1660 * @dentry: dentry of the new cgroup
1661 * @mode: mode to set on new directory.
ddbcc7e8 1662 */
bd89aabc 1663static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
ddbcc7e8
PM
1664 int mode)
1665{
1666 struct dentry *parent;
1667 int error = 0;
1668
bd89aabc
PM
1669 parent = cgrp->parent->dentry;
1670 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
ddbcc7e8 1671 if (!error) {
bd89aabc 1672 dentry->d_fsdata = cgrp;
ddbcc7e8 1673 inc_nlink(parent->d_inode);
bd89aabc 1674 cgrp->dentry = dentry;
ddbcc7e8
PM
1675 dget(dentry);
1676 }
1677 dput(dentry);
1678
1679 return error;
1680}
1681
bd89aabc 1682int cgroup_add_file(struct cgroup *cgrp,
ddbcc7e8
PM
1683 struct cgroup_subsys *subsys,
1684 const struct cftype *cft)
1685{
bd89aabc 1686 struct dentry *dir = cgrp->dentry;
ddbcc7e8
PM
1687 struct dentry *dentry;
1688 int error;
1689
1690 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
bd89aabc 1691 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
ddbcc7e8
PM
1692 strcpy(name, subsys->name);
1693 strcat(name, ".");
1694 }
1695 strcat(name, cft->name);
1696 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1697 dentry = lookup_one_len(name, dir, strlen(name));
1698 if (!IS_ERR(dentry)) {
1699 error = cgroup_create_file(dentry, 0644 | S_IFREG,
bd89aabc 1700 cgrp->root->sb);
ddbcc7e8
PM
1701 if (!error)
1702 dentry->d_fsdata = (void *)cft;
1703 dput(dentry);
1704 } else
1705 error = PTR_ERR(dentry);
1706 return error;
1707}
1708
bd89aabc 1709int cgroup_add_files(struct cgroup *cgrp,
ddbcc7e8
PM
1710 struct cgroup_subsys *subsys,
1711 const struct cftype cft[],
1712 int count)
1713{
1714 int i, err;
1715 for (i = 0; i < count; i++) {
bd89aabc 1716 err = cgroup_add_file(cgrp, subsys, &cft[i]);
ddbcc7e8
PM
1717 if (err)
1718 return err;
1719 }
1720 return 0;
1721}
1722
a043e3b2
LZ
1723/**
1724 * cgroup_task_count - count the number of tasks in a cgroup.
1725 * @cgrp: the cgroup in question
1726 *
1727 * Return the number of tasks in the cgroup.
1728 */
bd89aabc 1729int cgroup_task_count(const struct cgroup *cgrp)
bbcb81d0
PM
1730{
1731 int count = 0;
71cbb949 1732 struct cg_cgroup_link *link;
817929ec
PM
1733
1734 read_lock(&css_set_lock);
71cbb949 1735 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
146aa1bd 1736 count += atomic_read(&link->cg->refcount);
817929ec
PM
1737 }
1738 read_unlock(&css_set_lock);
bbcb81d0
PM
1739 return count;
1740}
1741
817929ec
PM
1742/*
1743 * Advance a list_head iterator. The iterator should be positioned at
1744 * the start of a css_set
1745 */
bd89aabc 1746static void cgroup_advance_iter(struct cgroup *cgrp,
817929ec
PM
1747 struct cgroup_iter *it)
1748{
1749 struct list_head *l = it->cg_link;
1750 struct cg_cgroup_link *link;
1751 struct css_set *cg;
1752
1753 /* Advance to the next non-empty css_set */
1754 do {
1755 l = l->next;
bd89aabc 1756 if (l == &cgrp->css_sets) {
817929ec
PM
1757 it->cg_link = NULL;
1758 return;
1759 }
bd89aabc 1760 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
817929ec
PM
1761 cg = link->cg;
1762 } while (list_empty(&cg->tasks));
1763 it->cg_link = l;
1764 it->task = cg->tasks.next;
1765}
1766
31a7df01
CW
1767/*
1768 * To reduce the fork() overhead for systems that are not actually
1769 * using their cgroups capability, we don't maintain the lists running
1770 * through each css_set to its tasks until we see the list actually
1771 * used - in other words after the first call to cgroup_iter_start().
1772 *
1773 * The tasklist_lock is not held here, as do_each_thread() and
1774 * while_each_thread() are protected by RCU.
1775 */
3df91fe3 1776static void cgroup_enable_task_cg_lists(void)
31a7df01
CW
1777{
1778 struct task_struct *p, *g;
1779 write_lock(&css_set_lock);
1780 use_task_css_set_links = 1;
1781 do_each_thread(g, p) {
1782 task_lock(p);
0e04388f
LZ
1783 /*
1784 * We should check if the process is exiting, otherwise
1785 * it will race with cgroup_exit() in that the list
1786 * entry won't be deleted though the process has exited.
1787 */
1788 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
31a7df01
CW
1789 list_add(&p->cg_list, &p->cgroups->tasks);
1790 task_unlock(p);
1791 } while_each_thread(g, p);
1792 write_unlock(&css_set_lock);
1793}
1794
bd89aabc 1795void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
1796{
1797 /*
1798 * The first time anyone tries to iterate across a cgroup,
1799 * we need to enable the list linking each css_set to its
1800 * tasks, and fix up all existing tasks.
1801 */
31a7df01
CW
1802 if (!use_task_css_set_links)
1803 cgroup_enable_task_cg_lists();
1804
817929ec 1805 read_lock(&css_set_lock);
bd89aabc
PM
1806 it->cg_link = &cgrp->css_sets;
1807 cgroup_advance_iter(cgrp, it);
817929ec
PM
1808}
1809
bd89aabc 1810struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
817929ec
PM
1811 struct cgroup_iter *it)
1812{
1813 struct task_struct *res;
1814 struct list_head *l = it->task;
1815
1816 /* If the iterator cg is NULL, we have no tasks */
1817 if (!it->cg_link)
1818 return NULL;
1819 res = list_entry(l, struct task_struct, cg_list);
1820 /* Advance iterator to find next entry */
1821 l = l->next;
1822 if (l == &res->cgroups->tasks) {
1823 /* We reached the end of this task list - move on to
1824 * the next cg_cgroup_link */
bd89aabc 1825 cgroup_advance_iter(cgrp, it);
817929ec
PM
1826 } else {
1827 it->task = l;
1828 }
1829 return res;
1830}
1831
bd89aabc 1832void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
1833{
1834 read_unlock(&css_set_lock);
1835}
1836
31a7df01
CW
1837static inline int started_after_time(struct task_struct *t1,
1838 struct timespec *time,
1839 struct task_struct *t2)
1840{
1841 int start_diff = timespec_compare(&t1->start_time, time);
1842 if (start_diff > 0) {
1843 return 1;
1844 } else if (start_diff < 0) {
1845 return 0;
1846 } else {
1847 /*
1848 * Arbitrarily, if two processes started at the same
1849 * time, we'll say that the lower pointer value
1850 * started first. Note that t2 may have exited by now
1851 * so this may not be a valid pointer any longer, but
1852 * that's fine - it still serves to distinguish
1853 * between two tasks started (effectively) simultaneously.
1854 */
1855 return t1 > t2;
1856 }
1857}
1858
1859/*
1860 * This function is a callback from heap_insert() and is used to order
1861 * the heap.
1862 * In this case we order the heap in descending task start time.
1863 */
1864static inline int started_after(void *p1, void *p2)
1865{
1866 struct task_struct *t1 = p1;
1867 struct task_struct *t2 = p2;
1868 return started_after_time(t1, &t2->start_time, t2);
1869}
1870
1871/**
1872 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1873 * @scan: struct cgroup_scanner containing arguments for the scan
1874 *
1875 * Arguments include pointers to callback functions test_task() and
1876 * process_task().
1877 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1878 * and if it returns true, call process_task() for it also.
1879 * The test_task pointer may be NULL, meaning always true (select all tasks).
1880 * Effectively duplicates cgroup_iter_{start,next,end}()
1881 * but does not lock css_set_lock for the call to process_task().
1882 * The struct cgroup_scanner may be embedded in any structure of the caller's
1883 * creation.
1884 * It is guaranteed that process_task() will act on every task that
1885 * is a member of the cgroup for the duration of this call. This
1886 * function may or may not call process_task() for tasks that exit
1887 * or move to a different cgroup during the call, or are forked or
1888 * move into the cgroup during the call.
1889 *
1890 * Note that test_task() may be called with locks held, and may in some
1891 * situations be called multiple times for the same task, so it should
1892 * be cheap.
1893 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1894 * pre-allocated and will be used for heap operations (and its "gt" member will
1895 * be overwritten), else a temporary heap will be used (allocation of which
1896 * may cause this function to fail).
1897 */
1898int cgroup_scan_tasks(struct cgroup_scanner *scan)
1899{
1900 int retval, i;
1901 struct cgroup_iter it;
1902 struct task_struct *p, *dropped;
1903 /* Never dereference latest_task, since it's not refcounted */
1904 struct task_struct *latest_task = NULL;
1905 struct ptr_heap tmp_heap;
1906 struct ptr_heap *heap;
1907 struct timespec latest_time = { 0, 0 };
1908
1909 if (scan->heap) {
1910 /* The caller supplied our heap and pre-allocated its memory */
1911 heap = scan->heap;
1912 heap->gt = &started_after;
1913 } else {
1914 /* We need to allocate our own heap memory */
1915 heap = &tmp_heap;
1916 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1917 if (retval)
1918 /* cannot allocate the heap */
1919 return retval;
1920 }
1921
1922 again:
1923 /*
1924 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1925 * to determine which are of interest, and using the scanner's
1926 * "process_task" callback to process any of them that need an update.
1927 * Since we don't want to hold any locks during the task updates,
1928 * gather tasks to be processed in a heap structure.
1929 * The heap is sorted by descending task start time.
1930 * If the statically-sized heap fills up, we overflow tasks that
1931 * started later, and in future iterations only consider tasks that
1932 * started after the latest task in the previous pass. This
1933 * guarantees forward progress and that we don't miss any tasks.
1934 */
1935 heap->size = 0;
1936 cgroup_iter_start(scan->cg, &it);
1937 while ((p = cgroup_iter_next(scan->cg, &it))) {
1938 /*
1939 * Only affect tasks that qualify per the caller's callback,
1940 * if he provided one
1941 */
1942 if (scan->test_task && !scan->test_task(p, scan))
1943 continue;
1944 /*
1945 * Only process tasks that started after the last task
1946 * we processed
1947 */
1948 if (!started_after_time(p, &latest_time, latest_task))
1949 continue;
1950 dropped = heap_insert(heap, p);
1951 if (dropped == NULL) {
1952 /*
1953 * The new task was inserted; the heap wasn't
1954 * previously full
1955 */
1956 get_task_struct(p);
1957 } else if (dropped != p) {
1958 /*
1959 * The new task was inserted, and pushed out a
1960 * different task
1961 */
1962 get_task_struct(p);
1963 put_task_struct(dropped);
1964 }
1965 /*
1966 * Else the new task was newer than anything already in
1967 * the heap and wasn't inserted
1968 */
1969 }
1970 cgroup_iter_end(scan->cg, &it);
1971
1972 if (heap->size) {
1973 for (i = 0; i < heap->size; i++) {
4fe91d51 1974 struct task_struct *q = heap->ptrs[i];
31a7df01 1975 if (i == 0) {
4fe91d51
PJ
1976 latest_time = q->start_time;
1977 latest_task = q;
31a7df01
CW
1978 }
1979 /* Process the task per the caller's callback */
4fe91d51
PJ
1980 scan->process_task(q, scan);
1981 put_task_struct(q);
31a7df01
CW
1982 }
1983 /*
1984 * If we had to process any tasks at all, scan again
1985 * in case some of them were in the middle of forking
1986 * children that didn't get processed.
1987 * Not the most efficient way to do it, but it avoids
1988 * having to take callback_mutex in the fork path
1989 */
1990 goto again;
1991 }
1992 if (heap == &tmp_heap)
1993 heap_free(&tmp_heap);
1994 return 0;
1995}
1996
bbcb81d0
PM
1997/*
1998 * Stuff for reading the 'tasks' file.
1999 *
2000 * Reading this file can return large amounts of data if a cgroup has
2001 * *lots* of attached tasks. So it may need several calls to read(),
2002 * but we cannot guarantee that the information we produce is correct
2003 * unless we produce it entirely atomically.
2004 *
bbcb81d0 2005 */
bbcb81d0
PM
2006
2007/*
2008 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
bd89aabc 2009 * 'cgrp'. Return actual number of pids loaded. No need to
bbcb81d0
PM
2010 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2011 * read section, so the css_set can't go away, and is
2012 * immutable after creation.
2013 */
bd89aabc 2014static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
bbcb81d0
PM
2015{
2016 int n = 0;
817929ec
PM
2017 struct cgroup_iter it;
2018 struct task_struct *tsk;
bd89aabc
PM
2019 cgroup_iter_start(cgrp, &it);
2020 while ((tsk = cgroup_iter_next(cgrp, &it))) {
817929ec
PM
2021 if (unlikely(n == npids))
2022 break;
73507f33 2023 pidarray[n++] = task_pid_vnr(tsk);
817929ec 2024 }
bd89aabc 2025 cgroup_iter_end(cgrp, &it);
bbcb81d0
PM
2026 return n;
2027}
2028
846c7bb0 2029/**
a043e3b2 2030 * cgroupstats_build - build and fill cgroupstats
846c7bb0
BS
2031 * @stats: cgroupstats to fill information into
2032 * @dentry: A dentry entry belonging to the cgroup for which stats have
2033 * been requested.
a043e3b2
LZ
2034 *
2035 * Build and fill cgroupstats so that taskstats can export it to user
2036 * space.
846c7bb0
BS
2037 */
2038int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2039{
2040 int ret = -EINVAL;
bd89aabc 2041 struct cgroup *cgrp;
846c7bb0
BS
2042 struct cgroup_iter it;
2043 struct task_struct *tsk;
33d283be 2044
846c7bb0 2045 /*
33d283be
LZ
2046 * Validate dentry by checking the superblock operations,
2047 * and make sure it's a directory.
846c7bb0 2048 */
33d283be
LZ
2049 if (dentry->d_sb->s_op != &cgroup_ops ||
2050 !S_ISDIR(dentry->d_inode->i_mode))
846c7bb0
BS
2051 goto err;
2052
2053 ret = 0;
bd89aabc 2054 cgrp = dentry->d_fsdata;
846c7bb0
BS
2055 rcu_read_lock();
2056
bd89aabc
PM
2057 cgroup_iter_start(cgrp, &it);
2058 while ((tsk = cgroup_iter_next(cgrp, &it))) {
846c7bb0
BS
2059 switch (tsk->state) {
2060 case TASK_RUNNING:
2061 stats->nr_running++;
2062 break;
2063 case TASK_INTERRUPTIBLE:
2064 stats->nr_sleeping++;
2065 break;
2066 case TASK_UNINTERRUPTIBLE:
2067 stats->nr_uninterruptible++;
2068 break;
2069 case TASK_STOPPED:
2070 stats->nr_stopped++;
2071 break;
2072 default:
2073 if (delayacct_is_task_waiting_on_io(tsk))
2074 stats->nr_io_wait++;
2075 break;
2076 }
2077 }
bd89aabc 2078 cgroup_iter_end(cgrp, &it);
846c7bb0
BS
2079
2080 rcu_read_unlock();
2081err:
2082 return ret;
2083}
2084
bbcb81d0
PM
2085static int cmppid(const void *a, const void *b)
2086{
2087 return *(pid_t *)a - *(pid_t *)b;
2088}
2089
cc31edce 2090
bbcb81d0 2091/*
cc31edce
PM
2092 * seq_file methods for the "tasks" file. The seq_file position is the
2093 * next pid to display; the seq_file iterator is a pointer to the pid
2094 * in the cgroup->tasks_pids array.
bbcb81d0 2095 */
cc31edce
PM
2096
2097static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
bbcb81d0 2098{
cc31edce
PM
2099 /*
2100 * Initially we receive a position value that corresponds to
2101 * one more than the last pid shown (or 0 on the first call or
2102 * after a seek to the start). Use a binary-search to find the
2103 * next pid to display, if any
2104 */
2105 struct cgroup *cgrp = s->private;
2106 int index = 0, pid = *pos;
2107 int *iter;
2108
2109 down_read(&cgrp->pids_mutex);
2110 if (pid) {
2111 int end = cgrp->pids_length;
20777766 2112
cc31edce
PM
2113 while (index < end) {
2114 int mid = (index + end) / 2;
2115 if (cgrp->tasks_pids[mid] == pid) {
2116 index = mid;
2117 break;
2118 } else if (cgrp->tasks_pids[mid] <= pid)
2119 index = mid + 1;
2120 else
2121 end = mid;
2122 }
2123 }
2124 /* If we're off the end of the array, we're done */
2125 if (index >= cgrp->pids_length)
2126 return NULL;
2127 /* Update the abstract position to be the actual pid that we found */
2128 iter = cgrp->tasks_pids + index;
2129 *pos = *iter;
2130 return iter;
2131}
2132
2133static void cgroup_tasks_stop(struct seq_file *s, void *v)
2134{
2135 struct cgroup *cgrp = s->private;
2136 up_read(&cgrp->pids_mutex);
2137}
2138
2139static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2140{
2141 struct cgroup *cgrp = s->private;
2142 int *p = v;
2143 int *end = cgrp->tasks_pids + cgrp->pids_length;
2144
2145 /*
2146 * Advance to the next pid in the array. If this goes off the
2147 * end, we're done
2148 */
2149 p++;
2150 if (p >= end) {
2151 return NULL;
2152 } else {
2153 *pos = *p;
2154 return p;
2155 }
2156}
2157
2158static int cgroup_tasks_show(struct seq_file *s, void *v)
2159{
2160 return seq_printf(s, "%d\n", *(int *)v);
2161}
bbcb81d0 2162
cc31edce
PM
2163static struct seq_operations cgroup_tasks_seq_operations = {
2164 .start = cgroup_tasks_start,
2165 .stop = cgroup_tasks_stop,
2166 .next = cgroup_tasks_next,
2167 .show = cgroup_tasks_show,
2168};
2169
2170static void release_cgroup_pid_array(struct cgroup *cgrp)
2171{
2172 down_write(&cgrp->pids_mutex);
2173 BUG_ON(!cgrp->pids_use_count);
2174 if (!--cgrp->pids_use_count) {
2175 kfree(cgrp->tasks_pids);
2176 cgrp->tasks_pids = NULL;
2177 cgrp->pids_length = 0;
2178 }
2179 up_write(&cgrp->pids_mutex);
bbcb81d0
PM
2180}
2181
cc31edce
PM
2182static int cgroup_tasks_release(struct inode *inode, struct file *file)
2183{
2184 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2185
2186 if (!(file->f_mode & FMODE_READ))
2187 return 0;
2188
2189 release_cgroup_pid_array(cgrp);
2190 return seq_release(inode, file);
2191}
2192
2193static struct file_operations cgroup_tasks_operations = {
2194 .read = seq_read,
2195 .llseek = seq_lseek,
2196 .write = cgroup_file_write,
2197 .release = cgroup_tasks_release,
2198};
2199
bbcb81d0 2200/*
cc31edce 2201 * Handle an open on 'tasks' file. Prepare an array containing the
bbcb81d0 2202 * process id's of tasks currently attached to the cgroup being opened.
bbcb81d0 2203 */
cc31edce 2204
bbcb81d0
PM
2205static int cgroup_tasks_open(struct inode *unused, struct file *file)
2206{
bd89aabc 2207 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
bbcb81d0
PM
2208 pid_t *pidarray;
2209 int npids;
cc31edce 2210 int retval;
bbcb81d0 2211
cc31edce 2212 /* Nothing to do for write-only files */
bbcb81d0
PM
2213 if (!(file->f_mode & FMODE_READ))
2214 return 0;
2215
bbcb81d0
PM
2216 /*
2217 * If cgroup gets more users after we read count, we won't have
2218 * enough space - tough. This race is indistinguishable to the
2219 * caller from the case that the additional cgroup users didn't
2220 * show up until sometime later on.
2221 */
bd89aabc 2222 npids = cgroup_task_count(cgrp);
cc31edce
PM
2223 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2224 if (!pidarray)
2225 return -ENOMEM;
2226 npids = pid_array_load(pidarray, npids, cgrp);
2227 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
bbcb81d0 2228
cc31edce
PM
2229 /*
2230 * Store the array in the cgroup, freeing the old
2231 * array if necessary
2232 */
2233 down_write(&cgrp->pids_mutex);
2234 kfree(cgrp->tasks_pids);
2235 cgrp->tasks_pids = pidarray;
2236 cgrp->pids_length = npids;
2237 cgrp->pids_use_count++;
2238 up_write(&cgrp->pids_mutex);
2239
2240 file->f_op = &cgroup_tasks_operations;
2241
2242 retval = seq_open(file, &cgroup_tasks_seq_operations);
2243 if (retval) {
2244 release_cgroup_pid_array(cgrp);
2245 return retval;
bbcb81d0 2246 }
cc31edce 2247 ((struct seq_file *)file->private_data)->private = cgrp;
bbcb81d0
PM
2248 return 0;
2249}
2250
bd89aabc 2251static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
81a6a5cd
PM
2252 struct cftype *cft)
2253{
bd89aabc 2254 return notify_on_release(cgrp);
81a6a5cd
PM
2255}
2256
6379c106
PM
2257static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2258 struct cftype *cft,
2259 u64 val)
2260{
2261 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2262 if (val)
2263 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2264 else
2265 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2266 return 0;
2267}
2268
bbcb81d0
PM
2269/*
2270 * for the common functions, 'private' gives the type of file
2271 */
81a6a5cd
PM
2272static struct cftype files[] = {
2273 {
2274 .name = "tasks",
2275 .open = cgroup_tasks_open,
af351026 2276 .write_u64 = cgroup_tasks_write,
81a6a5cd
PM
2277 .release = cgroup_tasks_release,
2278 .private = FILE_TASKLIST,
2279 },
2280
2281 {
2282 .name = "notify_on_release",
f4c753b7 2283 .read_u64 = cgroup_read_notify_on_release,
6379c106 2284 .write_u64 = cgroup_write_notify_on_release,
81a6a5cd
PM
2285 .private = FILE_NOTIFY_ON_RELEASE,
2286 },
81a6a5cd
PM
2287};
2288
2289static struct cftype cft_release_agent = {
2290 .name = "release_agent",
e788e066
PM
2291 .read_seq_string = cgroup_release_agent_show,
2292 .write_string = cgroup_release_agent_write,
2293 .max_write_len = PATH_MAX,
81a6a5cd 2294 .private = FILE_RELEASE_AGENT,
bbcb81d0
PM
2295};
2296
bd89aabc 2297static int cgroup_populate_dir(struct cgroup *cgrp)
ddbcc7e8
PM
2298{
2299 int err;
2300 struct cgroup_subsys *ss;
2301
2302 /* First clear out any existing files */
bd89aabc 2303 cgroup_clear_directory(cgrp->dentry);
ddbcc7e8 2304
bd89aabc 2305 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
bbcb81d0
PM
2306 if (err < 0)
2307 return err;
2308
bd89aabc
PM
2309 if (cgrp == cgrp->top_cgroup) {
2310 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
81a6a5cd
PM
2311 return err;
2312 }
2313
bd89aabc
PM
2314 for_each_subsys(cgrp->root, ss) {
2315 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
ddbcc7e8
PM
2316 return err;
2317 }
2318
2319 return 0;
2320}
2321
2322static void init_cgroup_css(struct cgroup_subsys_state *css,
2323 struct cgroup_subsys *ss,
bd89aabc 2324 struct cgroup *cgrp)
ddbcc7e8 2325{
bd89aabc 2326 css->cgroup = cgrp;
ddbcc7e8
PM
2327 atomic_set(&css->refcnt, 0);
2328 css->flags = 0;
bd89aabc 2329 if (cgrp == dummytop)
ddbcc7e8 2330 set_bit(CSS_ROOT, &css->flags);
bd89aabc
PM
2331 BUG_ON(cgrp->subsys[ss->subsys_id]);
2332 cgrp->subsys[ss->subsys_id] = css;
ddbcc7e8
PM
2333}
2334
2335/*
a043e3b2
LZ
2336 * cgroup_create - create a cgroup
2337 * @parent: cgroup that will be parent of the new cgroup
2338 * @dentry: dentry of the new cgroup
2339 * @mode: mode to set on new inode
ddbcc7e8 2340 *
a043e3b2 2341 * Must be called with the mutex on the parent inode held
ddbcc7e8 2342 */
ddbcc7e8
PM
2343static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2344 int mode)
2345{
bd89aabc 2346 struct cgroup *cgrp;
ddbcc7e8
PM
2347 struct cgroupfs_root *root = parent->root;
2348 int err = 0;
2349 struct cgroup_subsys *ss;
2350 struct super_block *sb = root->sb;
2351
bd89aabc
PM
2352 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2353 if (!cgrp)
ddbcc7e8
PM
2354 return -ENOMEM;
2355
2356 /* Grab a reference on the superblock so the hierarchy doesn't
2357 * get deleted on unmount if there are child cgroups. This
2358 * can be done outside cgroup_mutex, since the sb can't
2359 * disappear while someone has an open control file on the
2360 * fs */
2361 atomic_inc(&sb->s_active);
2362
2363 mutex_lock(&cgroup_mutex);
2364
cc31edce 2365 init_cgroup_housekeeping(cgrp);
ddbcc7e8 2366
bd89aabc
PM
2367 cgrp->parent = parent;
2368 cgrp->root = parent->root;
2369 cgrp->top_cgroup = parent->top_cgroup;
ddbcc7e8 2370
b6abdb0e
LZ
2371 if (notify_on_release(parent))
2372 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2373
ddbcc7e8 2374 for_each_subsys(root, ss) {
bd89aabc 2375 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
ddbcc7e8
PM
2376 if (IS_ERR(css)) {
2377 err = PTR_ERR(css);
2378 goto err_destroy;
2379 }
bd89aabc 2380 init_cgroup_css(css, ss, cgrp);
ddbcc7e8
PM
2381 }
2382
bd89aabc 2383 list_add(&cgrp->sibling, &cgrp->parent->children);
ddbcc7e8
PM
2384 root->number_of_cgroups++;
2385
bd89aabc 2386 err = cgroup_create_dir(cgrp, dentry, mode);
ddbcc7e8
PM
2387 if (err < 0)
2388 goto err_remove;
2389
2390 /* The cgroup directory was pre-locked for us */
bd89aabc 2391 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
ddbcc7e8 2392
bd89aabc 2393 err = cgroup_populate_dir(cgrp);
ddbcc7e8
PM
2394 /* If err < 0, we have a half-filled directory - oh well ;) */
2395
2396 mutex_unlock(&cgroup_mutex);
bd89aabc 2397 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
2398
2399 return 0;
2400
2401 err_remove:
2402
bd89aabc 2403 list_del(&cgrp->sibling);
ddbcc7e8
PM
2404 root->number_of_cgroups--;
2405
2406 err_destroy:
2407
2408 for_each_subsys(root, ss) {
bd89aabc
PM
2409 if (cgrp->subsys[ss->subsys_id])
2410 ss->destroy(ss, cgrp);
ddbcc7e8
PM
2411 }
2412
2413 mutex_unlock(&cgroup_mutex);
2414
2415 /* Release the reference count that we took on the superblock */
2416 deactivate_super(sb);
2417
bd89aabc 2418 kfree(cgrp);
ddbcc7e8
PM
2419 return err;
2420}
2421
2422static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2423{
2424 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2425
2426 /* the vfs holds inode->i_mutex already */
2427 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2428}
2429
55b6fd01 2430static int cgroup_has_css_refs(struct cgroup *cgrp)
81a6a5cd
PM
2431{
2432 /* Check the reference count on each subsystem. Since we
2433 * already established that there are no tasks in the
2434 * cgroup, if the css refcount is also 0, then there should
2435 * be no outstanding references, so the subsystem is safe to
2436 * destroy. We scan across all subsystems rather than using
2437 * the per-hierarchy linked list of mounted subsystems since
2438 * we can be called via check_for_release() with no
2439 * synchronization other than RCU, and the subsystem linked
2440 * list isn't RCU-safe */
2441 int i;
2442 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2443 struct cgroup_subsys *ss = subsys[i];
2444 struct cgroup_subsys_state *css;
2445 /* Skip subsystems not in this hierarchy */
bd89aabc 2446 if (ss->root != cgrp->root)
81a6a5cd 2447 continue;
bd89aabc 2448 css = cgrp->subsys[ss->subsys_id];
81a6a5cd
PM
2449 /* When called from check_for_release() it's possible
2450 * that by this point the cgroup has been removed
2451 * and the css deleted. But a false-positive doesn't
2452 * matter, since it can only happen if the cgroup
2453 * has been deleted and hence no longer needs the
2454 * release agent to be called anyway. */
e18f6318 2455 if (css && atomic_read(&css->refcnt))
81a6a5cd 2456 return 1;
81a6a5cd
PM
2457 }
2458 return 0;
2459}
2460
ddbcc7e8
PM
2461static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2462{
bd89aabc 2463 struct cgroup *cgrp = dentry->d_fsdata;
ddbcc7e8
PM
2464 struct dentry *d;
2465 struct cgroup *parent;
ddbcc7e8
PM
2466 struct super_block *sb;
2467 struct cgroupfs_root *root;
ddbcc7e8
PM
2468
2469 /* the vfs holds both inode->i_mutex already */
2470
2471 mutex_lock(&cgroup_mutex);
bd89aabc 2472 if (atomic_read(&cgrp->count) != 0) {
ddbcc7e8
PM
2473 mutex_unlock(&cgroup_mutex);
2474 return -EBUSY;
2475 }
bd89aabc 2476 if (!list_empty(&cgrp->children)) {
ddbcc7e8
PM
2477 mutex_unlock(&cgroup_mutex);
2478 return -EBUSY;
2479 }
3fa59dfb 2480 mutex_unlock(&cgroup_mutex);
a043e3b2 2481
4fca88c8 2482 /*
a043e3b2
LZ
2483 * Call pre_destroy handlers of subsys. Notify subsystems
2484 * that rmdir() request comes.
4fca88c8
KH
2485 */
2486 cgroup_call_pre_destroy(cgrp);
ddbcc7e8 2487
3fa59dfb
KH
2488 mutex_lock(&cgroup_mutex);
2489 parent = cgrp->parent;
2490 root = cgrp->root;
2491 sb = root->sb;
2492
2493 if (atomic_read(&cgrp->count)
2494 || !list_empty(&cgrp->children)
2495 || cgroup_has_css_refs(cgrp)) {
ddbcc7e8
PM
2496 mutex_unlock(&cgroup_mutex);
2497 return -EBUSY;
2498 }
2499
81a6a5cd 2500 spin_lock(&release_list_lock);
bd89aabc
PM
2501 set_bit(CGRP_REMOVED, &cgrp->flags);
2502 if (!list_empty(&cgrp->release_list))
2503 list_del(&cgrp->release_list);
81a6a5cd 2504 spin_unlock(&release_list_lock);
ddbcc7e8 2505 /* delete my sibling from parent->children */
bd89aabc
PM
2506 list_del(&cgrp->sibling);
2507 spin_lock(&cgrp->dentry->d_lock);
2508 d = dget(cgrp->dentry);
ddbcc7e8
PM
2509 spin_unlock(&d->d_lock);
2510
2511 cgroup_d_remove_dir(d);
2512 dput(d);
ddbcc7e8 2513
bd89aabc 2514 set_bit(CGRP_RELEASABLE, &parent->flags);
81a6a5cd
PM
2515 check_for_release(parent);
2516
ddbcc7e8 2517 mutex_unlock(&cgroup_mutex);
ddbcc7e8
PM
2518 return 0;
2519}
2520
06a11920 2521static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
ddbcc7e8 2522{
ddbcc7e8 2523 struct cgroup_subsys_state *css;
cfe36bde
DC
2524
2525 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
ddbcc7e8
PM
2526
2527 /* Create the top cgroup state for this subsystem */
2528 ss->root = &rootnode;
2529 css = ss->create(ss, dummytop);
2530 /* We don't handle early failures gracefully */
2531 BUG_ON(IS_ERR(css));
2532 init_cgroup_css(css, ss, dummytop);
2533
e8d55fde 2534 /* Update the init_css_set to contain a subsys
817929ec 2535 * pointer to this state - since the subsystem is
e8d55fde
LZ
2536 * newly registered, all tasks and hence the
2537 * init_css_set is in the subsystem's top cgroup. */
2538 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
ddbcc7e8
PM
2539
2540 need_forkexit_callback |= ss->fork || ss->exit;
2541
e8d55fde
LZ
2542 /* At system boot, before all subsystems have been
2543 * registered, no tasks have been forked, so we don't
2544 * need to invoke fork callbacks here. */
2545 BUG_ON(!list_empty(&init_task.tasks));
2546
ddbcc7e8
PM
2547 ss->active = 1;
2548}
2549
2550/**
a043e3b2
LZ
2551 * cgroup_init_early - cgroup initialization at system boot
2552 *
2553 * Initialize cgroups at system boot, and initialize any
2554 * subsystems that request early init.
ddbcc7e8
PM
2555 */
2556int __init cgroup_init_early(void)
2557{
2558 int i;
146aa1bd 2559 atomic_set(&init_css_set.refcount, 1);
817929ec
PM
2560 INIT_LIST_HEAD(&init_css_set.cg_links);
2561 INIT_LIST_HEAD(&init_css_set.tasks);
472b1053 2562 INIT_HLIST_NODE(&init_css_set.hlist);
817929ec 2563 css_set_count = 1;
ddbcc7e8
PM
2564 init_cgroup_root(&rootnode);
2565 list_add(&rootnode.root_list, &roots);
817929ec
PM
2566 root_count = 1;
2567 init_task.cgroups = &init_css_set;
2568
2569 init_css_set_link.cg = &init_css_set;
bd89aabc 2570 list_add(&init_css_set_link.cgrp_link_list,
817929ec
PM
2571 &rootnode.top_cgroup.css_sets);
2572 list_add(&init_css_set_link.cg_link_list,
2573 &init_css_set.cg_links);
ddbcc7e8 2574
472b1053
LZ
2575 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2576 INIT_HLIST_HEAD(&css_set_table[i]);
2577
ddbcc7e8
PM
2578 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2579 struct cgroup_subsys *ss = subsys[i];
2580
2581 BUG_ON(!ss->name);
2582 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2583 BUG_ON(!ss->create);
2584 BUG_ON(!ss->destroy);
2585 if (ss->subsys_id != i) {
cfe36bde 2586 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
ddbcc7e8
PM
2587 ss->name, ss->subsys_id);
2588 BUG();
2589 }
2590
2591 if (ss->early_init)
2592 cgroup_init_subsys(ss);
2593 }
2594 return 0;
2595}
2596
2597/**
a043e3b2
LZ
2598 * cgroup_init - cgroup initialization
2599 *
2600 * Register cgroup filesystem and /proc file, and initialize
2601 * any subsystems that didn't request early init.
ddbcc7e8
PM
2602 */
2603int __init cgroup_init(void)
2604{
2605 int err;
2606 int i;
472b1053 2607 struct hlist_head *hhead;
a424316c
PM
2608
2609 err = bdi_init(&cgroup_backing_dev_info);
2610 if (err)
2611 return err;
ddbcc7e8
PM
2612
2613 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2614 struct cgroup_subsys *ss = subsys[i];
2615 if (!ss->early_init)
2616 cgroup_init_subsys(ss);
2617 }
2618
472b1053
LZ
2619 /* Add init_css_set to the hash table */
2620 hhead = css_set_hash(init_css_set.subsys);
2621 hlist_add_head(&init_css_set.hlist, hhead);
2622
ddbcc7e8
PM
2623 err = register_filesystem(&cgroup_fs_type);
2624 if (err < 0)
2625 goto out;
2626
46ae220b 2627 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
a424316c 2628
ddbcc7e8 2629out:
a424316c
PM
2630 if (err)
2631 bdi_destroy(&cgroup_backing_dev_info);
2632
ddbcc7e8
PM
2633 return err;
2634}
b4f48b63 2635
a424316c
PM
2636/*
2637 * proc_cgroup_show()
2638 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2639 * - Used for /proc/<pid>/cgroup.
2640 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2641 * doesn't really matter if tsk->cgroup changes after we read it,
956db3ca 2642 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
a424316c
PM
2643 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2644 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2645 * cgroup to top_cgroup.
2646 */
2647
2648/* TODO: Use a proper seq_file iterator */
2649static int proc_cgroup_show(struct seq_file *m, void *v)
2650{
2651 struct pid *pid;
2652 struct task_struct *tsk;
2653 char *buf;
2654 int retval;
2655 struct cgroupfs_root *root;
2656
2657 retval = -ENOMEM;
2658 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2659 if (!buf)
2660 goto out;
2661
2662 retval = -ESRCH;
2663 pid = m->private;
2664 tsk = get_pid_task(pid, PIDTYPE_PID);
2665 if (!tsk)
2666 goto out_free;
2667
2668 retval = 0;
2669
2670 mutex_lock(&cgroup_mutex);
2671
2672 for_each_root(root) {
2673 struct cgroup_subsys *ss;
bd89aabc 2674 struct cgroup *cgrp;
a424316c
PM
2675 int subsys_id;
2676 int count = 0;
2677
2678 /* Skip this hierarchy if it has no active subsystems */
2679 if (!root->actual_subsys_bits)
2680 continue;
b6c3006d 2681 seq_printf(m, "%lu:", root->subsys_bits);
a424316c
PM
2682 for_each_subsys(root, ss)
2683 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2684 seq_putc(m, ':');
2685 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
bd89aabc
PM
2686 cgrp = task_cgroup(tsk, subsys_id);
2687 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
a424316c
PM
2688 if (retval < 0)
2689 goto out_unlock;
2690 seq_puts(m, buf);
2691 seq_putc(m, '\n');
2692 }
2693
2694out_unlock:
2695 mutex_unlock(&cgroup_mutex);
2696 put_task_struct(tsk);
2697out_free:
2698 kfree(buf);
2699out:
2700 return retval;
2701}
2702
2703static int cgroup_open(struct inode *inode, struct file *file)
2704{
2705 struct pid *pid = PROC_I(inode)->pid;
2706 return single_open(file, proc_cgroup_show, pid);
2707}
2708
2709struct file_operations proc_cgroup_operations = {
2710 .open = cgroup_open,
2711 .read = seq_read,
2712 .llseek = seq_lseek,
2713 .release = single_release,
2714};
2715
2716/* Display information about each subsystem and each hierarchy */
2717static int proc_cgroupstats_show(struct seq_file *m, void *v)
2718{
2719 int i;
a424316c 2720
8bab8dde 2721 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
a424316c 2722 mutex_lock(&cgroup_mutex);
a424316c
PM
2723 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2724 struct cgroup_subsys *ss = subsys[i];
8bab8dde 2725 seq_printf(m, "%s\t%lu\t%d\t%d\n",
817929ec 2726 ss->name, ss->root->subsys_bits,
8bab8dde 2727 ss->root->number_of_cgroups, !ss->disabled);
a424316c
PM
2728 }
2729 mutex_unlock(&cgroup_mutex);
2730 return 0;
2731}
2732
2733static int cgroupstats_open(struct inode *inode, struct file *file)
2734{
9dce07f1 2735 return single_open(file, proc_cgroupstats_show, NULL);
a424316c
PM
2736}
2737
2738static struct file_operations proc_cgroupstats_operations = {
2739 .open = cgroupstats_open,
2740 .read = seq_read,
2741 .llseek = seq_lseek,
2742 .release = single_release,
2743};
2744
b4f48b63
PM
2745/**
2746 * cgroup_fork - attach newly forked task to its parents cgroup.
a043e3b2 2747 * @child: pointer to task_struct of forking parent process.
b4f48b63
PM
2748 *
2749 * Description: A task inherits its parent's cgroup at fork().
2750 *
2751 * A pointer to the shared css_set was automatically copied in
2752 * fork.c by dup_task_struct(). However, we ignore that copy, since
2753 * it was not made under the protection of RCU or cgroup_mutex, so
956db3ca 2754 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
817929ec
PM
2755 * have already changed current->cgroups, allowing the previously
2756 * referenced cgroup group to be removed and freed.
b4f48b63
PM
2757 *
2758 * At the point that cgroup_fork() is called, 'current' is the parent
2759 * task, and the passed argument 'child' points to the child task.
2760 */
2761void cgroup_fork(struct task_struct *child)
2762{
817929ec
PM
2763 task_lock(current);
2764 child->cgroups = current->cgroups;
2765 get_css_set(child->cgroups);
2766 task_unlock(current);
2767 INIT_LIST_HEAD(&child->cg_list);
b4f48b63
PM
2768}
2769
2770/**
a043e3b2
LZ
2771 * cgroup_fork_callbacks - run fork callbacks
2772 * @child: the new task
2773 *
2774 * Called on a new task very soon before adding it to the
2775 * tasklist. No need to take any locks since no-one can
2776 * be operating on this task.
b4f48b63
PM
2777 */
2778void cgroup_fork_callbacks(struct task_struct *child)
2779{
2780 if (need_forkexit_callback) {
2781 int i;
2782 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2783 struct cgroup_subsys *ss = subsys[i];
2784 if (ss->fork)
2785 ss->fork(ss, child);
2786 }
2787 }
2788}
2789
817929ec 2790/**
a043e3b2
LZ
2791 * cgroup_post_fork - called on a new task after adding it to the task list
2792 * @child: the task in question
2793 *
2794 * Adds the task to the list running through its css_set if necessary.
2795 * Has to be after the task is visible on the task list in case we race
2796 * with the first call to cgroup_iter_start() - to guarantee that the
2797 * new task ends up on its list.
2798 */
817929ec
PM
2799void cgroup_post_fork(struct task_struct *child)
2800{
2801 if (use_task_css_set_links) {
2802 write_lock(&css_set_lock);
2803 if (list_empty(&child->cg_list))
2804 list_add(&child->cg_list, &child->cgroups->tasks);
2805 write_unlock(&css_set_lock);
2806 }
2807}
b4f48b63
PM
2808/**
2809 * cgroup_exit - detach cgroup from exiting task
2810 * @tsk: pointer to task_struct of exiting process
a043e3b2 2811 * @run_callback: run exit callbacks?
b4f48b63
PM
2812 *
2813 * Description: Detach cgroup from @tsk and release it.
2814 *
2815 * Note that cgroups marked notify_on_release force every task in
2816 * them to take the global cgroup_mutex mutex when exiting.
2817 * This could impact scaling on very large systems. Be reluctant to
2818 * use notify_on_release cgroups where very high task exit scaling
2819 * is required on large systems.
2820 *
2821 * the_top_cgroup_hack:
2822 *
2823 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2824 *
2825 * We call cgroup_exit() while the task is still competent to
2826 * handle notify_on_release(), then leave the task attached to the
2827 * root cgroup in each hierarchy for the remainder of its exit.
2828 *
2829 * To do this properly, we would increment the reference count on
2830 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2831 * code we would add a second cgroup function call, to drop that
2832 * reference. This would just create an unnecessary hot spot on
2833 * the top_cgroup reference count, to no avail.
2834 *
2835 * Normally, holding a reference to a cgroup without bumping its
2836 * count is unsafe. The cgroup could go away, or someone could
2837 * attach us to a different cgroup, decrementing the count on
2838 * the first cgroup that we never incremented. But in this case,
2839 * top_cgroup isn't going away, and either task has PF_EXITING set,
956db3ca
CW
2840 * which wards off any cgroup_attach_task() attempts, or task is a failed
2841 * fork, never visible to cgroup_attach_task.
b4f48b63
PM
2842 */
2843void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2844{
2845 int i;
817929ec 2846 struct css_set *cg;
b4f48b63
PM
2847
2848 if (run_callbacks && need_forkexit_callback) {
2849 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2850 struct cgroup_subsys *ss = subsys[i];
2851 if (ss->exit)
2852 ss->exit(ss, tsk);
2853 }
2854 }
817929ec
PM
2855
2856 /*
2857 * Unlink from the css_set task list if necessary.
2858 * Optimistically check cg_list before taking
2859 * css_set_lock
2860 */
2861 if (!list_empty(&tsk->cg_list)) {
2862 write_lock(&css_set_lock);
2863 if (!list_empty(&tsk->cg_list))
2864 list_del(&tsk->cg_list);
2865 write_unlock(&css_set_lock);
2866 }
2867
b4f48b63
PM
2868 /* Reassign the task to the init_css_set. */
2869 task_lock(tsk);
817929ec
PM
2870 cg = tsk->cgroups;
2871 tsk->cgroups = &init_css_set;
b4f48b63 2872 task_unlock(tsk);
817929ec 2873 if (cg)
81a6a5cd 2874 put_css_set_taskexit(cg);
b4f48b63 2875}
697f4161
PM
2876
2877/**
a043e3b2
LZ
2878 * cgroup_clone - clone the cgroup the given subsystem is attached to
2879 * @tsk: the task to be moved
2880 * @subsys: the given subsystem
e885dcde 2881 * @nodename: the name for the new cgroup
a043e3b2
LZ
2882 *
2883 * Duplicate the current cgroup in the hierarchy that the given
2884 * subsystem is attached to, and move this task into the new
2885 * child.
697f4161 2886 */
e885dcde
SH
2887int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
2888 char *nodename)
697f4161
PM
2889{
2890 struct dentry *dentry;
2891 int ret = 0;
697f4161
PM
2892 struct cgroup *parent, *child;
2893 struct inode *inode;
2894 struct css_set *cg;
2895 struct cgroupfs_root *root;
2896 struct cgroup_subsys *ss;
2897
2898 /* We shouldn't be called by an unregistered subsystem */
2899 BUG_ON(!subsys->active);
2900
2901 /* First figure out what hierarchy and cgroup we're dealing
2902 * with, and pin them so we can drop cgroup_mutex */
2903 mutex_lock(&cgroup_mutex);
2904 again:
2905 root = subsys->root;
2906 if (root == &rootnode) {
697f4161
PM
2907 mutex_unlock(&cgroup_mutex);
2908 return 0;
2909 }
817929ec 2910 cg = tsk->cgroups;
697f4161
PM
2911 parent = task_cgroup(tsk, subsys->subsys_id);
2912
697f4161 2913 /* Pin the hierarchy */
7b574b7b
LZ
2914 if (!atomic_inc_not_zero(&parent->root->sb->s_active)) {
2915 /* We race with the final deactivate_super() */
2916 mutex_unlock(&cgroup_mutex);
2917 return 0;
2918 }
697f4161 2919
817929ec
PM
2920 /* Keep the cgroup alive */
2921 get_css_set(cg);
697f4161
PM
2922 mutex_unlock(&cgroup_mutex);
2923
2924 /* Now do the VFS work to create a cgroup */
2925 inode = parent->dentry->d_inode;
2926
2927 /* Hold the parent directory mutex across this operation to
2928 * stop anyone else deleting the new cgroup */
2929 mutex_lock(&inode->i_mutex);
2930 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2931 if (IS_ERR(dentry)) {
2932 printk(KERN_INFO
cfe36bde 2933 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
697f4161
PM
2934 PTR_ERR(dentry));
2935 ret = PTR_ERR(dentry);
2936 goto out_release;
2937 }
2938
2939 /* Create the cgroup directory, which also creates the cgroup */
2940 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
bd89aabc 2941 child = __d_cgrp(dentry);
697f4161
PM
2942 dput(dentry);
2943 if (ret) {
2944 printk(KERN_INFO
2945 "Failed to create cgroup %s: %d\n", nodename,
2946 ret);
2947 goto out_release;
2948 }
2949
2950 if (!child) {
2951 printk(KERN_INFO
2952 "Couldn't find new cgroup %s\n", nodename);
2953 ret = -ENOMEM;
2954 goto out_release;
2955 }
2956
2957 /* The cgroup now exists. Retake cgroup_mutex and check
2958 * that we're still in the same state that we thought we
2959 * were. */
2960 mutex_lock(&cgroup_mutex);
2961 if ((root != subsys->root) ||
2962 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2963 /* Aargh, we raced ... */
2964 mutex_unlock(&inode->i_mutex);
817929ec 2965 put_css_set(cg);
697f4161
PM
2966
2967 deactivate_super(parent->root->sb);
2968 /* The cgroup is still accessible in the VFS, but
2969 * we're not going to try to rmdir() it at this
2970 * point. */
2971 printk(KERN_INFO
2972 "Race in cgroup_clone() - leaking cgroup %s\n",
2973 nodename);
2974 goto again;
2975 }
2976
2977 /* do any required auto-setup */
2978 for_each_subsys(root, ss) {
2979 if (ss->post_clone)
2980 ss->post_clone(ss, child);
2981 }
2982
2983 /* All seems fine. Finish by moving the task into the new cgroup */
956db3ca 2984 ret = cgroup_attach_task(child, tsk);
697f4161
PM
2985 mutex_unlock(&cgroup_mutex);
2986
2987 out_release:
2988 mutex_unlock(&inode->i_mutex);
81a6a5cd
PM
2989
2990 mutex_lock(&cgroup_mutex);
817929ec 2991 put_css_set(cg);
81a6a5cd 2992 mutex_unlock(&cgroup_mutex);
697f4161
PM
2993 deactivate_super(parent->root->sb);
2994 return ret;
2995}
2996
a043e3b2
LZ
2997/**
2998 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2999 * @cgrp: the cgroup in question
3000 *
3001 * See if @cgrp is a descendant of the current task's cgroup in
3002 * the appropriate hierarchy.
697f4161
PM
3003 *
3004 * If we are sending in dummytop, then presumably we are creating
3005 * the top cgroup in the subsystem.
3006 *
3007 * Called only by the ns (nsproxy) cgroup.
3008 */
bd89aabc 3009int cgroup_is_descendant(const struct cgroup *cgrp)
697f4161
PM
3010{
3011 int ret;
3012 struct cgroup *target;
3013 int subsys_id;
3014
bd89aabc 3015 if (cgrp == dummytop)
697f4161
PM
3016 return 1;
3017
bd89aabc 3018 get_first_subsys(cgrp, NULL, &subsys_id);
697f4161 3019 target = task_cgroup(current, subsys_id);
bd89aabc
PM
3020 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3021 cgrp = cgrp->parent;
3022 ret = (cgrp == target);
697f4161
PM
3023 return ret;
3024}
81a6a5cd 3025
bd89aabc 3026static void check_for_release(struct cgroup *cgrp)
81a6a5cd
PM
3027{
3028 /* All of these checks rely on RCU to keep the cgroup
3029 * structure alive */
bd89aabc
PM
3030 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3031 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
81a6a5cd
PM
3032 /* Control Group is currently removeable. If it's not
3033 * already queued for a userspace notification, queue
3034 * it now */
3035 int need_schedule_work = 0;
3036 spin_lock(&release_list_lock);
bd89aabc
PM
3037 if (!cgroup_is_removed(cgrp) &&
3038 list_empty(&cgrp->release_list)) {
3039 list_add(&cgrp->release_list, &release_list);
81a6a5cd
PM
3040 need_schedule_work = 1;
3041 }
3042 spin_unlock(&release_list_lock);
3043 if (need_schedule_work)
3044 schedule_work(&release_agent_work);
3045 }
3046}
3047
3048void __css_put(struct cgroup_subsys_state *css)
3049{
bd89aabc 3050 struct cgroup *cgrp = css->cgroup;
81a6a5cd 3051 rcu_read_lock();
bd89aabc
PM
3052 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3053 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3054 check_for_release(cgrp);
81a6a5cd
PM
3055 }
3056 rcu_read_unlock();
3057}
3058
3059/*
3060 * Notify userspace when a cgroup is released, by running the
3061 * configured release agent with the name of the cgroup (path
3062 * relative to the root of cgroup file system) as the argument.
3063 *
3064 * Most likely, this user command will try to rmdir this cgroup.
3065 *
3066 * This races with the possibility that some other task will be
3067 * attached to this cgroup before it is removed, or that some other
3068 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3069 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3070 * unused, and this cgroup will be reprieved from its death sentence,
3071 * to continue to serve a useful existence. Next time it's released,
3072 * we will get notified again, if it still has 'notify_on_release' set.
3073 *
3074 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3075 * means only wait until the task is successfully execve()'d. The
3076 * separate release agent task is forked by call_usermodehelper(),
3077 * then control in this thread returns here, without waiting for the
3078 * release agent task. We don't bother to wait because the caller of
3079 * this routine has no use for the exit status of the release agent
3080 * task, so no sense holding our caller up for that.
81a6a5cd 3081 */
81a6a5cd
PM
3082static void cgroup_release_agent(struct work_struct *work)
3083{
3084 BUG_ON(work != &release_agent_work);
3085 mutex_lock(&cgroup_mutex);
3086 spin_lock(&release_list_lock);
3087 while (!list_empty(&release_list)) {
3088 char *argv[3], *envp[3];
3089 int i;
e788e066 3090 char *pathbuf = NULL, *agentbuf = NULL;
bd89aabc 3091 struct cgroup *cgrp = list_entry(release_list.next,
81a6a5cd
PM
3092 struct cgroup,
3093 release_list);
bd89aabc 3094 list_del_init(&cgrp->release_list);
81a6a5cd
PM
3095 spin_unlock(&release_list_lock);
3096 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
e788e066
PM
3097 if (!pathbuf)
3098 goto continue_free;
3099 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3100 goto continue_free;
3101 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3102 if (!agentbuf)
3103 goto continue_free;
81a6a5cd
PM
3104
3105 i = 0;
e788e066
PM
3106 argv[i++] = agentbuf;
3107 argv[i++] = pathbuf;
81a6a5cd
PM
3108 argv[i] = NULL;
3109
3110 i = 0;
3111 /* minimal command environment */
3112 envp[i++] = "HOME=/";
3113 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3114 envp[i] = NULL;
3115
3116 /* Drop the lock while we invoke the usermode helper,
3117 * since the exec could involve hitting disk and hence
3118 * be a slow process */
3119 mutex_unlock(&cgroup_mutex);
3120 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
81a6a5cd 3121 mutex_lock(&cgroup_mutex);
e788e066
PM
3122 continue_free:
3123 kfree(pathbuf);
3124 kfree(agentbuf);
81a6a5cd
PM
3125 spin_lock(&release_list_lock);
3126 }
3127 spin_unlock(&release_list_lock);
3128 mutex_unlock(&cgroup_mutex);
3129}
8bab8dde
PM
3130
3131static int __init cgroup_disable(char *str)
3132{
3133 int i;
3134 char *token;
3135
3136 while ((token = strsep(&str, ",")) != NULL) {
3137 if (!*token)
3138 continue;
3139
3140 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3141 struct cgroup_subsys *ss = subsys[i];
3142
3143 if (!strcmp(token, ss->name)) {
3144 ss->disabled = 1;
3145 printk(KERN_INFO "Disabling %s control group"
3146 " subsystem\n", ss->name);
3147 break;
3148 }
3149 }
3150 }
3151 return 1;
3152}
3153__setup("cgroup_disable=", cgroup_disable);