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