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